大佬们,本人python菜鸟一枚,最近自己再弄一个串口助手上位机,想要可以持续接收串口消息,但是我写的上位机用另一个串口助手模拟下位机一直发送信号时接收一段时间后就会自己奔溃,退出程序,想问问是为什么呢?另外我串口助手有停止位这些,我写的接收时并没有处理,但是为啥也能接受正确?(新手啥也不懂。。。。)
这是主程序S_port.py
filename='data.txt' import serial import serial.tools.list_ports import sys import datetime from SerialPortTools import Ui_Form from PyQt5 import QtWidgets import threading Com_Open_Flag = False # 串口打开标志 COM_Band = {"9600", "115200"} custom_serial = serial.Serial datalst=[]#用于保存读取到的数据 # 获取串口列表 def Get_Com_List(): return list(serial.tools.list_ports.comports()) # 获取串口数据 '''def Com_Data_Rsv(): while True: while Com_Open_Flag: data = custom_serial.readline() data_n=str(data.hex()) if data == b'': continue else: with open(filename, 'a') as stu_txt: stu_txt.write(data_n) datalst.append(data.hex()) #print("receive : ", data) window.Set_Display_Data(data)''' def Com_Data_Rsv(): while True: while Com_Open_Flag: len = custom_serial.in_waiting if len>0: data = custom_serial.read(4) data_n=str(data.hex()) if data_n== '': continue else: #with open(filename, 'a') as stu_txt: #stu_txt.write(data_n) datalst.append(data.hex()) #print("receive : ", data) window.Set_Display_Data(data_n) # 界面UI按键程序重写 class Mywindow(QtWidgets.QMainWindow, Ui_Form): def __init__(self): super(Mywindow, self).__init__() self.setupUi(self) COM_List = Get_Com_List() # 获取串口列表 #print(COM_List[0][0],COM_List[1].name) #print(COM_List) for i in range(0, len(COM_List)): # 将列表导入到下拉框 self.Com_Port.addItem(COM_List[i].name) self.Com_Band.addItem("9600") self.Com_Band.addItem("115200") def Open_click_port(self): global custom_serial # 全局变量,需要加global global Com_Open_Flag print("点击了打开/关闭串口按钮") if self.Open_Com.text() == "打开串口": #print(self.Com_Port.currentText()) custom_serial = serial.Serial(self.Com_Port.currentText(), int(self.Com_Band.currentText()), timeout=0.01) if custom_serial.isOpen(): print("open success") Com_Open_Flag = True self.Open_Com.setText("关闭串口") self.Com_Band.setEnabled(False) # 串口号和波特率变为不可选择 self.Com_Port.setEnabled(False) else: print("open failed") else: Com_Open_Flag = False self.Open_Com.setText("打开串口") custom_serial.close() self.Com_Band.setEnabled(True) # 串口号和波特率变为可选择 self.Com_Port.setEnabled(True) print('读取到的数据为:',datalst) def Set_Display_Data(self, Data_n): ct = datetime.datetime.now() ct_str = ct.strftime("%Y-%m-%d %H:%M:%S") #self.Date_Display.insertPlainText(str(ct_str)+'\n') self.Date_Display.insertPlainText(Data_n) #self.Date_Display.insertPlainText(str(Data, encoding="gbk")+'\n')#也是解码的意思 #改为gbk就可以显示汉字了,gb2312自动换行不需\n def Send_data(self): print("点击了发送数据按钮") ct = datetime.datetime.now() ct_str = ct.strftime("%Y-%m-%d %H:%M:%S") self.Date_Display.insertPlainText(str(ct_str) + '\n') Data_Need_Send = self.Send_Data_Dsiplay.toPlainText() self.Date_Display.insertPlainText(str(Data_Need_Send)+'\n') if custom_serial.isOpen(): custom_serial.write(Data_Need_Send.encode("gbk")) else: print("请先打开串口") if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) window = Mywindow() window.show() t1=threading.Thread(target=Com_Data_Rsv) t1.setDaemon(True) #设置为守护线程 t1.start() sys.exit(app.exec_())
这是界面程序SerialPortTools.py
from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(591, 458) self.layoutWidget = QtWidgets.QWidget(Form) self.layoutWidget.setGeometry(QtCore.QRect(9, 9, 571, 430)) self.layoutWidget.setObjectName("layoutWidget") self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.layoutWidget) self.verticalLayout_2.setContentsMargins(0, 0, 0, 0) self.verticalLayout_2.setObjectName("verticalLayout_2") self.label_3 = QtWidgets.QLabel(self.layoutWidget) self.label_3.setObjectName("label_3") self.verticalLayout_2.addWidget(self.label_3) self.Date_Display = QtWidgets.QTextEdit(self.layoutWidget) self.Date_Display.setObjectName("Date_Display") self.verticalLayout_2.addWidget(self.Date_Display) self.label_4 = QtWidgets.QLabel(self.layoutWidget) self.label_4.setObjectName("label_4") self.verticalLayout_2.addWidget(self.label_4) self.horizontalLayout_2 = QtWidgets.QHBoxLayout() self.horizontalLayout_2.setObjectName("horizontalLayout_2") self.Send_Data_Dsiplay = QtWidgets.QTextEdit(self.layoutWidget) self.Send_Data_Dsiplay.setObjectName("Send_Data_Dsiplay") self.horizontalLayout_2.addWidget(self.Send_Data_Dsiplay) self.verticalLayout = QtWidgets.QVBoxLayout() self.verticalLayout.setObjectName("verticalLayout") self.gridLayout = QtWidgets.QGridLayout() self.gridLayout.setObjectName("gridLayout") self.Com_Band = QtWidgets.QComboBox(self.layoutWidget) self.Com_Band.setObjectName("Com_Band") self.gridLayout.addWidget(self.Com_Band, 1, 1, 1, 1) self.label_2 = QtWidgets.QLabel(self.layoutWidget) self.label_2.setObjectName("label_2") self.gridLayout.addWidget(self.label_2, 1, 0, 1, 1) self.label_1 = QtWidgets.QLabel(self.layoutWidget) self.label_1.setObjectName("label_1") self.gridLayout.addWidget(self.label_1, 0, 0, 1, 1) self.Com_Port = QtWidgets.QComboBox(self.layoutWidget) self.Com_Port.setObjectName("Com_Port") self.gridLayout.addWidget(self.Com_Port, 0, 1, 1, 1) self.verticalLayout.addLayout(self.gridLayout) self.horizontalLayout = QtWidgets.QHBoxLayout() self.horizontalLayout.setObjectName("horizontalLayout") self.pushButton = QtWidgets.QPushButton(self.layoutWidget) self.pushButton.setObjectName("pushButton") self.horizontalLayout.addWidget(self.pushButton) self.Open_Com = QtWidgets.QPushButton(self.layoutWidget) self.Open_Com.setObjectName("Open_Com") self.horizontalLayout.addWidget(self.Open_Com) self.verticalLayout.addLayout(self.horizontalLayout) self.horizontalLayout_2.addLayout(self.verticalLayout) self.verticalLayout_2.addLayout(self.horizontalLayout_2) self.retranslateUi(Form) self.Open_Com.clicked.connect(Form.Open_click_port) self.pushButton.clicked.connect(Form.Send_data) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Serial Port Tools")) self.label_3.setText(_translate("Form", "数据显示")) self.label_4.setText(_translate("Form", "数据发送")) self.label_2.setText(_translate("Form", "波特率")) self.label_1.setText(_translate("Form", "串口号")) self.pushButton.setText(_translate("Form", "发送数据")) self.Open_Com.setText(_translate("Form", "打开串口"))