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?

dda
  • 1,595
  • 1
  • 12
  • 17
Luz A
  • 69
  • 2
  • 10

3 Answers3

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();
   }
  }
Visual Micro
  • 1,027
  • 6
  • 13
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
  }

}
Mazaryk
  • 1,149
  • 6
  • 16
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.

Luz A
  • 69
  • 2
  • 10