Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 19-8: Reading from serial port

import processing.serial.*;

int val = 0; // To store data from serial port, used to color background
Serial port; // The serial port object

void setup() {
  size(200,200);
  
  // In case you want to see the list of available ports
  // println(Serial.list());
  
  // Using the first available port (might be different on your computer)
  port = new Serial(this, Serial.list()[0], 9600); 
}

void draw() {
  // The serial data is used to color the background.   
 background(val); 
}

// Called whenever there is something available to read
void serialEvent(Serial port) {
  // Data from the Serial port is read in serialEvent() using the read() function and assigned to the global variable: val
  val = port.read();
  // For debugging
  // println( "Raw Input:" + val);
}
  • Jessica

    I tried to get two computers to talk to each other through the serial port using Processing. However, It hasn’t worked. I tried this book and you had this example. I couldn’t find where the +input was coming from for the debugging statement inside the serialEvent(). I don’t think that the serialEvent() function is getting called. If you have any tips on this, that would be great.

  • http://www.learningprocessing.com Daniel Shiffman

    You may want to consider looking at the networking client/server examples for communication between machines as well. Otherwise, it’s hard to know why it isn’t working, perhaps use a console / terminal to make sure your serial ports and working and data is flowing before wondering if it’s an issue with your Processing code.

  • Logictom

    I believe
    // println( “Raw Input:” + input);
    in serialEvent should be:
    // println( “Raw Input:” + val);
    Unless input is some Prcoessing defined variable? (Only been using Processing < 1hr :P )

  • http://www.learningprocessing.com Daniel Shiffman

    Whoops, that’s right! Going to correct it now, thank you!