problem with string compare
hello guys!!
i'm kind of new in programming world. have small project , try read request arduino mega 2560 which comes other arduino(uno). can read coming string comparing part doesn't work right .would please take @ code , inform, me error .your 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 st1, request
allow = 1;
serial.println("passed st check");
}
}
for (int j=0; j < sizeof(check_bt); j++){ // check 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 expect gives me :
passed st check
passed bt check
passed st check
passed bt check
your appreciate
i'm kind of new in programming world. have small project , try read request arduino mega 2560 which comes other arduino(uno). can read coming string comparing part doesn't work right .would please take @ code , inform, me error .your appreciate.
sending part:
code: [select]
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;
}
}
}
code: [select]
receiving side:
code: [select]
#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 st1, request
allow = 1;
serial.println("passed st check");
}
}
for (int j=0; j < sizeof(check_bt); j++){ // check 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;
}
}
}
code: [select]
my result :
code: [select]
start checking requests
passed st check
st1,bt
st1,bt
st1,bt
st1,bt
st1,bt
code: [select]
which expect gives me :
code: [select]
passed st check
passed bt check
passed st check
passed bt check
code: [select]
your appreciate

hello guys!!
i'm kind of new in programming world. have small project , try read request arduino mega 2560 which comes other arduino(uno). can read coming string comparing part doesn't work right .would please take @ code , inform, me error .your appreciate.
sending part:code: [select]
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:code: [select]
#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 st1, request
allow = 1;
serial.println("passed st check");
}
}
for (int j=0; j < sizeof(check_bt); j++){ // check 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 :code: [select]
start checking requests
passed st check
st1,bt
st1,bt
st1,bt
st1,bt
st1,bt
what expect gives me :code: [select]
passed st check
passed bt check
passed st check
passed bt check
your appreciate![]()
Arduino Forum > Using Arduino > Programming Questions > problem with string compare
arduino
Comments
Post a Comment