User Tools

Site Tools


barcodescanner:reading_value_over_bluetooth

Getting barcode values in Processing

Information and how it works

This sketch has been tested and working with the LaserChamp II Barcode Scanner we have in the Interaction Workshop. A commercial software called SerialMagic can be used to obtain easily the barcode value directly in any application (just like a copy/paste). Unfortunately, this is not very friendly/useful to integrate in a project often. For basic use, you don't need that SerialMagic software.

The default mode of the scanner is to send barcode values over a serial channel. The scanner runs in the legacy mode just outputting raw barcode data. The host (computer or other device) needs to acknowledge the reception, otherwise the scanner keeps sending the value over and over [Update Oct 2012]. All the details of the protocol can be found in the scanner's documentation.

Steps/instructions:

  • First make sure you pair the scanner with your host (computer or other device). The passcode is 0000.
  • Find out which port is your bluetooth virtual serial port. The example sketch will list all the serial ports. It should be something like “LaserChampBlablabla”
  • Adjust the line myPort = new Serial(this, Serial.list()[0], 9600 4800); to reflect the right index value for the corresponding serial port.
  • The following note might not be applicable/valid with the latest configuration of the unit.
  • When the scanner sends a barcode value, it is serialized, meaning it starts with a special character and 4 digits are added at the end. The raw reading could be something like .4829939393930058. You have to do some string cleaning to get the right barcode data. The final data is 82993939393 in the previous case.

Processing sketch

The following sketch is more a proof of concept than a robust solution. It only outputs to the debug panel. There is a lot of room for improvement!

import processing.serial.*;

int cr = 13;    // CarriageReturn in ASCII
String myString = null;
String newScan = null;
Serial myPort;  // The serial port

void setup() {
  // List all the available serial ports
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[12], 4800);  // 9600 or 4800
  myPort.clear();
  
   
  // Throw out the first reading, in case we started reading 
  // in the middle of a string from the sender.
  myString = myPort.readStringUntil(lf);
  myString = null;
  
  // send command to scanner to clear all BarCodes that might be in memory
  myPort.write("ClearBarCodes");
  myPort.write(cr);
}

void draw() {
  while (myPort.available() > 0) {
    myString = myPort.readStringUntil(cr);
    if (myString != null) {
      
      // check for ackknowledge
      if(myString.indexOf("<ack dev") != -1){
        // we don't care and reset to read new message
        myString = null;
        
        
      }else if(myString.indexOf("<nack dev") != -1){
        
        // reset and get ready to read new message
        myString = null;
        newScan = null;
        
        // clear the barcode from memory on the scanner
        myPort.write("ClBc"); // ClBc + CR is the command to "Clear Barcode"
        myPort.write(cr);
         
      }else{
         // good scan
         println("Raw Scan = " + trim(myString));
         
         // remove first char, and chop off the last 4 from serialization
         // newScan = myString.substring(1, myString.length() - 5);
         // println("Barcode = " + newScan);
         
         // clear the barcode from memory on the scanner
         // myPort.write("ClBc");
         // myPort.write(cr);
      }
    }
  }
}
barcodescanner/reading_value_over_bluetooth.txt · Last modified: 2012/10/18 12:12 by camille