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

 
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 
GeneralRe: Help 'hijacking' USB HID input Pin
Pete BSC6-Mar-08 11:30
Pete BSC6-Mar-08 11:30 
Generalthank you!!! Pin
Gil.Schmidt25-Jan-08 23:38
Gil.Schmidt25-Jan-08 23:38 
QuestionSending report to USB Device Pin
Member 32434728-Jan-08 23:55
Member 32434728-Jan-08 23:55 
AnswerRe: Sending report to USB Device Pin
MDeepa3-Jul-09 1:32
MDeepa3-Jul-09 1:32 
Hi You have to write another method to handle the reports.

Steps For Handling Reports:

1) In win32usb.cs, add
HidD_SetOutputReport() for handling output reports and HidD_GetInputReport() for handling input reports.
-------------------------------------------------------------------------------------------
/// Send an output report to a HID device.
/// </summary>
/// <param name="HidDeviceObject">A handle to a Hid Device Object.</param>
/// <param name="ReportBuffer">The buffer of the output report to send to the device.</param>
/// <param name="ReportBufferLength">The size (in bytes) of ReportBuffer.</param>
/// <returns>'true' if successful.'false' otherwise.</returns>
[DllImport("hid.dll", SetLastError = true)] protected static extern bool HidD_SetOutputReport(IntPtr HidDeviceObject, byte[] ReportBuffer, int ReportBufferLength);
/// <summary>
/// Retrieve an input report from a HID device.
/// <param name=" HidDeviceObject">A handle to a Hid Device Object.</param>
/// <param name="ReportBuffer">The buffer that the input report should be placed into.The first byte of the buffer should be set to the report ID of the desired report.</param>
/// <param name="ReportBufferLength">The size (in bytes) of ReportBuffer. This value should be greater than or equal to the InputReportByteLength field as specified in the HIDP_CAPS structure for the device.</Param>
///<returns>'true' if successful.'false' otherwise. </returns>
[DllImport("hid.dll", SetLastError = true)] protected static extern bool HidD_GetInputReport(IntPtr HidDeviceObject, byte[] ReportBuffer, int ReportBufferLength);
-------------------------------------------------------------------------------------------
2) Write methods sendReport() and getReport() in HIDDevice.cs

protected void sendReport(OutputReport oOutRep)
{
try
{
// Call SetOutputReport to send this report buffer over the control pipe
HidD_SetOutputReport(m_hHandle, oOutRep.Buffer, oOutRep.BufferLength);

}
catch (Exception exx)
{
Console.WriteLine(exx.ToString());

}
}

protected void getReport(byte[] data,byte buffersize)
{
try
{

//Call GetInputReport to get the requested report buffer over the control pipe
HidD_GetInputReport(m_hHandle, data, buffersize);
}
catch (Exception exx)
{
Console.WriteLine(exx.ToString());
}
}


-------------------------------------------------------------------------------------------
3) In SpecifiedDevice.cs,
In class, public class SpecifiedDevice : HIDDevice write set_report() and get_report()

//set the request to send the report buffer over the control pipe.
public void set_report(byte[] data, byte buffersize)
{
SpecifiedOutputReport oRep = new SpecifiedOutputReport(this);
oRep.set_report(data, buffersize);
try
{
sendReport(oRep);

}
catch (HIDDeviceException ex)
{

}
}
//Receive the requested report buffer over the control pipe.
public void get_report(byte[] data,byte buffersize)
{
try
{
getReport(data,buffersize);

}
catch (HIDDeviceException ex)
{
}
}


-------------------------------------------------------------------------------------------

4) In SpecifiedOutputReport.cs,

public bool set_report(byte[] data, byte buffersize)
{
byte[] arrbuff = Buffer;
for (int i = 0; i < buffersize; i++)
{
arrbuff[i] = data[i];
}
if (arrbuff.Length < buffersize)
{
return false;
}
else
{
return true;
}
}

-------------------------------------------------------------------------------------------

5) In SpecifiedInputReport.cs,

public void get_report(byte[] inputbuffer)
{
inputbuffer = Buffer;
}

-------------------------------------------------------------------------------------------

Thank you,
Smile | :)
GeneralRe: Sending report to USB Device Pin
quad2618-Nov-10 20:15
quad2618-Nov-10 20:15 
QuestionHID devce not detected Pin
Samir Karve20-Nov-07 22:34
Samir Karve20-Nov-07 22:34 
AnswerRe: HID devce not detected Pin
Z789512316-Jul-09 15:51
Z789512316-Jul-09 15:51 
GeneralSending Data with Sniffer Pin
doc6013-Nov-07 12:41
doc6013-Nov-07 12:41 
QuestionDoes not work on 64 bit Vista? Pin
George Helyar5-Nov-07 14:32
George Helyar5-Nov-07 14:32 
AnswerRe: Does not work on 64 bit Vista? Pin
George Helyar7-Nov-07 3:49
George Helyar7-Nov-07 3:49 
GeneralRe: Problems with 64-bit Windows Pin
RenniePet28-Apr-08 4:36
RenniePet28-Apr-08 4:36 
GeneralRe: Problems with 64-bit Windows Pin
George Helyar28-Apr-08 8:46
George Helyar28-Apr-08 8:46 
GeneralRe: Problems with 64-bit Windows Pin
RenniePet28-Apr-08 9:32
RenniePet28-Apr-08 9:32 
QuestionRe: Problems with 64-bit Windows Pin
mellinge14-Feb-09 3:53
mellinge14-Feb-09 3:53 
AnswerRe: Does not work on 64 bit Vista? Pin
sophtware25-Jul-08 17:10
sophtware25-Jul-08 17:10 
GeneralRe: Does not work on 64 bit Vista? [modified] Pin
George Helyar26-Jul-08 0:32
George Helyar26-Jul-08 0:32 
GeneralRe: Does not work on 64 bit Vista? Pin
sophtware26-Jul-08 8:45
sophtware26-Jul-08 8:45 
GeneralRe: Does not work on 64 bit Vista? Pin
George Helyar26-Jul-08 10:04
George Helyar26-Jul-08 10:04 
GeneralRe: Does not work on 64 bit Vista? Pin
sophtware26-Jul-08 14:26
sophtware26-Jul-08 14:26 
GeneralRe: Does not work on 64 bit Vista? [modified] Pin
George Helyar26-Jul-08 22:23
George Helyar26-Jul-08 22:23 
RantRe: Does not work on 64 bit Vista? Pin
sophtware27-Jul-08 2:52
sophtware27-Jul-08 2:52 

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.