将 ser.read 输出转换为列表中的有效元素 - Python

Convert ser.read output to valid elements in a list - Python

我需要从串口读取数据并将其插入到列表中的位置 1 到 16。

rxbuf[1:16] = ser.read(16) 

虽然我得到如下错误:

TypeError: 'bytes' object does not support item assignment

如果我打印串口的输出,

rxbuf=ser.read(16)

输出如下所示:

b'\xf2\x97\x00\x00\x8er\x9a\xc0\x14\xff\xff|:F\x18\x00'

有没有办法转换上面的内容,并将其放入列表中? 我需要的是rxbuf[1] = 0xF2, rxbuf[2] = 0x97等等

试试这个

rxbuf=ser.read(16) rxbuf = ser.read(16).split("\") --- 因为您的响应被“\”正斜杠分割 它应该像这样打印...... rxbuf[1] = 0xF2 rxbuf[2] = 0x97

https://www.mkyong.com/python/python-how-to-split-a-string/