GolamMostafa:
1. Build the following connections (Fig-1) between two UNOs using Software UART Ports as the Hardware UART Ports are engaged with Serial Monitor and IDE for sketch debugging and uploading.
Figure-1:
2. Now carry out the following steps:
(1) Create SUART Port for Transmitter (UNO).
(2) Keep sending the following string to Receiver (NANO) at 1-sec interval
char mystr[] = "Hello"; //array contains 6 items with null-char as the last item
(3) Create SUART Port for Receiver (NANO)
(4) Keep receiving the string from UNO and check that it matches with "Hello" and then show it on SM2 (Serial Monitor 2) and blink LED1.
3. Convert the tasks of Step-2 into the following sketches and upload them into UNO and NANO.
Sketch for UNO
#include<SoftwareSerial.h> //include this file
SoftwareSerial SUART(2, 3); //SRX = DPin-2, STX = DPin-3
char myStr[] = "Hello"; //array holds 6 items with null-char
void setup()
{
Serial.begin(9600);
SUART.begin(9600);
}
void loop()
{
SUART.print(myStr); //send the ASCII codes of the charcaters of myStr to NANO using SUART Port
SUART.print('\n'); //send Newline character
delay(1000);
}
**Sketch for NANO**
#include<SoftwareSerial.h>
SoftwareSerial SUART(2, 3);
char myStr[20] = "";
void setup()
{
Serial.begin(9600);
SUART.begin(9600);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
}
void loop()
{
byte n = SUART.available(); //check that a charcater has arrived from UNO
if (n != 0)
{
SUART.readBytesUntil('\n', myStr, 20);//store received string in myStr[] array
byte x = strcmp(myStr, "Hello"); //comparing null-char terminated strings
if (x == 0) //strings are matched
{
Serial.println(myStr); //show the received string on SM2
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
memset(myStr, 0, 20); //array is rest to 0s.
}
}
}
**4.** Press the RESET key of both UNO and NANO.
**5.** Check that the message Hello is appearing on SM2 at 1-sec interval.
![SMgq.png|605x464](upload://nW6KtUsJm6JDruJvg8uE6NgiabI.png)
**6.** Check that LED1 at DPin-4 makes a blink at 1-sec interval as well to indicate the received string has matched with "Hello".
:fearful: :fearful: :fearful: :fearful: :fearful: :fearful: :fearful: :fearful:
J-M-L:
That’s how you can compare it with strncmp()
if (!strncmp(mystr,"Hello", 5)) {
// you received Hello
...
} else {
// you got something else
...
}
you can also use memcmp()
Thank you J-M-L, I didn't want to have to do GolamMastafa's thing :D