Click here to Skip to main content
12,354,533 members (61,998 online)
Click here to Skip to main content
Add your own
alternative version

Stats

124.1K views
18.5K downloads
62 bookmarked
Posted

C# USB HID Interface

, 2 Sep 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

And the built Prototype:

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:

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)

Share

About the Author

Szymon Roslowski
Software Developer
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionGreat job! Pin
Member 1244967215-Apr-16 8:04
memberMember 1244967215-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
memberMember 934117018-Mar-16 7:23 
PraiseThank you for making this library! Pin
Neuroprogrammer19-Dec-15 12:53
memberNeuroprogrammer19-Dec-15 12:53 
QuestionNot able to write in sniffers usbhid Pin
Member 1210633426-Nov-15 0:37
memberMember 1210633426-Nov-15 0:37 
QuestionNot able to detect usb in USB2550Hid TEST Pin
Member 1210633426-Nov-15 0:36
memberMember 1210633426-Nov-15 0:36 
QuestionWindow handle already exists. Pin
Member 120182358-Oct-15 19:39
memberMember 120182358-Oct-15 19:39 
QuestionGreat works, but it still can not send in Win7 32 Pin
Member 118842285-Aug-15 21:45
memberMember 118842285-Aug-15 21:45 
AnswerRe: Great works, but it still can not send in Win7 32 Pin
user11213-Aug-15 8:11
memberuser11213-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
memberBanjoo4-Aug-15 4:58 
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
memberSzymon Roslowski5-Aug-15 10:11 
GeneralRe: FindHidDevices has a possible leak Pin
Joe Woodbury5-Aug-15 12:22
professionalJoe Woodbury5-Aug-15 12:22 
QuestionPIC18F2550 used on 18F4550 Pin
sgoleary25-May-15 4:11
membersgoleary25-May-15 4:11 
QuestionUsing it with win 8.1 Pin
Member 416563014-May-15 9:44
memberMember 416563014-May-15 9:44 
QuestionUnable to open a readHandle to the device! Pin
Member 116599419-May-15 3:58
memberMember 116599419-May-15 3:58 
AnswerRe: Unable to open a readHandle to the device! Pin
Member 1176526829-Jul-15 20:00
memberMember 1176526829-Jul-15 20:00 
GeneralRe: Unable to open a readHandle to the device! Pin
Member 120182358-Oct-15 19:20
memberMember 120182358-Oct-15 19:20 
GeneralVery Useful Pin
jolati17-Mar-15 12:06
memberjolati17-Mar-15 12:06 
GeneralRe: Very Useful Pin
Szymon Roslowski17-Mar-15 23:24
memberSzymon Roslowski17-Mar-15 23:24 
QuestionNice Job Pin
rwest50031-Jan-15 3:17
memberrwest50031-Jan-15 3:17 
QuestionFailed while sending message from host (win7 Enterprise 64bit) to the device. Pin
nguyenthanhnam.epu19-Aug-14 6:56
membernguyenthanhnam.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
membernguyenthanhnam.epu19-Aug-14 8:41 
GeneralRe: Failed while sending message from host (win7 Enterprise 64bit) to the device. Pin
daewonking1-Apr-15 1:01
memberdaewonking1-Apr-15 1:01 
AnswerRe: Failed while sending message from host (win7 Enterprise 64bit) to the device. Pin
Szymon Roslowski7-Apr-15 3:07
memberSzymon Roslowski7-Apr-15 3:07 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160621.1 | Last Updated 2 Sep 2013
Article Copyright 2013 by Szymon Roslowski
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid