Ejemplo n.º 1
0
        private static void FillEndpointInfo(TreeNode tvEndpoints, UsbEndpointInfo i)
        {
            if (i.Descriptor != null)
            {
                TreeNode tvEpDesc = tvEndpoints.Nodes.Add(string.Format("Endpoint (ID: 0x{0:X4}) Descriptor [Length: {1}]", i.Descriptor.EndpointID, i.Descriptor.Length));
                tvEpDesc.Nodes.Add(string.Format("{0}: {1} (0x{2:X2})", "DescriptorType", i.Descriptor.DescriptorType, (int)i.Descriptor.DescriptorType));
                tvEpDesc.Nodes.Add(string.Format("{0}: {1}", "Attributes", i.Descriptor.Attributes));
                tvEpDesc.Nodes.Add(string.Format("{0}: {1}", "MaxPacketSize", i.Descriptor.MaxPacketSize));
                tvEpDesc.Nodes.Add(string.Format("{0}: {1}", "Interval", i.Descriptor.Interval));
                tvEpDesc.Nodes.Add(string.Format("{0}: {1}", "Refresh", i.Descriptor.Refresh));
                tvEpDesc.Nodes.Add(string.Format("{0}: 0x{1:X2}", "SynchAddress", i.Descriptor.SynchAddress));
            }

            if (i.CustomDescriptors != null)
            {
                TreeNode tvEpDesc = tvEndpoints.Nodes.Add(string.Format("EP Custom Descriptor [Length: {0}]", i.CustomDescriptors.Count));
                FillCustomDescriptor(i.CustomDescriptors, tvEpDesc);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Looks up endpoint/interface information in a configuration.
 /// </summary>
 /// <param name="currentConfigInfo">The config to seach.</param>
 /// <param name="endpointAddress">The endpoint address to look for.</param>
 /// <param name="usbInterfaceInfo">On success, the <see cref="UsbInterfaceInfo"/> class for this endpoint.</param>
 /// <param name="usbEndpointInfo">On success, the <see cref="UsbEndpointInfo"/> class for this endpoint.</param>
 /// <returns>True of the endpoint was found, otherwise false.</returns>
 public static bool LookupEndpointInfo(UsbConfigInfo currentConfigInfo, byte endpointAddress, out UsbInterfaceInfo usbInterfaceInfo, out UsbEndpointInfo usbEndpointInfo)
 {
     return(LookupEndpointInfo(currentConfigInfo, -1, endpointAddress, out usbInterfaceInfo, out usbEndpointInfo));
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Looks up endpoint/interface information in a configuration.
        /// </summary>
        /// <param name="currentConfigInfo">The config to seach.</param>
        /// <param name="altInterfaceID">Alternate interface id the endpoint exists in, or -1 for any alternate interface id.</param>
        /// <param name="endpointAddress">The endpoint address to look for.</param>
        /// <param name="usbInterfaceInfo">On success, the <see cref="UsbInterfaceInfo"/> class for this endpoint.</param>
        /// <param name="usbEndpointInfo">On success, the <see cref="UsbEndpointInfo"/> class for this endpoint.</param>
        /// <returns>True of the endpoint was found, otherwise false.</returns>
        public static bool LookupEndpointInfo(UsbConfigInfo currentConfigInfo, int altInterfaceID, byte endpointAddress, out UsbInterfaceInfo usbInterfaceInfo, out UsbEndpointInfo usbEndpointInfo)
        {
            bool found = false;

            usbInterfaceInfo = null;
            usbEndpointInfo  = null;
            foreach (UsbInterfaceInfo interfaceInfo in currentConfigInfo.InterfaceInfoList)
            {
                if (altInterfaceID == -1 || altInterfaceID == interfaceInfo.Descriptor.AlternateID)
                {
                    foreach (UsbEndpointInfo endpointInfo in interfaceInfo.EndpointInfoList)
                    {
                        if ((endpointAddress & UsbConstants.ENDPOINT_NUMBER_MASK) == 0)
                        {
                            // find first read/write endpoint
                            if ((endpointAddress & UsbConstants.ENDPOINT_DIR_MASK) == 0 &&
                                (endpointInfo.Descriptor.EndpointID & UsbConstants.ENDPOINT_DIR_MASK) == 0)
                            {
                                // first write endpoint
                                found = true;
                            }
                            if ((endpointAddress & UsbConstants.ENDPOINT_DIR_MASK) != 0 &&
                                (endpointInfo.Descriptor.EndpointID & UsbConstants.ENDPOINT_DIR_MASK) != 0)
                            {
                                // first read endpoint
                                found = true;
                            }
                        }
                        else if (endpointInfo.Descriptor.EndpointID == endpointAddress)
                        {
                            found = true;
                        }

                        if (found)
                        {
                            usbInterfaceInfo = interfaceInfo;
                            usbEndpointInfo  = endpointInfo;
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Looks up endpoint/interface information in a configuration.
        /// </summary>
        /// <param name="currentConfigInfo">The config to seach.</param>
        /// <param name="altInterfaceID">Alternate interface id the endpoint exists in, or -1 for any alternate interface id.</param>
        /// <param name="endpointAddress">The endpoint address to look for.</param>
        /// <param name="usbInterfaceInfo">On success, the <see cref="UsbInterfaceInfo"/> class for this endpoint.</param>
        /// <param name="usbEndpointInfo">On success, the <see cref="UsbEndpointInfo"/> class for this endpoint.</param>
        /// <returns>True of the endpoint was found, otherwise false.</returns>
        public static bool LookupEndpointInfo(UsbConfigInfo currentConfigInfo, int altInterfaceID, byte endpointAddress, out UsbInterfaceInfo usbInterfaceInfo, out UsbEndpointInfo usbEndpointInfo)
        {
            bool found = false;

            usbInterfaceInfo = null;
            usbEndpointInfo  = null;
            foreach (UsbInterfaceInfo interfaceInfo in currentConfigInfo.Interfaces)
            {
                if (altInterfaceID == -1 || altInterfaceID == interfaceInfo.AlternateSetting)
                {
                    foreach (UsbEndpointInfo endpointInfo in interfaceInfo.Endpoints)
                    {
                        if ((endpointAddress & UsbConstants.EndpointNumberMask) == 0)
                        {
                            // find first read/write endpoint
                            if ((endpointAddress & UsbConstants.EndpointDirectionMask) == 0 &&
                                (endpointInfo.EndpointAddress & UsbConstants.EndpointDirectionMask) == 0)
                            {
                                // first write endpoint
                                found = true;
                            }

                            if ((endpointAddress & UsbConstants.EndpointDirectionMask) != 0 &&
                                (endpointInfo.EndpointAddress & UsbConstants.EndpointDirectionMask) != 0)
                            {
                                // first read endpoint
                                found = true;
                            }
                        }
                        else if (endpointInfo.EndpointAddress == endpointAddress)
                        {
                            found = true;
                        }

                        if (found)
                        {
                            usbInterfaceInfo = interfaceInfo;
                            usbEndpointInfo  = endpointInfo;
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 5
0
        public static void Main(string[] args)
        {
            Error ec = Error.Success;

            using (UsbContext context = new UsbContext())
            {
                try
                {
                    UsbInterfaceInfo usbInterfaceInfo = null;
                    UsbEndpointInfo  usbEndpointInfo  = null;

                    // Find and open the usb device.
                    using (var regList = context.FindAll(MyUsbFinder))
                    {
                        if (regList.Count == 0)
                        {
                            throw new Exception("Device Not Found.");
                        }

                        // Look through all conected devices with this vid and pid until
                        // one is found that has and and endpoint that matches TRANFER_ENDPOINT.
                        //
                        foreach (var regDevice in regList)
                        {
                            if (regDevice.TryOpen())
                            {
                                if (regDevice.Configs.Count > 0)
                                {
                                    // if TRANFER_ENDPOINT is 0x80 or 0x00, LookupEndpointInfo will return the
                                    // first read or write (respectively).
                                    if (UsbEndpointBase.LookupEndpointInfo(MyUsbDevice.Configs[0], TRANFER_ENDPOINT,
                                                                           out usbInterfaceInfo, out usbEndpointInfo))
                                    {
                                        MyUsbDevice = regDevice.Clone();
                                        MyUsbDevice.Open();
                                        break;
                                    }

                                    regDevice.Close();
                                }
                            }
                        }
                    }

                    // If the device is open and ready
                    if (MyUsbDevice == null)
                    {
                        throw new Exception("Device Not Found.");
                    }

                    // If this is a "whole" usb device (libusb-win32, linux libusb-1.0)
                    // it exposes an IUsbDevice interface. If not (WinUSB) the
                    // 'wholeUsbDevice' variable will be null indicating this is
                    // an interface of a device; it does not require or support
                    // configuration and interface selection.
                    IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                    if (!ReferenceEquals(wholeUsbDevice, null))
                    {
                        // This is a "whole" USB device. Before it can be used,
                        // the desired configuration and interface must be selected.

                        // Select config #1
                        wholeUsbDevice.SetConfiguration(1);

                        // Claim interface #0.
                        wholeUsbDevice.ClaimInterface(usbInterfaceInfo.Number);
                    }

                    // open read endpoint.
                    var reader = MyUsbDevice.OpenEndpointReader(
                        (ReadEndpointID)usbEndpointInfo.EndpointAddress,
                        0,
                        (EndpointType)(usbEndpointInfo.Attributes & 0x3));

                    if (ReferenceEquals(reader, null))
                    {
                        throw new Exception("Failed locating read endpoint.");
                    }

                    reader.Reset();

                    // The benchmark device firmware works with this example but it must be put into PC read mode.
#if IS_BENCHMARK_DEVICE
                    int            transferred;
                    byte[]         ctrlData          = new byte[1];
                    UsbSetupPacket setTestTypePacket =
                        new UsbSetupPacket((byte)(UsbCtrlFlags.Direction_In | UsbCtrlFlags.Recipient_Device | UsbCtrlFlags.RequestType_Vendor),
                                           0x0E, 0x01, usbInterfaceInfo.Number, 1);
                    transferred = MyUsbDevice.ControlTransfer(setTestTypePacket, ctrlData, 0, 1);
#endif
                    TRANFER_SIZE -= (TRANFER_SIZE % usbEndpointInfo.MaxPacketSize);

                    UsbTransferQueue transferQeue = new UsbTransferQueue(reader,
                                                                         TRANFER_MAX_OUTSTANDING_IO,
                                                                         TRANFER_SIZE,
                                                                         5000,
                                                                         usbEndpointInfo.MaxPacketSize);

                    do
                    {
                        UsbTransferQueue.Handle handle;

                        // Begin submitting transfers until TRANFER_MAX_OUTSTANDING_IO has benn reached.
                        // then wait for the oldest outstanding transfer to complete.
                        //
                        ec = transferQeue.Transfer(out handle);
                        if (ec != Error.Success)
                        {
                            throw new Exception("Failed getting async result");
                        }

                        // Show some information on the completed transfer.
                        showTransfer(handle, mTransferCount);
                    } while (mTransferCount++ < TRANSFER_COUNT);

                    // Cancels any oustanding transfers and free's the transfer queue handles.
                    // NOTE: A transfer queue can be reused after it's freed.
                    transferQeue.Free();

                    Console.WriteLine("\r\nDone!\r\n");
                }
                catch (Exception ex)
                {
                    Console.WriteLine();
                    Console.WriteLine((ec != Error.Success ? ec + ":" : String.Empty) + ex.Message);
                }
                finally
                {
                    if (MyUsbDevice != null)
                    {
                        if (MyUsbDevice.IsOpen)
                        {
                            // If this is a "whole" usb device (libusb-win32, linux libusb-1.0)
                            // it exposes an IUsbDevice interface. If not (WinUSB) the
                            // 'wholeUsbDevice' variable will be null indicating this is
                            // an interface of a device; it does not require or support
                            // configuration and interface selection.
                            IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                            if (!ReferenceEquals(wholeUsbDevice, null))
                            {
                                // Release interface #0.
                                wholeUsbDevice.ReleaseInterface(0);
                            }

                            MyUsbDevice.Close();
                        }
                        MyUsbDevice = null;
                    }

                    // Wait for user input..
                    Console.ReadKey();
                }
            }
        }
Ejemplo n.º 6
0
        private void PollUSBDevice()
        {
            ErrorCode ec = ErrorCode.None;

            UsbDevice MyUsbDevice = null;

            UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(multimeter.VendorId, multimeter.ProductId);

            UsbEndpointReader reader = null;

            try
            {
                MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);

                UsbEndpointInfo  endpointInfo     = null;
                UsbInterfaceInfo usbInterfaceInfo = null;

                if (UsbEndpointBase.LookupEndpointInfo(MyUsbDevice.Configs[0], UsbConstants.ENDPOINT_DIR_MASK, out usbInterfaceInfo, out endpointInfo) == false)
                {
                    MyUsbDevice.Close();
                    MyUsbDevice = null;
                }

                if (MyUsbDevice == null)
                {
                    Application.Current.Dispatcher.BeginInvoke(
                        new WriteConsolasDelegate(multimeter.WriteConsolas),
                        new object[] { "Can't find multimeter device!", "Error" });
                }

                IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                if (!ReferenceEquals(wholeUsbDevice, null))
                {
                    wholeUsbDevice.SetConfiguration(1);
                    wholeUsbDevice.SetConfiguration(0);
                    wholeUsbDevice.SetConfiguration(1);

                    wholeUsbDevice.ClaimInterface(usbInterfaceInfo.Descriptor.InterfaceID);
                }

                UsbSetupPacket packetSettings = new UsbSetupPacket(0x21, 0x09, 0x0300, 0x0000, 0x0005);

                byte[] buffer      = { 0x60, 0x09, 0x00, 0x00, 0x03 };
                int    transferred = 0;

                MyUsbDevice.ControlTransfer(ref packetSettings, buffer, 5, out transferred);

                transferred = 0;
                MyUsbDevice.ControlTransfer(ref packetSettings, buffer, 5, out transferred);

                try
                {
                    reader = MyUsbDevice.OpenEndpointReader((ReadEndpointID)endpointInfo.Descriptor.EndpointID, 0, (EndpointType)(endpointInfo.Descriptor.Attributes & 0x3));
                }
                catch (Exception e)
                {
                    Application.Current.Dispatcher.BeginInvoke(
                        new WriteConsolasDelegate(multimeter.WriteConsolas),
                        new object[] { e.Message, "Error" });
                }


                Application.Current.Dispatcher.BeginInvoke(
                    new WriteConsolasDelegate(multimeter.WriteConsolas),
                    new object[] { String.Format("Starting read from device <{0}:{1}>.", multimeter.VendorId.ToString(), multimeter.ProductId.ToString()), "Log" });

                while (ec == ErrorCode.None && !stopRequested)
                {
                    if (fixRequested)
                    {
                        Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            CloseFix();
                        }));
                        transferred = 0;
                        MyUsbDevice.ControlTransfer(ref packetSettings, buffer, 5, out transferred);
                        transferred = 0;
                        MyUsbDevice.ControlTransfer(ref packetSettings, buffer, 5, out transferred);
                        transferred = 0;
                        MyUsbDevice.ControlTransfer(ref packetSettings, buffer, 5, out transferred);

                        Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            multimeter.WriteConsolas("Fix attempt ended.");
                        }));
                    }

                    byte[] readBuffer = new byte[8];
                    int    bytesRead;
                    ec = reader.Read(readBuffer, 100, out bytesRead);

                    byte[] cloneBuffer = readBuffer;

                    Application.Current.Dispatcher.BeginInvoke(
                        new OneArgDelegate(HandleData),
                        new object[] { cloneBuffer });
                }
            }
            catch (Exception ex)
            {
                try
                {
                    Application.Current.Dispatcher.BeginInvoke(
                        new WriteConsolasDelegate(multimeter.WriteConsolas),
                        new object[] { ex.Message, "Error" });
                }
                catch (Exception e)
                {
                }
            }
            finally
            {
                Application.Current.Dispatcher.BeginInvoke(
                    new WriteConsolasDelegate(multimeter.WriteConsolas),
                    new object[] { "Stopping read. " + ec.ToString(), "Log" });


                if (reader != null)
                {
                    reader.Abort();
                }
                if (MyUsbDevice != null)
                {
                    if (MyUsbDevice.IsOpen)
                    {
                        IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                        if (!ReferenceEquals(wholeUsbDevice, null))
                        {
                            wholeUsbDevice.ReleaseInterface(0);
                        }

                        MyUsbDevice.Close();
                    }
                    MyUsbDevice = null;
                    try
                    {
                        UsbDevice.Exit();
                    }
                    catch (Exception e)
                    {
                        Application.Current.Dispatcher.BeginInvoke(
                            new WriteConsolasDelegate(multimeter.WriteConsolas),
                            new object[] { e.Message, "Error" });
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public void readIso()
        {
            ErrorCode ec = ErrorCode.None;

            try
            {
                // Find and open the usb device.
                UsbRegDeviceList regList = UsbDevice.AllDevices.FindAll(MyUsbFinder);
                if (regList.Count == 0)
                {
                    throw new Exception("Device Not Found.");
                }

                UsbInterfaceInfo usbInterfaceInfo = null;
                UsbEndpointInfo  usbEndpointInfo  = null;

                // Look through all conected devices with this vid and pid until
                // one is found that has and and endpoint that matches TRANFER_ENDPOINT.
                //
                foreach (UsbRegistry regDevice in regList)
                {
                    if (regDevice.Open(out MyUsbDevice))
                    {
                        if (MyUsbDevice.Configs.Count > 0)
                        {
                            // if TRANFER_ENDPOINT is 0x80 or 0x00, LookupEndpointInfo will return the
                            // first read or write (respectively).
                            if (UsbEndpointBase.LookupEndpointInfo(MyUsbDevice.Configs[0], TRANFER_ENDPOINT,
                                                                   out usbInterfaceInfo, out usbEndpointInfo))
                            {
                                break;
                            }

                            MyUsbDevice.Close();
                            MyUsbDevice = null;
                        }
                    }
                }

                // If the device is open and ready
                if (MyUsbDevice == null)
                {
                    throw new Exception("Device Not Found.");
                }

                // If this is a "whole" usb device (libusb-win32, linux libusb-1.0)
                // it exposes an IUsbDevice interface. If not (WinUSB) the
                // 'wholeUsbDevice' variable will be null indicating this is
                // an interface of a device; it does not require or support
                // configuration and interface selection.
                IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                if (!ReferenceEquals(wholeUsbDevice, null))
                {
                    // This is a "whole" USB device. Before it can be used,
                    // the desired configuration and interface must be selected.

                    // Select config #1
                    wholeUsbDevice.SetConfiguration(1);

                    // Claim interface #0.
                    wholeUsbDevice.ClaimInterface(usbInterfaceInfo.Descriptor.InterfaceID);
                }

                // open read endpoint.
                UsbEndpointReader reader = MyUsbDevice.OpenEndpointReader(
                    (ReadEndpointID)usbEndpointInfo.Descriptor.EndpointID,
                    0,
                    (EndpointType)(usbEndpointInfo.Descriptor.Attributes & 0x3));

                if (ReferenceEquals(reader, null))
                {
                    throw new Exception("Failed locating read endpoint.");
                }

                reader.Reset();


                TRANFER_SIZE -= (TRANFER_SIZE % usbEndpointInfo.Descriptor.MaxPacketSize);

                UsbTransferQueue transferQeue = new UsbTransferQueue(reader,
                                                                     TRANFER_MAX_OUTSTANDING_IO,
                                                                     TRANFER_SIZE,
                                                                     5000,
                                                                     usbEndpointInfo.Descriptor.MaxPacketSize);

                do
                {
                    UsbTransferQueue.Handle handle;

                    // Begin submitting transfers until TRANFER_MAX_OUTSTANDING_IO has benn reached.
                    // then wait for the oldest outstanding transfer to complete.
                    //
                    ec = transferQeue.Transfer(out handle);
                    if (ec != ErrorCode.Success)
                    {
                        throw new Exception("Failed getting async result");
                    }

                    // Show some information on the completed transfer.
                    showTransfer(handle, mTransferCount);
                } while (mTransferCount++ < TRANSFER_COUNT);

                // Cancels any oustanding transfers and free's the transfer queue handles.
                // NOTE: A transfer queue can be reused after it's freed.
                transferQeue.Free();
            }
            catch (Exception ex)
            {
                form.setText("\r\n");
                form.setText((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message);
            }
            finally
            {
                if (MyUsbDevice != null)
                {
                    if (MyUsbDevice.IsOpen)
                    {
                        // If this is a "whole" usb device (libusb-win32, linux libusb-1.0)
                        // it exposes an IUsbDevice interface. If not (WinUSB) the
                        // 'wholeUsbDevice' variable will be null indicating this is
                        // an interface of a device; it does not require or support
                        // configuration and interface selection.
                        IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                        if (!ReferenceEquals(wholeUsbDevice, null))
                        {
                            // Release interface #0.
                            wholeUsbDevice.ReleaseInterface(0);
                        }

                        MyUsbDevice.Close();
                    }
                    MyUsbDevice = null;
                }

                // Free usb resources
                UsbDevice.Exit();
            }
        }
Ejemplo n.º 8
0
        public bool Connect()
        {
            if (IsConnected)
            {
                throw new Exception("Attempted to Connect an already connected connection.");
            }

            // There should be only one config, but whatevs.
            foreach (var config in usbDevice.Configs)
            {
                foreach (var interfaceInfo in config.InterfaceInfoList)
                {
                    foreach (var endpointInfo in interfaceInfo.EndpointInfoList)
                    {
                        if ((readInterfaceInfo == null && readEndpointInfo == null) &&
                            Config.USB_READ_ENDPOINT_ADDRESSES.Contains(endpointInfo.Descriptor.EndpointID))
                        {
                            readInterfaceInfo = interfaceInfo;
                            readEndpointInfo  = endpointInfo;
                        }

                        if ((writeInterfaceInfo == null && writeEndpointInfo == null) &&
                            Config.USB_WRITE_ENDPOINT_ADDRESSES.Contains(endpointInfo.Descriptor.EndpointID))
                        {
                            writeInterfaceInfo = interfaceInfo;
                            writeEndpointInfo  = endpointInfo;
                        }
                    }
                }
            }

            if (readEndpointInfo == null)
            {
                throw new Exception("Failed to locate read endpoint.");
            }

            if (writeEndpointInfo == null)
            {
                throw new Exception("Failed to locate write endpoint.");
            }

            if (writeInterfaceInfo.Descriptor.InterfaceID != readInterfaceInfo.Descriptor.InterfaceID)
            {
                throw new Exception("Read and write endpoints must be on the same interface.");
            }

            endpointReader = usbDevice.OpenEndpointReader(
                (ReadEndpointID)readEndpointInfo.Descriptor.EndpointID,
                readEndpointInfo.Descriptor.MaxPacketSize,
                (EndpointType)(readEndpointInfo.Descriptor.Attributes & 0x03));

            endpointReader.ReadThreadPriority  = ThreadPriority.AboveNormal;
            endpointReader.DataReceivedEnabled = true;

            endpointReader.DataReceived -= EndpointReader_DataReceived;
            endpointReader.DataReceived += EndpointReader_DataReceived;

            endpointWriter = usbDevice.OpenEndpointWriter(
                (WriteEndpointID)writeEndpointInfo.Descriptor.EndpointID,
                (EndpointType)(writeEndpointInfo.Descriptor.Attributes & 0x03));

            return(true);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Get Endpoint reader
        /// </summary>
        /// <returns></returns>
        private static UsbEndpointReader GetReader(out UsbDevice MyUsbDevice, out int interfaceID)
        {
            UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(0x1915, 0x7B);

            // Find and open the usb device.
            UsbRegDeviceList regList = UsbDevice.AllDevices.FindAll(MyUsbFinder);

            if (regList.Count == 0)
            {
                throw new Exception("No receiver devices found.\r\n");
            }

            UsbInterfaceInfo usbInterfaceInfo = null;
            UsbEndpointInfo  usbEndpointInfo  = null;

            MyUsbDevice = null;

            // Look through all conected devices with this vid and pid until
            // one is found that has and and endpoint that matches TRANFER_ENDPOINT.
            //
            foreach (UsbRegistry regDevice in regList)
            {
                if (regDevice.Open(out MyUsbDevice))
                {
                    if (MyUsbDevice.Configs.Count > 0)
                    {
                        // if TRANFER_ENDPOINT is 0x80 or 0x00, LookupEndpointInfo will return the
                        // first read or write (respectively).
                        if (UsbEndpointBase.LookupEndpointInfo(MyUsbDevice.Configs[0], 0x88,//TRANSFER_ENDPOINT,
                                                               out usbInterfaceInfo, out usbEndpointInfo))
                        {
                            break;
                        }

                        MyUsbDevice.Close();
                        MyUsbDevice = null;
                    }
                }
            }

            // If the device is open and ready
            if (MyUsbDevice == null)
            {
                throw new Exception("Receiver device was found, but did not have the correct endpoint address.\r\n");
            }

            // If this is a "whole" usb device (libusb-win32, linux libusb-1.0)
            // it exposes an IUsbDevice interface. If not (WinUSB) the
            // 'wholeUsbDevice' variable will be null indicating this is
            // an interface of a device; it does not require or support
            // configuration and interface selection.
            IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;

            if (!ReferenceEquals(wholeUsbDevice, null))
            {
                // This is a "whole" USB device. Before it can be used,
                // the desired configuration and interface must be selected.

                // Select config #1
                wholeUsbDevice.SetConfiguration(1);

                wholeUsbDevice.ClaimInterface(usbInterfaceInfo.Descriptor.InterfaceID);
            }
            else
            {
                throw new Exception("Could not find Penny receiver device");
            }

            interfaceID = usbInterfaceInfo.Descriptor.InterfaceID;

            // open read endpoint.
            UsbEndpointReader reader = MyUsbDevice.OpenEndpointReader(
                (ReadEndpointID)usbEndpointInfo.Descriptor.EndpointID,
                0,
                (EndpointType)(usbEndpointInfo.Descriptor.Attributes & 0x3));

            if (ReferenceEquals(reader, null))
            {
                throw new Exception("Failed to locate read endpoint for JAGA Penny receiver.\r\n");
            }

            reader.Reset();

            return(reader);
        }