2

I am using Processing to display data coming into the serial port. I have simple string values coming into the serial port.

I have cut down all my code to this basic point of failure and I cannot understand why it fails. The if statement never evaluates to true. (I'm not trying to compare strings with = or == )

Please help if you know why this is happening :) I'd really appreciate an answer.

Ugh Alright then, since I don't have a reputation of 10, I won't post the helpful screenshot. Here is the code that fails:

    enter code here
import processing.serial.*;
Serial myPort;

String val;  //Data recieved from the serial port

void setup() {
  size(400,300);

  String portName = Serial.list()[2]; //change the 0 to a 1 or 2 etc. to match your port //1 or 2  for USB on the Mac
  myPort = new Serial(this, portName, 9600); 
  myPort.bufferUntil('\n');
}



void serialEvent (Serial myPort){
 val = myPort.readStringUntil('\n');
 print(val);
  if(val != null){   
    if(val.equals("New sample set")){
      print("yes\n");
    } else {
      print("not equal\n");
    }

  }
}


void draw() {
  //The serialEvent controls the display
}  

I have tried taking out the spaces and making it only one letter, but this did not help. I have tried using if(val.equals("New sample set")==true) but that didn't help either.

Benice
  • 409
  • 3
  • 15
  • In the screenshot that I don't have permission to add, you can see a clue: When I print val in the console, it prints a new line, so I'm guessing the problem is because val actually contains a new line character. I have tried adding a \n but still no luck. I have also tried using trim() to get rid of any unwanted characters but this did not work either. – Benice Jul 26 '15 at 05:40
  • If I'm not mistaken, I don't believe `trim()` actually removes `\n` characters from strings. – Andrew Gies Jul 26 '15 at 06:11

1 Answers1

3

Alrighty, it only took a few hours but I have answered my own question :)

Yes, comparing strings must be done with .equals() and Yes, they must be trimmed so that all line feed characters are eliminated.

I was using trim incorrectly. Simply saying myString.trim(); will not do. this returns a string, so it needs to be placed somewhere; here's how: myString=myString.trim();

I know, kind of obvious to the experienced coder, but I just couldn't see it. And I thought bufferUntil('\n') would only buffer the stuff before the \n... I guess not. Hope the hours I spent in this silly little "boggle" can help someone else! :) Happy coding!!!

Successful processing code below:

import processing.serial.*;
Serial myPort;

String val;  //Data recieved from the serial port

void setup() {
  size(400,300);
  
  String portName = Serial.list()[2]; //change the 0 to a 1 or 2 etc. to match your port //1 or 2  for USB on the Mac
  myPort = new Serial(this, portName, 9600); 
  myPort.bufferUntil('\n');
}


void serialEvent (Serial myPort){
 val = myPort.readStringUntil('\n');
 print("After reading serial, val is "+val+".\n");
 val=val.trim();
 print("After trimming, val is "+val+".\n");
  if(val != null){   
     print("After comparing to null, val is "+val+".\n");
    if("N".equals(val)){
      print("yes\n");
    } else {
      print(val+" is not equal to \"N\".\n");
    }

  }

}


void draw() {
  //The serialEvent controls the display
}  
Benice
  • 409
  • 3
  • 15