Python PySerial 读取行超时
我正在使用 pyserial 与嵌入式设备进行通信。
ser = serial.Serial(PORT, BAUD, timeout = TOUT)
ser.write(CMD)
z = ser.readline(eol='\n')
因此,我们向设备发送 CMD,它会回复一个以 '\n'
结尾的长度可变的字符串,
如果设备无法重播,则 readline()
超时,并且z=''
如果设备中断或崩溃,它会发送数据,然后 readline()
超时 z 将是一个末尾没有 '\n'
的字符串。
除了检查 z 的状态之外,是否有一种好方法来检查 readline() 是否超时。
I'm using pyserial to communicate with a embedded devise.
ser = serial.Serial(PORT, BAUD, timeout = TOUT)
ser.write(CMD)
z = ser.readline(eol='\n')
So we send CMD to the device and it replies with an string of varing length ending in a '\n'
if the devise cant replay then readline()
times-out and z=''
if the devise is interrupted or crashes will it's sending the data then readline()
times-out
and z will be a string without a '\n'
at the end.
Is there a nice way to check if readline()
has timed-out other than checking the state of z.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你可能想做的是..
I think what you might like to do is..
PySerial 有 read_until 方法正是为此,它读取序列号,直到找到预期的序列(默认为“\n”)、超出大小或直到发生超时。
PySerial has read_until method exactly for this, it reads the serial until an expected sequence is found (‘\n’ by default), the size is exceeded or until timeout occurs.