Physical Interaction Design Using Processing

 

Serial Read

Here's about the simplest serial input program you could have for Processing. This example takes in bytes from a serial port and prints their values to the window.

Note that you'll need a font. You can make your own font in Processing from the Tools menu and replace the one I used with your own. You'll also need to pick the appropriate serial port, but the setup() method prints a list of the serial ports, so you can figure out which one is the one you want.

/* serial available() and read() example
 by Tom Igoe
 
 Created 20 April 2005
 Updated 5 July 2005
 */

import processing.serial.*;

Serial myPort;  // The serial port
PFont myFont;

void setup() {
  size(200,200);
  myFont = loadFont("GaramondPremrPro-Disp-48.vlw");
  textFont (myFont, 48);
  // List all the available serial ports
  println(Serial.list());
  // I know that the third port in the serial list on my mac
  // is always my  Keyspan adaptor, so I open Serial.list()[2].

  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[2], 9600);
}

void draw() {

  while (myPort.available() > 0) {
    background(0);
    fill(255);
    int inByte = myPort.read();
    text(inByte, 75,100);
  }
}