python - Python 中的 SerialTimeoutException 未按预期工作

标签 python serial-port

有时我没有通过串行接口(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/

相关文章:

python - 无法在 mac os x 上使用 python 和 opencv2 编写视频

c# - IronPython/C# 脚本运行后无法重新打开串行端口

python - 如何自动获取端口号。 python串行通信中的硬件?

Android 2.1蓝牙SPP转LM058(串口线更换)问题

python - 如何使用 python 查找 iframe 中按钮的 XPath?

python - 从 python 中的数据帧矩阵打印标题值

python - Django 是否有可以检测 URL 并将其转换为超链接的模板标记?

c# - .NET SerialPort Write() 添加字符和移动字节

vb.net - 串口读取包括vb.net中已经写入的内容

python - 如何在 python 中弹出子列表的内容?