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) .
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 !