1. 安装pyserial
pip3 install pyserial
2. 使用方式
config.py
import serial
port = "COM1"
baudrate = 2400
bytesize = serial.SEVENBITS
stopbits = serial.STOPBITS_TWO
parity = serial.PARITY_NONE
timeout = 10
main.py
import serial
import config
ser = serial.Serial(port=config.port,baudrate=config.baudrate,bytesize=config.bytesize, stopbits=config.stopbits, parity=config.parity, timeout=config.timeout)
if ser.isOpen():
print("打开串口成功")
else:
print(f'打开串口失败')
while True:
try:
data = ser.readline()
print(data)
except Exception as e:
print('接收消息数据出错')
标签:parity,通讯,ser,python,串口,print,serial,config
From: https://www.cnblogs.com/wstong2052/p/17555378.html