Click here to Skip to main content
16,015,991 members
Articles / Desktop Programming / Win32
Tip/Trick

C# USB HID Interface

Rate me:
Please Sign up or sign in to vote.
5.00/5 (66 votes)
2 Sep 2013CPOL5 min read 486.4K   36.4K   101   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

 
GeneralSTM32 sample project Pin
Member 1437427931-Jul-22 9:28
Member 1437427931-Jul-22 9:28 
QuestionSample doesn't transmit any data Pin
Paul Shaffer8-May-21 16:03
Paul Shaffer8-May-21 16:03 
I see that the sample will detect my device and generate events. Everything looks perfect for detection. The connect and disconnect events are firing. Problem is that in WriteRawReportToDevice, Kernel32.WriteFile returns true, but no data reaches the device.

Then I changed the IMessage byte array length (65) to equal the OutputReportByteLength (9) and transmit worked.

modified 9-May-21 16:15pm.

Questionhow to set target supported framework to 4.8 , Pin
Member 134932471-Nov-19 1:17
Member 134932471-Nov-19 1:17 
QuestionAccess Denied during ReadHandle Pin
Member 140965536-May-19 22:55
Member 140965536-May-19 22:55 
QuestionCross Platform USB/Hid Access Pin
TheChristian21-Jan-19 22:34
TheChristian21-Jan-19 22:34 
GeneralMy vote of 5 Pin
Member 771366221-Jul-17 14:54
Member 771366221-Jul-17 14:54 
QuestionRegarding code:-how to read the usb device and to show the files in that device Pin
Member 1322203525-May-17 4:06
Member 1322203525-May-17 4:06 
GeneralMy vote of 5 Pin
Robert_Dyball25-Apr-17 23:51
professionalRobert_Dyball25-Apr-17 23:51 
Questionwritefile fail on windows 7 with getlasterror() = 87 Pin
Member 1298682312-Feb-17 0:12
Member 1298682312-Feb-17 0:12 
QuestionUsing the device in differents forms Pin
antonioclj7-Nov-16 6:35
antonioclj7-Nov-16 6:35 
AnswerRe: Using the device in differents forms Pin
antonioclj8-Nov-16 1:28
antonioclj8-Nov-16 1:28 
QuestionResetting the USB system Pin
Member 1276924420-Oct-16 9:21
Member 1276924420-Oct-16 9:21 
AnswerRe: Resetting the USB system Pin
Member 1276924420-Oct-16 10:31
Member 1276924420-Oct-16 10:31 
QuestionSlight Improvement Pin
Szymon Roslowski16-Aug-16 0:29
professionalSzymon Roslowski16-Aug-16 0:29 
QuestionGreat job! Pin
Filip Albert15-Apr-16 8:04
Filip Albert15-Apr-16 8:04 
Questionselect a device based on serial number (of two devices with same VID/PID) Pin
Member 934117018-Mar-16 7:23
Member 934117018-Mar-16 7:23 
PraiseThank you for making this library! Pin
Neuroprogrammer19-Dec-15 12:53
Neuroprogrammer19-Dec-15 12:53 
QuestionNot able to write in sniffers usbhid Pin
Member 1210633426-Nov-15 0:37
Member 1210633426-Nov-15 0:37 
QuestionNot able to detect usb in USB2550Hid TEST Pin
Member 1210633426-Nov-15 0:36
Member 1210633426-Nov-15 0:36 
QuestionWindow handle already exists. Pin
Member 120182358-Oct-15 19:39
Member 120182358-Oct-15 19:39 
AnswerRe: Window handle already exists. Pin
thomca18-Jul-22 9:32
thomca18-Jul-22 9:32 
QuestionGreat works, but it still can not send in Win7 32 Pin
Member 118842285-Aug-15 21:45
Member 118842285-Aug-15 21:45 
AnswerRe: Great works, but it still can not send in Win7 32 Pin
user11213-Aug-15 8:11
user11213-Aug-15 8:11 
GeneralMy vote of 5 Pin
Joe Woodbury5-Aug-15 12:23
professionalJoe Woodbury5-Aug-15 12:23 
General5 Stars Pin
Banjoo4-Aug-15 4:58
Banjoo4-Aug-15 4:58 

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.