Page 1 of 1

How to use Libusb in visual studio 2005 (In C#)

Posted: Fri Aug 31, 2007 1:13 pm
by Rockna
I'm beginner for in C# Visual studio
I want to use Libusb in C# project but i don't know How to include it
Please Help me about some example for define it in code project

Using Libusb with C#

Posted: Tue Sep 11, 2007 5:01 pm
by roboknight
You'll have better luck with LibUsbDotNet ...

You can do things like this:

Code: Select all

using LibUsbDotNet;

namespace App
{
     public partial class Form1 : Form
     {
            CLibUsb usb;
           
             < your code/functions here>

             private void btnChkUSB_Click(object sender, EventArgs e)
             {
                 usb.RefreshDevices(true);
                 for (int i = 0; i < usb.Devices.Count; i++)
                 {
                    InfoDevice = Device = usb.Devices[i];
                    textBox1.AppendText(String.Format("\nVendor:{0:X4} Product:{1:X4}\n", Device.IdVendor, Device.IdProduct);
                 }
             }
            private void Form1_Load(object sender, EventArgs e)
            {
                usb = new CLibUsb();
            }
     }
}



You'll have to look at LibUsbDotNet itself to know which functions you need to call when, but that will get it included. You will also have to add a reference to the LibUsbDotNet.dll (Project/Add Reference)... but it should get you started. I've also created a new CLibUsb object on startup. You can do that anywhere, but doing it on a form load is convenient.

Just make sure, before you send any data over an endpoint that you CLAIM the interface you want to use. If you don't, then you won't be able to write anything to it. You don't necessarily have to claim the interface for control messages, but you will have to for sending and receiving data over an endpoint. Also, if the interface is claimed by a driver other than Libusb, then you'll get hosed. So make sure there isn't another driver claiming the interface.

The function above was associated with a button click, and is probably not where you really want your USB code, but it shows a quick way to go through the USB devices and find the one you are interested in (via vendor and product id). I also had a text box where I just wanted to dump some junk output. There are better ways to do that, but you get the idea.

Since you are a beginner, you'll probably have a lot of reading to do as this is by no means a complete example. But it should give you a place to look.

Re: How to use Libusb in visual studio 2005 (In C#)

Posted: Thu Sep 30, 2010 8:23 am
by gmon
Hi,
Could someone assist me how to access PowerSwitch from LibUSBDotNet?

Thanks in anticipation
gmon