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.
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); } } } }