import processing.serial.*; PrintWriter output; // output file Serial myPort; // The serial port int xPos = 1; // hor position graph float previousValueX; float previousValueY; void setup() { previousValueX = -1; previousValueY = -1; size(800, 800); // set the window size: println(Serial.list()); // List serial ports myPort = new Serial(this, Serial.list()[0], 9600); // COM8 myPort.bufferUntil('\n'); background(0); // set inital background: println("Click on image and hit 's' to start"); // will start serial data println("Hit 'w' to write to file"); // dump to file ad stop String file = String.valueOf(year()); file = file + "." + String.valueOf(month()); file = file + "." + String.valueOf(day()); file = file + "." + String.valueOf(hour()); file = file + "." + String.valueOf(minute()); file = file + "." + String.valueOf(second()) + ".mat"; println(file); output = createWriter(file); // Sketch->Show_Sketch_fie } // setup void draw() { if(keyPressed) { if(key == 's' || key == 'S') { myPort.write("H"); } // if (key == 's' || key == 'S') if(key == 'w' || key == 'W') { output.flush(); // Writes the remaining data to the file output.close(); // Finishes the file exit(); // Stops the program } // if (key == 'w' || key == 'W') } // if( keyPressed) } // draw () /** * For processing 2.0 release, It's here because: * http://forum.processing.org/two/discussion/838/function-readstringuntilint-does-not-exist */ public String readStringUntil(Serial myPort, int inByte) { byte temp[] = myPort.readBytesUntil(inByte); if (temp == null) { return null; } else { return new String(temp); } } void serialEvent(Serial myPort) { String inString = myPort.readStringUntil('\n'); // get the ASCII string if(inString != null) { inString = trim(inString); // trim whitespace println(inString); if(inString.equals("@")) // Control character '@' means 'Start curve' { previousValueX = -1; previousValueY = -1; } else if(inString.startsWith("#")) // Control character '#' means 'End curve' { textSize(10); text(inString.substring(1), previousValueX + 2, height - previousValueY); // Print Ub voltage right on curve } else if(inString.equals("$")) // Control character '$' means 'Tracing finished' { output.flush(); // Writes the remaining data to the file output.close(); // Finishes the file //exit(); // Stops the program } else if(inString.equals("!")) // Control character '$' means 'Tracing finished' { ; } else { int[] vv = int(split(inString, ' ')); output.println(inString ); float valueY = float(vv[0]); // collector current float valueX = float(vv[1]); // Uce voltage valueY = map(valueY, 0, 1023, 0, height * .95); valueX = map(valueX, 0, 1023, 0, height * .95); if(previousValueX == -1) { previousValueX = valueX; previousValueY = valueY; } else { stroke(127, 34, 255); // color to draw line(previousValueX, height - previousValueY, valueX, height - valueY); // draw the line previousValueX = valueX; previousValueY = valueY; } /*if(xPos >= 6 * width) { xPos = 0; // auto redraw background(0); } // if (xPos >= 2*width) else { xPos = xPos + 1; }*/ // else } // if(inString.startsWith('#') } // if (inString != null) } // serialEvent (Serial myPort)