Click here to Skip to main content
15,888,293 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

 
GeneralRe: Can't get it to work Pin
Bill Ewing7-Nov-08 3:32
Bill Ewing7-Nov-08 3:32 
Questionnot recognizing my usb product id and vendor id Pin
Member 37226873-Sep-08 6:17
Member 37226873-Sep-08 6:17 
AnswerRe: not recognizing my usb product id and vendor id Pin
Trhacz3-Oct-08 11:01
Trhacz3-Oct-08 11:01 
GeneralThis code recognized my device immediately Pin
Bill Ewing7-Nov-08 3:28
Bill Ewing7-Nov-08 3:28 
GeneralRe: not recognizing my usb product id and vendor id Pin
yesman14-Jul-09 20:18
yesman14-Jul-09 20:18 
AnswerRe: not recognizing my usb product id and vendor id Pin
sarunasg26-Nov-09 22:04
sarunasg26-Nov-09 22:04 
QuestionUSB-At89c5131 code Pin
Nycil29-Jul-08 21:06
Nycil29-Jul-08 21:06 
GeneralHelp on USB Barcode Scanner using as HID Device Pin
Kinuis2-Jul-08 2:55
Kinuis2-Jul-08 2:55 
I see you have used the sample for a special Barcode Scanner.

I'm trying to get data which I scan with the USB barcode scanner 'Datalogic Heron G'.

Following error occurs during the Inialize Method of the HDIDevice Class:

UsbLibrary.HIDDeviceException: Msg:UsbLibrary.HIDDeviceException: Msg:Failed to create device file WinEr:00000005
at UsbLibrary.HIDDevice.Initialise(String strPath) in HIDDevice.cs:line 136
at UsbLibrary.HIDDevice.FindDevice(Int32 nVid, Int32 nPid, Type oType) in HIDDevice.cs:line 287
at UsbLibrary.HIDDevice.FindDevice(Int32 nVid, Int32 nPid, Type oType) in HIDDevice.cs:line 295
at UsbLibrary.SpecifiedDevice.FindSpecifiedDevice(Int32 vendor_id, Int32 product_id) in SpecifiedDevice.cs:line 42
at UsbLibrary.UsbHidPort.CheckDevicePresent() in UsbHidPort.cs:line 218

The Windows Error Code 5 is 'Access Denied' during Method CreateFile.

I tried your sample application on Windows XP (32-bit) and Windows Vista (64-bit)
with bug fixing of DeviceInterfaceDetailData size problem 5 or 8, doesn't help.

What I found strange was the DevicePath with the value
devicePath = "\\\\?\\hid#vid_080c&pid_0300#7&6f2335e&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}"


Can you give me a hint where could be the problem?
GeneralRe: Help on USB Barcode Scanner using as HID Device Pin
Member 331961127-Aug-08 8:42
Member 331961127-Aug-08 8:42 
GeneralRe: Help on USB Barcode Scanner using as HID Device Pin
kevinjheng28-Aug-08 19:45
kevinjheng28-Aug-08 19:45 
GeneralRe: Help on USB Barcode Scanner using as HID Device Pin
PubLee14-Sep-08 3:29
PubLee14-Sep-08 3:29 
QuestionPausing Read From USB Pin
Noman Aftab30-Jun-08 2:49
Noman Aftab30-Jun-08 2:49 
AnswerRe: Pausing Read From USB Pin
sophtware25-Jul-08 16:49
sophtware25-Jul-08 16:49 
Questionhid lib and visual c++ ... howto? Pin
Sandro Cavazzoni5-Jun-08 0:20
Sandro Cavazzoni5-Jun-08 0:20 
AnswerRe: hid lib and visual c++ ... howto? Pin
Bill Ewing7-Nov-08 3:24
Bill Ewing7-Nov-08 3:24 
AnswerRe: hid lib and visual c++ ... howto? Pin
yesman27-Aug-09 4:42
yesman27-Aug-09 4:42 
AnswerThanks for the Great Article Pin
Tradon-Dev30-May-08 6:07
Tradon-Dev30-May-08 6:07 
QuestionHow to unregister HID device Pin
abetrader15-Apr-08 6:40
abetrader15-Apr-08 6:40 
AnswerRe: How to unregister HID device Pin
nhustak17-Apr-09 4:54
nhustak17-Apr-09 4:54 
GeneralRe: How to unregister HID device Pin
ScruffR22-Jun-09 3:33
ScruffR22-Jun-09 3:33 
GeneralRe: How to unregister HID device [modified] Pin
MDeepa3-Jul-09 0:10
MDeepa3-Jul-09 0:10 
GeneralAdding to this component. Pin
Bakaneko9-Apr-08 0:39
Bakaneko9-Apr-08 0:39 
GeneralFor people having problems with sending [modified] Pin
Bakaneko8-Apr-08 12:09
Bakaneko8-Apr-08 12:09 
QuestionWPF ? Pin
Loic Berthollet1-Apr-08 6:12
Loic Berthollet1-Apr-08 6:12 
GeneralHelp 'hijacking' USB HID input Pin
Danny__T6-Feb-08 5:09
Danny__T6-Feb-08 5:09 

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.