import processing.serial.*; PrintWriter output; Serial myPort; // The serial port: void setup() { // List all the available serial ports: println(Serial.list()); myPort = new Serial(this, Serial.list()[0], 4800); // Create a new file in the sketch directory output = createWriter("data.txt"); } void draw() { while (myPort.available() > 0) { String inBuffer = myPort.readString(); if (inBuffer != null) { print(inBuffer); output.print(inBuffer); } } delay(10); } void keyPressed() { println("flush"); output.flush(); // Writes the remaining data to the file output.close(); // Finishes the file exit(); // Stops the program }