0

I am trying to work with the sim900, what I am trying to do, is: 1- read the serial port, 2- input everything into a string, 3- search a parameter in that string, 4- clean the string. The code is really simple, but i cant understand what i am doing wrong. If anyone make something similar, or knows how to do it I will be graceful. Thank you very much Jose Luis

String leido = " ";
void setup(){ // the Serial1 baud rate   
  Serial.begin(9600);
Serial1.begin(9600);  
}


    String leido = " ";
void setup(){
                 // the Serial1 baud rate   
  Serial.begin(9600);
Serial1.begin(9600);  
}
void loop()
    {
    //if (Serial1.available()) {  Serial.write(Serial1.read()); } // Sim900
    if (Serial.available()) {  Serial1.write(Serial.read()); } // pc
    leido = LeerSerial();
    Serial.println(leido);
    if (find_text("READY",leido)==1){leido = " ";}

    }


    String LeerSerial(){
     char character;
     while(Serial1.available()) {
     character = Serial1.read();
     leido.concat(character);
     delay (10); } 
     if (leido != "") { Serial1.println(leido);return leido; }
    }


    int find_text(String needle, String haystack) {
      int foundpos = -1;
      for (int i = 0; (i < haystack.length() - needle.length()); i++) {
        if (haystack.substring(i,needle.length()+i) == needle) {
          foundpos = 1;
        }
      }
      return foundpos;
    }

2 Answers2

0

You shouldn't be using == to compare strings in C/C++, since that compares pointers. A better option is strcmp or even better strncmp, check this reference.

Back to your code, try something like this:

if (strncmp(haystack.substring(i,needle.length()+i), needle, needle.length()) == 0) {
  foundpos = 1;
}
jlhonora
  • 10,179
  • 10
  • 46
  • 70
0

Can you get away by simply using String's indexOf() ?:

String leido = " ";
void setup() {
  // the Serial1 baud rate
  Serial.begin(9600);
  Serial1.begin(9600);
}
void loop()
{
  //if (Serial1.available()) {  Serial.write(Serial1.read()); } // Sim900
  if (Serial.available()) {
    Serial1.write(Serial.read());  // pc
  }
  leido = LeerSerial();
  Serial.println(leido);
  if (leido.indexOf("READY") == 1) {
    leido = " ";
  }

}


String LeerSerial() {
  char character;
  while (Serial1.available()) {
    character = Serial1.read();
    leido.concat(character);
    delay (10);
  }
  if (leido != "") {
    Serial1.println(leido);
    return leido;
  }
}

Note that this assumes "READY" is always at index 1. Maybe it's worth checking if the indexOf("READY") is greater than -1 (present in the string) ?

George Profenza
  • 50,687
  • 19
  • 144
  • 218
  • Mate! thank for helping me! I have change code a little bit – Jose Luis Lujan Aug 21 '15 at 09:25
  • void loop() { //if (Serial1.available()) { Serial.write(Serial1.read()); } // Sim900 if (Serial.available()) { Serial1.write(Serial.read()); }// pc leido = LeerSerial(); Serial.println(leido); Serial.println(leido.indexOf("Ready")); if (leido.indexOf("Ready") != -1) { leido = "";Serial.write("Encontrado"); } delay(1000); } – Jose Luis Lujan Aug 21 '15 at 09:27
  • String LeerSerial() { while (Serial1.available()) { char c = Serial1.read(); leido += c; } return leido; } – Jose Luis Lujan Aug 21 '15 at 09:27
  • the problem is that in this response "NORMAL POWER DOWN RDY +CFUN: 1 +CPIN: READY Call Ready" it doesnt find the word ready, do you know what could it be? regards – Jose Luis Lujan Aug 21 '15 at 09:27
  • Can you please post an updated version of the code you used to get this output ? I did a basic test using IndexOf and it outputs 38 based on the string above as expected:```String msg = "NORMAL POWER DOWN RDY +CFUN: 1 +CPIN: READY Call Ready"; void setup() { Serial.begin(9600); } void loop() { Serial.println(msg.indexOf("READY")); delay(2000); }``` – George Profenza Aug 21 '15 at 15:31