"""
pyqt5串口通信文件SerialCommunication.py
"""
import binascii
import os
import serial
import serial.tools.list_ports
from PyQt5.QtGui import QPixmap
Ret = False
port_list_number = []
port_list_name = []
imgLedOff = os.path.join(os.getcwd() + "\\images\\light_off.png")
imgLedOn = os.path.join(os.getcwd() + "\\images\\light_on.png")class Communication(object):"""Python串口通信封装类"""def __init__(self, com, bps, timeout):self.port = com self.bps = bps self.timeout = timeout self.main_engine = None global RetRet = Falseself.data = Noneself.b_c_text = Nonetry:self.main_engine = serial.Serial(self.port, self.bps, timeout=self.timeout)if self.main_engine.is_open:Ret = Trueexcept Exception as e:print("---异常---:", e)def Print_Name(self):"""打印设备基本信息"""print(self.main_engine.name) print(self.main_engine.port) print(self.main_engine.baudrate) print(self.main_engine.bytesize) print(self.main_engine.parity) print(self.main_engine.stopbits) print(self.main_engine.timeout) print(self.main_engine.writeTimeout) print(self.main_engine.xonxoff) print(self.main_engine.rtscts) print(self.main_engine.dsrdtr) print(self.main_engine.interCharTimeout) def Open_Engine(self):"""打开串口"""global Retif not self.main_engine.is_open:self.main_engine.open()Ret = Truedef Close_Engine(self):"""关闭串口"""global Retif self.main_engine.is_open:self.main_engine.close() Ret = False@staticmethoddef Print_Used_Com():"""打印可用串口列表"""port_list_name.clear()port_list_number.clear()port_list = list(serial.tools.list_ports.comports())if len(port_list) <= 0:print("The Serial port can't find!")else:for each_port in port_list:port_list_number.append(each_port[0])port_list_name.append(each_port[1])print(port_list_number)print(port_list_name)def Read_Size(self, size):"""接收指定大小的数据:param size::return:"""return self.main_engine.read(size=size)def Read_Line(self):"""接收一行数据:return:"""return self.main_engine.readline()def Send_data(self, data):"""发数据:param data:"""self.main_engine.write(data)def Receive_data(self, way):"""接收数据:param way:"""print("开始接收数据:")bWaitRec = Truewhile bWaitRec:try:if self.main_engine.in_waiting:if way == 0:for i in range(self.main_engine.in_waiting):print("接收ascii数据:" + str(self.Read_Size(1)))data1 = self.Read_Size(1).hex() data2 = int(data1, 16) print("收到数据十六进制:"+data1+" 收到数据十进制:"+str(data2))if way == 1:self.data = self.main_engine.read_all() print("接收ascii数据:", self.data)self.b_c_text = binascii.hexlify(self.data) print("Bytes数据:", self.b_c_text)if self.data.strip() != '':bWaitRec = Falseexcept Exception as e:print("异常报错:", e)print("接收数据完毕!")
"""
pyqt5主对话框文件main.py
"""import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtGui import QPixmap
import SerialCommunication
class MyPyQTMainForm(QMainWindow, BitmapFontCreate.Ui_MainWindow):"""主界面"""def __init__(self):super(MyPyQTMainForm, self).__init__()self.setupUi(self)self.myEngine = Noneself.strComNum = None def ComLedIndicator(self, comboIndex):"""串口状态用LED指示灯指示。串口打开LED亮;串口关闭LED灭;"""if comboIndex >= 0:self.strComNum = SerialCommunication.port_list_number[comboIndex]self.myEngine = SerialCommunication.Communication(self.strComNum, 9600, 0.5)else:self.myEngine = Noneif SerialCommunication.Ret:icoLedOn = QPixmap(SerialCommunication.imgLedOn)myPyMainForm.labIndicator.setPixmap(icoLedOn)myPyMainForm.labIndicator.setScaledContents(True)else:icoLedOff = QPixmap(SerialCommunication.imgLedOff)myPyMainForm.labIndicator.setPixmap(icoLedOff)myPyMainForm.labIndicator.setScaledContents(True)def ComScan(self):"""串口扫描"""if self.myEngine:self.myEngine.Close_Engine()myPyMainForm.comboComNum.clearEditText()myPyMainForm.comboComNum.clear()SerialCommunication.Communication.Print_Used_Com()myPyMainForm.comboComNum.addItems(SerialCommunication.port_list_name)"""
主函数
"""
if __name__ == '__main__':app = QApplication(sys.argv)myPyMainForm = MyPyQTMainForm()myPyMainForm.setWindowFlags(QtCore.Qt.WindowMinimizeButtonHint | QtCore.Qt.WindowCloseButtonHint)myPyMainForm.setFixedSize(myPyMainForm.width(), myPyMainForm.height())myPyMainForm.comboComNum.clear()SerialCommunication.Communication.Print_Used_Com()myPyMainForm.comboComNum.addItems(SerialCommunication.port_list_name)myPyMainForm.show()sys.exit(app.exec_())