serial.serialutil.SerialException: [Errno 16] could not open port: [Errno 16] device or resource busy: '/dev/ttyACM0'

僤鯓⒐⒋嵵緔 提交于 2019-12-08 12:57:49

问题


I'm trying to print serial output from Arduino Uno onto my screen using Pyserial python library.

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);
}

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)

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'

回答1:


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.




回答2:


You can't have two programs use the same serial port.

Close the serial monitor in the Arduino IDE.


PS: You are also trying to read lines in Python, but you aren't sending them from the Arduino.

Print the numbers as lines with Serial.println(rand);.



来源:https://stackoverflow.com/questions/50356224/serial-serialutil-serialexception-errno-16-could-not-open-port-errno-16-de

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!