首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >串行通信: python

串行通信: python
EN

Stack Overflow用户
提问于 2017-11-07 10:14:21
回答 1查看 723关注 0票数 0

我正在使用PythonAnaconda2.7。我想使用串行通信切换端口,但是我收到了错误:AttributeError: 'module' object has no attribute 'Serial'

我的示例程序是:

代码语言:javascript
复制
import serial 
import time 
#remember to adjust “COM3”
port = serial.Serial("COM3", 19200, timeout=0.5) 
#turn on port 2, sleep 2 seconds, turn off port 2 
port.write(b"\nF2\r") 
time.sleep(2.0) 
port.write(b"\nF2\r")             
#turn on port 2 and 7, sleep 2 seconds, turn off port 2 and 7 
port.write(b"\nF2\r\nF7\r") 
time.sleep(2) 
port.write(b"\nF2\r\nF7\r")             
#close the port 
port.close()

我尝试过许多解决方案:

  1. 将文件名从“serial.py”改为“any_other_name.py”,反之亦然
  2. 删除相关的.pyc文件
  3. 安装“pip install pyserial”
  4. 从串行导入序列执行

当我从精神病患者那里运行相同的程序时,它工作得很好。我不知道怎么解决。如果有人能给我建议,这对我会有很大的帮助。提前感谢你。

拉维

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-07 10:24:11

您的代码似乎很好,所以您遇到的问题可能是由于错误的导入。

您确实应该避免将您的python脚本命名为“标准”模块(serial.py,string.py.),因为这样做,您就暴露了意外地导入这些文件而不是正确的文件(可能发生在您身上的情况)。

如果您需要确定您要导入的内容,请尝试如下:

代码语言:javascript
复制
import serial
print serial.__file__  # this will tell you the position of the file you've imported and help you to be sure of what you're using.

# in case you're not importing a module, only a class... try this : 
help(serial) # even without any help, it will give you at the end the path where this object is defined :)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47155256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档