Skip to main content

Python 串口库 pyserial

Python Serial Port Extension for Win32, OSX, Linux, BSD, Jython, IronPython ...

安装 pyserial

pip install pyserial

简单使用

  • 开启串口
import serial

_ser = serial.Serial(port="/dev/ttyACM0", baudrate=115200, timeout=0.01)
  • 接收串口
str_read = _ser.read(6).hex()
# 6 读取六个字节
# .hex() 十六进制读取
  • 发送串口
str_write = 'write_str'
_ser.write(str_write)
  • 关闭串口
_ser.close()

Hex to int

16进制转为int

hex_input  = 'C0A80026'
output_list = []
for i in range(0,len(hex_input),2)
output_list.append(int(hex_input[i:i+2], 16))

参考