本文介绍了Arduino和处理代码错误"disabling_serialevent()";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在打开和关闭处理应用程序时遇到以下错误,有时效果很好.
I am facing the following error in Processing application off and on, sometimes it works perfectly.
空
以下是代码:
Arduino的:
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue1 = analogRead(A0);
int sensorValue2 = analogRead(A1);
Serial.print(sensorValue1);
Serial.write("-");
Serial.println(sensorValue2);
delay(1);
}
正在处理:
import processing.serial.*;
Serial myPort; // The serial port
void setup () {
size(1043, 102);
background(255);
myPort = new Serial(this, Serial.list()[1], 9600);
println(Serial.list());
myPort.bufferUntil('\n');
}
void draw () {
// everything happens in the serialEvent()
}
void serialEvent (Serial myPort) {
background(255);
String inString = myPort.readStringUntil('\n');
if (inString != null) {
inString = trim(inString);
// String inByte = inString;
int[] inStr = int(split(inString,'-'));
println(inStr);
fill(0);
rect(10,2,inStr[0],46);
rect(10,52,inStr[1],46);
fill(255);
rect(400,14,245,21);
fill(0);
textAlign(CENTER);
textSize(14);
text("1st value: "+inStr[0]+" 2nd value: "+inStr[1],width/2,30);
}
}
当我删除以下代码部分时,该应用程序运行正常.
When I remove following part of code, the application works fine.
fill(0);
rect(10,2,inStr[0],46);
rect(10,52,inStr[1],46);
fill(255);
rect(400,14,245,21);
fill(0);
textAlign(CENTER);
textSize(14);
text("1st value: "+inStr[0]+" 2nd value: "+inStr[1],width/2,30);
我正在使用Win7,正在处理ver2.2.1& Arduino ver1.0.5-r2.
I am using Win7, Processing ver2.2.1 & Arduino ver1.0.5-r2.
我是所有串行通信人员的新手.
I'm new to all serial communication stuff..
推荐答案
您必须将您的serialEvent方法主体包含在try-catch中:
You have to surround your serialEvent method body in try-catch:
void serialEvent (Serial myPort) {
try {
...
your code
...
}
catch(RuntimeException e) {
e.printStackTrace();
}
}
您的serialEvent实现中可能会引发异常.
There is probably an exception thrown from your serialEvent implementation.
这篇关于Arduino和处理代码错误"disabling_serialevent()";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!