Search code examples
pythonarduinonetwork-programmingudpserversocket

UDP- Trouble reading bytes from socket in python?


I'm trying to send 14bit sensor data from a microcontroller to a PC using UDP Protocol. When I send the data and receive it on the package sender application I am getting data in hex as expected.

Energia Code:

Python, Package Sender app screenshots

Here, I am receiving it as char.

Sensor value in decimal: (855) --- hex(357) higher byte 03, lower byte 57. 57h is char W in Ascii table

So when received through the socket, python outputs this as 03W

How to receive in hex and convert it to decimal? Thank you in advance!!


Solution

  • You can use the struct module to unpack it like this:

    import struct
    data = ['\x03W']
    val = struct.unpack('>H', data[0])  # now an integer