我试图通过UART发送和接收Python数据。发送正常,但读数仍然错误。谁能帮忙吗?在import time, os, re, urllib
import msvcrt
import sys
import serial
import struct
port="COM38"
def getAndSend1(test,type):
global port
if type=='int' :
value=int(test) ##casting string to int
ba=bytearray(struct.pack("i",value))
elif type=='float' :
value=float(test) ##casting string to float
ba=bytearray(struct.pack("f",value))
else:
print("undefined type")
return 0
ba= b'\x7E\x7E'+ba+b'\x01\x01' ##adding header and tail to the byte array
print("sent: "+str([ "0x%02x" % b for b in ba])) ##print the packet before sending it
ser = serial.Serial(port, 115200) ##open serial port
while ser.isOpen()==0: ##waiting till port get open
1==1
time.sleep(2)
ser.write(ba) ##sending the packet
result = ser.read(8) ##reading the serial port
print("recieved: "+str([ "0x%02x" % r for r in result]))
resVal=struct.unpack("f",result[2:6]);
print(str(resVal[0])+"\n")
getAndSend1(1,'int')
当我运行它时,我得到了以下错误:
^{pr2}$