我正在用 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')
当我运行代码时,我得到:
C:\Users\אורי\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/אורי/PycharmProjects/untitled2/test.py
Traceback (most recent call last): File "C:/Users/����/PycharmProjects/untitled2/test.py", line 1, in import serial ImportError: No module named serial
最佳答案
你是如何安装串口模块的? 如果你想确保它会被检测到, 进入你的控制台
pip install serial
并从控制台运行你的代码
python test.py # make sure your console is in the right folder path
或
找到模块的安装位置,比如“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
关于python - "ImportError: No module named serial"- 安装 pyserial 之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49916535/