0

I want to send values from a Python program on a Raspberry Pi to an Arduino. My RPi program looks like that:

import time, serial
time.sleep(3)
ser = serial.Serial('/dev/ttyACM0', 9600)
ser.write(b'5')

My Arduino program:

void setup() {
    Serial.begin(9600);                    // set the baud rate
    Serial.println("Ready");               // initial "ready" signal
}

void loop() {
    char inByte = ' ';
    if(Serial.available()) {         
        inByte = Serial.read();
        Serial.println(inByte);
    } 
    delay(100)
}

My problem is that when I run the program in Python and afterwards open the serial monitor in the Arduino IDE, it only shows "ready", but not the sent value. If I want to have the monitor open at the same time as the program, there is the error:

Device or resource busy: '/dev/ttyACM0'

I am using Python 3.6 by the way.

I am sorry if this is an easy error, but I am very new to serial and Arduino.

Michael S
  • 104
  • 11
  • 1
    This makes sense, as the serial monitor you are opening in the Arduino IDE is the same serial interface that is being used by your python program, so you can't use them at the same time. Have you tried doing a read on the python side to see if the byte is being sent back from your `println`? – Easton Bornemeier Aug 08 '18 at 14:37
  • Possible duplicate of [serial.serialutil.SerialException: \[Errno 16\] could not open port: \[Errno 16\] device or resource busy: '/dev/ttyACM0'](https://stackoverflow.com/q/50356224) – gre_gor Aug 08 '18 at 14:54
  • Possible duplicate of [avrdude: ser\_open(): can't open device "/dev/ttyACM0": Device or resource busy](https://stackoverflow.com/questions/40951728/avrdude-ser-open-cant-open-device-dev-ttyacm0-device-or-resource-busy) – Parth Sarthi Sharma Aug 08 '18 at 15:27

1 Answers1

0

You can connect to your Arduino serial port from only one application at a time. When you are connected to it via Serial port Monitor, Python couldn't connect to it.

You have two variants of solution:

  1. Use serial sniffer instead of Arduino IDE's Serial Monitor. There are another answered question on this topic: https://unix.stackexchange.com/questions/12359/how-can-i-monitor-serial-port-traffic

  2. Don't use any serial monitor, use Python! You can just continiualy read from serial, and print what have been received, something like this:

    import time, serial
    time.sleep(3)
    ser = serial.Serial('/dev/ttyACM0', 9600)
    # Write your data:
    ser.write(b'5')
    
    # Infinite loop to read data back:
    while True:
        try:
            # get the amount of bytes available at the input queue
            bytesToRead = ser.inWaiting() 
        if bytesToRead:
            # read the bytes and print it out:
            line = ser.read(bytesToRead) 
            print("Output: " + line.strip())
    except IOError:        
        raise IOError()
    
MihanEntalpo
  • 1,952
  • 2
  • 14
  • 31
  • I now got it working, thanks! Is it now possible to also communicate with the RPi via Bluetooth (RPi and Smartphone) or would that use the serial port which is already being used by the Arduino / RPi communication? – Michael S Aug 09 '18 at 12:25
  • Did my answer helped you, or you just figure out yourself how to deal with the problem? If you'll connect bluetooth dongle to your RPi, it wouldn't utilize your serial port. If your RPi has bluetooth chip built-in, also, shouldn't be any problem with serial. It'sa arduino, who limited with serial ports, not the RPI, which is more like the PC than microcontroller platform. – MihanEntalpo Aug 10 '18 at 04:30
  • At first I tried your code, but it somehow still didn't work, then I found this guide: http://forum.arduino.cc/index.php?topic=396450 and it worked perfectly as I wanted it :) And I'm glad that bluetooth will be possible, now I just have to figure out how. – Michael S Aug 10 '18 at 11:43
  • Ok, it's always better to read the docs first when you try to make something with the technology :) – MihanEntalpo Aug 10 '18 at 12:40