Reading data from a uLog logger, saving to a file

Here’s a Processing sketch that takes data from Sparkfun’s uLog datalogger via a serial port, and saves the results to a file. To use this, you’ll need:

/*
    uLog reader
 
 This sketch reads data from SparkFun's uLog data logger
 (sku: WIG-09228) and saves the results to a file.
 
 created 26 Oct 2009
 by Tom Igoe
 
 
 To use it:
 1) Connect logger to a USB-to-serial adaptor
   * Logger RX to Serial TX
   * Logger TX to Serial RX
   * Logger ground to Serial ground
   * Logger 3.3V to 3.3V
 2) Run sketch.  Type 'r' in the window to read data
 3) From the Sketch menu, choose "show sketch folder". 
 The resulting file is called "datalog.txt"
 4) When you get a ? prompt, you can erase data from the logger by typing 'e'
 
 */

import processing.serial.*;

Serial myPort;                  // instance of the serial library
String dataString = "";         // string of data for each set of readings
String[] data = {
  ""};                          // array to save strings to a file
boolean reading = false;        // whether or not you're reading from the logger

String filename = "datalog.txt";  // string to save the file to
int lineCount = 0;              // count of the number of lines in the file

void setup() {
  // open the serial port:
  myPort = new Serial(this, Serial.list()[0], 38400);
  // flush the serial buffer:
  myPort.clear();
}

void draw() {

}

void serialEvent(Serial myPort) {
  // get a byte:
  int inByte = myPort.read();

  // do something different depending on what the byte is:
  switch(inByte) {
  case '?':          // response prompt
    // if you're already reading from the logger, 
    //save the results to a file:
    if (reading) {
      saveStrings(filename, data); 
      // you're done reading:
      reading = false;
      // print the linecount:
      print("lineCount: " + lineCount + "\0\r");
    }
    // print the prompt:
    println(char(inByte));
    break;
  case '\r':      // carriage return at the end of each line:
    // make sure you have at least four characters:
    if (dataString.length() > 4) {
      // add this string to the data array:
      data = append(data, dataString);
      // increment the line count:
      lineCount++;
      
    }
    // clear the string variable for the next string:
    dataString = "";
    break;
  case '\n':        // newline: get rid of them
    break;
  default:    // any other character:
    // add character to the string:
    dataString += char(inByte);
    break;
  }
}

void keyReleased() {
  if (key == 'r') {
    reading = true; 
    println("Reading from logger...");
    // send keystroke to the logger
    myPort.write(key); 
  }

  if (key == 'e') {
    println("erasing data from logger...");

    // you're erasing all the data on the logger
    // wait until the LEDS stop flashing before you disconnect it!
    // send keystroke to the logger
    myPort.write(key); 
  }

}
This entry was posted in Processing, circuits and tagged , , . Bookmark the permalink.

2 Responses to Reading data from a uLog logger, saving to a file

  1. Pingback: Xiaoyang's ITP Life » Tutorial: uLog data logger from Sparkfun

  2. Pingback: SINULOG FESTIVAL | My Supernatural Stories