by jamie allen Feb 2007

I was having a bit of trouble using the UNIX concatenate ('cat') command to quickly write incoming serial data to a file (my computer kept hanging/crashing), so this is a replacement using Processing.

This can be useful for collecting off-line data for testing, calibration or development, as well as being a very simple form of 'datalogging' to a local computer.

logs data to a file
by Jamie Allen 2007

Created Feb 2007

import processing.serial.*;

Serial myPort;  // The serial port
PFont myFont;     // The display font: 

PrintWriter output; 

// initial variables:
int inByte = -1;
int wrote = 0;
int offset = 0;
int offsettext = 25;

void setup() 

  size(100, 100);        // window size  
  // List all the available serial ports
  // 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()[3], 9600);

  output = createWriter("data.txt"); // Create a new file in the sketch directory

void draw() {

  if (myPort.available() > 0) {
    inByte =;  


void keyPressed() {
  output.flush(); // Writes the remaining data to the file
  output.close(); // Finishes the file
  exit(); // Stops the program