Software serial with echo

Hi all. I'm working on a project that I will need to be able to monitor serial data between two Arduinos and eventually an Arduino and a Raspberry Pi. I'm having issues with getting an echo from the software serial to the serial monitor however when I use the serial port it works great. Here is my code.

Tx

#include<SoftwareSerial.h>

SoftwareSerial mySerial(2, 3);

void setup() {
  // Begin the Serial at 9600 Baud
  mySerial.begin(9600);
}

void loop() {
  mySerial.write("test"); //Write the serial data
  delay(100);
}

Rx

#include<SoftwareSerial.h>

SoftwareSerial mySerial(2, 3);

char rd;

void setup() {
  // Begin the Serial at 9600 Baud
  Serial.begin(9600);
  mySerial.begin(9600);
  while (!mySerial){
    ;
  }
}

void loop() {
  if (mySerial.available()){
    rd = mySerial.read();
    Serial.print(rd);
  }
}

I've tried several different versions of the Rx code to no avail. This is one of the latest. It looks like This User Had the same issue but he said he got it to work with this code but as another user pointed out, it doesn't echo either. Any advise would be wonderful.

Thanks

How have you connected the boards to each other? Please show us a schematic, hand drawn and photographed is fine.
How to post an image

My guesses:
You have connected Tx to Tx and Rx to Rx
You have not connected the grounds.

It might be easier for me to just tell you how I have it hooked up. Both Arduino's are powered externally from my bench power supply. Therefore they do have a common ground. I have pin 2 going to pin 3 and pin 3 going to pin 2 from board to board. As I mentioned in my OP, it works great using the serial port only but I can’t seem to get the software serial to read.

If I need to take a pic I can do that latter. I was hoping it was something in my code. Maybe something extra I had to include.

And the 'Arduinos' you are using are ?

Try removing this:

  while (!mySerial){
    ;
  }

I don't see that you need it and you don't have in the transmit code.

Change:

void loop() {
  mySerial.write("test"); //Write the serial data
  delay(100);
}

to:

void loop() {
 mySerial.print("test"); //Write the serial data
 delay(100);
}

If that doesn't fix it then we need photos and schematics please.

KrafterHD:
It might be easier for me to just tell you how I have it hooked up. [shakespeare description follows...]

Sure, easier for you...

PerryBebbington:
Try removing this:

  while (!mySerial){

;
  }



I don't see that you need it and you don't have in the transmit code.

This is in the sample code in the IDE. It looks to me as it waits for the comm port to initialize. Not necessary I take it?

Regard less, I was able to get it to work by changing from pins 2 and 3 to pins 10 and 11. Is there any reason why 2 and 3 wont work? I even tried switching back to pins 2 and 3 after I got it working with pins 10 and 11 but it still didn't work. Same code and same wiring by the way.

aarg:
Sure, easier for you...

I get it lol. I used to by very active on other support forums. I know how over confident some people can be but this circuit is very easy. Power and Tx to Rx and Rx to Tx. I picked pins 2 and 3 for a reason. I've made up countless RS2323 DB9 cables in my life lol.

This is in the sample code in the IDE. It looks to me as it waits for the comm port to initialize. Not necessary I take it?

It's there to wait for the serial port to be ready, but I have never found it much use. In your particular case I applied the principal of making something as simple as possible to test something out, then later add in more functionality when the simple stuff is working. In this case that meant stripping out anything that is not essential, and that bit of code is not essential.

Is there any reason why 2 and 3 won't work?

Yes, well, obviously there is a reason!!!! As to what it is I do not know, maybe someone else will come along and tell us both. If you said which Arduino you have I missed it, maybe there's a clue there.

++Karma; // For finding the answer to your problem.

PerryBebbington:
If you said which Arduino you have I missed it, maybe there's a clue there.

Sorry. I did not mention it. They are both Mega 2560s.

And thank you for the help.

They are both Mega 2560s.

Then why are you using software serial at all?

PerryBebbington:
Then why are you using software serial at all?

To be honest. I'm starting from ground zero on a bigger project that I have in mind. I need to understand how to transmit, receive and parse data over a serial port. I'm not sure what type of board I will end up with in the end but it will probably be a micro and may need to monitor data in a serial monitor and update software.

And to be a little more honest and more at what you are getting at, I just noticed that these boards have four serial ports lol. With a quick google search, I found how to use them but at least I got software serial working.

Thanks again.

I'm starting from ground zero on a bigger project that I have in mind. I need to understand how to transmit, receive and parse data over a serial port.

Have a read of Robin2's excellent Serial input basics

And to be a little more honest and more at what you are getting at, I just noticed that these boards have four serial ports.

The first is taken for the serial monitor, the other 3 (1, 2, 3) are free for you to use as you please. Always use a hardware serial port if there is one available.

using the hardware serial ports is the way to go. of course Rx goes to Tx and Tx goes to Rx.

Both Arduino's are powered externally from my bench power supply. Therefore they do have a common ground

As you are looking out for bugs, I would get peace of mind and put a wire between the two GND pins of the Arduinos... just in case...