我正在使用 pySerial 库从 Arduino 获取 Python 脚本日志数据。当脚本无法连接到您提供的端口时,我正在尝试处理 SerialException,并且 Eclipse 显示“ undefined variable :SerialException”。有没有我忘记导入的东西?
代码:
try:
ser = serial.Serial(port, 9600)
connected = 1
except SerialException:
print "No connection to the device could be established"
最佳答案
你可能想要:
except serial.SerialException:
...
在 python 中,Exception
是派生自 Exception
的类。因此,当一个模块/包定义它自己的自定义异常时,它们通常会得到 imported in the module/packages's namespace just like the other classes/functions。 .这就是说,放一个:
from serial import SerialException
在你的文件的顶部可能也可以做到这一点。
关于python - undefined variable : SerialException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14504438/