
THE SIMPLEST NETWORK 61
Open the Processing application and
enter the following code:
/*
Serial String Reader
Language: Processing
reads in a string of characters from a serial port
until it gets a linefeed (ASCII 10).
Then splits the string into sections separated by commas.
Then converts the sections to ints, and prints them out.
*/
import processing.serial.*; // import the Processing serial library
int linefeed = 10; // Linefeed in ASCII
Serial myPort; // The serial port
void setup() {
// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Arduino module, so I open Serial.list()[0].
// Change the 0 to the appropriate number of the serial port
// that your microcontroller is attached to.
myPort = new Serial(this, Serial.list()[0], 9600);
// read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil(linefeed);
}
void draw() {
// twiddle your thumbs
}
// serialEvent method is run automatically by the Processing sketch
// whenever the buffer reaches the byte value set in the bufferUntil()
// method in the setup():
void serialEvent(Serial myPort) {
// read the serial buffer:
String myString = myPort.readStringUntil(linefeed);
// if you got any bytes other than the linefeed:
if (myString != null) {
»
Try It
easier to send the data as ASCII. ...