Python 串口库 pyserial
Python Serial Port Extension for Win32, OSX, Linux, BSD, Jython, IronPython ...
安装 pyserial
- pip
- conda
pip install pyserial
conda install -c conda-forge 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)
- 按位发送
data = []
ser.write(bytes(data))
- 关闭串口
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))