在Python中接收16位整数。
16lz
2021-01-22
I'm reading 16-bit integers from a piece of hardware over the serial port.
我正在通过串口从一块硬件上读取16位整数。
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?
使用Python,如何正确地获取LSB和MSB,并使Python理解我正在处理的是一个16位有符号整数,而不仅仅是两个字节的数据?
1 个解决方案
#1
21
Try using the struct module:
尝试使用struct模块:
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
更多相关文章
- Python3 串口两进程同时读写
- 'str'对象不能解释为groupby上的整数
- 用python将二进制整数或字符串写入文件
- 我的电脑认为有符号整数比-1小?
- Linux 下串口编程之三 termios结构体介绍
- tty、串口、console等的区别
- Linux串口应用编程
- 请教用sql loader导入数据,最后一位是整数的问题
- Android studio 中NDK的配置和JNI实现的完整过程SerialPort andr