Barcode Scanning

This week marked my first foray into the world of the .NET Compact Framework. We’re using Motorola MC9090 scanners running Windows Mobile 5.0 and our application needs to read scanned barcodes directly into an existing SQL Server database.

First job was to download the Motorola EMDK kit containing the relevant .dll’s for the scanners, it even comes with a sample project demo’ing how to use them. The two main classes of note are symbol.barcode.reader and symbol.barcode.readerdata, which are pretty self explanatory and deal with the scanned data from the scanner. You need to setup a Reader object to read from the scanner and a ReaderData object to handle the read data (see told you they were self explanatory) and probably an event handler to do something with it once you have scanned.

The code snippet below demonstrates how to capture scanned data and display in a Windows MessageBox. (I’m using a Windows Smart Device Application for Windows Mobile 5.0 and .NET compact Framework 3.5)..

public partial class Form1 : Form
{
    //setup Reader & ReaderData objects
    Symbol.Barcode.Reader barcodeReader = null;
    Symbol.Barcode.ReaderData barcodeReaderData = null;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        barcodeReader = new Symbol.Barcode.Reader();
        //sets up ReaderData to receive text and allocates max buffer size for barcode (7905 bytes).
        barcodeReaderData = new Symbol.Barcode.ReaderData(Symbol.Barcode.ReaderDataTypes.Text, Symbol.Barcode.ReaderDataLengths.MaximumLabel);

        barcodeReader.Actions.Enable();  //enable scanner hardware.
        barcodeReader.ReadNotify += new EventHandler(barcodeReader_Read);  //eventHandler for when read is complete.
        barcodeReader.Actions.Read(barcodeReaderData);  //read scan.          
    }

    private void barcodeReader_Read(object sender, EventArgs e)
    {
        Symbol.Barcode.ReaderData nextReaderData = barcodeReader.GetNextReaderData();  //Get(s)NextReaderData
        MessageBox.Show(nextReaderData.Text);  //Display output in messagebox.
        barcodeReader.Actions.Read(barcodeReaderData);  //await next scan.
    }

    private void Form1_Closing(object sender, CancelEventArgs e)
    {
        //Dispose of everything
        barcodeReader.Actions.Flush();
        barcodeReader.Actions.Disable();
        barcodeReader.Dispose();
        barcodeReaderData.Dispose();
    }
}
Advertisements

4 thoughts on “Barcode Scanning

  1. Hi, I have a little problem, i used your code for a project personal, when i open de window works, but when i close the window and i open de new not work and appears this message in visual studio 2008; ” No symbols are loaded for any call stack frame. The source code cannot be displayed”
    I hope you can help me

    excuse me for the bad English

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s