tags:

views:

7551

answers:

8

How to read a value from a barcode reader using c#.

+2  A: 

The barcode reader will send you a text string. Just like a keyboard...

rangitatanz
+14  A: 

In my experience a barcode reader acts as a HID and mimics a keyboard. When a barcode is read, it is sent to the OS and to the app as a sequence of keystrokes. So the best way to capture that info is often to create a text field and set focus to it in anticipation of receiving a barcode.

Or if you want to be crafty you could just listen for keystroke events in your app and record them silently or something. As Renaud pointed out, some barcode readers can emit "announcement" data before the actual data, but in my experience this is not consistent. If you can ensure all your users will have the same model reader, this can be a very helpful route to take. Otherwise, maybe not.

Rex M
+8  A: 

Most barcode readers are HID devices: they act just like a keyboard and return keystrokes, as if they were typed on a keyboard.

Method 1

For those, you can basically catch all keyboard activity for a given input field where users can either enter the bar code manually or using a barcode reader.

Method 2

Another method is to program the bar code reader to use a special prefix sequence before sending the barcode string.

Your program then catches all keystrokes at the application-level and if it detects this prefix, then it knows that the remaining characters are a bar code.

Bar code readers can always be programmed using either a software or some special barcodes you'll find in their user manual.
You can easily make them produce a special prefix and suffix so that everytime you read a barcode, it will be enclosed in a special sequence like this for instance:

[>-     01928374645    ->]
 ^           ^          ^
prefix    barcode   suffix

Other types of bar code readers may have some special interface and special drivers, or they could communicate over RS232, but I don't believe there is a standard for those (except the most simple RS232 interface) and communicating with them will properly require a special SDK or drivers from the manufacturer.

In most cases though, for handheld readers, the methods above are all you need.

Renaud Bompuis
I have had to work with some very diverse handheld readers, and IIRC a few of those were not configurable. All on the cheap end, support a handful of barcode formats out of the box, and nothing else.
Rex M
They must have been very cheap indeed :-) The cheapest I ever had (~US$25) at least had some Chinglish manual on a disk that you could print to program preambles.
Renaud Bompuis
HID device? Human interface device device? Someone's got RAS syndrome ;)
Gareth
+1  A: 

It really depends on the make of the reader. Some readers, f.ex. "field readers" (i.e. readers who're stand alone, and not hooked up to any other hardware when in use), will store some the reads in XML, or a large text-file.

Some readers will return the bar code simply like a string - which is what the barcode is in the end, a string represented by bars.

Marcus L
+4  A: 

A barcode scanner can operate in two modes

  • as Rex M mentioned: like a keyboard
  • or as a serial device.

To get it into the latter you will need to 'program' the device (most likely using a special barcode in the manual) and connect to it using SerialPort. For example:

void setup()
{
    scannerSerialPort = new SerialPort("com1", 9600, Parity.None, 8, StopBits.One);
    if (!scannerSerialPort.IsOpen)
    {
        scannerSerialPort.Open();
        scannerSerialPort.DataReceived += new SerialDataReceivedEventHandler(scannerSerialPort_DataReceived);
    }
}

void scannerSerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        scan += scannerSerialPort.ReadExisting();
        if (scan.EndsWith("\r\n")) {
            scan = scan.Substring(0, scan.Length - 2);
            //act on new value
            UpdateDisplay(scan);
            scan = "";
        }
    }

This code is specific to the one I have used (Metrologic) so YMMV.

alphabeat
I've worked with a number of barcode scanners that were USB and did not have a serial mode (or any kind of configurability). It would be helpful if we knew how broad the device support needs to be for this question.
Rex M
Agreed. At the start of the project that used the barcode scanner we went in not knowing how to utilise it properly and ended up calling the manufacturer who was more than happy to tell us how to interface programatically.
alphabeat
A: 

There is an iPhone application which (I think) uses the camera to capture an image of a barcode and then soft translates to a string.

Desiny
+2  A: 

Most of the barcode readers I regularly use will act as a keyboard wedge which means that scanned data appears as if typed on the keyboard. To capture this we use two methods.

  1. Have a text box with input focus and the data appears as if typed
  2. Capture keyboard events and convert to a string

We use hand held scanners that are able to scan both 1D and 2D codes and are configurable as to which codes will be recognised.

There are also serial attached readers available which I have no experience of but you would require serial communication routines and the necessary communication protocol to be implemented.

The reader can be connected to the PC in a number of ways.

  1. PS2 keyboard wedge. This is where the scanner is connected to the PS2 keyboard port of the computer and the keyboard is then connected to a duplicate port supplied by the scanner cable.
  2. USB interface. This is usually configurable to allow either a keyboard input device (HID) or serial interface to the scanner.
  3. Serial interface. This provides a serial connection to the scanner and although I have no experience with this I believe that there is no standard protocol between manufacturers.
Swinders
A: 

Prior to this, I've worked with barcode reading technologies. Aspose.Barcode is by far the best value and the best to work with. You can read and create barcodes. An excellent buy and piece of software.

http://www.aspose.com/categories/visual-components/aspose.barcode-for-.net-and-java/default.aspx

Danny G