I have used software serial for serial communication, below are the sender's and receiver's code which sends and receives 3 characters. The characters received has extra junk characters. I need assistance to remove those junk characters. Along with sending and receiving i have section of code which receives acknowledgement after successfully sending the characters. Kindly assist
************ SENDER CODE **************
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX(Receiver and transmitter)
char mystr_0[4] = "CAT"; //serial message to be communicated
char txt_msg[3]; //Initialized buffer/array to store received acknowledgement
#define SIZE_OF_RCV (sizeof(txt_msg))
void setup() {
Serial.begin(9600);
// Begin the Serial at 1000 Baud or data transfer rate
mySerial.begin(1000);
}
void loop() {
mySerial.write(mystr_0, 4); //Write the serial data
delay(1000);
if(mySerial.available() == 3)
{
for(int i = 0; i < SIZE_OF_RCV; ++i){
char rcv = mySerial.read(); // to read the received char by char
txt_msg[i] = rcv;
//digitalWrite(13, HIGH); // configuring LED for confirmation
}
Serial.println(txt_msg); // printing it on serial monitor to check the message
}
}
************ RECEIVER'S CODE **************
#include <SoftwareSerial.h>
SoftwareSerial mySerial_1(2, 3); // RX, TX
char mystr[3]; //Initialized buffer/array to store received data
char data[3];
#define SIZE_OF_PACKET (sizeof(mystr))
void setup() {
Serial.begin(9600); // Begin the Serial at 9600 Baud
mySerial_1.begin(1000); // Input signal (message) rate at 1000
}
void loop() {
int j;
// Serial.println(SIZE_OF_PACKET);
if (mySerial_1.available() > 0)
{
char b = mySerial_1.read();
if (b == 'C')
{
mystr[0] = b;
}
char c = mySerial_1.read();
if(c == 'A'){
mystr[1] = c;
}
char d = mySerial_1.read();
if(d == 'T'){
mystr[2] = d;
}
//Serial.println(mystr);
}
if(sizeof(mystr) == 3)
{
char msg_check[4] = "YES"; // message confirming that data is transferred successfully.
//Serial.println(msg_check);
Serial.println(mystr);
mySerial_1.write(msg_check, 4); // writing the acknowledged message
}
}
Serial port output
12:56:46.846 -> CAT
12:56:46.879 -> CAT
12:56:46.914 ->
12:56:46.914 -> CAT
12:56:46.948 -> CAT
12:56:47.017 -> CAT#
12:56:47.052 -> CAT+