aseylys
aseylys

Reputation: 344

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

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

Upvotes: 0

Views: 2029

Answers (2)

SoreDakeNoKoto
SoreDakeNoKoto

Reputation: 1185

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.

Upvotes: 0

marcelm
marcelm

Reputation: 1080

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'>

Upvotes: 0

Related Questions