Click here to Skip to main content
6,822,613 members and growing! (17,239 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate License: The Code Project Open License (CPOL)

A USB HID Component for C#

By wimar

A component to communicate with a USB HID device
C#2.0.NET2.0, WinXPVS2005, Dev
Posted:22 Mar 2007
Views:259,185
Bookmarked:95 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
29 votes for this article.
Popularity: 5.52 Rating: 3.77 out of 5
5 votes, 17.2%
1
3 votes, 10.3%
2
1 vote, 3.4%
3
4 votes, 13.8%
4
16 votes, 55.2%
5

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.

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)

About the Author

wimar


Member

Occupation: Web Developer
Location: Netherlands Netherlands

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (View All Threads)FirstPrevNext
GeneralFindSpecifiedDevice Problem PinmemberIwan Budihalim0:55 27 Aug '07  
Hi there, I need to get Specified Device to return the Product Name and vendor Name, but there's a problem when I try to Find the Specified Device :

This is the code I put after Device Arrived :
private void usb_OnDeviceArrived(object sender, EventArgs e)
{
this.lb_message.Items.Add("Found a Device");
HIDDevice hid1 = SpecifiedDevice.FindSpecifiedDevice(int.Parse("4D8", System.Globalization.NumberStyles.HexNumber), int.Parse("FF9D", System.Globalization.NumberStyles.HexNumber));
}

and it raised error :
Additional information: Msg:UsbLibrary.HIDDeviceException: Msg:Failed to get the detailed data from the hid. WinEr:00000000
at UsbLibrary.HIDDevice.Initialise(String strPath) in C:\Documents and Settings\wimar.GRAFIDRUK\My Documents\Avans School\BLOK 12\Project RF Sniffer\PC Applicatie\Sniffer\UsbLibrary\HIDDevice.cs:line 111
at UsbLibrary.HIDDevice.FindDevice(Int32 nVid, Int32 nPid, Type oType) in C:\Documents and Settings\wimar.GRAFIDRUK\My Documents\Avans School\BLOK 12\Project RF Sniffer\PC Applicatie\Sniffer\UsbLibrary\HIDDevice.cs:line 257

What's Wrong ?

GeneralRe: FindSpecifiedDevice Problem PinmemberIwan Budihalim2:53 27 Aug '07  
GeneralRe: FindSpecifiedDevice Problem Pinmembertaithien21:44 22 Jan '08  
GeneralRe: FindSpecifiedDevice Problem Pinmembersbeaubien8:41 18 Mar '08  
GeneralRe: FindSpecifiedDevice Problem PinmemberZ789512317:04 16 Jul '09  
GeneralRe: FindSpecifiedDevice Problem Pinmembertolgakayahan1:36 23 Aug '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 22 Mar 2007
Editor: Deeksha Shenoy
Copyright 2007 by wimar
Everything else Copyright © CodeProject, 1999-2010
Web18 | Advertise on the Code Project