pyserial是python下读写串口的模块,项目正好需要使用,有个坑,需要填一下。
正常情况下,只需要
import serial
ser = serial.Serial(port='/dev/ttyUSB1', baudrate=9600,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE
)
这样就可以对串口进行读写了。但是今天用在TX2上面,就报错了:IOError: [Errno 32] Broken pipe
同样的代码在台式机上面使用,就没有问题。而且之前也在TX2上面用过。在搜索后,经过尝试,需要在上述配置里面,再增加两项rtscts=True,dsrdtr=True,即:
import serial
ser = serial.Serial(port='/dev/ttyUSB1', baudrate=9600,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
rtscts=True,dsrdtr=True,
)
这样才能正常打开使用。