I’m communicating between a python script on a PC and embedded c. I have the following issue:
From the mcu (embedded c), I’m sending the binary representation of integers and floats. Using Br@y’s terminal, I have verified that the binary representation of the numbers are being sent from the mcu. In the python script, I use pySerial’s serial.read to get bytes, putting them into a list.
If I send the following number from the mcu: 0x10000000 (16 base 10, in little endian), it shows up in the python script as ‘\x10\x00\x00\x00’, which isn’t horrible, since I can just pull characters out of consistent indices. However, when two hex digits represent an ascii character, the string will have the ascii character instead of \xDD.
Is there any way to force the string to use only the \xDD representation, rather than the ascii values? Or will I have to just iterate over the string, removing ‘\x’ and converting the ascii values as I go?
Thanks
Are you sure the
\xis in the string? That looks like Python’s representation string (repr) for binary data, but the characters\xare not actually in the string.The struct module can unpack the binary data.
iis now 16 (int).