python中pyserial发送数据使用的方法是write()
关于write() 方法,需要了解如下几点:
pyserial接受数据使用的方法是read()
关于 read() 方法,需要了解如下几点:
以下是示例代码。
import serial
import time
# 打开 COM2,将波特率配置为9600.
ser = serial.Serial(port="COM2", baudrate=9600)
ser.close()
if not serialport.is_open:
serialport.open()
time.sleep(0.5) #时间设置参考串口传输速率
while True:
num = serialport.inWaiting()
if num > 0:
data = serialport.read(20)
print(data)