Software Serial Communication Problems

Hi. I have an Arduino UNO and Mega. I am trying to use Software Serial, so the Mega can send data to the Uno. From there, the Uno can carry out a function.

Here is my code:

  1. Mega Code:
//MEGA CODE


#include <SoftwareSerial.h>

SoftwareSerial mySerial(50, 14); // RX, TX

void setup() {
  Serial.begin(4800);
  mySerial.begin(4800);
}

void loop() { // run over and over

  mySerial.write("Message From Mega to Uno");
  delay(1000);
}
  1. Uno Code:
//UNO CODE

#include <SoftwareSerial.h>

SoftwareSerial mySerial(0, 1); // RX, TX

void setup() {
Serial.begin(4800);
mySerial.begin(4800);
Serial.print("Ready");

}


void loop() { // run over and over

    Serial.print(mySerial.read());

}

But, on the Serial monitor, (of the Uno) I get a bunch of gibberish! (See attachment) What the mega code is supposed to do is send a bit of data (in this case send "Message From Mega to Uno") Then in the Uno code it is supposed to take that data and display it on the Serial Monitor. See the Attachment for what the Serial Monitor (Of the Uno, that is) looks like.

Pins 0 and 1 on the UNO are for the hardware serial port. So in effect you have both Serial and mySerial on the same pins. Move your SoftwareSerial to different pins.

Pin 14 on the Mega2560 is also a hardware serial pin, TX3. Why not use one of the hardware serial ports on the Mega anyway. There are 4.

You can send from HardwareSerial on the Mega to SoftwareSerial on the Uno. It doesn't make any sense to use SoftwareSerial on a Mega.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

OldSteve:
Pins 0 and 1 on the UNO are for the hardware serial port. So in effect you have both Serial and mySerial on the same pins. Move your SoftwareSerial to different pins.

Pin 14 on the Mega2560 is also a hardware serial pin, TX3. Why not use one of the hardware serial ports on the Mega anyway. There are 4.

Updated the code with new pins.
Mega:

//MEGA CODE


#include <SoftwareSerial.h>

SoftwareSerial mySerial(50, 51); // RX, TX

void setup() {
  Serial.begin(4800);
  mySerial.begin(4800);
}

void loop() { // run over and over

  mySerial.write("Message From Mega to Uno");
  delay(1000);
}

Uno Code:

//UNO CODE

#include <SoftwareSerial.h>

SoftwareSerial mySerial(12, 13); // RX, TX

void setup() {
Serial.begin(4800);
mySerial.begin(4800);
Serial.print("Ready");

}


void loop() { // run over and over

    Serial.print(mySerial.read());
    delay(1000);

}

When I activate the Uno alone, I get a bunch of "-1-1-1-1-1" on the Uno's Serial Monitor (I assume that means nothing is coming through the port.) When the activate the Mega, I get signals on the Serial Monitor! However, these signals are just random numbers, and mean nothing to me. I assume they are a 'code' for the incoming data, but then how do I decode these random numbers to what I want them to be? (Actual text!)

At the Uno side:

mySerial.read() returns -1 if there is no character received (red the reference). You can use mySerial.available() to check if there is actually something to read.

  if(mySerial.avaliable() > 0)
  {
    Serial.print(mySerial.read());
  }

At the Mega side, use mySerial.print (or mySerial.println) instead of write.

  mySerial.println("Message From Mega to Uno");

I assume they are a 'code' for the incoming data, but then how do I decode these random numbers to what I want them to be? (Actual text!)

We can't see the "random" data. It is most likely NOT random.

Serial.read() returns an int. If you know that there is data to read, the data can be stored in a char. Doing so will change what gets print()ed.

void loop()
{ // Down here where it belongs
   if(mySerial.available() > 0)
   {
      char c = mySerial.read();
      Serial.print(c);
      // NO stupid delay(1000);
   }
}

PaulS:
the data can be stored in a char.

Yay! I got it transmitting!
Now I have some modified code on the Uno:

#include <SoftwareSerial.h>
SoftwareSerial Master(12, 13); // RX, TX

void setup()
{
  Master.begin(4800);                   

}


void loop() {
    
    char v = Master.read();


if (v == 'cca') {
// DO SOMETHING HERE

  }


  
}

However, it does not do the thing I put in the "DO SOMETHING HERE" spot. From some testing with other code, I can see that the Mega does transmit 'cca.' However, the Uno does not seem to preforming the If statement! Can someone tell me why this is.
(I also modified the Mega code. Now it only transmits the data ONCE, rather than over and over again in the loop.)

if (v == 'cca') is not going to work considering that v is a char.

You really should put into some effort into actually learning the language instead of shotgunning this.

ieee488:
if (v == 'cca') is not going to work considering that v is a char.

But... But...

EDIT: So how DO you compare Chars in the way I want?!

You could look up 'strcmp()' or 'strncmp()' for string comparison. Just don't be tempted to use the "String" class - it's evil. :smiling_imp:

Also multiple ASCII characters are a C string. Must be nul terminated with '\0' and enclosed within double quotation marks.
'a' for a single character, "abcdef" for a string.
(The example you linked showed a single character comparison.)

Edit: Also, 'v' would need to be a char array, big enough for the full string plus terminating nul, not a single char variable.

As ieee488 said, "You really should put into some effort into actually learning the language instead of shotgunning this."

To get an understanding how to approach this, read serial input basics - updated