Fighting with computers

Computers are not always friendly.

Thursday, August 09, 2012

Read from multiple input devices

One of the cool features of USB is that using Human Interface Device (HID) you can get a new device to behave as, for example, a keyboard. You can do this now with a Teensy or an Arduino UNO (but not the FTDI-based older Arduino boards). The key is that by using the proper USB messages a device can talk to the system in a way that looks like a mouse, a keyboard, etc.

The good thing about talking like a keyboard is that applications that expect a keyboard input may be fed directly with the data from that device. This many times the case with devices like a barcode reader, that can input the scanned code to the application software as if the user was typing that number on the corresponding box of the program, which is convenient just in case the code is unreadable for any reason by the reader but not by the user (or if the barcode scanner is broken or stolen).

My current project required reading RFID codes and for that I already bought a RFID reader with I2C interface that could be used from an Arduino board. However, my only interest was to read the card ID and to feed that number into the application software on a PC. I have later found a more convenient way of doing so, that requires no firmware to written: a USB ID reader. This device behaves as a keyboard and it provides as keystrokes the sequences of numbers from the card's ID. It can easily become a data source for my registering application.

However if you want to have more than a single application running in the same computer focus problems may appear and data might be delivered to the wrong software. What was like a nice and clean way of solving the problem does come with its own set of restrictions. I was looking for a way to make sure that a certain program is reading from a certain HID device and I have found that Windows has something called Raw Input that can be used precisely for that purpose. And some sample code does not hurt either. User-mode Linux driver can be used too.