USB Barcode Scanner – Here’s my code…

Here's my code to read barcode data and display the data in a ListBox - of course this could also be extended to lookup the barcode data in a SQL Database or XML file and provide more information like "Grande Vanilla Latte", "Lemon Pound Cake", and todays price or special offer - but that's another matter.

I've installed the Windows Embedded for Point of Service SDK (WEPOS) - (an evaluation kit for WEPOS can be ordered from here) - this provides a 'managed driver library' for the most common point of service/point of sale peripherals, which of course includes Mag Stripe Readers, Barcode Scanners, and more.

Am I cheating by stating that I'm only writing a handful of lines of code to get this working ? - no, of course not, I'm making use of the tools that are available to me, which allows me to focus on my application, not focus on dealing with the hardware - my overall development time for the application is shortened, which leaves more time for squash, and Starbucks [:)]

So, here's how I wrote the C# applciation... I start by adding a reference to Microsoft.PointOfService.dll - add a couple of "using" directives for the PointOfService functions and System.Text (we need to translate the byte data from the barcode reader into a string) .


using Microsoft.PointOfService;

using System.Text;


I declare two variables, one is the PosExplorer (very useful, can be used to enumerate known devices, create instances of devices etc...), the other is the scanner object we're going to use in the application.


        private PosExplorer explorer;

        private Scanner scanner;


Here's the form load function for my form - I create an instance of the PosExplorer and also add an event handler so we get to know about devices being dynamically added to our hardware (isn't Plug and Play useful!)


        private void Form1_Load(object sender, EventArgs e)


            explorer = new PosExplorer(this);

            explorer.DeviceAddedEvent += new DeviceChangedEventHandler(explorer_DeviceAddedEvent);



Here's the DeviceAddedEvent handler - we determine whether the device is a barcode scanner, we create an instance of the scanner device, open the device, claim the device (many devices need to be claimed for exclusive use by your application - for example a two line LED/LCD display), we enable the device, create an event handler so we get informed about new scan events, and we're done.


        void explorer_DeviceAddedEvent(object sender, DeviceChangedEventArgs e)


            if (e.Device.Type == "Scanner")


                scanner = (Scanner)explorer.CreateInstance(e.Device);



                scanner.DeviceEnabled = true;

                scanner.DataEvent += new DataEventHandler(scanner_DataEvent);

                scanner.DataEventEnabled = true;

                scanner.DecodeData = true;





And here's the meat of the application - we need to translate the bardocde data from a binary/byte format into a string so we can drop this into the applications listbox (or search the string in a SQL/XML file).


        void scanner_DataEvent(object sender, DataEventArgs e)


            ASCIIEncoding encoding = new ASCIIEncoding();

            scanner.DataEventEnabled = true;




And it's just as simple to add support for an MSR (Mag Stripe Reader), Cash Drawer, two line display, receipt printer, or other often used peripherals found in self service kiosks or retail point of service/sale scenarios.


In this example I've taken a slightly longer route to getting the code up and running, this sample doesn't assume a specific bardocde reader and therefore deals with users swapping out peripherals, I can reduce the amount of code used by specifiying (in the FormLoad function ) the device I want to use and creating an instance of the device once only.


IMVHO WEPOS has benefits for you, the developer - rapid application development without needing to deal directly with the specific hardware being used in your device - and for the point of use - if a peripheral stops working you don't need to take down the entire device or application, simply swap the device and keep working, this therefore extends the lifetime of the hardware.


Oh, I forgot to mention, total time to write, build, and test this application was just a couple of minutes - I spent more time writing this blog entry !


- Mike

Comments (12)

  1. Joe says:

    > And it’s just as simple to add support for … , receipt printer

    Input devices, 2-line displays and the cash drawer are relatively easy.

    But many of the UPOS APIs are poorly designed – mostly by device manufacturers rather than application developers.

    The receipt printer in particular is far from being just as simple. Handling different capabilities, different receipt cut position, paper jams and paper out, and the difference between manufacturer’s badly written UPOS drivers is a nightmare.

  2. Dan McCarty says:

    > ASCIIEncoding encoding = new ASCIIEncoding();

    > scanner.DataEventEnabled = true;

    > listBox1.Items.Add(encoding.GetString(scanner.ScanDataLabel));

    Now that’s just cheating. How are us programmers going to stay employed if you guys make it that easy?!

    (That splashing sound you hear is me rowing to India. 🙂

  3. mikehall says:

    What’s the result?

    1200000230 (or at least that’s what I got when I scanned a barcode on a box in my office) <g>

    – Mike

  4. Good! 🙂

    I think I’ve to encourage myself to bring Symbol device from my office to home and then play with scanner counting lines of code in some weekend 🙂

  5. mikehall says:

    Mateusz, sounds like you’re going to have a great weekend !

    How about installing the Windows Embedded for Point of Service SDK, dropping the lines above into a C# project – then you’re done! – the rest of the weekend is yours to enjoy!

    – Mike

  6. > Mateusz, sounds like you’re going to

    > have a great weekend !

    Yup, but it will be next weekend. I’m after work for a few hours.

    You mean installing Windows Embedded where?

    I have Symbol MC-3000 in my office so I can only test your code 🙂

  7. Steve says:

    You know what would be even cooler (for those of us w/o a barcode scanner):

    A class that can take a picture of a barcode w/ my phone or pda camera, and convert that to the barcode number.

  8. Windows Embedded for Point of Service (WEPOS) 1.1 has been released – you can get the POS for .NET 1.1…

Skip to main content