SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

How to communicate with an usb device when I have identified it?

Last post 06-12-2007 1:42 PM by Muhammad Kamran. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 08-23-2006 12:00 PM

    • bisao
    • Not Ranked
    • Joined on 08-22-2006
    • Posts 2

    How to communicate with an usb device when I have identified it?

    Hi,I'm studing USBLIB for communciate with a device connected to my PC.

    When I found a device who I want to talk,what will I do?

    I use device.BulkRead(0, msg) for read data from the endpoint. "0" is correct?

    For example I have connected my USB Flash disk to the pc and I found it with the follow code line...

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

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace ConsoleApplicationUSB
    {
        class Program
        {
            static void Main(string[] args)
            {
               
               
                byte[] msg = new byte[8];
               
                foreach (ICSharpCode.USBlib.Bus bus in ICSharpCode.USBlib.Bus.Busses)
                {
                    Console.WriteLine(bus);
                    foreach (ICSharpCode.USBlib.Descriptor descriptor in bus.Descriptors)
                    {       
                        try
                        {     

                         if (descriptor.VendorId == 0xEA0 && descriptor.ProductId == 0x2168)   //identifier or my USB flash disk
                            {                           
                                ICSharpCode.USBlib.Device device = descriptor.OpenDevice();
                                Console.WriteLine("\t\t     Product: " + device.Product);
                                Console.WriteLine("\t\tManufacturer: " + device.Manufacturer);
                                Console.WriteLine();
                              
                                while (true)
                                {   device.SendControlMessage                               
                                    device.BulkRead(0, msg);
                                                           
                                    for (int i = 0; i < msg.Length; i++)
                                        Console.Write(Convert.ToString(msg[i]));

                                    Console.WriteLine();
                                    System.Threading.Thread.Sleep(500);
                                   
                                }
                             
                            }

                            else
                               Console.WriteLine("Not found");
                        }
                        catch (ICSharpCode.USBlib.UsbException e)
                        {
                            Console.WriteLine("Got Exception : " + e);
                        }
                    }
                }
               
            }
        }
      
    }
     

    For testing the application I have transfer some data from the USB flash disk with the HDD of my pc, and at the same time I run my application that it read forever from the endpoint 0 and it prints the values read.

    But the result of the bulkread is always "0"

    Someone can tell me how to resolve my problem?

    Thanks.

    Nicola

     

    Filed under:
  • 06-07-2007 12:52 PM In reply to

    Re: How to communicate with an usb device when I have identified it?

    Dear I got the same problem and have no way infact quit stumped here so plz if you got the answer then plz reply it to me. I will be thankful to you.

     

    Filed under:
  • 06-08-2007 7:58 PM In reply to

    Re: How to communicate with an usb device when I have identified it?

    I'll be completely honest, with a recent usb app I wrote, I started out using SharpUsbLib, and even though it was a nice API, I never once got the BulkRead to work.  I tried for over two weeks to read data back from my usb device to no avail, although I could write to it using SendControlMsg. 

    I swapped over to LibUsbDotNet, and within a day or two had reading and writing working just fine, and its based off of libusb-win32 just like SharpUsbLib.  I highly recommend it, its much more thorough, just as easy to use, and I've personally communicated with the author of the library when I had an issue.  SharpUsbLib is no longer being developed or supported.   

     If its a non-HID compliant device, I recommend LibUsbDotNet, or if it is a HID compliant device, there is a HID component available here.  I have used both of these and successfully used them to both read and write to my device.

     

  • 06-12-2007 1:42 PM In reply to

    Re: How to communicate with an usb device when I have identified it?

    Please if you can send me the code through which you got success to read and write data from usb. I will be very much thankful to you.
Page 1 of 1 (4 items)
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.