serial.write() 方法似乎只发送字符串数据.我有像 [0xc0,0x04,0x00] 这样的数组,并且希望能够通过串口发送/接收它们?是否有任何单独的原始 I/O 方法?
serial.write() method in pyserial seems to only send string data. I have arrays like [0xc0,0x04,0x00] and want to be able to send/receive them via the serial port? Are there any separate methods for raw I/O?
我想我可能需要将数组更改为 ['xc0','x04','x00'],但空字符可能会造成问题.
I think I might need to change the arrays to ['xc0','x04','x00'], still, null character might pose a problem.
您需要将数据转换为字符串
You need to convert your data to a string
"xc0x04x00"
空字符在 Python 中不是问题——字符串不是以空字符结尾的,零字节的行为就像另一个字节 "x00"
.
Null characters are not a problem in Python -- strings are not null-terminated the zero byte behaves just like another byte "x00"
.
一种方法:
>>> import array
>>> array.array('B', [0xc0, 0x04, 0x00]).tostring()
'xc0x04x00'
这篇关于带有pyserial(python串口)的二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!