Pages: [1]   Go Down
Author Topic: Text comparing  (Read 522 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
 
  }
}
« Last Edit: May 07, 2013, 05:30:52 am by danjun » Logged

Netherlands
Offline Offline
Jr. Member
**
Karma: 1
Posts: 93
Profile before you Optimize.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Hope it helps.
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25735
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: May 07, 2013, 06:48:57 am by AWOL » Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Melbourne, Australia
Offline Offline
God Member
*****
Karma: 8
Posts: 567
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: May 07, 2013, 08:36:01 am by aarondc » Logged

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

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3414
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: May 07, 2013, 08:49:02 am by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Leeds, England
Offline Offline
God Member
*****
Karma: 14
Posts: 587
Quick, chuck it in the bin before the boss finds out...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

http://arduino.cc/forum/index.php/topic,165084.0.html
Logged

Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

Melbourne, Australia
Offline Offline
God Member
*****
Karma: 8
Posts: 567
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: