0

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+

Abd
  • 3
  • 3
  • These are some of the examples of serial window with more clarity of junk characters 12:56:47.531 -> CAT⸮ 12:56:47.565 -> CAT⸮ 12:56:47.600 -> CAT⸮ 12:56:47.635 -> CAT⸮ 12:56:47.670 -> CAT⸮ 12:56:47.739 -> CAT⸮ 12:56:47.840 -> CAT⸮ 12:56:47.874 -> CAT⸮ 12:56:47.943 -> CAT⸮ 12:56:47.977 -> CAT⸮ – Abd Jan 09 '20 at 11:32
  • 3
    `println` needs a nul terminated array for strings. `mystr` in receiver is not nul terminated. – kaylum Jan 09 '20 at 11:47
  • If there's one character, you read three. Sooner or later mystr will contain the three characters you expect (and some following garbage), because you never store anything else in there. – datafiddler Jan 09 '20 at 12:17
  • This problem has nothing to do with Arduino or serial communication. You simply need to study how strings work in C. – Lundin Jan 09 '20 at 12:31

1 Answers1

0

I am not sure if this causes your bug, but it is definitely wrong if(sizeof(mystr) == 3) iirc sizeof(mystr) is determined at compile time, meaning that sizeof(mystr) is ever 3. So that if statement is actually saying if(3 == 3)