Encode/decode RS232串口数据
Encode/decode data from RS232 serial port
这是我第一次必须通过 RS232 串口连接到设备以 read/write 数据,我被困在 encoding/decoding 程序上。
我正在使用库“pyserial”完成 Python 3 中的所有操作。这是我到目前为止所做的:
import serial
ser = serial.Serial()
ser.port = '/dev/ttyUSB0'
ser.baudrate = 115200
ser.bytesize = serial.EIGHTBITS
ser.parity = serial.PARITY_NONE
ser.stopbits = serial.STOPBITS_ONE
ser.timeout = 3
ser.open()
device_write = ser.write(bytearray.fromhex('AA 55 00 00 07 00 12 19 00'))
device_read = ser.read_until()
connection/communication 似乎按预期工作。 device_read
的输出是
b'M1830130A2IMU v3.2.9.1 26.04.19\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x0527641\x00\x00\x00IMHF R.1.0.0 10.28.2018 td: 6.500ms\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14\x00'
这就是我被困的地方。我不知道如何解释这个。附件是数据表中的图像,它解释了输出应该代表什么。
数据表显示我拥有的设备 "fields in bytes 98 to 164 are empty"。有人可以帮助我理解将 ser.read_until()
的输出转换为 "human readable" 并表示图像中数据的形式需要做什么吗?我不需要有人为我编写代码,但我什至不确定从哪里开始。同样,这是我第一次这样做,所以我对发生的事情有点迷茫。
如果你想写一个十六进制值为 12(十进制 18)的单个字节,我相信你需要做的是 ser.write(bytes([0x12]))
,这相当于 ser.write(bytes([18]))
.
看起来您的输出是 154 字节而不是 98 字节,而且其中大部分是人类不可读的。
但是如果你确实有图中描述的数据,你可以像这样分解它:
ID_sn = device_read[0:8].decode('ascii')
ID_fw = device_read[8:48].decode('ascii')
Press_Sens = device_read[48]
等等。
这不是答案,只是充实了@ozangds 的想法(可能会节省您的输入时间):
def decode_bytes(data, start, stop):
return data[start:stop+1].decode('ascii').rstrip('\x00')
device_read = b'M1830130A2IMU v3.2.9.1 26.04.19\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x0527641\x00\x00\x00IMHF R.1.0.0 10.28.2018 td: 6.500ms\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14\x00'
ID_sn = decode_bytes(device_read, 0, 7)
ID_fw = decode_bytes(device_read, 8, 47)
Press_sens = device_read[48]
IMU_type = device_read[49]
IMU_sn = decode_bytes(device_read, 50, 57)
IMU_fw = decode_bytes(device_read, 58, 97)
label_fmt = '{:>10}: {!r}'
print(label_fmt.format('ID_sn', ID_sn))
print(label_fmt.format('ID_fw', ID_fw))
print(label_fmt.format('Press_sens', Press_sens))
print(label_fmt.format('IMU_type', IMU_type))
print(label_fmt.format('IMU_sn', IMU_sn))
print(label_fmt.format('IMU_fw', IMU_fw))
输出:
ID_sn: 'M1830130'
ID_fw: 'A2IMU v3.2.9.1 26.04.19'
Press_sens: 2
IMU_type: 5
IMU_sn: '27641'
IMU_fw: 'IMHF R.1.0.0 10.28.2018 td: 6.500ms'
这是我第一次必须通过 RS232 串口连接到设备以 read/write 数据,我被困在 encoding/decoding 程序上。
我正在使用库“pyserial”完成 Python 3 中的所有操作。这是我到目前为止所做的:
import serial
ser = serial.Serial()
ser.port = '/dev/ttyUSB0'
ser.baudrate = 115200
ser.bytesize = serial.EIGHTBITS
ser.parity = serial.PARITY_NONE
ser.stopbits = serial.STOPBITS_ONE
ser.timeout = 3
ser.open()
device_write = ser.write(bytearray.fromhex('AA 55 00 00 07 00 12 19 00'))
device_read = ser.read_until()
connection/communication 似乎按预期工作。 device_read
的输出是
b'M1830130A2IMU v3.2.9.1 26.04.19\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x0527641\x00\x00\x00IMHF R.1.0.0 10.28.2018 td: 6.500ms\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14\x00'
这就是我被困的地方。我不知道如何解释这个。附件是数据表中的图像,它解释了输出应该代表什么。
数据表显示我拥有的设备 "fields in bytes 98 to 164 are empty"。有人可以帮助我理解将 ser.read_until()
的输出转换为 "human readable" 并表示图像中数据的形式需要做什么吗?我不需要有人为我编写代码,但我什至不确定从哪里开始。同样,这是我第一次这样做,所以我对发生的事情有点迷茫。
如果你想写一个十六进制值为 12(十进制 18)的单个字节,我相信你需要做的是 ser.write(bytes([0x12]))
,这相当于 ser.write(bytes([18]))
.
看起来您的输出是 154 字节而不是 98 字节,而且其中大部分是人类不可读的。 但是如果你确实有图中描述的数据,你可以像这样分解它:
ID_sn = device_read[0:8].decode('ascii')
ID_fw = device_read[8:48].decode('ascii')
Press_Sens = device_read[48]
等等。
这不是答案,只是充实了@ozangds 的想法(可能会节省您的输入时间):
def decode_bytes(data, start, stop):
return data[start:stop+1].decode('ascii').rstrip('\x00')
device_read = b'M1830130A2IMU v3.2.9.1 26.04.19\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x0527641\x00\x00\x00IMHF R.1.0.0 10.28.2018 td: 6.500ms\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14\x00'
ID_sn = decode_bytes(device_read, 0, 7)
ID_fw = decode_bytes(device_read, 8, 47)
Press_sens = device_read[48]
IMU_type = device_read[49]
IMU_sn = decode_bytes(device_read, 50, 57)
IMU_fw = decode_bytes(device_read, 58, 97)
label_fmt = '{:>10}: {!r}'
print(label_fmt.format('ID_sn', ID_sn))
print(label_fmt.format('ID_fw', ID_fw))
print(label_fmt.format('Press_sens', Press_sens))
print(label_fmt.format('IMU_type', IMU_type))
print(label_fmt.format('IMU_sn', IMU_sn))
print(label_fmt.format('IMU_fw', IMU_fw))
输出:
ID_sn: 'M1830130'
ID_fw: 'A2IMU v3.2.9.1 26.04.19'
Press_sens: 2
IMU_type: 5
IMU_sn: '27641'
IMU_fw: 'IMHF R.1.0.0 10.28.2018 td: 6.500ms'