problem with string compare

Hello guys!!
I'm kind of new in programming world. I have a small project and I try to read the request by arduino mega 2560 which comes from other Arduino(uno). I can read my coming string but for comparing part it doesn't work right .would you please take a look at my code and inform, me where is my error .your help be appreciate.

sending part:


String inputString = "" ;
void setup(){

Serial.begin(19200);
inputString.reserve(2000);

}
void loop (){
Serial.println("ST1,BT");
delay(10000);
if(StringComplete){
inputString = "";
stringComplete = false;
}
}
void serialEvent(){
while(Serial.available(){
char inchar = (char)Serial.read();
inputString += inchar;
if(inChar == '\n'){
StringComplete = true;
}
}
}
receiving side:
#include<string.h>
#include<stdio.h>
boolean stringComplete = false;
String inString = "";

int j = 0;
int allow = 0;
int count_check = 0;
char checkCommand[20];
char check_ST1[]="0000";
char check_BT[2];
char check_GPS[3];
char check_SYNC[4];
char check_INFO[4];
char* s1;
char* s2;
char* s3;
char* s4;
char* s5;
char* s6;
char* string;
char* tofree;

void setup(){

Serial.begin(115200); //
Serial2.begin(19200); // wireless communication
Serial.println("start checking requests");

}
void loop(){
check_array();
}
void check_array(){

string = strdup("/^ST1,BT:GPS%SYNC~INFO;");
if (string != NULL) {
tofree = string;
s1 = strsep(&string, "^");
s2 = strsep(&string, "
"); //ST1,
s3 = strsep(&string, ":"); //BT
s4 = strsep(&string, "%"); //GPS
s5 = strsep(&string, "~"); //SYNC
s6 = strsep(&string, ";"); //INFO
free(tofree);

}

while(Serial2.available()){
char check = (char)Serial2.read();
inString += check;
checkCommand[count_check] = check ;
count_check++;
if(check =='\n'){
stringComplete = true;
}

if(stringComplete){

for (int j = 0; j < sizeof(check_ST1); j++){
check_ST1[j] = checkCommand[j];

if(strcmp(check_ST1,s2)==0){ //check for ST1, request
allow = 1;
Serial.println("passed St check");
}
}
for (int j=0; j < sizeof(check_BT); j++){ // check for BT request
check_BT[j] = checkCommand[j+4];
}
if(strcmp(check_BT,s3)==0 && allow == 1){
allow = 0;
Serial.println("passed BT check");
}

Serial.println(inString);
inString= "";
stringComplete = false;
count_check = 0;
}
}
}


my result :


start checking requests
passed St check
ST1,BT

ST1,BT

ST1,BT

ST1,BT

ST1,BT


which I expect that gives me :


passed St check
passed BT check
passed St check
passed BT check


your help be appreciate :slight_smile:

rabbit85:
Hello guys!!
I'm kind of new in programming world. I have a small project and I try to read the request by arduino mega 2560 which comes from other Arduino(uno). I can read my coming string but for comparing part it doesn't work right .would you please take a look at my code and inform, me where is my error .your help be appreciate.

sending part:

String inputString  = "" ;

void setup(){

Serial.begin(19200);
inputString.reserve(2000);

}
void loop (){
  Serial.println("ST1,BT");
  delay(10000);
  if(StringComplete){
  inputString = "";
  stringComplete = false;
  }
}
void serialEvent(){
  while(Serial.available(){
     char inchar = (char)Serial.read();
     inputString += inchar;
     if(inChar == '\n'){
        StringComplete = true;
     }
  }
}




receiving side:


#include<string.h>
#include<stdio.h>
boolean stringComplete = false;
String inString = "";

int j = 0;
int allow = 0;
int count_check = 0;
char checkCommand[20];
char check_ST1[]="0000";
char check_BT[2];
char check_GPS[3];
char check_SYNC[4];
char check_INFO[4];
char* s1;
char* s2;
char* s3;
char* s4;
char* s5;
char* s6;
char* string;
char* tofree;

void setup(){
 
 Serial.begin(115200);   //
 Serial2.begin(19200);  // wireless communication
 Serial.println("start checking requests");
 
}
void loop(){
 check_array();
}
void check_array(){
 
 string = strdup("/^ST1,BT:GPS%SYNC~INFO;");
  if (string != NULL) {
     tofree = string;
     s1 = strsep(&string, "^");
     s2 = strsep(&string, "
");   //ST1,
     s3 = strsep(&string, ":");   //BT
     s4 = strsep(&string, "%");   //GPS
     s5 = strsep(&string, "~");   //SYNC
     s6 = strsep(&string, ";");   //INFO
     free(tofree);

}
 
  while(Serial2.available()){
     char check = (char)Serial2.read();
     inString += check;
     checkCommand[count_check] = check ;
     count_check++;
     if(check =='\n'){
        stringComplete = true;
     }

if(stringComplete){
       
     for (int j = 0; j < sizeof(check_ST1); j++){
        check_ST1[j] = checkCommand[j];
       
        if(strcmp(check_ST1,s2)==0){                //check for ST1, request
           allow = 1;
           Serial.println("passed St check");
        }
     }
     for (int j=0; j < sizeof(check_BT); j++){       // check for BT request
        check_BT[j] = checkCommand[j+4];
     }
        if(strcmp(check_BT,s3)==0 && allow == 1){    
           allow = 0;
           Serial.println("passed BT check");
        }
     
        Serial.println(inString);
        inString= "";
        stringComplete = false;
        count_check = 0;
     }
  }  
}





my result :


start checking requests
passed St check
ST1,BT

ST1,BT

ST1,BT

ST1,BT

ST1,BT




what I expect that gives me :


passed St check
passed BT check
passed St check
passed BT check



your help be appreciate :)

You can edit your post, you don't have to post a new reply if you need to fix something.

What are you mixing Strings and strings? Pick one, and stick with it.

Your "sending part" seems to be trying to receive strings. Is that what you intended ?

michinyon:
Your "sending part" seems to be trying to receive strings. Is that what you intended ?

actually I have no problem with sending part . In receiving side I do receive but when I try to compare my string I can't make it to work.

Arrch:
You can edit your post, you don't have to post a new reply if you need to fix something.

this is my first post .I will use your suggestion for future.