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

General discussions about V-USB, our firmware-only implementation of a low speed USB device on Atmel's AVR microcontrollers
Post Reply
Rockna

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

Post by Rockna » Fri Aug 31, 2007 1:13 pm

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

roboknight

Using Libusb with C#

Post by roboknight » Tue Sep 11, 2007 5:01 pm

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.

gmon
Posts: 1
Joined: Thu Sep 30, 2010 8:16 am

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

Post by gmon » Thu Sep 30, 2010 8:23 am

Hi,
Could someone assist me how to access PowerSwitch from LibUSBDotNet?

Thanks in anticipation
gmon

Post Reply