Example #1
0
        private static USBDeviceDescriptor GetDeviceDescriptor(string devicePath)
        {
            try
            {
                USBDeviceDescriptor descriptor;
                using (API.WinUSBDevice wuDevice = new API.WinUSBDevice())
                {
                    wuDevice.OpenDevice(devicePath);
                    API.USB_DEVICE_DESCRIPTOR deviceDesc = wuDevice.GetDeviceDescriptor();
                    // string q = wuDevice.GetStringDescriptor(0);
                    // TODO: use language id properly
                    string manufacturer = null, product = null, serialNumber = null;
                    byte idx = 0;
                    idx = deviceDesc.iManufacturer;
                    if (idx > 0)
                        manufacturer = wuDevice.GetStringDescriptor(idx);

                    idx = deviceDesc.iProduct;
                    if (idx > 0)
                        product = wuDevice.GetStringDescriptor(idx);

                    idx = deviceDesc.iSerialNumber;
                    if (idx > 0)
                        serialNumber = wuDevice.GetStringDescriptor(idx);
                    descriptor = new USBDeviceDescriptor(devicePath, deviceDesc, manufacturer, product, serialNumber);
                }
                return descriptor;

            }
            catch (API.APIException e)
            {
                throw new USBException("Failed to retrieve device descriptor.", e);
            }
        }
Example #2
0
        private static USBDeviceDescriptor GetDeviceDescriptor(string devicePath)
        {
            try
            {
                USBDeviceDescriptor descriptor;
                using (API.WinUSBDevice wuDevice = new API.WinUSBDevice())
                {
                    wuDevice.OpenDevice(devicePath);
                    API.USB_DEVICE_DESCRIPTOR deviceDesc = wuDevice.GetDeviceDescriptor();

                    // Patch from https://delog.wordpress.com/2013/06/19/winusbnet-patch-to-handle-language-id/
                    string q = wuDevice.GetStringDescriptor(0, 0);
                    if (q.Length == 0)
                        throw new USBException("Failed to get USB string descriptor (0).");
                    // TODO: Using the first language id. Need to improve API.
                    ushort langID = q[0];

                    string manufacturer = null, product = null, serialNumber = null;
                    byte idx = 0;
                    idx = deviceDesc.iManufacturer;
                    if (idx > 0)
                        manufacturer = wuDevice.GetStringDescriptor(idx, langID);

                    idx = deviceDesc.iProduct;
                    if (idx > 0)
                        product = wuDevice.GetStringDescriptor(idx, langID);

                    /*idx = deviceDesc.iSerialNumber;
                    if (idx > 0)
                        serialNumber = wuDevice.GetStringDescriptor(idx, langID);*/

                    descriptor = new USBDeviceDescriptor(devicePath, deviceDesc, manufacturer, product, serialNumber);
                }
                return descriptor;

            }
            catch (API.APIException e)
            {
                throw new USBException("Failed to retrieve device descriptor.", e);
            }
        }
Example #3
0
 /// <summary>
 /// Constructs a new USB device
 /// </summary>
 /// <param name="devicePathName">Device path name of the USB device to create</param>
 public USBDevice(string devicePathName)
 {
     Descriptor = GetDeviceDescriptor(devicePathName);
     _wuDevice = new API.WinUSBDevice();
     try
     {
         _wuDevice.OpenDevice(devicePathName);
         InitializeInterfaces();
     }
     catch (API.APIException e)
     {
         _wuDevice.Dispose();
         throw new USBException("Failed to open device.", e);
     }
 }