概述
import serial
import serial.tools.list_ports as lip
from PyQt5 import QtCore, QtGui, QtWidgets
import _thread
isopen = False
custom_serial = serial.Serial
def get_ports():
return list(lip.comports())
class Ui_mainWindow(object):
def setupUi(self, mainWindow):
mainWindow.setObjectName("mainWindow")
mainWindow.resize(851, 600)
self.centralwidget = QtWidgets.QWidget(mainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(50, 10, 101, 31))
self.label.setObjectName("label")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(50, 40, 761, 311))
self.textEdit.setObjectName("textEdit")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(590, 390, 72, 15))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(590, 430, 72, 15))
self.label_3.setObjectName("label_3")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(670, 380, 141, 31))
self.comboBox.setObjectName("comboBox")
portlist=get_ports()
for i in range(len(portlist)):
self.comboBox.addItem(portlist[i].name)
self.comboBox_2 = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_2.setGeometry(QtCore.QRect(670, 420, 141, 31))
self.comboBox_2.setObjectName("comboBox_2")
self.comboBox_2.addItem("9600")
self.comboBox_2.addItem("115200")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(590, 480, 91, 41))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(720, 480, 91, 41))
self.pushButton_2.setObjectName("pushButton_2")
self.textEdit_2 = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit_2.setGeometry(QtCore.QRect(50, 380, 511, 151))
self.textEdit_2.setObjectName("textEdit_2")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(50, 360, 91, 16))
self.label_4.setObjectName("label_4")
mainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(mainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 851, 26))
self.menubar.setObjectName("menubar")
mainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(mainWindow)
self.statusbar.setObjectName("statusbar")
mainWindow.setStatusBar(self.statusbar)
self.retranslateUi(mainWindow)
self.pushButton.clicked.connect(self.send) # type: ignore
self.pushButton_2.clicked.connect(self.openserial)
self.isopen=False
QtCore.QMetaObject.connectSlotsByName(mainWindow)
def send(self):
print("点击了发送按钮")
Data=self.textEdit_2.toPlainText()
if self.custom.isOpen():
self.custom.write(Data.encode("utf-8"))
print("发送成功")
else:
print("请先打开串口")
def Com_Data_Rsv(self,treadname):
while True:
while isopen:
data = self.custom.readall(2)
if data == b'':
continue
else:
print("receive : ", data)
self.textEdit.insertPlainText(data.decode("utf-8"))
def openserial(self):
global
isopen
if self.pushButton_2.text()=="打开串口":
print("点击了打开串口按钮")
self.custom=serial.Serial(self.comboBox.currentText(),int(self.comboBox_2.currentText()),timeout=0.5)
if self.custom.isOpen():
print(isopen)
isopen=True
print(isopen)
from PyQt5.QtWidgets import QMessageBox
QMessageBox.information(MainWindow,"tishikuang","welcome",QMessageBox.Yes)
print("打开串口成功")
else:
print("打开串口失败")
self.pushButton_2.setText("关闭串口")
self.comboBox.setEnabled(False)
self.comboBox_2.setEnabled(False)
else:
print("点击了关闭串口")
isopen=False
self.custom.close()
self.comboBox_2.setEnabled(True)
self.comboBox.setEnabled(True)
self.pushButton_2.setText("打开串口")
def retranslateUi(self, mainWindow):
_translate = QtCore.QCoreApplication.translate
mainWindow.setWindowTitle(_translate("mainWindow", "串口调试助手"))
self.label.setText(_translate("mainWindow", "数据显示"))
self.label_2.setText(_translate("mainWindow", "串口号"))
self.label_3.setText(_translate("mainWindow", "波特率"))
self.pushButton.setText(_translate("mainWindow", "发送数据"))
self.pushButton_2.setText(_translate("mainWindow", "打开串口"))
self.label_4.setText(_translate("mainWindow", "数据发送框"))
if __name__ =="__main__":
import sys
app=QtWidgets.QApplication(sys.argv)
MainWindow=QtWidgets.QMainWindow()
ui=Ui_mainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
_thread.start_new_thread(ui.Com_Data_Rsv ,("Thread-1",))
sys.exit(app.exec_())
引用双线程进行数据接受和发送,具体详见源代码。
最后
以上就是感性巨人为你收集整理的python+pysrial+pyqt5的串口通讯助手(含有源代码)的全部内容,希望文章能够帮你解决python+pysrial+pyqt5的串口通讯助手(含有源代码)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复