Click here to Skip to main content
Click here to Skip to main content

C# USB HID Interface

, 2 Sep 2013
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)

About the Author

Szymon Roslowski
Software Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
QuestionNo HID-Device Connect PinmemberIngmar Andreas Berner21-Jul-14 23:01 
Question"window handle already exists": Your HIDTest + Microchip HID implementation Pinmemberyyyy119-Jul-14 1:32 
AnswerRe: "window handle already exists": Your HIDTest + Microchip HID implementation PinmemberSzymon Roslowski19-Jul-14 2:19 
BugProblem with connection. PinmemberMember 96736372-Jun-14 2:41 
GeneralRe: Problem with connection. PinmemberSzymon Roslowski19-Jul-14 2:22 
SuggestionUsb Hid Interface Lite - for Szymon Pinmemberjakubbukaj30-May-14 21:14 
QuestionMagic 32 bytes removed on getting name of device in IsNotificationForTargetDevice method Pinmemberjakubbukaj29-May-14 4:33 
QuestionWorks Great for Windows 8.1 but Windows 7... :( PinmemberD Petticord23-May-14 17:33 
AnswerRe: Works Great for Windows 8.1 but Windows 7... :( PinmemberSzymon Roslowski24-May-14 2:02 
GeneralRe: Works Great for Windows 8.1 but Windows 7... :( [modified] PinmemberD Petticord25-May-14 5:47 
GeneralRe: PROBLEM SOLVED! Suggested Bug Fix PinmemberD Petticord25-May-14 8:06 
GeneralRe: PROBLEM SOLVED! Suggested Bug Fix PinmemberSzymon Roslowski25-May-14 22:27 
QuestionTransfering data on endpoint 0 and on an OUT endpoint PinmemberCorey Wilner7-Apr-14 13:33 
AnswerRe: Transfering data on endpoint 0 and on an OUT endpoint PinmemberSzymon Roslowski7-Apr-14 22:59 
GeneralRe: Transfering data on endpoint 0 and on an OUT endpoint PinmemberCorey Wilner8-Apr-14 7:30 
GeneralRe: Transfering data on endpoint 0 and on an OUT endpoint PinmemberSzymon Roslowski8-Apr-14 23:10 
QuestionTransmit Data USB PinmemberMember 1052099017-Mar-14 18:21 
AnswerRe: Transmit Data USB PinmemberMember 1039416618-Mar-14 9:25 
GeneralRe: Transmit Data USB PinmemberMember 1052099018-Mar-14 20:15 
GeneralRe: Transmit Data USB PinmemberMember 1052099021-Mar-14 1:52 
GeneralRe: Transmit Data USB PinmemberSzymon Roslowski25-Mar-14 0:14 
GeneralRe: Transmit Data USB PinmemberMember 1052099025-Mar-14 4:18 
GeneralRe: Transmit Data USB PinmemberSzymon Roslowski25-Mar-14 5:16 
GeneralRe: Transmit Data USB PinmemberMember 1052099025-Mar-14 15:50 
GeneralRe: Transmit Data USB PinmemberMember 1052099027-Mar-14 19:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.8.140721.1 | Last Updated 2 Sep 2013
Article Copyright 2013 by Szymon Roslowski
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid