Python中的SerialTimeoutException未按预期工作

2024-06-06 03:14:24 发布

您现在位置:Python中文网/ 问答频道 /正文

有时,我无法通过串行接口获取任何数据,我希望通过以下方式捕获此情况,但有一个例外:

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()

但是,当我运行这个程序时,如果没有 数据在缓冲区中,我陷入了一个无止境的循环。任何人一个主意 我在这里做错了什么?


Tags: 数据read方式timeoutserial情况somedump