Sunday, September 01, 2013

C# USB HID Interface

Inline image 2

Introduction
This article is just my little contribution to make life of those who want to interface with HID Devices a little bit easier.

Background
The reason I have got involved into this is that I have played a bit as a hobby with PIC chips from microchip and lately came across some of those that can become a USB device and I couldn't find any c# Hid Interface that would be easy to understand. I don't like to just download and use a given library, I prefer to actually understand what is going on. I have gone thru a few examples and libraries that are available out there and always found that if not one then a few features were missing.

The features I needed from my library to name a few were as follows:

  • Modularity - can be plugged into any framework and/or application and each of the features can be controlled separately ie: Notification of device removal can be switched off or ignored.

  • Readability - the code has to be clear enough for me not to have to figure out what the hell is this doing here.

  • Simplicity - I don't really want it to be ready to be a ready to go plugin, I would rather prefer it to be a good/solid staring point for anything that derives from it. Some other examples I found on the net were polluted with functions that did very specific thing to the application they were initially designed for, automatically polluting the library itself with stuff that may not be relevant or worse, interfere with what this may be used for in some other application. Often that app specific code was part of the class itself just making it messier to read if you wanted to understand it. Always though if you can wrap something up in a few sentences, why write an essay about it.

  • True real-time communication with the device. Or as true real-time as it can be. Some other examples lacked that option basing the read of state / messages from the device on the timer on the form. This was unacceptable for me. If the device wants to say something to me i want to know it straight away, so this class creates a separate thread dedicated to dealing with incoming messages as they come.

  • Stability - Some other examples I found would work ok only as long as nothing unexpected happened. If I removed the device for example the class would hang the application or just crap out in its own little way. That was unacceptable for me again so I did as much as I can do cleanly deal with anomalies.

I have to say that most of this code is "borrowed" from all those other examples I have studied and there was so many of them that it would be impossible to copy/paste all comments or file headers from all of them into this one. There is a possibility that if you made a class like this one before and published it somewhere, you may find and recognize some chunks of code copied straight from yours. I have no intention to steal your code or breach any licences / conditions that you may have placed with your code. At the end of the day I feel that if I need to open a text file and read it I may write a function for it myself or copy it quickly from the first result on the net just to save time, but regardless if it came from a file written by you or not the function will look the same because it is just text file reading. I you feel that I have stolen something from you and I really need to put a comment in the code that it came from your file please contact me and I will be happy to do so, but for now I have been concentrating on its functionality rather than legality.

Read more: Codeproject
QR: Inline image 1