Janak Varma Janak Varma - 1 year ago 98
Python Question

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:

Package Sender
app screenshots

Here, I am receiving it as char.

Sensor value in decimal:
(855) --- hex(357)
higher byte 03, lower byte 57.
is char
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!!

Answer Source

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

import struct
data = ['\x03W']
val = struct.unpack('>H', data[0])  # now an integer
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download