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中显示
import numpy as np
import random
data=np.random.random([100,4]) #这里随机创建一组100*4的数据
str = '\n'
f=open('C:\\Users\\Desktop\\Runresult.txt','w')
f.write((str.join('%s'%i for i in data))) #将其导入提前写好的Runresult.txt文件中
f.close()
根据自己的需要将data换成自己代码输出的数据即可,文件的路径可以调整
串口发来的数据应该是比特类型的吧,我建议你看看你那串口一个心跳传多少数据,一股脑写txt文件里,到时候取出来你都不知道是啥。
直接使用file= open("文本")然后 file.write(内容) 最后 file.close()关闭文件即可。
pandas 的,to_csv() 函数即可
如果已经解决你的问题,请采纳,如有问题,请留言
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为追加模式
QFile file('result.txt')
file.open(( QIODevice::ReadWrite|QIODevice::Truncate))
file.write(data)
file.close()
可以用open
opt = open('result.txt','w')
opt.write('数据')
opt.close()