Arduino.Read Serial, create String, search into the string, clean String -
i trying work sim900, trying do, is: 1- read serial port, 2- input string, 3- search parameter in string, 4- clean string. code simple, cant understand doing wrong. if make similar, or knows how graceful. thank jose luis
string leido = " "; void setup(){ // serial1 baud rate serial.begin(9600); serial1.begin(9600); } string leido = " "; void setup(){ // 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; (int = 0; (i < haystack.length() - needle.length()); i++) { if (haystack.substring(i,needle.length()+i) == needle) { foundpos = 1; } } return foundpos; }
you shouldn't using ==
compare strings in c/c++, since compares pointers. better option strcmp
or better strncmp
, check this reference.
back code, try this:
if (strncmp(haystack.substring(i,needle.length()+i), needle, needle.length()) == 0) { foundpos = 1; }
Comments
Post a Comment