有时,我无法通过串行接口获取任何数据,我希望通过以下方式捕获此情况,但有一个例外:
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()
但是,当我运行这个程序时,如果没有 数据在缓冲区中,我陷入了一个无止境的循环。任何人一个主意 我在这里做错了什么?
我甚至不知道那个例外。在查看the API documentation之后,您将看到此异常仅适用于
write()
。如果您
read()
,您将得到一个缩短的甚至是空的输出。这只适用于在超时情况下打开连接的情况。类似于
serial.Serial(3, timeout=.1)
。相关问题 更多 >
编程相关推荐