serial.serialutil.SerialException:[Errno 16] 无法打开端口:[Errno 16] 设备或资源繁忙:'/dev/ttyACM0' [英] serial.serialutil.SerialException: [Errno 16] could not open port: [Errno 16] device or resource busy: '/dev/ttyACM0'
问题描述
我正在尝试使用 Pyserial python 库将 Arduino Uno 的串行输出打印到我的屏幕上.
I'm trying to print serial output from Arduino Uno onto my screen using Pyserial python library.
这是我的 Arduino 代码.它只是生成随机数并将其打印到串行监视器:
Here is my Arduino code. It just produces and prints random numbers to serial monitor:
void setup() {
Serial.begin(9600);
}
void loop() {
long rand = random(10);
Serial.print(rand);
}
我的python代码只是应该将串行值打印到命令行上,这是代码:
My python code is just supposed to print the values from serial onto the command line, here is the code:
#!/usr/bin/python
import serial
ser = serial.Serial("/dev/ttyACM0",9600)
while True:
thing = ser.readline()
print(thing)
当 Arduino 向串行监视器打印随机数时,我运行了我的 python 脚本并得到错误:
While Arduino is printing random numbers to the serial monitor, I run my python script and get the error:
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'
推荐答案
我收到 resource busy 错误
因为 Python 正在尝试访问串行监视器,但是因为我将代码上传到 Arduino命令:sudo make upload monitor clean
,我的电脑正在从 Arduino 访问串行监视器,这阻止了 Python 能够从 Arduino 访问串行监视器.现在,如果我打算使用 Python 访问串行监视器,我只需使用 sudo make upload clean
将代码上传到 Arduino,并排除命令 monitor
.
I was getting the resource busy error
because Python was trying to access the serial monitor, but since I uploaded my code to Arduino with the command: sudo make upload monitor clean
, my PC was getting access to the serial monitor from Arduino, which prevented Python from being able to access the serial monitor from Arduino. Now I just upload the code to Arduino with sudo make upload clean
and exclude the command monitor
if I plan to use Python to access the serial monitor.
这篇关于serial.serialutil.SerialException:[Errno 16] 无法打开端口:[Errno 16] 设备或资源繁忙:'/dev/ttyACM0'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!