问题遇到的现象和发生背景
python串口已经编写好 想把接收到的信号全部转存到提醒他文件应该用什么函数 怎么写
问题相关代码,请勿粘贴截图
#接收到数据长度 查看发送过来的数据
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_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)#鼠标放在最后面
opt = open('result.txt', 'w') # 创建文件写入模式,原有的内容被覆盖# 不想被覆盖 w变成a a为追加模式
opt.write(data)
opt.close()
运行结果及报错内容
报错
TypeError: write() argument must be str, not QByteArray #把write内换成data就不能运行但是添加 '11' 这样就可以运行了
我的解答思路和尝试过的方法
我想要达到的结果
上位机反馈的内容,可以在txt中显示