- Mutating the sketch’s main canvas outside the “Animation Thread” crashes the program!
- Callback serialEvent() is run by the Serial’s Thread btW.
- So even calling something like background() can be fatal!
- Actually, due to a bad design, any Processing function which depends on color() will get wrong results when they’re multithreading invoked!
- So any canvas mutating operations should be moved to draw() or called by it.
- Here’s a post link for a bare minimum Serial data receiver demo:
/**
* Efficient Serial Reading (v1.02)
* GoToLoop (2016-Jan-19)
*
* Forum.Processing.org/two/discussion/14534/
* myport-available-always-0#Item_1
*
* Forum.Processing.org/two/discussion/16618/
* processing-with-arduino-void-serialevent#Item_5
*
* Discourse.Processing.org/t/
* map-function-worked-in-processing-2-and-early-3-
* but-broke-in-3-5-3-need-guidance/11371/2
*/
import processing.serial.Serial;
static final int PORT_INDEX = 0, BAUDS = 9600;
String myString = "";
void setup() {
noLoop();
final String[] ports = Serial.list();
printArray(ports);
new Serial(this, ports[PORT_INDEX], BAUDS).bufferUntil(ENTER);
}
void draw() {
println(myString);
}
void serialEvent(final Serial s) {
myString = s.readString().trim();
redraw = true;
}