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

更多相关文章

  1. Python3 串口两进程同时读写
  2. 'str'对象不能解释为groupby上的整数
  3. 用python将二进制整数或字符串写入文件
  4. 我的电脑认为有符号整数比-1小?
  5. Linux 下串口编程之三 termios结构体介绍
  6. tty、串口、console等的区别
  7. Linux串口应用编程
  8. 请教用sql loader导入数据,最后一位是整数的问题
  9. Android studio 中NDK的配置和JNI实现的完整过程SerialPort andr

随机推荐

  1. 如果没有匹配,则使用默认值执行左连接。
  2. mysql非安装包安装教程
  3. 解决Navicat数据传输问题:The‘InnoDB’f
  4. 如何在codeigniter上的一个提交中插入多
  5. Mysql数据库四大特性、事物的四个隔离、
  6. solr6.3与MySQL结合使用的简明教程(五)——
  7. mysql 判断null 和 空字符串
  8. 通过Bash脚本语言逃避MYSQL命令行。
  9. 如何创建一个新表,该表根据名称获取其他表
  10. 错误1452:无法添加或更新子行:外键约束失败