Serial Communication between two arduinos uno, char

I'm practicing with serial communication between two arduino. I'm sending two char from an arduino to another.

Wiring: || both on vcc(usb) || rx<-->tx || tx<-->rx || gnd<--> gnd ||

Arduino (tx)

///Tx//COM3
char mystr[5] = "Hello"; //String data
char mystr2[9] = "MyFliends"; //String data 2

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

void loop() {
  Serial.write(mystr,5); //Write the serial data
  //Serial.write("",1);  //space
  Serial.write(mystr2,9);
  delay(1000);
}

Arduino (rx)

///Rx//COM4
char mystr[10]; //Initialized variable to store recieved data
char mystr2_rx[15];

char match1[5] = "Hello";
char match2[9] = "MyFliends";
int i; //iteration

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

void loop() {

  
  Serial.print("The size of mystr is:  ");
  Serial.println(sizeof(mystr));
  Serial.println("");
  Serial.readBytes(mystr,5);
  //Serial.readBytes(mystr,6); //Read the serial data and store in var //including space
  
  
  if(mystr != match1) {
    Serial.println("Error in string 1: ");
    setup();
  }
  Serial.println(mystr); //Print data on Serial Monitor
  delay(1000);

  Serial.print("The size of mystr2 is:  ");
  Serial.println(sizeof(mystr2_rx));
  Serial.println("");
  Serial.readBytes(mystr2_rx,9); //Read the serial data and store in var
    if(mystr2_rx != match2) {
    Serial.println("Error in string 2: ");
    setup();
  }
  Serial.println(mystr2_rx); //Print data on Serial Monitor
  delay(1000);
}
  1. So the communication seems ok, but after the 10th loop the caracters begin to mixed up.

  2. I declared two equal char variables in the Arduino(rx) and asked if the received string that has sent Arduino(tx) are equal to declared ones in the Arduino(rx) and they are never equal.

Why is happening this? How can I see what's really behind this communication? Thank You

char mystr[5] = "Hello"; The string "Hello" is SIX chars long.
You forgot about the terminating zero.
You also forgot the terminating zero at the receiver.

How can I see what's really behind this communication?

Look in a mirror?

Have a look at the examples in Serial Input Basics - simple reliable non-blocking 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 and Arduino to PC 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

...R

To compare c-strings (nul terminated character arrays), you use strcmp, not != or ==.

You will also need to add a nul-terminator before you can use strcmp.

You can use SerialTransfer.h to automatically packetize and parse your data for inter-Arduino communication without the headace. The library is installable through the Arduino IDE and includes many examples.

Here are the library's features:

This library:

  • can be downloaded via the Arduino IDE's Libraries Manager (search "SerialTransfer.h")
  • works with "software-serial" libraries
  • is non blocking
  • uses packet delimiters
  • uses consistent overhead byte stuffing
  • uses CRC-8 (Polynomial 0x9B with lookup table)
  • allows the use of dynamically sized packets (packets can have payload lengths anywhere from 1 to 255 bytes)
  • can transfer bytes, ints, floats, and even structs!!

Example TX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  myTransfer.txBuff[0] = 'h';
  myTransfer.txBuff[1] = 'i';
  myTransfer.txBuff[2] = '\n';
  
  myTransfer.sendData(3);
  delay(100);
}

Example RX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  if(myTransfer.available())
  {
    Serial.println("New Data");
    for(byte i = 0; i < myTransfer.bytesRead; i++)
      Serial.write(myTransfer.rxBuff[i]);
    Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");
    Serial.println(myTransfer.status);
  }
}

You probably can use the String functions to capture and parse what is sent without all the "buffer this" and 'terminate that" overhead.

zoomkat:
You probably can use the String functions to capture and parse what is sent without all the "buffer this" and 'terminate that" overhead.

And end with a code that has a chance of crashing unexpectedly.