问题描述
我的代码有一些问题,我可以运行它,因为我总是收到此错误消息
I have some problems in my code, i can run it because i always get this error message
错误,为 COM3 null 禁用 serialEvent()
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.
推荐答案
您的 serialEvent()
代码不处理异常.您对串行数据的处理可能包含一个错误,该错误随后会导致未处理的 StringIndexOutOfBoundsException
,因此程序崩溃.可以肯定的是,我们需要了解通过COM3
传输的实际数据.所以这里有两个建议:
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:
- 将整个代码放在
serialEvent()
中的try
/catch
块中,并打印出异常的堆栈跟踪.这应该会为您提供有关导致崩溃的原因的更多提示. - 编辑您的问题并添加 Arduino 串行监视器输出的片段.
- 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.
这篇关于Arduino + 处理代码错误“disabling_serialevent()"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!