python - undefined variable : SerialException

标签 python exception serial-port pyserial

我正在使用 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/

相关文章:

python - 如何完全删除python目录?

java - 无法发送消息,无法从我的串行端口连接设备中读取响应

java - Spring:REST 异常被捕获为 403 Forbidden 异常

java arduino串口通信

bash - 串行数据重复

python - 将文件名添加到 .csv 文件开头的第一列

Python - Openpyxl - "UserWarning: Unknown extension"问题

python - 删除 Pandas DataFrame 中重复超过 5 次的值

java - 对 Tomcat 日志文件的惊人攻击

java - ArrayIndexOutOfBound 异常