【发布时间】:2017-01-29 18:48:05
【问题描述】:
我是使用 Python3 进行数据采集的新手。我正在尝试找到一种方法来从 Linux 上的串行端口解析二进制数据。
import serial
ser = serial.Serial(
port='/dev/ttyS0',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1)
counter = 0
while 1:
x = ser.read(31)
print (x)
这给了我一个我不确定格式的字符串:
x='\x00\x00\x91\x00\x02\x88BM\x00\x1c\x00\x00\x00\x01\x00\x01\x00\x00\x00\x01\x00\x01\x00\xe1\x00K\x00\x1a\x00\x02\x00\x00'
使用
x.encode('hex')
给出一串十六进制值
x='000091000288**424d**001c00000001000100000001000100e1004b001a00020000'
其中0x42 是消息的结尾,0x4d 是消息的开头。
我可以使用
将其转换为以 10 为底的列表y = map(ord,x)
print(y)
然后我有一种方法可以使用索引重新排序消息,但肯定有更简洁的方法吗?如何创建一个以 0x4d 开头的列表进行解析?
【问题讨论】:
标签: python python-3.x stream binary pyserial