Click here to Skip to main content
15,902,492 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   241   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

 
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 
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 
Hi, I'm trying to use this library in a seperate class. Class should handle all communication from the main application to the USB via properties. I have some trouble to get the usb library events into this class. As this class (obviously) doesn't contain a form I can't drop the component on the form..Smile | :) I am a bit puzzled if and how I should handle below mentioned events (as they don't exists in a non-form class?)

protected override void OnHandleCreated(EventArgs e) and<br />
protected override void WndProc(ref Message m)


At the moment I have the below (simplified) skeleton in place, where I would like to get the behaviour that when the hardware isn't inserted the loop continues till the hardware is inserted. Unfortunately it doesn't work....I think I do something wrong with the eventhandlers....Frown | :-(

Any hints appreciated...tnx Boris.



Main form:

public partial class FrmMain : Form
    {
        measClass myMeas;
        
        public FrmMain()
        {
            InitializeComponent();
            myMeas = new measClass();
        }
        private void FrmMain_Load(object sender, EventArgs e)
        {
            while (!myMeas.portopen)
            {
                if (MessageBox.Show("USB hardware not found, check USB connection", "Hardware Error", MessageBoxButtons.AbortRetryIgnore,               MessageBoxIcon.Error) == DialogResult.Abort)
                {
                    Environment.Exit(0);
                }
            }
            // do rest of the things, main applications starts
        }
     }


MeasClass in seperate .cs file.

public class measClass
        {
            UsbHidPort myUsb;
            public bool portopen = false;

            public measClass()
            {
                myUsb = new UsbHidPort();
                myUsb.OnSpecifiedDeviceRemoved += new System.EventHandler(this.usb_OnSpecifiedDeviceRemoved);
                myUsb.OnDeviceArrived += new System.EventHandler(this.usb_OnDeviceArrived);
                myUsb.OnDeviceRemoved += new System.EventHandler(this.usb_OnDeviceRemoved);
                myUsb.OnDataRecieved += new UsbLibrary.DataRecievedEventHandler(this.usb_OnDataRecieved);
                myUsb.OnDataSend += new System.EventHandler(this.usb_OnDataSend);
                myUsb.OnSpecifiedDeviceArrived += new System.EventHandler(this.usb_OnSpecifiedDeviceArrived);
                myUsb.VendorId = 0x4242;
                myUsb.ProductId = 0x0002;
                myUsb.CheckDevicePresent();
            }

            private void usb_OnSpecifiedDeviceArrived(object sender, EventArgs e)
            {
                portopen = true;
            }

            // same way all the other usb_on handlers.
        }

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 
GeneralHappy but Perplexed Pin
gregmemphis20-Feb-10 21:15
gregmemphis20-Feb-10 21:15 
GeneralRe: Happy but Perplexed Pin
Hosenite19-Mar-10 9:53
Hosenite19-Mar-10 9:53 
GeneralRe: Happy but Perplexed [modified] Pin
aklein5315-Feb-11 11:48
aklein5315-Feb-11 11:48 
QuestionCapability Questions Pin
jschaenzle6-Feb-10 6:47
jschaenzle6-Feb-10 6:47 
GeneralMy vote of 1 Pin
Render_V1-Feb-10 19:42
Render_V1-Feb-10 19: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.