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);
}
So the communication seems ok, but after the 10th loop the caracters begin to mixed up.
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?
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)
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!!