2

I have a Raspberry Pi 3 connected to the serial of my Arduino Uno (pins 0 and 1), but I also have another device connected to two other serial pins on the Arduino (pins 10 and 11, using the SoftwareSerial library).

What I want to do is send a string from the RPI, then the Arduino gets it and resends that information through the other serial port to my device.

Until now the Arduino can receive the information from the RPI, but I'm not sure how to send that same string through the other serial port, the one going to the device.

Here is the code I'm using:

#include <SoftwareSerial.h>

String b = "0";
SoftwareSerial portOne(2, 3); //RX= pin 2  TX= pin 3

void setup() {
  Serial.begin(9600, SERIAL_8N1); 
  portOne.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    b = Serial.readString();
    Serial.println("Data from RPI:");
  }
  portOne.listen();
  while(portOne.available() > 0){
      Serial.println("Data from port one:");
      Serial.println(b);
    }
  }

I just get the data coming from the RPI, but nothing from portOne.

Any thoughts?

2
  • You might find this interesting about buffers on both sides of a Nano connected to a Raspian system running a Python data logger, using just a regular USB programming cable between the two: arduino.stackexchange.com/questions/11710/…
    – SDsolar
    Aug 24, 2017 at 23:29
  • Instead of just using pins 1 and 0 I use the regular serial-to-USB programming cable. Ont he Pi it comes in as /dev/ttyUSB0 or /dev/tty ACM0 depending on the board. The main advantage of doing this is that you can WiFi int o your Pi with RDP and remotely reprogram the ASrduinos on the fly as necessary. Your strange readings may well be because of the way you are cabling to the module. Perhaps try software.serial and regular pins?
    – SDsolar
    Sep 20, 2017 at 7:23

3 Answers 3

2

Your code is not reading the SoftwareSerial. b is only populated when the Pi sent data. The loop below sends what it reads from the SoftwareSerial, after that is prints a new line println() (only because that's what your code is doing)

The listen() method of SoftwareSerial is only required if you have more than one SoftwareSerial connection.

#include <SoftwareSerial.h>

String b = "0";
SoftwareSerial portOne(2, 3); //RX= pin 2  TX= pin 3

void setup() {
  Serial.begin(9600, SERIAL_8N1); 
  portOne.begin(9600);
} 

void loop() {
  if (Serial.available() > 0) {
    b = Serial.readString();
    Serial.println("Data from RPI:");
  }

 //not required with only one software serial connection
 //portOne.listen();

  if (portOne.available() > 0)
  {
   while(portOne.available() > 0){
      byte byteRead = portOne.read();
      Serial.write(byteRead);
    }
    Serial.println();
   }
  }
4
  • I tried something like this with no luck. I read that a limitation SoftwareSerial has, is that the ports can retrieve data one at a time. Could this be affecting me? should I interrupt the serial port between the RPI and the Arduino?
    – Luz A
    Apr 27, 2017 at 20:19
  • Try the softwareSerial on pins 2 and 3 otherwise post a code example. You haven't given much information such as baud rates so the issue could be anything. There isn't normally an issue using serial and soft serial together. Apr 28, 2017 at 10:46
  • I added an Edit of the question where I added the code.
    – Luz A
    Apr 28, 2017 at 19:02
  • I have updated the answer, see if that makes sense to you. Apr 30, 2017 at 4:02
1

If you goal is to relay a string from RPI through the Arduino to a device connected via SoftwareSerial then you should be doing something like this:

#include <SoftwareSerial.h>

String b = "0";
SoftwareSerial portOne(2, 3); //RX= pin 2  TX= pin 3

void setup() {
  Serial.begin(9600, SERIAL_8N1); 
  portOne.begin(9600);
} 

void loop() {

  if (Serial.available() > 0) { // data available on HW Serial
    b = Serial.readString();    // Read it
    portOne.print(b);           // and write it to SW Serial
  }

}
0

After reading more about the SoftwareSerial, I managed to get the communication I wanted in a simpler way. This is what I ended up using in my void loop:

void loop() { // run over and over

  if (Serial.available()>0) {
    HW = Serial.read();
    mySerial.write(HW);

  }

  if (mySerial.available()>0) {
    SW = mySerial.read();
    Serial.write(SW);
  }
}

Thank you all for your help, your answers gave me also a better understanding of what I was doing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.