Click here to Skip to main content
15,893,588 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.6K   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

 
QuestionHelp with the event implementation Pin
Enie300015-Oct-11 3:14
Enie300015-Oct-11 3:14 
QuestionMissing Byte? Pin
PGP_Protector14-Oct-11 10:37
PGP_Protector14-Oct-11 10:37 
QuestionWindows CE Pin
Fiendlive21-Sep-11 12:08
Fiendlive21-Sep-11 12:08 
QuestionThat't cool Pin
wavelet120813-Sep-11 20:56
wavelet120813-Sep-11 20:56 
QuestionObject reference not set to an object. Pin
Jamilce11-Sep-11 17:18
Jamilce11-Sep-11 17:18 
QuestionDoes with this work with Visual Studio 2010 Pin
Member 806060831-Aug-11 11:46
Member 806060831-Aug-11 11:46 
AnswerRe: Does with this work with Visual Studio 2010 Pin
GS-Scooter1-Sep-11 4:54
GS-Scooter1-Sep-11 4:54 
GeneralRe: Does with this work with Visual Studio 2010 - (Don't send all zeros in record) Pin
Member 80606081-Sep-11 5:43
Member 80606081-Sep-11 5:43 
I want to express my appreciation to GS-Scooter for his response.

He diagnosed the problem exactly. The sniffer hid app has a send button with a bunch of default 0 bytes. I was just hitting the button and sending all zeros to the hid device.

I'm using one of the new MSP430550X USB devices from TI. The TI sample app was written in C++ and works fine, so I incorrectly assumed that it was some sort of .NET or C# problem, since I'm new to C#. After getting the above response, I looked at the TI demo amp and realized that it was setting a record ID= 63, the length of data, and then the data itself in the output report. I copied this data into the output report of the sniffer hid app, and it worked beautifully!!! I even got the expected response.

My bad assumption was that the device would just be sent all zeros, and windows wouldn't know the difference. It never occurred to me that the function will throw an exception based on data in the report given that it's just a bunch of bytes. Any other time, I've written to a file or device, the content of the data itself is not critical to whether the stream operation fails or not.

I guess with USB, it needs at minimum the first byte of the report to be a valid report number, otherwise the USB device rejects the data, and an a vague exception bubbles up the chain, and you end up waisting 3 days of time trying to figure out what is wrong.

I'm not sure if I'm the only ignorant person who uses this sample app, but it would be nice to comment in the code somewhere, or even have a message box warning pop up if all the data is zero, to warn people that they must send a valid record, or an exception will occur.

Anyhow - thanks again.
GeneralRe: Does with this work with Visual Studio 2010 - (Don't send all zeros in record) Pin
GS-Scooter1-Sep-11 5:55
GS-Scooter1-Sep-11 5:55 
QuestionVendor Name / Product Name Pin
uniqo814-Aug-11 20:43
uniqo814-Aug-11 20:43 
AnswerRe: Vendor Name / Product Name Pin
uniqo818-Aug-11 8:20
uniqo818-Aug-11 8:20 
GeneralRe: Vendor Name / Product Name Pin
ivanovici mihai5-Nov-11 0:17
ivanovici mihai5-Nov-11 0:17 
QuestionCreateFile returns -1 (Failed to create device file WinEr:00000005) Pin
blackbondi30-Jul-11 2:00
blackbondi30-Jul-11 2:00 
AnswerRe: CreateFile returns -1 (Failed to create device file WinEr:00000005) Pin
yosmany2-Sep-11 5:58
yosmany2-Sep-11 5:58 
AnswerRe: CreateFile returns -1 (Failed to create device file WinEr:00000005) Pin
Jason Stern1-May-12 10:46
Jason Stern1-May-12 10:46 
QuestionUsbHidPort1.SpecifiedDevice.Dispose() Pin
Tony Palumbo10-Jul-11 14:24
Tony Palumbo10-Jul-11 14:24 
QuestionI'm wanting to communicate with my Tripp Lite UPS via USB - but it's already opened. Pin
myklk10-Jun-11 8:10
myklk10-Jun-11 8:10 
QuestionRe: I'm wanting to communicate with my Tripp Lite UPS via USB - but it's already opened. Pin
mhandzlik7-Nov-11 5:26
mhandzlik7-Nov-11 5:26 
AnswerRe: I'm wanting to communicate with my Tripp Lite UPS via USB - but it's already opened. Pin
Jason Stern1-May-12 10:50
Jason Stern1-May-12 10:50 
Questionit will always return null and it won't select any device... Pin
MEhran.NET17-May-11 0:31
MEhran.NET17-May-11 0:31 
AnswerRe: it will always return null and it won't select any device... Pin
mikah trent17-May-11 5:06
mikah trent17-May-11 5:06 
GeneralRe: it will always return null and it won't select any device... [modified] Pin
MEhran.NET18-May-11 0:12
MEhran.NET18-May-11 0:12 
GeneralRe: it will always return null and it won't select any device... Pin
mikah trent18-May-11 3:16
mikah trent18-May-11 3:16 
GeneralRe: it will always return null and it won't select any device... [modified] Pin
mtmonte8-Dec-11 5:45
mtmonte8-Dec-11 5:45 
GeneralModified UsbLibrary to support multiple USB devices of the same kind Pin
dr34m26-Apr-11 11:42
dr34m26-Apr-11 11:42 

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.