加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 未定义的变量:SerialException

发布时间:2020-12-20 12:21:34 所属栏目:Python 来源:网络整理
导读:我正在使用pySerial库从Arduino获取 Python脚本日志数据.我正在尝试处理SerialException,当脚本无法连接到您提供的端口时,Eclipse会说“Undefined variable:SerialException”.有什么我忘了导入的东西吗? 码: try: ser = serial.Serial(port,9600) connec
我正在使用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派生的类.因此,当一个模块/包定义它自己的自定义异常时,它们通常会得到imported in the module/packages’s namespace just like the other classes/functions.这就是说,放一个:

from serial import SerialException

在你的文件的顶部可能也会做到这一点.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读