我正在使用PythonAnaconda2.7。我想使用串行通信切换端口,但是我收到了错误:AttributeError: 'module' object has no attribute 'Serial'
我的示例程序是:
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()我尝试过许多解决方案:
当我从精神病患者那里运行相同的程序时,它工作得很好。我不知道怎么解决。如果有人能给我建议,这对我会有很大的帮助。提前感谢你。
拉维
发布于 2017-11-07 10:24:11
您的代码似乎很好,所以您遇到的问题可能是由于错误的导入。
您确实应该避免将您的python脚本命名为“标准”模块(serial.py,string.py.),因为这样做,您就暴露了意外地导入这些文件而不是正确的文件(可能发生在您身上的情况)。
如果您需要确定您要导入的内容,请尝试如下:
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 :)https://stackoverflow.com/questions/47155256
复制相似问题