Go Down

Topic: Text comparing (Read 665 times) previous topic - next topic

danjun

May 07, 2013, 12:29 pm Last Edit: May 07, 2013, 12:30 pm by danjun Reason: 1
Hello,
if you can, please help me with this script.
After I get the string, need to compare with number
or number as text

example

i get from serial 000xxxx
and need compare in script with "000xxx"

the whole script is here

thank you
dan

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

void setup() {
// initialize serial:
Serial.begin(9600);
}

void loop() {
 // print the string when a newline arrives:
if (stringComplete) {

      if (inputString == "0007324804") {
      Serial.print("THEY ARE EQUAL");
   }

   Serial.println(inputString);
   // clear the string:
   inputString = "";
   stringComplete = false;
   }

  }

void serialEvent() {
 while (Serial.available()) {
 // get the new byte:
 char inChar = (char)Serial.read();
 // add it to the inputString:
 delay (10);
 inputString += inChar;
 stringComplete = true;
//Serial.println(inputString);

 }
}

obiwanjacobi

- you're not calling serialEvent()
- this wont work: inputString += inChar;
create a char array (char inputString[10];) and index it for each character you receive.

Code: [Select]
for(int i = 0; i < 10; i++)
{
  inputString[i] = (char)Serial.read();
}


Hope it helps.

AWOL

#2
May 07, 2013, 01:46 pm Last Edit: May 07, 2013, 01:48 pm by AWOL Reason: 1
Quote
you're not calling serialEvent()

No, but the sketch hopefully is. http://arduino.cc/en/Reference/SerialEvent

Quote
this wont work: inputString += inChar;

Why not?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

danjun

Hi,
thanks for all,

but here isnt problem to capture data from serial, this is working.
The problem is how to compare string with text
if (stringComplete) {

       if (inputString == "0007324804") {
       Serial.print("THEY ARE EQUAL");
    }
...................


Input string is captured and the comparsion does not work.

thanks
Dan

aarondc

#4
May 07, 2013, 03:34 pm Last Edit: May 07, 2013, 03:36 pm by aarondc Reason: 1
Have you looked at the input string to check that it is in fact the same?

It's my experience that not all chars arrive at once, so you may be setting Stringcomplete to true before they all arrive, and then doing the test.

Better to have some sort of test for string complete - like a length or delimiter check.
Windows serial port monitor: Tellurium | Arduino serial port debugging library: DBG | Cusom LCD char generator | Technical questions will only be answered in forum threads

HazardsMind

#5
May 07, 2013, 03:44 pm Last Edit: May 07, 2013, 03:49 pm by HazardsMind Reason: 1
Quote
Input string is captured and the comparsion does not work.
Of coarse it is not working, your code is comparing one char at a time, instead of the full string.

Quote
It's my experience that not all chars arrive at once, so you may be setting Stringcomplete to true before they all arrive, and then doing the test.

He is going to need to because if they do not match, his code will clear the string.
Quote
Serial.println(inputString);
   // clear the string:
   inputString = "";
   stringComplete = false;


He needs to send a period or some special char so that, only when that char is found, will it stop storing and try to match them.

A simple IF/ELSE should fix this.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

dannable

There's a similar thread running next door which might help!

http://arduino.cc/forum/index.php/topic,165084.0.html
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

aarondc


He needs to send a period or some special char so that, only when that char is found, will it stop storing and try to match them.


Which is why I said:

Quote
Better to have some sort of test for string complete - like a length or delimiter check.



A simple IF/ELSE should fix this.


I doubt it.
Windows serial port monitor: Tellurium | Arduino serial port debugging library: DBG | Cusom LCD char generator | Technical questions will only be answered in forum threads

zoomkat

Quote
The problem is how to compare string with text


Below is some String comparison info. You might need to set "0007324804" as a String variable and then make the comparison.

http://arduino.cc/en/Tutorial/StringComparisonOperators
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Go Up