Serial and string compare

I'm having trouble catching a string. I am using an Arduino to print the following...
START
0,342
1,493
END

Here's part of my setup. (int lf = 10)

void setup() {
  println(Serial.list()); 
  myPort = new Serial(this, Serial.list()[5], 38400);
  myPort.bufferUntil(lf);
}

And the serial event...

void serialEvent(Serial p) { 
  //println("DEBUG 1");
  inString = p.readString(); 

  if (inString.equals("START")) {
    println("DEBUG 2 - Start Found");
    startProcessing = true;
  } 
  else if (inString.equals("END")) {
    println("DEBUG 3 - End Found");
    startProcessing = false;
    count = 0;
  } 
  else {
    //Must be data - Might new to check for new data
    //println("DEBUG 4 - New Data");
    //Display inString
    println(inString);
    newDataToBeProcessed++;
    newDataLine = inString;
  }
} 

And here's part of the output.
781,11374
782,11697
783,11282
END

I miss both the START and END if statement. What I'm seeing is just the println(inString) in the else statement. I know there is \n character after the START and END. Any idea as to why I'm not catching the START and END statement? Thanks in advance.

Diego

Tagged:

Answers

  • edited March 2014

    Ugh. I didn't realize it put all those html tags! Let's try this again.

    Setup:

    void setup() {
      println(Serial.list()); 
      myPort = new Serial(this, Serial.list()[5], 38400);
      myPort.bufferUntil(lf);
    

    And serial events:

    void serialEvent(Serial p) { 
      //println("DEBUG 1");
      inString = p.readString(); 
    
      if (inString.equals("START")) {
        println("DEBUG 2 - Start Found");
        startProcessing = true;
      } 
      else if (inString.equals("END")) {
        println("DEBUG 3 - End Found");
        startProcessing = false;
        count = 0;
      } 
      else {
        //Must be data - Might new to check for new data
        //println("DEBUG 4 - New Data");
        //Display inString
        println(inString);
        newDataToBeProcessed++;
        newDataLine = inString;
      }
    } 
    
  • edited March 2014

    "Let's try this again."
    Do you know you can edit your first message?
    And have you read the sticky? To newcomers in this forum: read attentively these instructions Don't forget that the code must be surrounded by blank lines.

    Note: you should use trim() on received strings, they often have spaces or newlines included in them.

  • Hey PhiLho,

    I did a preview before I posted. Apparently is doesn't take into account the span tag. I'll be sure to read the sticky before I post more code.

    I'll take a look at the trim command. I'm sure that after START and END there has a new line character (\n). I thought the bufferuntil(lf) would remove the new line character.

    Never mind. The trim command worked! Thanks.

Sign In or Register to comment.