问题遇到的现象和发生背景
时间戳加上了 接收框内显示时间,想加一个收发显示
问题相关代码,请勿粘贴截图
#接收到数据长度 查看发送过来的数据
def slot_ReadData(self,data):
self.Receivelength=self.Receivelength+len(data)#收到数据的长度
self.ui.label_recview.setText("接收:"+str(self.Receivelength))
#Byte_data=bytes(data)#强制转换
#添加时间戳
if self.ui.checkBox_Time.checkState():
time_str=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())#+ "\r\n "
self.ui.textEdit_recview.setTextColor(QColor(255,100,100))
self.ui.textEdit_recview.insertPlainText(time_str)
print("打印时间"+time_str)
self.ui.textEdit_recview.setTextColor(QColor(0,0,0))
Byte_data=bytes(data)#强制转换
if self.ui.checkBox_16view.checkState():
View_data=''#新建空字符
for i in range(0,len(Byte_data)):#返回的字符对象的长度
View_data=View_data+'{:02x}'.format(Byte_data[i])+' '#每个字节转换成16进制
self.ui.textEdit_recview.insertPlainText(View_data)#输出框显示
else:
print("字符串显示")
self.ui.textEdit_recview.insertPlainText(Byte_data.decode('utf-8','ignore'))
self.ui.textEdit_recview.moveCursor(QTextCursor.End)#鼠标放在最后面
#判断时间 定义一个时钟
now = datetime.now()
date_time = now.strftime("%Y-%m-%d, %H:%M:%S")
print(date_time)
#检测com口
com_str= self.ui.comboBox_Com.currentText()
f = "{}{}.txt".format(now.strftime("%Y-%m-%d %H点%M分"), com_str)
opt = open(f, 'a') # 创建文件写入模式,w原有的内容被覆盖# 不想被覆盖 w变成a a为追加模式
opt.write(str(data, encoding='utf-8'))
opt.close()
运行结果及报错内容
希望的效果
我的解答思路和尝试过的方法
不管有没有采纳 提供思路就会有打赏 谢谢大家