problem with Serial communication

hey you all ..

I am having a problem with my project . my project is having two arduinos talking to each other serially using IEC 62056-21 protocol . master should send a message to slave and after comparing slave should reply back .

here is my code

String byteReceived ;

byte hShake[] = {0x2F,0x3F,0x21,0x0D,0x0A}; 

void setup() {
  
  Serial.begin(9600);

  pinMode(Master_Control, OUTPUT);    
 
  Serial1.begin(9600);   
}

void loop() {

  if (Serial.available())

  {
    byteReceived =Serial.readString();  //this is so it only sends when i type in serial monitor

    Serial.println(byteReceived);  

       for(int i=0; i<5 ; i++){ 

         Serial1.write( handShake[i]);   }
          
            Serial.println("what is sent : "); //check what is acctually sent


       for(int i=0; i<5 ; i++){ 

           Serial.print( hShake[i]);  

            }

             // for(int i=0; i<5 ; i++){ // send it ones

      //  hShake[i]= 0; }

   }
    Serial.println("------------------ ");

    delay(200);       //

slave :

String recieved;

char confirmHShake[] = {0x2F,0x3F,0x21};

void setup() {
   
  Serial.begin(9600);

  pinMode(Slave_Control, OUTPUT);

  digitalWrite(Slave_Control, LOW);  
  Serial1.begin(9600);   

}

void loop() {      
   
       digitalWrite(Slave_Control, LOW);  
                                 while (Serial1.available()>0) { 

                                      digitalWrite(Slave_Control, LOW); 

                                         inChar = Serial1.read();

                                        recieved+= inChar; 
                                           
   recievedBytes.trim();                                                        }  

  int y = recieved.length();

 int b = sizeof(confirmHShake);

    char recievedString[y];

    recieved.toCharArray(recievedString, y);

   
     if ( (strcmp(recievedString,confirmHShake  )== 0) ){ // it never enters here 

                                Serial.print("Yes they are the same : " );

                                digitalWrite(Slave_Control, HIGH); //enable sending

                     for(int i = 0; i<10 ; i++){

                                Serial1.write(byte_send_arr[i]); } 
       }else {
        
                     Serial.println("No they are not the same  " );
   
         }

the problem is that the code never inters the comparasion its always giving me that they are not the same . even thought i made sure they have the same size and i sent the same bytes

is there something i am missing ?? any help plz

please indent your code it's unreadable as it is... also probably not complete

what is Master_Control ?
what does recievedBytes.trim(); is supposed to do? what's that variable...

you do

int y = recieved.length();
char recievedString[y];
recieved.toCharArray(recievedString, y);

are you sure there is enough space in that buffer or (in case you would need one, your '\0' will overflow?)

(By the way, there is no need to create a new buffer, the String class as a c_str() function you can use to access the null terminated raw buffer of data)

when you do your strcmp(recievedString,confirmHShake) with char confirmHShake[] = {0x2F,0x3F,0x21}; defined without '\0' at the end what do you think will happen?

You probably want to read Serial Input Basics

(and use c-string rather than the String class and the functions from stdlib.h and string.h)

hiii htanks for the quick replay

master control is a control pin. as i am using RS485 transciver i need

to control the secnding/recieving wires : low for recieving mode and

high for sending .

the tirm(); function iam using it for cutting the extra newline the arduino
sending autumaticly with the string recived from master .
i add it recently because i thought i might be the reason why string never
enters the comparasion .

i created the buffer because stcmp never works with a normal string

<<(and use c-string rather than the String class and the functions from  stdlib.h and  string.h)>>> and what do you mean by this i am kind of new to arduino so am not really following you

Hi

You don't apply trim to the right object was my first point

My second one is that you should avid using the String class and stick to simple null terminated char arrays (which are known as c-strings and on which you can use many functions like strlen() or strcmp() like you do in part of your code)

One of Your main issue is that you use strcmp() which needs '\0' terminated char arrays to work and you are passing arrays that are not compliant

so you are suggesting that i receive the data to an array of characters right away and terminate the array !

or you saying that i need to find another way to compare strings ?

I suggest you indeed use char arrays and that you read the Serial Input Basics tutorial pointed above. That will set you on the right track to better code

iwill ,, thank you very much

What was that??

it is a mistake >< thank you for asking

and thank you for the help ,i finally solved the problem and my code is working fine .

Good news!