We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have an Arduino program that is sending data to Processing via serial. It is meant to send a heads up message based on what kind of data is to follow. For example, when I want the Arduino to pipe some image data via serial, the message begins with ">IMG:". So I'd like to test for that string, then do something with the array of numbers that follow.
if ( port.available() > 0) { // If data is available,
//val = port.read(); // read it and store it in val
String test[] = {
"","","","",""
};
for (int i = 0; i < test.length; i++) {
test[i] = str(char(port.read()));
}
println(test);
if(test == "> I M G :") {
println("yup");
}
I can never figure out how to make Processing play nice with strings. I just want to take in the first 4 chars and see if combined they form ">IMG:"
Also, if the Arduino is sending serial data as HEX, is there anything special I need to do when reading via Processing to make sure it safely converts to a usable int of the same value?
Answers
http://processing.org/reference/String_equals_.html
Hmm no dice. Even though it is clearly printing "> I M G :" in the console, it isn't passing the IF statement
Perhaps trim() might help that out: http://processing.org/reference/trim_.html
Yes, most people having problems with equality of strings coming from serial solve them with trim(), because there can be extra spaces or newline chars.