compnaion
Posts: 15
Joined: Thu Apr 04, 2019 3:25 pm

Raspberry pi-Arduino communication, cannot send data from rpi to arduino.

Tue Jul 02, 2019 12:10 pm

I am using this https://classes.engineering.wustl.edu/e ... 26_Arduino, and can send message from ard to rasp. But when I try to send anything from rasp to ard, it doesn't work.

If I try to send the signal while serial monitor on the arduino IDE is closed, something happens, I mean arduino's leds blink at the very least.
If I open serial monitor this error occurs.

Code: Select all

Traceback (most recent call last):
  File "/usr/lib/python3/dist-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/ttyUSB0'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/Desktop/Python/Send_deneme.py", line 2, in <module>
    ser = serial.Serial('/dev/ttyUSB0', 9600)
  File "/usr/lib/python3/dist-packages/serial/serialutil.py", line 236, in __init__
    self.open()
  File "/usr/lib/python3/dist-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/ttyUSB0: [Errno 16] Device or resource busy: '/dev/ttyUSB0'

The code I use on Python

Code: Select all

import serial
ser = serial.Serial('/dev/ttyUSB0', 9600)
ser.write(b'3')
ser.write(b'5')
ser.write(b'7')
Code on IDE

Code: Select all

int r = 1;
void setup(){
  Serial.begin(9600);
}
void loop(){
  if(Serial.available()){         //From RPi to Arduino
    r = r * (Serial.read() - '0');  //conveting the value of chars to integer
    Serial.println(r);
  }
}
Why does this happens, and what should I do?

stevieb9
Posts: 15
Joined: Fri Jan 13, 2017 2:40 pm

Re: Raspberry pi-Arduino communication, cannot send data from rpi to arduino.

Tue Jul 02, 2019 3:19 pm

You can't use the physical serial on the Arduino to communicate with another device while the Serial Monitor is open. That's why you're getting "device busy".

If you need to use the Serial Monitor while communicating with other devices, instantiate and use a Software Serial interface for the Pi comms instead:

Code: Select all

#include <SoftwareSerial.h>

#define RX 2
#define TX 3

SoftwareSerial pi(RX, TX);

void setup(){
  pi.begin(9600);
   // ...
}

void loop() {
  if (pi.available > 0){
    // ...
  }
}
Then attach the Pi's serial pins to the new pins you define in your sketch as RX and TX.

compnaion
Posts: 15
Joined: Thu Apr 04, 2019 3:25 pm

Re: Raspberry pi-Arduino communication, cannot send data from rpi to arduino.

Tue Jul 02, 2019 6:38 pm

stevieb9 wrote:
Tue Jul 02, 2019 3:19 pm
You can't use the physical serial on the Arduino to communicate with another device while the Serial Monitor is open. That's why you're getting "device busy".

If you need to use the Serial Monitor while communicating with other devices, instantiate and use a Software Serial interface for the Pi comms instead:

Code: Select all

#include <SoftwareSerial.h>

#define RX 2
#define TX 3

SoftwareSerial pi(RX, TX);

void setup(){
  pi.begin(9600);
   // ...
}

void loop() {
  if (pi.available > 0){
    // ...
  }
}
Then attach the Pi's serial pins to the new pins you define in your sketch as RX and TX.
Oh I see. Thank you for the answer.

Return to “Advanced users”