我正在用 python 编写一个项目,该项目最终应该在 LinkIt One IoT 设备上运行。
我已经编写了一些测试代码来检查我是否能够在 Arduino IDE 和 python 之间进行连接(我正在使用 Pycharm)。
测试代码是:
import serial
import time
arduino = serial.Serial('COM1', 115200, timeout=.1)
time.sleep(1) #give the connection a second to settle
arduino.write("Hello from Python!")
while True:
data = arduino.readline()
if data:
print data.rstrip('\n')
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,我得到:
C:\Users\????\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/????/PycharmProjects/untitled2/test.py
回溯(最近一次调用最后一次):文件“C:/Users/????/PycharmProjects/untitled2/test.py”,第 1 行,在导入序列中导入错误:没有名为序列的模块
你是如何安装串口模块的?如果你想确保它会被检测到,请进入你的控制台
pip install serial
Run Code Online (Sandbox Code Playgroud)
并从控制台运行您的代码
python test.py # make sure your console is in the right folder path
Run Code Online (Sandbox Code Playgroud)
或者
找到模块的安装位置,例如“C:\Python27\Lib\site-packages”
import sys
sys.path.append("C:\Python27\Lib\site-packages") # this is where python stores modules, yours could be different
import serial
Run Code Online (Sandbox Code Playgroud)