FAQ
Cover
\
Build
\
Source
\
Bugs
\
Reference
\
Libraries
\
Tools
The bugs database has moved
here
.
Bug 1422 : Possible bug when using "serialEvent"
Last modified: 2010-02-17 20:05
P
roject:
processing
trash
Version:
unspecified
Co
m
ponent:
android
book
core
libraries
pde
reference
tools
web
Status:
RESOLVED
Resolution:
INVALID -
Pr
i
ority:
P2
Severity:
normal
Platform
All
O
S:
All
Windows
Mac OS
Linux
Other
Reporter:
Andreas Gazis
Assigned To:
fry
Attachment
Type
Created
Size
Actions
Description
: Opened: 2009-12-28 12:59
Processing 1.0.9, running on Kubuntu Linux 9.10.
I include code which will open a serial port using the "bufferUntil"
function and then wait in function "setup" until a serialEvent is fired.
The way this is done is by having "serialEvent" raise a flag once this
happens (the idea is for an initial String to be read from the serial port
before we head into "draw"). To test this code you need something talking
to the serial port, an arduino firing off linefeed separated strings every
second or so will do nicely (I am using a Duemilanove sending NMEA strings
from a GPS). The minimal code is below:
import processing.serial.*;
int lf = 10; // Linefeed in ASCII
Serial myPort;
boolean flag = false;
String inString;
void setup() {
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
myPort.bufferUntil(lf);
while (!flag) {
print("");
}
println(inString);
exit();
}
void serialEvent(Serial p) {
inString = (myPort.readString());
myPort.clear();
flag = true;
}
Now, this works fine. However, try removing the, apparently useless,
"print("");" statement inside the while loop and the code hangs. Ideally, a
"while (!flag);" statement should have been all that's needed for the code
to wait until a linefeed terminated string appears on the serial port.
However, it seems to need something in the while loop and not just anything
(I tried a "int x=3;" and it still hangs) but a print of some sort (even
empty).
Any ideas?
Additional Comment
#1 From fry 2010-02-17 20:05
you've probably figured this out in the meantime, but you should never use
a while() loop in that fashion. unlike on microcontrollers, there are
several threads running at once in this case, so by locking one of them,
you'll have bad/inconsistent results for anything else that tries to happen
in that time. this is just bad code in this context.