// Serial port comm in Processing (http://processing.org) to capture data from // Optoelectronics 3000A+ frequency counter. // RS232 levels (+/- 10V) on 3.5mm TRS miniplug (tip-ring-sleeve) // TRS wiring on 3000A+: Tip: RX from PC, Ring: TX to PC, Sleeve: GND // Serial port setup: 8N1 at 4800 bps // PC sends cr (0x0d), and 3000A+ responds with 11 chars + 0x0d // by John Beale Feb. 15 2011 import processing.serial.*; Serial myPort; // The serial port String fword[] = new String[13]; int serialCount = 0; // how many bytes read on serial port int wordCount = 0; // how many frequency values read from counter PrintWriter fout; // output text data (.csv) file float sec; // elapsed program runtime, seconds int lf = 10; // ASCII linefeed int cr = 13; // ASCII carriage return float nowTime, interval; // millisecond time units // ==================================================== void setup() { size(255, 255); // output window this many pixels (x,y) noStroke(); colorMode(HSB, 256); // HSB = hue, saturation, brightness background(0); // black background color // List all the available serial ports: println("Available serial ports:"); println(Serial.list()); /* Use selected serial port at 4800 bps */ myPort = new Serial(this, Serial.list()[3], 4800); myPort.clear(); // clear serial port buffer // log data to file String fname = "Counter" + str(month()) + str(day()) + str(hour()) + str(minute()) + ".csv"; fout = createWriter(fname); // open log data file fout.println("# Frequency Counter Data recorder v0.1 by J.Beale 2/17/2011"); fout.print("# Start at "); fout.println(month() + "/" + day() + "/" + year() + " " + hour() + ":" + minute() + ":" + second() + " PST"); fout.println("seconds,frequency"); sec=millis()/1000.0; // processor runtime elapsed float hours = sec / 3600.0; fout.print(hours + ","); // write to file print(hours + ", "); // display on screen fout.flush(); // write out any buffered characters to data file } // end setup() // ---------------------------------------------------- void draw() { // main loop myPort.write(cr); // send carriage return delay(1000); // wait x milliseconds } // end draw() void serialEvent(Serial myPort) { // run each time character is received // read a byte from the serial port: char c = (char) myPort.read(); if (c != 0x0d) { print(c); // Opto 3000A+ sends 11 chars plus 0x0d to mark EOL fout.print(c); // write character to file } serialCount++; // If we have 12 bytes: if (serialCount > 11 ) { wordCount++; serialCount = 0; // reset serial count println(); // newline on screen fout.println(); // newline in file sec=millis()/1000.0; // processor runtime elapsed float hours = roundd( sec / 3600.0, 1000); print(hours + ", "); // display on screen fout.print(hours + ","); // write to file if (wordCount%30 == 0) { // every 5 minutes fout.flush(); // write out any buffered characters to data file } } } void shutdown() { fout.print("# file stopped at "); fout.println(month() + "/" + day() + "/" + year() + " " + hour() + ":" + minute() + ":" +second()); fout.flush(); // write out any buffered characters to data file fout.close(); // close the output data file exit(); // stops program } float roundd(float in, float dig) { return( ((int)((in*dig) + 0.5))* 1.0/dig); }