我正在通过串行端口从硬件读取 16 位整数。
使用Python,我怎样才能得到正确的LSB和MSB,并使Python明白它是我正在摆弄的16位有符号整数,而不仅仅是两个字节的数据?
尝试使用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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)