import processing.serial.*;
String outputString = "";
PFont arial;

Serial port;

void setup() {
  size(600, 200);
  noStroke();
  fill(0);
  arial = loadFont("arial.vlw");

  textFont(arial, 48);
  // Open the port that the board is connected to and use the same speed (9600 bps)
  port = new Serial(this, Serial.list()[0], 9600);

}

void draw() {
  background(255);
  if (0 < port.available()) {  // If data is available,
    int val = port.read();         // read it and store it in val
    print(char(val));
  }
  text(outputString,15,height/2);
}

void keyPressed(){

  if(key == '1'){  
    // auto baud
    port.write('\r');
    port.write('\r');
  } else if(key == '2'){
    // open file with date format
    String d = "" +hour() + minute() + second();
    println(d);
    port.write("ow A:\\"+ d +".TXT\r");
  } else if(key == '3'){
    // write file with a certain amount of chars
    int s = outputString.length()+1;
    port.write("w #1 "+ s + "\r");
  } else if(key == '4'){
    // write output string to file
    port.write(outputString + "\n\r");
  } else if(key == '5'){
    // close file
    port.write("q\r");  
  } else {
    // add characters to output
    outputString += "" + key; 
  }
}