The Barcode is the one of the biggest inventions in business since the creation of the wheel and money. It allows businesses to process data on huge amounts of various products – instantly. You are able to obtain information such as the price and the location of the products when they’re on the move.
*
Recently, I was assigned the task of connecting this fundamental device to the Dynamics CRM system. The reason a need for this integration between the two systems came about was one of our customers needed to check event attendance levels. It was decided that a barcode scanner would be the most efficient way to implement this feature.
The exact device model is ‘Symbol LS2208’, a USB-enabled device that can be plugged in as a keyboard or using a virtual USB port. A requirement of this integration is that the barcode scanner needed to be plugged to a computer and work without interrupting tasks which are running on the PC. Another point to note is scanned barcodes should be collected in special text file for further import.
My First Attempt
Since keyboard is the default mode used by the scanner, it doesn’t need extra configuration and it is easy to handle. I did my first attempt to work that out. Basically, what I needed to do was to set a hook onto the keyboard, distinguish scanner input and write it to a separate file.
The steps to build a solution were obvious:
1. Hook keyboard input.
2. Check if it is from the scanner.
3. Write it to the file.
4. Block it.
Here is a Flowchart to demonstrate:
Fig 1. Keyboard handler logic
1. Getting input was the easiest part of the program. I had to implement the ‘IMessageFilter’ interface and register it in Application class.
2. To check origin of the keypress you have to do P/Invoke calls for GetRawInputData, GetRawInputDeviceInfo, GetRawInputDeviceList methods from User32.dll library and import several structures like RAWINPUTHEADER, RAWINPUTHKEYBOARD and RAWINPUTDEV.
You can find all these imports and structures at http://www.codeproject.com/KB/system/rawinput.aspx
NOTE: this sample uses WINAPI to hook messages but you can use the IMessageFilter interface to process messages. This is an equal mechanism except it is much easier to code.
3. Filter the data by name of the device – this part is experimental and specific to each scanner model and make.
4. Block message. You have to import PeekMessage method from user32.dll. But there’s one limitation – PeekMessage works for a current thread only and doesn’t block messages on a system-wide level. This means you will get a barcode number typed into active window unless it is your own application.
So if your application will be run exclusively the entire time you use the barcode scanner, this approach will suit your needs. However, if you have any other application that is running when scanning in done –another solution will be required.
Follow my next two blogs for my second and third attempts at the Dynamics CRM-Barcode Scanner integration.
* Image from http://www.sagedata.com/images/2007/Code_128_Barcode_Graphic.jpg