views:

246

answers:

3

I am trying to write a program that can will read a barcode scanner. In addition, I need it to read the input even when the application is not the window in focus (i.e., running in system tray, etc).

I found this article, titled Distinguishing Barcode Scanners from the Keyboard in WinForms, that seems to solve the exact problem. It is working pretty good, it detects my device and handles the WM_INPUT message.

However, it is checking to see if the RAWINPUT.keyboard.Message is WM_KEYDOWN (0x100). It never seems to receive this. The only line of code I've altered in the code provided in the article is adding a Console.Out.WriteLine to output the actual values of that message:

Console.Out.WriteLine("message: {0}", raw.keyboard.Message.ToString("X"));
if (raw.keyboard.Message == NativeMethods.WM_KEYDOWN)
{
    ....

Here is what it outputs:

message: B
message: 1000B
message: 3
message: 10003
message: 8
message: 10008
message: 3
message: 10003
message: 5
message: 10005
message: 3
message: 10003
message: 8
message: 10008
message: 8
message: 10008
message: 4
message: 10004
message: 9
message: 10009
message: 9
message: 10009
message: 3
message: 10003

The value I'm expecting to receive when this completes correctly is:

257232709

Which I verified by scanning to notepad.

I don't know if the Operation System is relevant here, but I figured I should mention that I'm running this in Windows 7 64 and Visual Studio 2010 and .NET Framework 3.5. Scanner is a USB Barcode Scanner, Symbol LS2208, setup as "HID KEYBOARD EMULATION"

A: 

One non-software solution that could save you a lot of aggravation would be to get the RS-232 cable for this scanner and just read from the COM port. Then you wouldn't need to care which window or control had focus. That particular scanner has a COM port emulation driver, though it probably won't work under Windows 7.

Tim Trout
I tried to add a link to the driver download page, but it didn't format correctly. It's easy enough to Google, though.
Tim Trout
Thank you for your comment Tim. That was certainly an option, but I had to use a USB connection. Some coworkers had also suggested something similar, and writing a driver for it.. I wasn't ready to give up on the simpler solution and luckily didn't have.
Bryce Fischer
A: 

It occurred to me that I don't really need the solution that the article in question solves. After reading over several times i realized that it was meant to handle the situation where you couldn't setup the scanner to use prefix and suffix data. Luckily for me, I can do this.

Otherwise, I chatted with the author of the article and we believe the issue was because I was running Windows 64. I think some of the code uses incorrect datatypes that may cause 64 bit to marshal the data incorrectly... I wasn't able to verify this though.

Was a great exercise in lower level windows programming though. Great resource at pinvoke.net for using interops...

Bryce Fischer
+1  A: 

just wanted to add, that microsoft POS.net, a free library from microsoft for building point of service systems, allows you to read most common barcode scanners, and gives you an event when a barcode is scanned. The library is free, and pretty well supported, and works with any barcode scanner that has an OPOS driver.

Tom
Thanks. I got that bookmarked for future reference.
Bryce Fischer
I googled and found this http://www.codeproject.com/KB/miscctrl/posScanner.aspx. Maybe helpful.
Nano HE