2

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'
Ramces Gonzalez
  • 139
  • 1
  • 4
  • 16
  • 1
    Just 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 May 15 '18 at 18:21
  • Also, take a look at this similar question: https://stackoverflow.com/q/40951728/2615940. It's not quite a duplicate, but may have some useful info. – skrrgwasme May 15 '18 at 18:25
  • Hi, yes, that is correct. Thanks, I will take a look at that link. – Ramces Gonzalez May 16 '18 at 02:14

2 Answers2

6

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

gre_gor
  • 6,669
  • 9
  • 47
  • 52
  • Thanks 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 May 15 '18 at 18:08
  • I tried using Serial.end() but the python script gave the same error: 'resource busy' – Ramces Gonzalez May 15 '18 at 18:34
  • `Serial.end()` just disables the serial on the Arduino. It doesn't enable access of multiple programs to a single serial port. – gre_gor May 15 '18 at 18:42
  • I changed `Serial.print(rand);` to `Serial.println(rand);` still get the same error 'resource busy', I'm out of ideas for now. – Ramces Gonzalez May 15 '18 at 23:38
  • Ok, 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 May 16 '18 at 02:03
  • However, I still can't get the python program to print out each value coming from the Arduino – Ramces Gonzalez May 16 '18 at 02:13
  • Without `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'?](https://stackoverflow.com/q/8359489) – gre_gor May 16 '18 at 14:45
3

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.

Ramces Gonzalez
  • 139
  • 1
  • 4
  • 16