none
USB and C#

    Question

  • Are there any libraries or classes that allow C# applications to talk to devices on a the USB (Universal Serial Bus)?
    Friday, October 14, 2005 12:34 AM

Answers

All replies

  • I don't think theres any specific namespaces in the framework. You might want to take a look at this article which deals with working with USB devices and has some good pointers.

    Cathal
    Friday, October 14, 2005 2:02 AM
    Moderator
  • this code defintly help u...


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using UsbLibrary;

    namespace UsbApp
    {
        public partial class Sniffer : Form
        {
            public Sniffer()
            {
                InitializeComponent();
            }

            private void usb_OnDeviceArrived(object sender, EventArgs e)
            {
                this.lb_message.Items.Add("Found a Device");
            }

            private void usb_OnDeviceRemoved(object sender, EventArgs e)
            {
                if (InvokeRequired)
                {
                    Invoke(new EventHandler(usb_OnDeviceRemoved), new object[] { sender, e });
                }
                else
                {
                    this.lb_message.Items.Add("Device was removed");
                }
            }

            private void usb_OnSpecifiedDeviceArrived(object sender, EventArgs e)
            {
                this.lb_message.Items.Add("My device was found");

                //setting string form for sending data
                string text = "";
                for (int i = 0; i < this.usb.SpecifiedDevice.OutputReportLength - 1; i++)
                {
                    text += "000 ";
                }
                this.tb_send.Text = text;
            }

            protected override void OnHandleCreated(EventArgs e)
            {
                base.OnHandleCreated(e);
                usb.RegisterHandle(Handle);
            }

            protected override void WndProc(ref Message m)
            {
                usb.ParseMessages(ref m);
                base.WndProc(ref m);    // pass message on to base form
            }

            private void btn_ok_Click(object sender, EventArgs e)
            {
                try
                {
                    this.usb.ProductId = Int32.Parse(this.tb_product.Text, System.Globalization.NumberStyles.HexNumber);
                    this.usb.VendorId = Int32.Parse(this.tb_vendor.Text, System.Globalization.NumberStyles.HexNumber);
                    this.usb.CheckDevicePresent();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

            private void btn_send_Click(object sender, EventArgs e)
            {
                try
                {
                    string text = this.tb_send.Text + " ";
                    text.Trim();
                    string[] arrText = text.Split(' ');
                    byte[] data = new byte[arrText.Length];
                    for (int i = 0; i < arrText.Length; i++)
                    {
                        if (arrText[i] != "")
                        {
                            int value = Int32.Parse(arrText[i], System.Globalization.NumberStyles.Number);
                            data[i] = (byte)Convert.ToByte(value);
                        }
                    }

                    if (this.usb.SpecifiedDevice != null)
                    {
                        this.usb.SpecifiedDevice.SendData(data);
                    }
                    else
                    {
                        MessageBox.Show("Sorry but your device is not present. Plug it in!! ");
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

            private void usb_OnSpecifiedDeviceRemoved(object sender, EventArgs e)
            {
                if (InvokeRequired)
                {
                    Invoke(new EventHandler(usb_OnSpecifiedDeviceRemoved), new object[] { sender, e });
                }
                else
                {
                    this.lb_message.Items.Add("My device was removed");
                }
            }

            private void usb_OnDataRecieved(object sender, DataRecievedEventArgs args)
            {
                if (InvokeRequired)
                {
                    try
                    {
                        Invoke(new DataRecievedEventHandler(usb_OnDataRecieved), new object[] { sender, args });
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                }
                else
                {

                    string rec_data = "Data: ";
                    foreach (byte myData in args.data)
                    {
                        if (myData.ToString().Length == 1)
                        {
                            rec_data += "00";
                        }

                        if (myData.ToString().Length == 2)
                        {
                            rec_data += "0";
                        }

                        rec_data += myData.ToString() + " ";
                    }

                    this.lb_read.Items.Insert(0, rec_data);
                }
            }

            private void usb_OnDataSend(object sender, EventArgs e)
            {
                this.lb_message.Items.Add("Some data was send");
            }  
        }
    }
    • Proposed as answer by sahil goyal Friday, December 18, 2009 12:18 PM
    Friday, December 18, 2009 8:22 AM
  • Hi Sahil,

    Thankyou for posting the code. How about the usbLibrary that was used on the code? Is that handling the USB connectivity - how was implemented? I appricate if you could discuss little more about that. Thank you.
    ZZZ
    Tuesday, February 16, 2010 4:40 PM
  • any library for usb in .net ????? which library have been used by sahil???? i feel lack of discussion....i want to communicate with usb devices.....Thanksss in advance with ur help that u u would put forward now....
    Friday, March 26, 2010 6:44 AM
  • Hi !

    I do not know about the program here above, but you could be interested in this library:

    http://www.icsharpcode.net/opensource/sharpusblib/

    Regards,

    scamb

    Wednesday, April 28, 2010 10:47 AM
  • Hi there,

    The code in that Sniffer app was a big help to me but I've found a limitation in it and the usblibrary that it's using - it is not capable of picking up a usb device that does not have suitable drivers on the PC.

    I'm looking to make an application that will work as a kind of auditor/monitor of anything that is plugged into a PC (for security) but I can't find anything that will let me identify a USB device with no drivers. I believe that this is because such a device does not get associated with any Device class. Does anyone know of a way to handle such cases? It would be massively appreciated!

    Tuesday, May 04, 2010 1:50 PM
  • I'll be coming with a great usb help(c#) soon as i am into it deeply now...Guys have to wait till I succeeed....Thankss
    Wednesday, May 05, 2010 4:31 AM
  • All usb devices must have a driver associated with it. This is because you are connecting to microprocessor, and you need 'someone' (or in this case something) that 'talks' the same language of the microprocessor. That's what a driver does, it translates the signals send by the microprocessor.

     

    And more important than that, you need to know when a specific microprocessor was connected.

    The problem you reported was to do with the way the operation system handles usb devices.

    Sunday, April 03, 2011 6:19 PM
  • Please, visit this website: 

    http://www.USBHidNetClass.org 

    Interface WindForms USB Control for Visual c# and Visual BASIC and Microcontroller

    Best Regards

    simplicio

    Tuesday, May 03, 2011 9:41 PM
  • now, USB was used very popular, why do Microsoft not yet support some class to working with USB in net framework???
    Thursday, May 19, 2011 4:18 PM
  • Hello.

    Do you could make it work on windows 64 bits?

    Thank you very much,

    Marcos

    Wednesday, December 19, 2012 3:45 PM
  • Yeah I know... its pretty dumb and how sad that they don't make framework for USB... <sigh>

    Ron Boucher

    Thursday, July 11, 2013 2:56 PM