【发布时间】:2022-02-02 15:50:31
【问题描述】:
我有从串口读取数据的代码。
try:
dataIn = self.port.read(100)
except serial.SerialException:
#do some work
return None
如果我断开设备连接,我会捕捉到我无法处理的异常。
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/serial/serialposix.py", line 475, in read
raise SerialException('device reports readiness to read but returned no data (device disconnected or multiple access on port?)')
serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected or multiple access on port?)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/katkov/work/obd2rds/obd2rds/testing/python/main.py", line 48, in <module>
main()
File "/home/katkov/work/obd2rds/obd2rds/testing/python/main.py", line 41, in main
dataIn = serPort.read()
File "/home/katkov/work/obd2rds/obd2rds/testing/python/uart.py", line 55, in read
dataIn = self.port.read(100)
File "/usr/local/lib/python3.4/dist-packages/serial/serialposix.py", line 480, in read
if e[0] != errno.EAGAIN:
TypeError: 'SerialException' object does not support indexing
我怎样才能捕捉到它被正确处理的异常。 谢谢!
【问题讨论】:
-
据我所知,pySerial 不支持您在此处使用的 python 3.4。因此,您需要根据需要使用不同的库,或者在 python 2.7 中重写代码。我建议找一个不同的库。
-
如前所述here python3.x 可用。