2-way Serial Communication with 2 Arduinos - Issues with handshaking

First off I'm fairly new to this so I've probably made quite a few mistakes. Secondly, I've researched about SoftwareSerial connections and after finding some very helpful forum posts, I have come up with this code, but now I'm having no luck in finding a solution to my problem. So any help is much appreciated!

The aim; I'm trying to get two ardunios to communicate with each other using a SoftwareSerial connection. I need to transmit a few floats from each arduino to the other. I'm using two UNOs (both R3) to prototype this but the final system will use a Nano and Leonardo. I've got the Tx of one wired to the Rx of the other and vise versa, they also share a common ground.

The problem; I'm having issues making the two boards handshake in the setup phase, it seems when one board is transmitting the initialisation/communication check, the other doesn't seem to acknowledge the data transmission?

I'll post the code below, I'll use the terms 'master' and 'slave' just to differentiate, even though both transmit and receive.

When the boards power up (I've been resetting them at the same time after upload), I get the following out from the serial monitor of the master:

Waiting for handshake..
Comms incoming...
Inter-Arduino Connection ERROR

and the following out from the serial monitor of the slave:

Waiting for handshake...
Comms incoming...
Inter-Arduino Connection ERROR

I'll try add the codes in txt files, as i'm hitting the character limit

Slave_Code.txt (4.24 KB)

Master_Code.txt (3.96 KB)

You've got both of your devices waiting for the other one to stick out it's hand first.

The master needs to act like a politician, and stick it's hand out first. It keeps trying, every few seconds, to shake hands with the slave, until finally the slave wakes up.

If the slave wakes up first, it needs to reply as though asked, over and over, every few seconds, until the master wakes up and attempts the secret handshake.

After a cursory glance I see two issues - likely there are more.

  char tempChars[numChars];        // temporary array for use when parsing  
  char receivedChars[numChars];
  if (newData == true) {
    Serial.println(receivedChars);
    strcpy(tempChars, receivedChars);
  }
  if (tempChars == "Initialisation Check") {

tempChars and receivedChars are declared in this section. They are also declared as globals. OnReceive has local versions too, which means that there will be nothing in the ones you're testing here. Get rid of the local declarations.

You don't compare character strings using ==. Take a look at strcmp.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

It makes debugging much easier if you send data in human readable form. I would only send binary data if it was the only way to achieve the required performance.

...R

Before making an attempt to wake-up a remote Slave (a Machine), the Master (a Machine) must inform the owner (a Man or a Machine) of the Slave via Phone (a verbal or electronic message) that he (the owner of Slave) must apply power to the Slave and press the Reset Button to bring the Slave into 'Ready State'. And then the Master can proceed to wake-up the Slave through hand-shaking where known 'messages/codes' will be exchanged.

Master UNO Codes:

#include<SoftwareSerial.h>
SoftwareSerial SUART (4, 5); //SRX, STX
#define AreYouReady  0x01
bool flag1 = false;

void setup()
{
  Serial.begin(9600);
  SUART.begin(9600);
  //The Slave must be in the Ready State-----
  Serial.println("Waiting for handshake...");

  for (int i = 0; i < 5; i++)
  {
    SUART.write(AreYouReady);
    if (SUART.available() > 0)
    {
      byte x1 = SUART.read();
      if (x1 == 0x02)   //Slave ready message is 0x02
      {
        Serial.println("Slave is Ready...");
        flag1 = true;
        break;
      }
    }
  }
  if (flag1 == false)
  {
    Serial.println("Inter - Arduino Connection ERROR");
    while (1);
  }
}

void loop()
{

}

Slave NANO Codes:

#include<SoftwareSerial.h>
SoftwareSerial SUART (4, 5); //SRX, STX

void setup()
{
  Serial.begin(9600);
  SUART.begin(9600);
  while (1)
  {
    if (SUART.available() > 0)
    {
      if (SUART.read() == 0x01)
      {
        SUART.write(0x02);
        break;
      }
    }
  }
}

void loop()
{

}

sm48.png

sm48.png

That's fantastic, many thanks for the input! I see now that my methods were vitally flawed!
All is now working well. Thanks again.

aero_shadez:
That's fantastic, many thanks for the input! I see now that my methods were vitally flawed!
All is now working well. Thanks again.

Can you please share the code that worked for you. I am facing the same problem. Thanks!