I'm reading 16 bit integers from a piece of hardware over the serial port.
Using python, how can I get the LSB and MSB right, and make python understand that it is a 16 bit signed integer I'm fiddling with, and not just two bytes of data?
I'm reading 16 bit integers from a piece of hardware over the serial port.
Using python, how can I get the LSB and MSB right, and make python understand that it is a 16 bit signed integer I'm fiddling with, and not just two bytes of data?
Try using the struct module:
import struct
# read 2 bytes from hardware as a string
s = hardware.readbytes(2)
# h means signed short
# < means "little-endian, standard size (16 bit)"
# > means "big-endian, standard size (16 bit)"
value = struct.unpack("<h", s) # hardware returns little-endian
value = struct.unpack(">h", s) # hardware returns big-endian