pyqt串口通信的项目实践
更新时间:2023年12月26日 11:20:20 作者:QT真的好难呀
串口通信工业控制上面常用的,本文主要介绍了pyqt串口通信的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
串口通信工业控制上面常用的,能够很简单的实现出来
说明都在代码中,下面是主要逻辑代码,窗口类的创建,是直接继承了ui窗口类,进行初始化操作
import sys import mianWindow from PyQt5.QtSerialPort import QSerialPortInfo, QSerialPort from PyQt5.QtWidgets import QApplication, QWidget from PyQt5.QtCore import QIODevice, QTextCodec # 直接写一个类继承mainWindow,方便去操作窗口 class MyWindow(QWidget, mianWindow.Ui_Form): serialPort = QSerialPort() # 通信用的对象 def __init__(self): super(MyWindow, self).__init__() self.setupUi(self) # 初始化窗口 self.enumComPort() self.serialPort.readyRead.connect(self.recvMessage) self.pushButton_open.clicked.connect(self.openCom) self.pushButton_send.clicked.connect(self.sendMessage) def enumComPort(self): self.comboBox_Port.clear() comInfo = QSerialPortInfo.availablePorts() for item in comInfo: self.comboBox_Port.addItem(item.portName()) def openCom(self): # 串口号 name = self.comboBox_Port.currentText() # 波特率 baudRate = int(self.comboBox_BaudRate.currentText()) # 数据位 dataBits=0 if self.comboBox_DataBits.currentIndex() == 0: dataBits = QSerialPort.DataBits.Data5 elif self.comboBox_DataBits.currentIndex() == 1: dataBits = QSerialPort.DataBits.Data6 elif self.comboBox_DataBits.currentIndex() == 2: dataBits = QSerialPort.DataBits.Data7 elif self.comboBox_DataBits.currentIndex() == 3: dataBits = QSerialPort.DataBits.Data8 # 停止位 stopBits = 0 if self.comboBox_StopBits.currentIndex() == 0: stopBits = QSerialPort.StopBits.OneStop elif self.comboBox_StopBits.currentIndex() == 1: stopBits = QSerialPort.StopBits.OneAndHalfStop elif self.comboBox_StopBits.currentIndex() == 2: stopBits = QSerialPort.StopBits.TwoStop # 校验位 parity = 0 if self.comboBox_Parity.currentIndex() == 0: parity = QSerialPort.Parity.NoParity elif self.comboBox_Parity.currentIndex() == 1: parity = QSerialPort.Parity.EvenParity elif self.comboBox_Parity.currentIndex() == 2: parity = QSerialPort.Parity.OddParity elif self.comboBox_Parity.currentIndex() == 3: parity = QSerialPort.Parity.SpaceParity elif self.comboBox_Parity.currentIndex() == 4: parity = QSerialPort.Parity.MarkParity # 流控 flowControl = 0 if self.comboBox_FlowControl.currentIndex() == 0: flowControl = QSerialPort.FlowControl.NoFlowControl elif self.comboBox_FlowControl.currentIndex() == 1: flowControl = QSerialPort.FlowControl.HardwareControl elif self.comboBox_FlowControl.currentIndex() == 2: flowControl = QSerialPort.FlowControl.SoftwareControl # 设置获取到的串口信息 self.serialPort.setPortName(name) self.serialPort.setBaudRate(baudRate) self.serialPort.setDataBits(dataBits) self.serialPort.setParity(parity) self.serialPort.setStopBits(stopBits) self.serialPort.setFlowControl(flowControl) if not self.serialPort.isOpen(): if not self.serialPort.open(QIODevice.OpenModeFlag.ReadWrite): return else: self.pushButton_open.setText("关闭串口") else: self.serialPort.close() self.pushButton_open.setText("关闭串口") # 对于消息的收发需要自己稍微处理一下,主要是对于发送的数据的转换,我这里是转为了本地字符集,对于各自的情况,做不同的处理 def recvMessage(self): data = self.serialPort.readAll() self.textBrowser_recvStr.append(QTextCodec.codecForName("UTF-8").toUnicode(data)) def sendMessage(self): data = self.lineEdit_sendStr.text() self.serialPort.write(QTextCodec.codecForName("UTF-8").fromUnicode(data).data()) if __name__ == '__main__': app = QApplication(sys.argv) w = MyWindow() w.show() sys.exit(app.exec_())
窗口ui的,python代码
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'mianWindow.ui' # # Created by: PyQt5 UI code generator 5.15.9 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again. Do not edit this file unless you know what you are doing. from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(456, 434) self.gridLayout = QtWidgets.QGridLayout(Form) self.gridLayout.setObjectName("gridLayout") self.groupBox = QtWidgets.QGroupBox(Form) self.groupBox.setObjectName("groupBox") self.verticalLayout = QtWidgets.QVBoxLayout(self.groupBox) self.verticalLayout.setObjectName("verticalLayout") self.horizontalLayout = QtWidgets.QHBoxLayout() self.horizontalLayout.setObjectName("horizontalLayout") self.label = QtWidgets.QLabel(self.groupBox) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth()) self.label.setSizePolicy(sizePolicy) self.label.setObjectName("label") self.horizontalLayout.addWidget(self.label) self.comboBox_Port = QtWidgets.QComboBox(self.groupBox) self.comboBox_Port.setObjectName("comboBox_Port") self.horizontalLayout.addWidget(self.comboBox_Port) self.verticalLayout.addLayout(self.horizontalLayout) self.horizontalLayout_2 = QtWidgets.QHBoxLayout() self.horizontalLayout_2.setObjectName("horizontalLayout_2") self.label_2 = QtWidgets.QLabel(self.groupBox) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.label_2.sizePolicy().hasHeightForWidth()) self.label_2.setSizePolicy(sizePolicy) self.label_2.setObjectName("label_2") self.horizontalLayout_2.addWidget(self.label_2) self.comboBox_BaudRate = QtWidgets.QComboBox(self.groupBox) self.comboBox_BaudRate.setObjectName("comboBox_BaudRate") self.comboBox_BaudRate.addItem("") self.comboBox_BaudRate.addItem("") self.comboBox_BaudRate.addItem("") self.comboBox_BaudRate.addItem("") self.comboBox_BaudRate.addItem("") self.comboBox_BaudRate.addItem("") self.comboBox_BaudRate.addItem("") self.comboBox_BaudRate.addItem("") self.horizontalLayout_2.addWidget(self.comboBox_BaudRate) self.verticalLayout.addLayout(self.horizontalLayout_2) self.horizontalLayout_3 = QtWidgets.QHBoxLayout() self.horizontalLayout_3.setObjectName("horizontalLayout_3") self.label_3 = QtWidgets.QLabel(self.groupBox) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.label_3.sizePolicy().hasHeightForWidth()) self.label_3.setSizePolicy(sizePolicy) self.label_3.setObjectName("label_3") self.horizontalLayout_3.addWidget(self.label_3) self.comboBox_DataBits = QtWidgets.QComboBox(self.groupBox) self.comboBox_DataBits.setObjectName("comboBox_DataBits") self.comboBox_DataBits.addItem("") self.comboBox_DataBits.addItem("") self.comboBox_DataBits.addItem("") self.comboBox_DataBits.addItem("") self.horizontalLayout_3.addWidget(self.comboBox_DataBits) self.verticalLayout.addLayout(self.horizontalLayout_3) self.horizontalLayout_4 = QtWidgets.QHBoxLayout() self.horizontalLayout_4.setObjectName("horizontalLayout_4") self.label_4 = QtWidgets.QLabel(self.groupBox) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.label_4.sizePolicy().hasHeightForWidth()) self.label_4.setSizePolicy(sizePolicy) self.label_4.setObjectName("label_4") self.horizontalLayout_4.addWidget(self.label_4) self.comboBox_StopBits = QtWidgets.QComboBox(self.groupBox) self.comboBox_StopBits.setObjectName("comboBox_StopBits") self.comboBox_StopBits.addItem("") self.comboBox_StopBits.addItem("") self.comboBox_StopBits.addItem("") self.horizontalLayout_4.addWidget(self.comboBox_StopBits) self.verticalLayout.addLayout(self.horizontalLayout_4) self.horizontalLayout_5 = QtWidgets.QHBoxLayout() self.horizontalLayout_5.setObjectName("horizontalLayout_5") self.label_5 = QtWidgets.QLabel(self.groupBox) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.label_5.sizePolicy().hasHeightForWidth()) self.label_5.setSizePolicy(sizePolicy) self.label_5.setObjectName("label_5") self.horizontalLayout_5.addWidget(self.label_5) self.comboBox_Parity = QtWidgets.QComboBox(self.groupBox) self.comboBox_Parity.setObjectName("comboBox_Parity") self.comboBox_Parity.addItem("") self.comboBox_Parity.addItem("") self.comboBox_Parity.addItem("") self.comboBox_Parity.addItem("") self.comboBox_Parity.addItem("") self.horizontalLayout_5.addWidget(self.comboBox_Parity) self.verticalLayout.addLayout(self.horizontalLayout_5) self.horizontalLayout_6 = QtWidgets.QHBoxLayout() self.horizontalLayout_6.setObjectName("horizontalLayout_6") self.label_6 = QtWidgets.QLabel(self.groupBox) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.label_6.sizePolicy().hasHeightForWidth()) self.label_6.setSizePolicy(sizePolicy) self.label_6.setObjectName("label_6") self.horizontalLayout_6.addWidget(self.label_6) self.comboBox_FlowControl = QtWidgets.QComboBox(self.groupBox) self.comboBox_FlowControl.setObjectName("comboBox_FlowControl") self.comboBox_FlowControl.addItem("") self.comboBox_FlowControl.addItem("") self.comboBox_FlowControl.addItem("") self.horizontalLayout_6.addWidget(self.comboBox_FlowControl) self.verticalLayout.addLayout(self.horizontalLayout_6) self.horizontalLayout_7 = QtWidgets.QHBoxLayout() self.horizontalLayout_7.setObjectName("horizontalLayout_7") spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.horizontalLayout_7.addItem(spacerItem) self.pushButton_open = QtWidgets.QPushButton(self.groupBox) self.pushButton_open.setStyleSheet("") self.pushButton_open.setObjectName("pushButton_open") self.horizontalLayout_7.addWidget(self.pushButton_open) self.verticalLayout.addLayout(self.horizontalLayout_7) self.gridLayout.addWidget(self.groupBox, 0, 0, 1, 1) self.verticalLayout_3 = QtWidgets.QVBoxLayout() self.verticalLayout_3.setObjectName("verticalLayout_3") self.textBrowser_recvStr = QtWidgets.QTextBrowser(Form) self.textBrowser_recvStr.setObjectName("textBrowser_recvStr") self.verticalLayout_3.addWidget(self.textBrowser_recvStr) self.lineEdit_sendStr = QtWidgets.QLineEdit(Form) self.lineEdit_sendStr.setObjectName("lineEdit_sendStr") self.verticalLayout_3.addWidget(self.lineEdit_sendStr) self.gridLayout.addLayout(self.verticalLayout_3, 0, 1, 1, 1) self.horizontalLayout_8 = QtWidgets.QHBoxLayout() self.horizontalLayout_8.setObjectName("horizontalLayout_8") spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.horizontalLayout_8.addItem(spacerItem1) self.pushButton_send = QtWidgets.QPushButton(Form) self.pushButton_send.setObjectName("pushButton_send") self.horizontalLayout_8.addWidget(self.pushButton_send) self.gridLayout.addLayout(self.horizontalLayout_8, 1, 0, 1, 2) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.groupBox.setTitle(_translate("Form", "串口设置")) self.label.setText(_translate("Form", "串口号:")) self.label_2.setText(_translate("Form", "波特率:")) self.comboBox_BaudRate.setItemText(0, _translate("Form", "1200")) self.comboBox_BaudRate.setItemText(1, _translate("Form", "2400")) self.comboBox_BaudRate.setItemText(2, _translate("Form", "4800")) self.comboBox_BaudRate.setItemText(3, _translate("Form", "9600")) self.comboBox_BaudRate.setItemText(4, _translate("Form", "19200")) self.comboBox_BaudRate.setItemText(5, _translate("Form", "38400")) self.comboBox_BaudRate.setItemText(6, _translate("Form", "57600")) self.comboBox_BaudRate.setItemText(7, _translate("Form", "115200")) self.label_3.setText(_translate("Form", "数据位:")) self.comboBox_DataBits.setItemText(0, _translate("Form", "Data5")) self.comboBox_DataBits.setItemText(1, _translate("Form", "Data6")) self.comboBox_DataBits.setItemText(2, _translate("Form", "Data7")) self.comboBox_DataBits.setItemText(3, _translate("Form", "Data8")) self.label_4.setText(_translate("Form", "停止位:")) self.comboBox_StopBits.setItemText(0, _translate("Form", "OneStop")) self.comboBox_StopBits.setItemText(1, _translate("Form", "OnendHalfStop")) self.comboBox_StopBits.setItemText(2, _translate("Form", "TwoStop")) self.label_5.setText(_translate("Form", "校验位:")) self.comboBox_Parity.setItemText(0, _translate("Form", "NoParity")) self.comboBox_Parity.setItemText(1, _translate("Form", "EvenParity")) self.comboBox_Parity.setItemText(2, _translate("Form", "OddParity")) self.comboBox_Parity.setItemText(3, _translate("Form", "SpaceParity")) self.comboBox_Parity.setItemText(4, _translate("Form", "MarkParity")) self.label_6.setText(_translate("Form", "流 控:")) self.comboBox_FlowControl.setItemText(0, _translate("Form", "NoFlowControl")) self.comboBox_FlowControl.setItemText(1, _translate("Form", "HardwareControl")) self.comboBox_FlowControl.setItemText(2, _translate("Form", "SoftwareControl")) self.pushButton_open.setText(_translate("Form", "打开串口")) self.pushButton_send.setText(_translate("Form", "发送"))
运行界面:
对于这种模块的使用,主要就是要注意一些数据的获取和设置,要先搞清楚,使用这个模块要获取那些数据,怎么设置,使用的一个过程,是使用什么对象来操作这些数据
到此这篇关于pyqt串口通信的项目实践的文章就介绍到这了,更多相关pyqt串口通信内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
最新评论