serial.serialutil.SerialException:[Errno 16] 无法打开端口:[Errno 16] 设备或​​资源繁忙:'/dev/ttyACM0'

Ram*_*lez 2 python linux arduino pyserial python-3.x

我正在尝试使用 Pyserial python 库将 Arduino Uno 的串行输出打印到我的屏幕上。

这是我的 Arduino 代码。它只是生成随机数并将其打印到串行监视器:

void setup() {
Serial.begin(9600);

}

void loop() {
    long rand = random(10);
    Serial.print(rand);
}
Run Code Online (Sandbox Code Playgroud)

我的 python 代码只是应该将序列中的值打印到命令行上,代码如下:

#!/usr/bin/python

import serial

ser = serial.Serial("/dev/ttyACM0",9600)
while True:
    thing = ser.readline()
    print(thing)
Run Code Online (Sandbox Code Playgroud)

当 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'
Run Code Online (Sandbox Code Playgroud)

gre*_*gor 6

不能让两个程序使用同一个串行端口。

关闭 Arduino IDE 中的串行监视器。


PS:您还尝试读取 Python 中的行,但您不是从 Arduino 发送它们。

将数字打印为带有 的行Serial.println(rand);