User Tools

Site Tools


barcodescannerlogger:logging_barcodes_in_a_text_file

Saving barcode readouts in a text file

The basic notes for using the barcode scanner is available here

laserchampscanner_logger.zip

import processing.serial.*;
 
int lf = 13;    // Linefeed in ASCII
String myString = null;
String newScan = null;
Serial myPort;
PrintWriter output;
 
void setup() {
  // List all the available serial ports
  println(Serial.list());
 
  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[0], 4800);
  myPort.clear();
  // Throw out the first reading, in case we started reading 
  // in the middle of a string from the sender.
 
  myPort.write("ClearBarCodes");
  myPort.write(13);
 
  myString = myPort.readStringUntil(lf);
  myString = null;
 
  // open file logger
  SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss");
  Calendar date = Calendar.getInstance(); // the current date and time
  String dateStr = df.format(date.getTime());
 
  String filename = "log_" + dateStr + ".txt";
  output = createWriter(filename);
}
 
void draw() {
 
  while (myPort.available () > 0) {
    myString = myPort.readStringUntil(lf);
    if (myString != null) {
 
      // check for ackknowledge
      if (myString.indexOf("<ack dev") != -1) {
        myString = null;
      }
      else if (myString.indexOf("<nack dev") != -1) {
        myString = null;
        newScan = null;
        // clear the barcode from memory on the scanner
        myPort.write("ClBc");
        myPort.write(lf);
      }
      else {
        // good scan
        println("Raw Scan = " + trim(myString));
 
        // log to txt file 
        output.println(trim(myString));
        output.flush();
 
        // clear the barcode from memory on the scanner
        myPort.write("ClBc");
        myPort.write(lf);
      }
    }
  }
}
 
public void stop() {
  output.flush();  // Writes the remaining data to the file
  output.close();  // Finishes the file
  super.stop();
} 
barcodescannerlogger/logging_barcodes_in_a_text_file.txt · Last modified: 2012/10/18 12:13 by camille