问题
I have some problems in my code, i can run it because i always get this error message
Error, disabling serialEvent() for COM3 null
import processing.serial.*;
Serial port;
String c = " ";
String d = " ";
String data = " ";
PFont font;
int index = 0;
void setup() {
size(2024, 1024);
port = new Serial(this, "COM3", 9600);
port.bufferUntil('.');
font = loadFont("run.vlw");
textFont(font, 60);
}
void draw() {
background(150, 50, 200);
fill(46, 20, 2);
text(c, 70, 175);
fill(46, 20, 2);
text(d, 70, 215);
}
void serialEvent(Serial port) {
data = port.readStringUntil('.');
data = data.substring(0, data.length() - 1);
index = data.indexOf(",");
c = data.substring(0, index);
d = data.substring(index + 1, data.length());
}
I am new in this thing, sorry if i made a big mistake.
回答1:
Your serialEvent()
code doesn't handle exceptions. It is likely that your handling of the serial data contains an error which subsequently causes an unhandled StringIndexOutOfBoundsException
, so the program crashes. To be sure, we would need knowledge of the actual data that gets transmitted via COM3
.
So here are two suggestions:
- Put the whole code inside of
serialEvent()
in atry
/catch
block and print out the exception's stack trace. This should give you more hints about what causes the crash. - Edit your question and add a snippet of your Arduino Serial Monitor's output.
Can't comment yet, so I have to post this as an answer.
来源:https://stackoverflow.com/questions/42618052/arduino-proccesing-code-error-disabling-serialevent