import java.io.FileNotFoundException; import java.io.FileOutputStream; import processing.core.PApplet; import processing.serial.Serial; public class serialtofile extends PApplet { /** * */ private static final long serialVersionUID = 1L; Serial port; // The serial port int xpos; FileOutputStream file; public void setup() { size(800, 600); // Stage size noStroke(); // No border on the next thing drawn // Print a list of the serial ports, for debugging purposes to find out // what your ports are called: println(Serial.list()); port = new Serial(this, Serial.list()[0], 9600); // you can pull the // name out of the // list // port = new Serial(this, “COM32″, 9600); // or you can just specify it port.write(65); // Send a capital A in case the microcontroller is // waiting to hear from you try { file = new FileOutputStream("log.txt", true); } catch (FileNotFoundException loui) { // TODO Auto-generated catch block loui.printStackTrace(); } // bool tells to append background(50); } public void draw() { } public void serialEvent(Serial port) { String input = port.readStringUntil(10); // make sure you return (Ascii // 13) at the end of your // transmission if (input != null) { println("Raw Input: " + input); String[] parts = input.split(","); // this will only work if you put // commas (Ascii 44) between // things in your transmission if (parts.length > 1) { try { int breath = Integer.parseInt((parts[0].trim()));// - 450; // int heart = Integer.parseInt((parts[1].trim())); fill(255, 255, 255); ellipse(xpos, breath, 1, 1); // fill(255, 0, 255); // ellipse(xpos, heart, 2, 2); xpos = xpos + 2; if (xpos >= width) { xpos = 0; background(50); } } catch (NumberFormatException e) { println("Not a line of Numbers"); } } else { } try { long now = System.currentTimeMillis(); String output = now + "," + input; file.write(output.getBytes("UTF8")); // (string, start char, end // char) } catch (Exception e) { println("Error: Can’t write file!"); } } } }