从rapberry pi上的UART从设备读取数据时遇到一些问题。 该设备的手册是:https://app.box.com/shared/static/4l8fiyowu4agg26lc7fu234edci1n8no.pdf
在第12页上,它指示从设备发送的数据为Little Endian格式。这是我第一次使用python,但我并没有好运试图找到一种解释此类数据的方法。
如果我发送[7,75,0,76]的字节数组(第17页,平均读取功率),我得到的消息是:b'K \ x1c \ x00W'
到目前为止,这是我的代码:
import serial
import time
import struct
ser = serial.Serial('/dev/ttyS0',
baudrate=19200,
parity='N',
bytesize=8,
stopbits=1,
timeout = 1)
print ("Starting up")
print(ser.name)
connected = False
commandToSend = '[7,75,0,76]' # get the distance in mm
while True:
print ("Writing: ", commandToSend)
values = bytearray([7,75,0,76])
ser.write(values)
while True:
try:
print ("Attempt to Read")
ans = ser.read(4)
print ("Reading: ", ans)
time.sleep(5)
break
except:
pass
print ("Restart--------------------------------")
ser.flush() #flush the buffer
对于任何其他字节大小为1的命令,我都没有任何问题。至少我知道校验和和消息发送正确,唯一的问题是读取超过1的消息。
感谢您的帮助