Click here to Skip to main content
15,888,286 members
Articles / Programming Languages / C#
Article

A USB HID Component for C#

Rate me:
Please Sign up or sign in to vote.
4.12/5 (86 votes)
22 Mar 2007CPOL1 min read 2M   68.5K   240   339
A component to communicate with a USB HID device

Introduction

This article is about a USB HID component which enables you to communicate with HID devices over USB. There is no default component available for USB at this moment, and this component should provide you with a good starting point when writing your own USB HID enabled applications.

This article provides a sample application as well as the component itself.

Background

The component is based on various sources. A good start for USB in C# is this website. Also the book USB COMPLETE (third edition) by Jan Axelson is a must read for anyone wishing to incorporate USB HID into her/his application.

The component is developed during a project at the Avans Hogeschool in 's-Hertogenbosch, The Netherlands.

Using the Code

In the provided sample application, there is a good demonstration on how to include the component. Moreover, the use of the component is very well demonstrated. The only thing that must be done in your own application is implementing the events.

You'll also have to override the following functions in your form, so that your program is USB aware. In the property box, you'll have to provide a vendor and product id of your USB device in order to detect the correct device.

C#
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
usb.RegisterHandle(Handle);
}
protected override void WndProc(ref Messagea m)
{
usb.ParseMessages(ref m);
base.WndProc(ref m); // pass message on to base form
}

Points of Interest

A mouse is always in use by Windows, and cannot be captured by your own application. This also applies to HID devices in use by other applications.

History

  • 22nd March, 2007: First version, currently in development

Updates will be posted if there is a need for them.

License

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


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

Comments and Discussions

 
Questionhow to communicate with iphone Pin
Ali Habib14-Mar-11 23:27
Ali Habib14-Mar-11 23:27 
Questionhow to adapt the library for Windows CE 6.0? Pin
MyJok10-Feb-11 23:33
MyJok10-Feb-11 23:33 
GeneralMy vote of 5 Pin
anming20-Jan-11 20:08
anming20-Jan-11 20:08 
GeneralRe: My vote of 5 Pin
Enie300014-Oct-11 9:19
Enie300014-Oct-11 9:19 
QuestionComposite devices (multiple HID) Pin
João Martins18-Jan-11 6:05
João Martins18-Jan-11 6:05 
Generalnew circuit Pin
Myn9216-Nov-10 20:23
Myn9216-Nov-10 20:23 
GeneralMy vote of 5 Pin
Shahbaz Ali13-Oct-10 1:06
Shahbaz Ali13-Oct-10 1:06 
GeneralCheckDevicePresent how to... Pin
transparency26-Sep-10 16:48
transparency26-Sep-10 16:48 
Hi,

I was read the code and do a little modifications on that.

But I have a problem that is every time when I called CheckDevicePresent(), it seems cause the DataRecievedEventHandler called twice.

In other words to say,
If the code as follows::
...
usb.CheckDevicePresent();
usb.CheckDevicePresent();
usb.CheckDevicePresent();
...
then when every time data received, it will do OnDataReceived() three times....and so on..

so, how to prevent it happened?
GeneralRe: CheckDevicePresent how to... Pin
Paul Norström13-Jan-16 21:47
Paul Norström13-Jan-16 21:47 
GeneralMultiple instances of the same device Pin
Thomas Kjørnes7-Jul-10 11:25
Thomas Kjørnes7-Jul-10 11:25 
GeneralNeed to close application to connect find device again [modified] Pin
witekm30-Jun-10 19:12
witekm30-Jun-10 19:12 
GeneralThanks Pin
Member 66486821-Jun-10 4:40
Member 66486821-Jun-10 4:40 
QuestionUsing in seperate class ? Pin
borisdekat6-Jun-10 8:07
borisdekat6-Jun-10 8:07 
AnswerRe: Using in seperate class ? Pin
Erik Haverkamp25-Mar-11 23:51
Erik Haverkamp25-Mar-11 23:51 
GeneralWindows 7 64-bit Pin
SamNasr6-May-10 4:39
SamNasr6-May-10 4:39 
GeneralRe: Windows 7 64-bit Pin
dvrotsos3-Sep-10 9:22
dvrotsos3-Sep-10 9:22 
GeneralRe: Windows 7 64-bit Pin
SamNasr3-Sep-10 9:48
SamNasr3-Sep-10 9:48 
GeneralRe: Windows 7 64-bit Pin
dvrotsos3-Sep-10 12:02
dvrotsos3-Sep-10 12:02 
GeneralRe: Windows 7 64-bit Pin
PhantomHawk30-Sep-10 6:43
PhantomHawk30-Sep-10 6:43 
GeneralRe: Windows 7 64-bit Pin
mikah trent17-May-11 4:57
mikah trent17-May-11 4:57 
GeneralRe: Windows 7 64-bit Pin
Member 1003725110-Feb-15 12:23
Member 1003725110-Feb-15 12:23 
GeneralRe: Windows 7 64-bit Pin
melntess8-Jul-11 11:11
melntess8-Jul-11 11:11 
QuestionSolution: Losing Input Reports? Pin
faav4-May-10 10:24
faav4-May-10 10:24 
GeneralI don't understand how to use the example project Pin
12313s4-May-10 8:05
12313s4-May-10 8:05 
Questionthere are a new version of it? Pin
hilbi8830-Apr-10 23:22
hilbi8830-Apr-10 23:22 

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.