Reputation: 1
I am facing the following error in a Processing application off and on, and sometimes it works perfectly.
Error, disabling serialEvent() for COM2
null
Here is the code:
Arduino's:
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);
I am using Windows 7, Processing version 2.2.1, and Arduino version 1.0.5-r2.
I'm new to all serial communication stuff...
Upvotes: 0
Views: 11924
Reputation: 1
// At the top of the program:
float xPos = 400; // Horizontal position of the graph
float yPos = 300; // Vertical position of the graph
float zPos = 300;
float regiypos = yPos;
float regizpos = zPos;
int par = 5;
import processing.serial.*;
Serial myPort; // The serial port
int lf = 10; // Linefeed in ASCII
String myString = null;
float num;
void setup () {
size(800, 600); // (Width, Height) window size
myPort = new Serial(this, "COM4", 115200);
background(#081640);
noLoop(); //-- Ne fussál folyamatosan
}
void draw () {
// Draw the line in a pretty color:
stroke(#A8D9A7);
line(xPos-par, regiypos, xPos, yPos); // (x1, y1, x2, y2) Origó a bal felső sarokban
regiypos = yPos; // ami most új volt azt megőrzi, hogy legyen mivel összekötni
stroke(#ff00A7);
line(xPos-par, regizpos, xPos, zPos);
regizpos = zPos;
// At the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
// Clear the screen by resetting the background:
background(#EDA430);
background(#081640);
}
else {
// Increment the horizontal position for the next reading:
xPos += par;
}
}
void serialEvent (Serial myPort) { // -- Nem fut folyamatosan, hanem csak akkor ha adat érkezik, mert akkor meghívodik ez a fv.
// Get the byte:
// float inByte = myPort.read();
// print it:
// println(inByte);
myString = myPort.readStringUntil(lf);
if (myString != null) {
print(myString); // Prints String
String[] q = splitTokens(myString, ":");
if (q.length == 2) {
print(q[0]); // Acceltoroll print
print(q[1]); // Gyrotoroll print
num = float(q[0]); // Converts and prints float
yPos = height/2 - num;
num = float(q[1]); // Converts and prints float
zPos = height/2 - num;
redraw(); // -- Melynek a végén van a redraw() parancs, mely egyszer lefutattaja a draw()-ban lévő részt.
}
}
}
And the highlighted part is this:
if (q.length == 2) {
print(q[0]); // Acceltoroll print
print(q[1]); // Gyrotoroll print
num = float(q[0]); // Converts and prints float
yPos = height/2 - num;
num = float(q[1]); // Converts and prints float
zPos = height/2 - num;
redraw(); // -- Melynek a végén van a redraw() parancs, mely egyszer lefutattaja a draw()-ban lévő részt.
}
This will solve your problem. The problem is that q1 may not exist if the buffer is full and begins to overwrite the data...
Just paste the code to Notepad++ to get a better view.
Upvotes: 0
Reputation: 163
Put your code inside the draw() function, and just use serialEvent() to change the value of variables (remember to initialize them).
Make inStr a global variable. You can define its length and initialize it maybe with 0.
It should be something like:
import processing.serial.*;
Serial myPort; // The serial port
int[] inStr = {0, 0};
void setup () {
size(1043, 102);
background(255);
myPort = new Serial(this, Serial.list()[1], 9600);
println(Serial.list());
myPort.bufferUntil('\n');
}
void draw () {
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);
}
void serialEvent (Serial myPort) {
String inString = myPort.readStringUntil('\n');
if (inString != null) {
inString = trim(inString);
inStr = int(split(inString, '-'));
println(inStr);
}
}
In the future, try not to use serialEvent to do the work of draw().
Upvotes: 0
Reputation: 4014
You have to surround your serialEvent method body in try-catch:
void serialEvent (Serial myPort) {
try {
...
your code
...
}
catch(RuntimeException e) {
e.printStackTrace();
}
}
There is probably an exception thrown from your serialEvent implementation.
Upvotes: 3