【发布时间】:2018-10-25 15:25:50
【问题描述】:
我正在尝试使用 Pyserial python 库将 Arduino Uno 的串行输出打印到我的屏幕上。
这是我的 Arduino 代码。它只是生成随机数并将其打印到串行监视器:
void setup() {
Serial.begin(9600);
}
void loop() {
long rand = random(10);
Serial.print(rand);
}
我的 python 代码只是应该将值从串行打印到命令行,这里是代码:
#!/usr/bin/python
import serial
ser = serial.Serial("/dev/ttyACM0",9600)
while True:
thing = ser.readline()
print(thing)
当 Arduino 向串行监视器打印随机数时,我运行我的 python 脚本并得到错误:
Traceback (most recent call last):
File "/home/archimedes/anaconda3/lib/python3.6/site-packages/serial/serialposix.py", line 265, in open
self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
OSError: [Errno 16] Device or resource busy: '/dev/ttyACM0'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "pythonSerialTest.py", line 6, in <module>
ser = serial.Serial("/dev/ttyACM0",9600)
File "/home/archimedes/anaconda3/lib/python3.6/site-packages/serial/serialutil.py", line 240, in __init__
self.open()
File "/home/archimedes/anaconda3/lib/python3.6/site-packages/serial/serialposix.py", line 268, in open
raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg))
serial.serialutil.SerialException: [Errno 16] could not open port /dev/ttyACM0: [Errno 16] Device or resource busy: '/dev/ttyACM0'
【问题讨论】:
-
只是为了确保我正确理解这一点:您的 Arduino 通过串行转 USB 电缆连接到计算机,并且您的 Python 脚本正在计算机上运行,试图读取 Arduino 的内容正在写入串行端口?对吗?
-
另外,看看这个类似的问题:stackoverflow.com/q/40951728/2615940。它不是完全重复的,但可能有一些有用的信息。
-
嗨,是的,这是正确的。谢谢,我会看看那个链接。
标签: python linux python-3.x arduino pyserial