增加串口断开重连功能。保存数据COM号在最前
This commit is contained in:
parent
0f3a8797e7
commit
5823199abe
@ -274,7 +274,7 @@ class PyQt5Serial(QtWidgets.QWidget,Ui_UartAssistant):
|
|||||||
#日志保存
|
#日志保存
|
||||||
# 格式化日期时间字符串,用于文件名
|
# 格式化日期时间字符串,用于文件名
|
||||||
# 例如:2024-05-28_12-34-56.txt
|
# 例如:2024-05-28_12-34-56.txt
|
||||||
self.file =time.strftime("%Y%m%d%H%M%S", time.localtime())+self.ser.port
|
self.file = self.ser.port+"-"+time.strftime("%Y%m%d%H%M%S", time.localtime())
|
||||||
self.filename = self.file + ".txt"
|
self.filename = self.file + ".txt"
|
||||||
|
|
||||||
|
|
||||||
@ -293,7 +293,10 @@ class PyQt5Serial(QtWidgets.QWidget,Ui_UartAssistant):
|
|||||||
|
|
||||||
if num>0 :
|
if num>0 :
|
||||||
#print("接收数据",datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"))
|
#print("接收数据",datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"))
|
||||||
data = self.ser.read(num)
|
try:
|
||||||
|
data = self.ser.read(num)
|
||||||
|
except:
|
||||||
|
QMessageBox.critical(self, '串口异常')
|
||||||
self.buffer+=data
|
self.buffer+=data
|
||||||
|
|
||||||
#print("接收完成",datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"))
|
#print("接收完成",datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"))
|
||||||
@ -337,11 +340,30 @@ class PyQt5Serial(QtWidgets.QWidget,Ui_UartAssistant):
|
|||||||
|
|
||||||
|
|
||||||
except:
|
except:
|
||||||
QMessageBox.critical(self, '串口异常', '串口接收数据异常,请重新连接设备!')
|
# QMessageBox.critical(self, '串口异常', '串口接收数据异常,请重新连接设备!')
|
||||||
self.port_close()
|
# 获取到text光标
|
||||||
return None
|
textCursor = self.textEditReceive.textCursor()
|
||||||
|
# 滚动到底部
|
||||||
|
textCursor.movePosition(textCursor.End)
|
||||||
|
# 设置光标到text中去
|
||||||
|
self.textEditReceive.setTextCursor(textCursor)
|
||||||
|
self.textEditReceive.insertPlainText("串口断开,重连中...\r\n")
|
||||||
|
self.ser.close()
|
||||||
|
|
||||||
|
try:
|
||||||
|
print("重连中...")
|
||||||
|
time.sleep(1)
|
||||||
|
if not self.ser.is_open:
|
||||||
|
self.ser.open()
|
||||||
|
print("重连成功")
|
||||||
|
else:
|
||||||
|
print("串口已连接,无需重连")
|
||||||
|
except Exception as e:
|
||||||
|
|
||||||
|
print(f"重连失败{e}")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# %%定时发送数据
|
# %%定时发送数据
|
||||||
def data_send_timer(self):
|
def data_send_timer(self):
|
||||||
try:
|
try:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user