problem with serial communication

Hello guys!!
I’m using arduino UNO for sending commands and mega2560 for getting receive and execute
I able to get my string and it does compare my string once and after acting crazy .
sending string is “ST1,BT” .
I jump to check function but if you guys need more feel free to post .
receiving side code:

int j = 0;
int BT_request = 0;
int GPS_request = 0;
int INFO_request = 0;
int count_check = 0;
int allow = 0;
char statment;
char checkCommand[20];
char check_ST1[4];
char check_BT[2];
char check_GPS[3];
char check_SYNC[4];
char* s1;
char* s2;
char* s3;
char* s4;
char* string;
char* tofree;
char check_INFO[4];
void setup() {
 Serial.begin(115200);          
  Serial1.begin(115200);                   //bluetooth
  Serial2.begin(19200);                 //synapse
  Serial3.begin(4800);                  //GPS
}
void loop()
{
  if(Serial2.available()>0){
  check();
  }
   serial();
 // GPS();
}
void check()
{  
   string = strdup("ST1,*BT:GPS%SYNC;");
   if (string != NULL) {
      tofree = string;
      s1 = strsep(&string, "*");
      s2 = strsep(&string, ":");
      s3 = strsep(&string, "%");
      s4 = strsep(&string, ";");
      free(tofree);
   }
   char check = Serial2.read();    
   inString += check;
   if(check == '\0'){
      count_check = 0;
      inString= ""; 
   } 
   checkCommand[count_check] = check ;
   count_check++;        
   //inString.toCharArray(checkCommand, 20); 
   for (int j = 0; j < sizeof(check_ST1); j++){
      check_ST1[j] = checkCommand[j];
   }
     if(strcmp(check_ST1,s1)==0){
        allow = 1;
        Serial.println("passed St check");
     }
        for (int j=0; j < sizeof(check_SYNC); j++){      // check for time SYNC request 
           check_SYNC[j] = checkCommand[j+4];
        } 
           if(strcmp(check_SYNC,s4)==0 && allow == 1){
             Serial.println("passed sync check");
           //   statment = 'd';
              allow = 0;
            }
            
        for (int j=0; j < sizeof(check_BT); j++){       // check for BT request
           check_BT[j] = checkCommand[j+4];
        } 
           if(strcmp(check_BT,s2)==0 && allow == 1){    
            //  statment = 'a';
              allow = 0;
              Serial.println("passed BT check");
            }
        for (int j=0; j < sizeof(check_GPS); j++){    // check for GPS request
           check_GPS[j] = checkCommand[j+4];
        } 
           if(strcmp(check_GPS,s3)==0 && allow == 1 ){
           //   statment = 'b';
           Serial.println("passed GPS check");
              GPS_request = 1;
              allow = 0; 
           }  
     }
}

this is the result shows in serial monitor window :

passed St check
passed St check
passed BT check
×ST1,BT
ST1,BT
ST1,BT
assed St check
passed St check
passed St check
passed St check
passed St check
passed St check
passed St check
passed St check

and after first BT it takes few minutes to keep looping
your help be really appreciate !!!

Start here

I'm betting that you should show the entire receiving sketch, 'cause there's a lot of stuff in there that's not defined. You're looking for why things are going batty, I'm seeing you free memory with no idea what memory your freeing. Stuff like that.

-jim lee

Post all of your code. Considering inString isn't declared anywhere in your code, that code won't compile. Ergo, that's not the code you are using.

sketch_jul24a.ino: In function ‘void loop()’:
sketch_jul24a:31: error: ‘serial’ was not declared in this scope
sketch_jul24a.ino: In function ‘void check()’:
sketch_jul24a:46: error: ‘inString’ was not declared in this scope
sketch_jul24a.ino: At global scope:
sketch_jul24a:88: error: expected declaration before ‘}’ token

afshin, put the entire sketch up. Little bits here and there won't net you a lot of help. And, I'm betting you meant Serial nor serial.

-jim lee