Click here to Skip to main content
15,867,488 members
Articles / Desktop Programming / Win32
Tip/Trick

C# USB HID Interface

Rate me:
Please Sign up or sign in to vote.
5.00/5 (65 votes)
2 Sep 2013CPOL5 min read 461K   35.9K   99   92
Just another approach to understanding communication between HID devices and C#.

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:

  1. 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.
  2. Readability - the code has to be clear enough for me not to have to figure out what the hell is this doing here.
  3. 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.
  4. 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.
  5. 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.

From my point of view, from the moment I publish this class, please by all means feel free to use it as is, derive from it, take chunks that interest you, or you may find useful, or learn from it as much as you can without any obligation to me or even mention that I was involved in it. Just have fun and if you can find it useful, I'm happy.

I do have to mention that the main inspiration and the biggest proportion of "borrowed" code came from a framework written by Simon Inns from : http://www.waitingforfriday.com/.


In reply to a number of requests I have decided to put a working example together.

I will start with a schematic diagram of a minimal circuit necessary for USB communication based on PIC18f2550.

Image 1

And the built Prototype:

Image 2

I will also attach the source code and compiled hex for the chip, written under MPLAB 8.90 for C18 Compiler v3.45 which was the latest at the time:

After programming the chip and plugging it into the usb port I had the following effect:

Image 3

Image 4

After which I have started the small application (link below) under Visual Studio and with each click of the button the PIC replied replacing first four bytes of the sent message.

I hope this helps.

Thanks.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionFindHidDevices has a possible leak Pin
Joe Woodbury26-Jul-15 12:53
professionalJoe Woodbury26-Jul-15 12:53 
AnswerRe: FindHidDevices has a possible leak Pin
Szymon Roslowski5-Aug-15 10:11
professionalSzymon Roslowski5-Aug-15 10:11 
GeneralRe: FindHidDevices has a possible leak Pin
Joe Woodbury5-Aug-15 12:22
professionalJoe Woodbury5-Aug-15 12:22 
GeneralRe: FindHidDevices has a possible leak Pin
Member 1322203525-May-17 5:03
Member 1322203525-May-17 5:03 
QuestionPIC18F2550 used on 18F4550 Pin
sgoleary25-May-15 4:11
sgoleary25-May-15 4:11 
QuestionUsing it with win 8.1 Pin
Member 416563014-May-15 9:44
Member 416563014-May-15 9:44 
QuestionUnable to open a readHandle to the device! Pin
Member 116599419-May-15 3:58
Member 116599419-May-15 3:58 
AnswerRe: Unable to open a readHandle to the device! Pin
Member 1176526829-Jul-15 20:00
Member 1176526829-Jul-15 20:00 
did you resolve this, I'm having the same issue
GeneralRe: Unable to open a readHandle to the device! Pin
Member 120182358-Oct-15 19:20
Member 120182358-Oct-15 19:20 
GeneralVery Useful Pin
jolati17-Mar-15 12:06
jolati17-Mar-15 12:06 
GeneralRe: Very Useful Pin
Szymon Roslowski17-Mar-15 23:24
professionalSzymon Roslowski17-Mar-15 23:24 
QuestionNice Job Pin
rwest50031-Jan-15 3:17
rwest50031-Jan-15 3:17 
QuestionFailed while sending message from host (win7 Enterprise 64bit) to the device. Pin
nguyenthanhnam.epu19-Aug-14 6:56
nguyenthanhnam.epu19-Aug-14 6:56 
AnswerRe: Failed while sending message from host (win7 Enterprise 64bit) to the device. Pin
nguyenthanhnam.epu19-Aug-14 8:41
nguyenthanhnam.epu19-Aug-14 8:41 
GeneralRe: Failed while sending message from host (win7 Enterprise 64bit) to the device. Pin
daewonking1-Apr-15 1:01
daewonking1-Apr-15 1:01 
AnswerRe: Failed while sending message from host (win7 Enterprise 64bit) to the device. Pin
Szymon Roslowski7-Apr-15 3:07
professionalSzymon Roslowski7-Apr-15 3:07 
QuestionNo HID-Device Connect Pin
Ingmar Andreas Berner21-Jul-14 23:01
Ingmar Andreas Berner21-Jul-14 23:01 
AnswerRe: No HID-Device Connect Pin
Member 1210633426-Nov-15 0:39
Member 1210633426-Nov-15 0:39 
Question"window handle already exists": Your HIDTest + Microchip HID implementation Pin
yyyy119-Jul-14 1:32
yyyy119-Jul-14 1:32 
AnswerRe: "window handle already exists": Your HIDTest + Microchip HID implementation Pin
Szymon Roslowski19-Jul-14 2:19
professionalSzymon Roslowski19-Jul-14 2:19 
SuggestionRe: "window handle already exists": Your HIDTest + Microchip HID implementation Pin
sben_emka2-Feb-15 5:49
professionalsben_emka2-Feb-15 5:49 
BugProblem with connection. Pin
Member 96736372-Jun-14 2:41
Member 96736372-Jun-14 2:41 
GeneralRe: Problem with connection. Pin
Szymon Roslowski19-Jul-14 2:22
professionalSzymon Roslowski19-Jul-14 2:22 
SuggestionUsb Hid Interface Lite - for Szymon Pin
jakubbukaj30-May-14 21:14
jakubbukaj30-May-14 21:14 
QuestionMagic 32 bytes removed on getting name of device in IsNotificationForTargetDevice method Pin
jakubbukaj29-May-14 4:33
jakubbukaj29-May-14 4:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.