附上代码先
# 收发数据
if self.textEdit_2.toPlainText() != "":
list_use = []
num = self.textEdit_2.toPlainText().split(" ")
for i in range(len(num)):
list_use.append(int(num[i], 16))
data_all = bytes(list_use)
jy_dz = self.textEdit_2.toPlainText()
use = self.crc16Add(jy_dz)
data_all = data_all + bytes([int(use[0:2], 16), int(use[3:5], 16)])
data01 = ''.join(
map(lambda x: (' ' if len(hex(x)) >= 4 else ' 0') + hex(x)[2:], data_all))
self.SerialPort.write(data_all)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
print('发送指令:' + str(data01.upper()))
print(" ")
time.sleep(0.5)
if self.SerialPort.inWaiting() > 0:
while self.SerialPort.isOpen() and self.SerialPort.inWaiting() > 0:
num = self.SerialPort.readline(self.SerialPort.inWaiting())
# data02 = ''.join(
# map(lambda x: (' ' if len(hex(x)) >= 4 else ' 0') + hex(x)[2:], num))
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
print("接收指令:" + num.decode('gbk'))
# print("接收指令:" + num.decode('iso-8859-1'))
# print("接收指令:" + str(num))
# print("接收指令:" + data02.upper())
print("")
else:
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
print("抱歉串口超时")
print(" ")
else:
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
print("未输入")
print(" ")
用python自制的串口工具,发送数据后单片机返回的是文字加浮点数的形式
问题是:
1.在向单片机发送数据并向读取单片机返回的数据的时候,会分成好几包返回
2.接受时好时坏,有时候会编码失败,或许是编码方式不对
请求大佬看看是什么情况,是延时问题还是数据处理方式问题