Pages: [1]   Go Down
Author Topic: Comparing Strings with serialEvent  (Read 564 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
I must be doing something stupid with my Mega2560 (Arduino 1.0.1) when trying to test a String that's input using the serialEvent example with my own strings. In the example below, checkMystring() always returns 'String not found'. I've tried all sorts of variations. Perhaps I can't get my head round the object/variable/constant uncertainty that seems to surround the Arduino String object, at least in the minds of newbies.

Quote
/*
  Serial Event example
 
 When new serial data arrives, this sketch adds it to a String.
 When a newline is received, the loop prints the string and 
 clears it.
 
 A good test for this is to try it with a GPS receiver 
 that sends out NMEA 0183 sentences. 
 
 Created 9 May 2011
 by Tom Igoe
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/SerialEvent
 
 */

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false// whether the string is complete
// Can't get these commented-out ones to work:
//String myString = String("chris");
//String myString = "chris";
String myString1;

void  setup() {
  // initialize serial:
  Serial.begin(9600);
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);
  myString1 = String("chris");
}

void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {
    checkMystring();
    Serial.println(inputString);
    // clear the string:
    inputString = "";
    stringComplete = false;
  } 
}
  
  void checkMystring() {
    // if (inputString == myString1)  .. doesn't work either
    if (inputString.equalsIgnoreCase(myString1))
      {Serial.println("Matches"); }
    else {Serial.println("Match not found"); } 
}

/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    } 
  }
}


Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Notice that you append the carriage return onto the String, but the to be compared to String does not have a carriage return.

Also, notice that you do not return when the carriage return arrives. So, you append the line feed, too, if you are appending both in the Serial Monitor.
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

About String initialization, I've just tried this:

Code:
String str1("first string");
String str2("second string");

void setup() {
    Serial.begin(115200);    
}


void loop() {
    Serial.print("str1=");
    Serial.println(str1);
    Serial.print("str2=");
    Serial.println(str2);
    
    delay(2000);
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
About String initialization, I've just tried this
And? It worked? It fell flat on its face? It laughed at you? It went up in smoke?
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It was just an answer to these comments:

Code:
// Can't get these commented-out ones to work:
//String myString = String("chris");
//String myString = "chris";

Quote
And? It worked? It fell flat on its face? It laughed at you? It went up in smoke?

You're certainly able to compile and run it, and see for yourself what happens.
Logged

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

Many thanks PaulS. I suppose you could change the order of commands in void serialEvent() so the newline doesn't get appended to inputString, but presume the author had reasons for not doing that. A bit of browsing turns up method trim() which does the job nicely. Here's my corrected and tested version, with "chris" declared as though it were a constant:

 
Code:
/*
  Serial Event example, with addition by crlMIDI
 
 When new serial data arrives, this sketch adds it to a String.
 When a newline is received, the loop prints the string and
 clears it.
 
 A good test for this is to try it with a GPS receiver
 that sends out NMEA 0183 sentences.
 
 Created 9 May 2011
 by Tom Igoe
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/SerialEvent
 
 */

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete
// the string we are going to compare inputString with
String myString1 = "chris";

void  setup() {
  // initialize serial:
  Serial.begin(9600);
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);
}

void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {
    checkMystring();
    Serial.println(inputString);
    // clear the string:
    inputString = "";
    stringComplete = false;
  }
}
 
  void checkMystring() {
    // trim is a method of the String class that removes whitespace
    // including Newline and Carrriage return
    inputString.trim();
    // either of the following 2 commands is OK:
     if (inputString == myString1)
    // if (inputString.equalsIgnoreCase(myString1))
      {Serial.println("Matches"); }
    else {Serial.println("Match not found"); } 
}

/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

Logged

Pages: [1]   Go Up
Jump to: