问题
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