serial.serialutil.SerialException: [Errno 16] could not open port: [Errno 16] device or resource busy: '/dev/ttyACM0'
Solution 1
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);
.
Solution 2
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.
Related videos on Youtube
Ramces Gonzalez
I am a self-taught programmer who enjoys writing code in python and developing useful tools for others. "Unfortunately, no one can be told what the Matrix is. You have to see it for yourself." -Morpheus: The Matrix (1999)
Updated on June 04, 2022Comments
-
Ramces Gonzalez almost 2 years
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'
-
skrrgwasme about 6 yearsJust to make sure I'm understanding this correctly: Your Arduino is connected to a computer via a serial-to-USB cable, and your Python script is running on the computer trying to read what the Arduino is writing to the serial port? Is that right?
-
skrrgwasme about 6 yearsAlso, take a look at this similar question: stackoverflow.com/q/40951728/2615940. It's not quite a duplicate, but may have some useful info.
-
Ramces Gonzalez about 6 yearsHi, yes, that is correct. Thanks, I will take a look at that link.
-
-
Ramces Gonzalez about 6 yearsThanks for the suggestion, I tried closing the serial monitor for the Arduino and then I ran the python script. However, this time, nothing happens, the cursor just sits below the command line. I think that it is waiting for the Arduino to stop printing values to serial. I'll try adding a Serial.end(); to my Arduino code.
-
Ramces Gonzalez about 6 yearsI tried using Serial.end() but the python script gave the same error: 'resource busy'
-
gre_gor about 6 years
Serial.end()
just disables the serial on the Arduino. It doesn't enable access of multiple programs to a single serial port. -
Ramces Gonzalez about 6 yearsI changed
Serial.print(rand);
toSerial.println(rand);
still get the same error 'resource busy', I'm out of ideas for now. -
Ramces Gonzalez about 6 yearsOk, I made a little progress, I excluded the line
Serial.begin(9600);
in my Arduino code and the following program ran without issues:#!/usr/bin/python import serial ser = serial.Serial("/dev/ttyACM0",9600) print(ser.name) ser.close()
the output was/dev/ttyACM0
-
Ramces Gonzalez about 6 yearsHowever, I still can't get the python program to print out each value coming from the Arduino
-
gre_gor about 6 yearsWithout
Serial.begin(9600);
Arduino won't send anything. If you get "resource busy" then something else on your computer is connected to that serial port. Read: Kill process that raises Device or resource busy: '/dev/ttyUSB0'?