We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey guys.
Im trying to write a processing sketch to receive and work with serial messages sent from my Arduino.
When viewed in the Arduino serial monitor, This is what is found:
-----------------
Pollux Serial Debugging
(C) 2014
-----------------
Test Mode: True
When I use the following code in processing:
import processing.serial.*;
Serial myPort;
String val;
void setup() {
myPort = new Serial(this, "COM5", 9600);
}
void draw() {
if ( myPort.available() > 0)
{ // If data is available,
val = myPort.readStringUntil('\n');
}
println(val);
}
The output I get in the processing console is all the messages as expected, But the last one loops indefinitely, or if another message is sent that is looped instead.
Does anybody have any thoughts on how I can receive the message just once and print it, like the output I get from Arduino serial monitor.
Thanks
Answers
Try this?
I tried some code similar to that last night, and both my code and the code you suggested come up with Null pointer exceptions, Line 17 in the following.
Ah I see. That's because val is only initiated when a new message is being sent. The solution is simple... initiate val beforehand :)
Still no dice unfortunately, It appears the NPE occurs when the serial connects to the Arduino, and if I put some dummy values into the strings, they will print into the console, then the serial connects and the NPE occurs.
Im using 2.0.3 Win 32 because the latest version has a bug in the Serial library and it was advised on the issues tracker to downgrade, would that be a cause?
That could be a bug. The solution is to check if the String is null before evaluating it:
That works exactly as expected, Thanks :)
Why not simply doing this: