Arduino 'Error communicating…unicode strings are not supported, please encode to bytes' PySerial

回眸只為那壹抹淺笑 提交于 2019-12-25 09:12:10

问题


I'm trying to connect to my MultiWii over PySerial, however I keep getting this error.

Error communicating...unicode strings are not supported, please encode to bytes: '$M<\x00ll'

This is the part of the code that's failing:

BASIC="\x24\x4d\x3c\x00"
MSP_ATTITUDE=BASIC+"\x6C\x6C"
ser.write(MSP_ATTITUDE)

I've tried encoding the strings with .encode() in which I get this error:

Error communicating...'bytes' object has no attribute 'encode'

I've tried bytearray(MSP_ATTITUDE,'ascii') and get the previous error.

I'm only asking this because this error circle doesn't really make sense. Can anyone help? I can provide more information regarding the code if it'll help.

Thanks in advance


回答1:


Don't create strings.

Strings are for unicode text, which \x24\x4d\x3c\x00 is not.

For arbitrary bytes, use byte strings. You can construct those directly using b''.

>>> type('foo')
<class 'str'>

>>> type(b'foo')
<class 'bytes'>



回答2:


You should try:

BASIC = b"\x24\x4d\x3c\x00"
MSP_ATTITUDE = BASIC + b"\x6C\x6C"

So that they are treated as bytes objects and not unicode strings.



来源:https://stackoverflow.com/questions/39692285/arduino-error-communicating-unicode-strings-are-not-supported-please-encode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!