有时我没有通过串行接口(interface)获取任何数据,我想通过以下方式捕获这种情况:
ser = serial.Serial(3)
ser.baudrate = 115200
timeout = 1
while (some condidion)
try:
dump = ser.read(40)
except ser1.SerialTimeoutException:
print('Data could not be read')
ser.close()
但是,当我运行这个程序时,没有 数据在缓冲区中,我陷入了无限循环。任何人的想法 我在这里做错了什么?
最佳答案
我什至不知道那个异常(exception)。看了之后the API documentation ,您会看到此异常仅适用于 write()
。
如果你read()
,你只会得到一个缩短的甚至是空的输出。
这仅适用于您打开超时连接的情况。类似于 serial.Serial(3, timeout=.1)
。
关于python - Python 中的 SerialTimeoutException 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10978224/