コード例 #1
0
        private static void ConfigTest()
        {
            // Dump all devices and descriptor information to console output.
            UsbRegDeviceList allDevices = UsbDevice.AllDevices;

            foreach (UsbRegistry usbRegistry in allDevices)
            {
                if (usbRegistry.Open(out MyUsbDevice))
                {
                    Console.WriteLine(MyUsbDevice.Info.ToString());
                    for (int iConfig = 0; iConfig < MyUsbDevice.Configs.Count; iConfig++)
                    {
                        UsbConfigInfo configInfo = MyUsbDevice.Configs[iConfig];
                        Console.WriteLine(configInfo.ToString());

                        ReadOnlyCollection <UsbInterfaceInfo> interfaceList = configInfo.InterfaceInfoList;
                        for (int iInterface = 0; iInterface < interfaceList.Count; iInterface++)
                        {
                            UsbInterfaceInfo interfaceInfo = interfaceList[iInterface];
                            Console.WriteLine(interfaceInfo.ToString());

                            ReadOnlyCollection <UsbEndpointInfo> endpointList = interfaceInfo.EndpointInfoList;
                            for (int iEndpoint = 0; iEndpoint < endpointList.Count; iEndpoint++)
                            {
                                Console.WriteLine(endpointList[iEndpoint].ToString());
                            }
                        }
                    }
                }
            }
        }
コード例 #2
0
ファイル: USBReader.cs プロジェクト: Karlaila/Tamte_Prywatki
        public void showInfo()
        {
            // Dump all devices and descriptor information to console output.
            UsbRegDeviceList allDevices = UsbDevice.AllDevices;

            foreach (UsbRegistry usbRegistry in allDevices)
            {
                if (usbRegistry.Open(out MyUsbDevice))
                {
                    form.setText(MyUsbDevice.Info.ToString());
                    for (int iConfig = 0; iConfig < MyUsbDevice.Configs.Count; iConfig++)
                    {
                        UsbConfigInfo configInfo = MyUsbDevice.Configs[iConfig];
                        form.setText(configInfo.ToString());

                        ReadOnlyCollection <UsbInterfaceInfo> interfaceList = configInfo.InterfaceInfoList;
                        for (int iInterface = 0; iInterface < interfaceList.Count; iInterface++)
                        {
                            UsbInterfaceInfo interfaceInfo = interfaceList[iInterface];
                            form.setText(interfaceInfo.ToString());

                            ReadOnlyCollection <UsbEndpointInfo> endpointList = interfaceInfo.EndpointInfoList;
                            for (int iEndpoint = 0; iEndpoint < endpointList.Count; iEndpoint++)
                            {
                                form.setText(endpointList[iEndpoint].ToString());
                            }
                        }
                    }
                }
            }

            // Free usb resources.
            // This is necessary for libusb-1.0 and Linux compatibility.
            UsbDevice.Exit();
        }
コード例 #3
0
 private static bool IsDfuInterface(UsbInterfaceInfo iinfo)
 {
     return(((byte)iinfo.Descriptor.Class == InterfaceClass) &&
            (iinfo.Descriptor.SubClass == InterfaceSubClass) &&
            ((iinfo.Descriptor.Protocol == InterfaceProtocol_Runtime) ||
             (iinfo.Descriptor.Protocol == InterfaceProtocol_DFU)));
 }
コード例 #4
0
ファイル: Device.cs プロジェクト: wxsoc/LibUsbDfu
 private static bool IsDfuInterface(UsbInterfaceInfo iinfo)
 {
     return(((byte)iinfo.Descriptor.Class == InterfaceClass) &&
            (iinfo.Descriptor.SubClass == InterfaceSubClass) &&
            ((iinfo.Descriptor.Protocol == InterfaceProtocol_Runtime) || (iinfo.Descriptor.Protocol == InterfaceProtocol_DFU)) &&
            (iinfo.CustomDescriptors.Count == 1) &&
            (iinfo.CustomDescriptors[0].Length == FunctionalDescriptor.Size));
 }
コード例 #5
0
        private void view_usb(object sender, RoutedEventArgs e)
        {
            // Dump all devices and descriptor information to console output.
            DebugInfo = "";
            UsbRegDeviceList allDevices = UsbDevice.AllDevices;

            foreach (UsbRegistry usbRegistry in allDevices)
            {
                if (usbRegistry.Open(out MyUsbDevice))
                {
                    DebugInfo += "usbdevice info\n";
                    DebugInfo += MyUsbDevice.Info.ToString();
                    DebugInfo += "\n\n\n";
                    //Console.WriteLine(MyUsbDevice.Info.ToString());
                    for (int iConfig = 0; iConfig < MyUsbDevice.Configs.Count; iConfig++)
                    {
                        UsbConfigInfo configInfo = MyUsbDevice.Configs[iConfig];
                        DebugInfo += "configInfo info\n";
                        DebugInfo += configInfo.ToString();
                        DebugInfo += "\n\n\n";
                        //Console.WriteLine(configInfo.ToString());

                        ReadOnlyCollection <UsbInterfaceInfo> interfaceList = configInfo.InterfaceInfoList;
                        for (int iInterface = 0; iInterface < interfaceList.Count; iInterface++)
                        {
                            UsbInterfaceInfo interfaceInfo = interfaceList[iInterface];
                            DebugInfo += "interfaceInfo info\n";
                            DebugInfo += interfaceInfo.ToString();
                            //Console.WriteLine(interfaceInfo.ToString());
                            DebugInfo += "\n\n\n";

                            ReadOnlyCollection <UsbEndpointInfo> endpointList = interfaceInfo.EndpointInfoList;
                            for (int iEndpoint = 0; iEndpoint < endpointList.Count; iEndpoint++)
                            {
                                DebugInfo += "endpointList iEndpoint[" + iEndpoint.ToString() + "]:\n";
                                DebugInfo += endpointList[iEndpoint].ToString();
                                DebugInfo += "\n\n\n";
                                //Console.WriteLine(endpointList[iEndpoint].ToString());
                            }
                        }
                    }
                }
            }
            // Free usb resources.
            // This is necessary for libusb-1.0 and Linux compatibility.
            UsbDevice.Exit();

            // Wait for user input..
            Console.Read();
        }
コード例 #6
0
        public UsbConfigInfo ToUsbConfigInfo(MonoUsbDevice usbDevice)
        {
            List <UsbInterfaceInfo> usbInterfaceInfos = new List <UsbInterfaceInfo>();

            foreach (MonoUsbInterface usbInterface in InterfaceList)
            {
                List <MonoUsbAltInterfaceDescriptor> monoUSBAltInterfaces = usbInterface.AltInterfaceList;
                foreach (MonoUsbAltInterfaceDescriptor monoUSBAltInterface in monoUSBAltInterfaces)
                {
                    UsbInterfaceInfo usbInterfaceInfo = monoUSBAltInterface.ToUsbInterfaceInfo(usbDevice);
                    usbInterfaceInfos.Add(usbInterfaceInfo);
                }
            }

            return(new UsbConfigInfo(usbDevice, this, usbInterfaceInfos));
        }
コード例 #7
0
        private void findDeviceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "=======================================================\n";

            // Dump all devices and descriptor information to console output.
            UsbRegDeviceList allDevices = UsbDevice.AllDevices;

            foreach (UsbRegistry usbRegistry in allDevices)
            {
                if (usbRegistry.Open(out MyUsbDevice))
                {
                    richTextBox1.Text += "=======================================================\n";
                    richTextBox1.Text += MyUsbDevice.Info.ToString();
                    Console.WriteLine(MyUsbDevice.Info.ToString());
                    for (int iConfig = 0; iConfig < MyUsbDevice.Configs.Count; iConfig++)
                    {
                        UsbConfigInfo configInfo = MyUsbDevice.Configs[iConfig];
                        richTextBox1.Text += "=======================================================\n";
                        richTextBox1.Text += configInfo.ToString();
                        Console.WriteLine(configInfo.ToString());

                        ReadOnlyCollection <UsbInterfaceInfo> interfaceList = configInfo.InterfaceInfoList;
                        for (int iInterface = 0; iInterface < interfaceList.Count; iInterface++)
                        {
                            UsbInterfaceInfo interfaceInfo = interfaceList[iInterface];
                            richTextBox1.Text += "=======================================================\n";
                            richTextBox1.Text += interfaceInfo.ToString();
                            Console.WriteLine(interfaceInfo.ToString());

                            ReadOnlyCollection <UsbEndpointInfo> endpointList = interfaceInfo.EndpointInfoList;
                            for (int iEndpoint = 0; iEndpoint < endpointList.Count; iEndpoint++)
                            {
                                richTextBox1.Text += "=======================================================\n";
                                richTextBox1.Text += endpointList[iEndpoint].ToString();
                                Console.WriteLine(endpointList[iEndpoint].ToString());
                            }
                        }
                    }
                }
            }

            this.richTextBox1.Select(this.richTextBox1.TextLength, 0);//设置光标的位置到文本尾
        }
コード例 #8
0
ファイル: ShowInfo.cs プロジェクト: angleszhaixd/LibUsbDotNet
        public static void Main(string[] args)
        {
            // Dump all devices and descriptor information to console output.
            var allDevices = UsbDevice.AllDevices;

            foreach (var usbRegistry in allDevices)
            {
                Console.WriteLine(usbRegistry.Info.ToString());

                if (usbRegistry.Open())
                {
                    for (int iConfig = 0; iConfig < usbRegistry.Configs.Count; iConfig++)
                    {
                        UsbConfigInfo configInfo = usbRegistry.Configs[iConfig];
                        Console.WriteLine(configInfo.ToString());

                        ReadOnlyCollection <UsbInterfaceInfo> interfaceList = configInfo.InterfaceInfoList;
                        for (int iInterface = 0; iInterface < interfaceList.Count; iInterface++)
                        {
                            UsbInterfaceInfo interfaceInfo = interfaceList[iInterface];
                            Console.WriteLine(interfaceInfo.ToString());

                            ReadOnlyCollection <UsbEndpointInfo> endpointList = interfaceInfo.EndpointInfoList;
                            for (int iEndpoint = 0; iEndpoint < endpointList.Count; iEndpoint++)
                            {
                                Console.WriteLine(endpointList[iEndpoint].ToString());
                            }
                        }
                    }

                    usbRegistry.Close();
                }
            }


            // Free usb resources.
            // This is necessary for libusb-1.0 and Linux compatibility.
            UsbDevice.Exit();

            // Wait for user input..
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
コード例 #9
0
ファイル: dfu.cs プロジェクト: glocklueng/NXP_DFU
 /// <summary>
 ///   Find the interface that supports DFU. Return true if found.
 /// </summary>
 public bool findInterface()
 {
   byte currentConfig;
   
   foreach (UsbConfigInfo config in myUSBDevice.Configs)
     {
       currentConfig=config.Descriptor.ConfigID;
       foreach (UsbInterfaceInfo iface in config.InterfaceInfoList)
         {
           if (((byte)iface.Descriptor.Class == 0xFE /*Application Specific*/) &&
               ((byte)iface.Descriptor.SubClass == 0x01 /* DFU */) ) {
             DFUinterface=iface.Descriptor.InterfaceID;
             DFUconfig=currentConfig;
             myDFUconfig=config;
             myDFUiface=iface;
             return(true);
           }
         }
     }
   return(false);
 }
コード例 #10
0
ファイル: ShowInfo.cs プロジェクト: zombie-king/LibUsbDotNet
        public static void Main(string[] args)
        {
            // Dump all devices and descriptor information to console output.
            using (UsbContext context = new UsbContext())
            {
                var allDevices = context.List();
                foreach (var usbRegistry in allDevices)
                {
                    Console.WriteLine(usbRegistry.Info.ToString());

                    if (usbRegistry.TryOpen())
                    {
                        for (int iConfig = 0; iConfig < usbRegistry.Configs.Count; iConfig++)
                        {
                            UsbConfigInfo configInfo = usbRegistry.Configs[iConfig];
                            Console.WriteLine(configInfo.ToString());

                            ReadOnlyCollection <UsbInterfaceInfo> interfaceList = configInfo.Interfaces;
                            for (int iInterface = 0; iInterface < interfaceList.Count; iInterface++)
                            {
                                UsbInterfaceInfo interfaceInfo = interfaceList[iInterface];
                                Console.WriteLine(interfaceInfo.ToString());

                                ReadOnlyCollection <UsbEndpointInfo> endpointList = interfaceInfo.Endpoints;
                                for (int iEndpoint = 0; iEndpoint < endpointList.Count; iEndpoint++)
                                {
                                    Console.WriteLine(endpointList[iEndpoint].ToString());
                                }
                            }
                        }

                        usbRegistry.Close();
                    }
                }
            }

            // Wait for user input..
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
コード例 #11
0
ファイル: Program.cs プロジェクト: lzziaini/LibUsbDotNetDemos
        public static void Main(string[] args)
        {
            // Dump all devices and descriptor information to console output.
            UsbRegDeviceList allDevices = UsbDevice.AllDevices;

            Debug.WriteLine(allDevices.Count);
            foreach (UsbRegistry usbRegistry in allDevices)
            {
                if (usbRegistry.Open(out MyUsbDevice))
                {
                    Console.WriteLine(MyUsbDevice.Info.ToString() + "--------1-----------");
                    for (int iConfig = 0; iConfig < MyUsbDevice.Configs.Count; iConfig++)
                    {
                        UsbConfigInfo configInfo = MyUsbDevice.Configs[iConfig];
                        Console.WriteLine(configInfo.ToString() + "---------2-----------");

                        ReadOnlyCollection <UsbInterfaceInfo> interfaceList = configInfo.InterfaceInfoList;
                        for (int iInterface = 0; iInterface < interfaceList.Count; iInterface++)
                        {
                            UsbInterfaceInfo interfaceInfo = interfaceList[iInterface];
                            Console.WriteLine(interfaceInfo.ToString() + "--------3-------");

                            ReadOnlyCollection <UsbEndpointInfo> endpointList = interfaceInfo.EndpointInfoList;
                            for (int iEndpoint = 0; iEndpoint < endpointList.Count; iEndpoint++)
                            {
                                Console.WriteLine(endpointList[iEndpoint].ToString() + "-------4--------");
                            }
                        }
                    }
                }
            }


            // Free usb resources.
            // This is necessary for libusb-1.0 and Linux compatibility.
            UsbDevice.Exit();

            // Wait for user input..
            Console.ReadKey();
        }
コード例 #12
0
        public static UsbLibDotNetHIDDevice FindDevice(int vendorID, int productID, byte configID = 1, byte interfaceID = 0)
        {
            UsbLibDotNetHIDDevice newDev    = null;
            UsbDeviceFinder       usbFinder = new UsbDeviceFinder(vendorID, productID);
            UsbDevice             usbDev    = null;
            //Byte configID = 255;
            bool endpointsFound = false;

            try
            {
                // Find and open the usb device.
                usbDev = UsbDevice.OpenUsbDevice(usbFinder);

                // If the device is open and ready
                if (usbDev == null)
                {
                    Console.WriteLine("Device Not Found [0x" + vendorID.ToString("x4") + ":0x" + productID.ToString("x4") + "].");
                    return(null);
                }

                newDev = new UsbLibDotNetHIDDevice();

                for (int iConfig = 0; iConfig < usbDev.Configs.Count; iConfig++)
                {
                    UsbConfigInfo configInfo = usbDev.Configs[iConfig];

                    if (configID == configInfo.Descriptor.ConfigID)
                    {
                        ReadOnlyCollection <UsbInterfaceInfo> interfaceList = configInfo.InterfaceInfoList;

                        //Console.WriteLine("Config Found: " + configInfo.Descriptor.ConfigID.ToString());
                        for (int iInterface = 0; iInterface < interfaceList.Count; iInterface++)
                        {
                            UsbInterfaceInfo interfaceInfo = interfaceList[iInterface];

                            if (interfaceID == interfaceInfo.Descriptor.InterfaceID)
                            {
                                //Console.WriteLine("Interface Found: " + interfaceInfo.Descriptor.EndpointCount.ToString());
                                // We need 2 Endpoints
                                if (interfaceInfo.Descriptor.EndpointCount == 2)
                                {
                                    ReadOnlyCollection <UsbEndpointInfo> endpointList = interfaceInfo.EndpointInfoList;

                                    //Console.WriteLine("Two Endpoints Found");
                                    for (int iEndpoint = 0; iEndpoint < endpointList.Count; iEndpoint++)
                                    {
                                        if (iEndpoint == 0)
                                        {
                                            newDev.m_readEndpoint = (ReadEndpointID)endpointList[iEndpoint].Descriptor.EndpointID;
                                        }
                                        else
                                        {
                                            newDev.m_writeEndpoint = (WriteEndpointID)endpointList[iEndpoint].Descriptor.EndpointID;
                                        }

                                        newDev.m_configID    = configInfo.Descriptor.ConfigID;
                                        newDev.m_interfaceID = interfaceInfo.Descriptor.InterfaceID;
                                    }

                                    endpointsFound = true;
                                }
                            }
                        }
                    }
                }

                if (String.Compare(System.Environment.GetEnvironmentVariable("USBLIBDOTNET_VERBOSE"), "yes", true) == 0)
                {
                    Console.WriteLine("*** GD77 USB Device Infos:\n  - " + usbDev.Info.ToString().Replace("\n", "\n  - "));
                    for (int iConfig = 0; iConfig < usbDev.Configs.Count; iConfig++)
                    {
                        UsbConfigInfo configInfo = usbDev.Configs[iConfig];

                        Console.WriteLine("   *** ConfigID: " + configInfo.Descriptor.ConfigID);
                        Console.WriteLine("   CONFIGURATION INFO: \n     - " + configInfo.ToString().Replace("\n", "\n        - "));

                        ReadOnlyCollection <UsbInterfaceInfo> interfaceList = configInfo.InterfaceInfoList;
                        for (int iInterface = 0; iInterface < interfaceList.Count; iInterface++)
                        {
                            UsbInterfaceInfo interfaceInfo = interfaceList[iInterface];
                            Console.WriteLine("         *** InterfaceID: " + interfaceInfo.Descriptor.InterfaceID);
                            Console.WriteLine("         INTERFACE INFO: \n        - " + interfaceInfo.ToString().Replace("\n", "\n        - "));

                            ReadOnlyCollection <UsbEndpointInfo> endpointList = interfaceInfo.EndpointInfoList;
                            for (int iEndpoint = 0; iEndpoint < endpointList.Count; iEndpoint++)
                            {
                                Console.WriteLine("            ENDPOINT LIST: \n           - " + endpointList[iEndpoint].ToString().Replace("\n", "\n           - "));
                            }
                        }
                    }
                    Console.WriteLine("***\n");
                }
                if (endpointsFound == false)
                {
                    Console.WriteLine("Couldn't find 2 endpoints for interface #" + interfaceID.ToString() + " of configuration #" + configID.ToString());
                    return(null);
                }
                // If this is a "whole" usb device (libusb-win32, linux libusb)
                // it will have an IUsbDevice interface. If not (WinUSB) the
                // variable will be null indicating this is an interface of a
                // device.
                IUsbDevice wholeUsbDevice = usbDev as IUsbDevice;
                if (!ReferenceEquals(wholeUsbDevice, null))
                {
#if DUMP_USB_INFOS
                    Console.WriteLine("*** ConfigID: " + newDev.m_configID);
                    Console.WriteLine("*** InterfaceID: " + newDev.m_interfaceID);
#endif
                    // This is a "whole" USB device. Before it can be used,
                    // the desired configuration and interface must be selected.

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

                    // Claim interface #0.
                    wholeUsbDevice.ClaimInterface(newDev.m_interfaceID);
                }

                // open read endpoint 1.
                newDev.m_usbReader = usbDev.OpenEndpointReader(newDev.m_readEndpoint);
                newDev.m_usbReader.ReadThreadPriority = ThreadPriority.AboveNormal;

                // open write endpoint 2
                newDev.m_usbWriter = usbDev.OpenEndpointWriter(newDev.m_writeEndpoint);
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: " + ex.Message);
                return(null);
            }

            newDev.m_usbDevice = usbDev;
            newDev.m_vendorID  = vendorID;
            newDev.m_productID = productID;

            return(newDev);
        }
コード例 #13
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));
 }
コード例 #14
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);
        }
コード例 #15
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);
        }
コード例 #16
0
ファイル: USBReader.cs プロジェクト: Karlaila/Tamte_Prywatki
        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();
            }
        }
コード例 #17
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();
                }
            }
        }
コード例 #18
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" });
                    }
                }
            }
        }
コード例 #19
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);
        }
コード例 #20
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);
        }
コード例 #21
0
ファイル: Form1.cs プロジェクト: AlexeyShchekin/HID_USB_Form
        private void Form1_Load(object sender, EventArgs e)
        {
            UsbRegDeviceList allDevices = UsbDevice.AllDevices;

            foreach (UsbRegistry usbRegistry in allDevices)
            {
                if (usbRegistry.Open(out MyUsbDevice))
                {
                    logBox.AppendText(MyUsbDevice.Info.ToString());
                    for (int iConfig = 0; iConfig < MyUsbDevice.Configs.Count; iConfig++)
                    {
                        UsbConfigInfo configInfo = MyUsbDevice.Configs[iConfig];
                        logBox.AppendText(configInfo.ToString());

                        ReadOnlyCollection <UsbInterfaceInfo> interfaceList = configInfo.InterfaceInfoList;
                        for (int iInterface = 0; iInterface < interfaceList.Count; iInterface++)
                        {
                            UsbInterfaceInfo interfaceInfo = interfaceList[iInterface];
                            logBox.AppendText(interfaceInfo.ToString());

                            ReadOnlyCollection <UsbEndpointInfo> endpointList = interfaceInfo.EndpointInfoList;
                            for (int iEndpoint = 0; iEndpoint < endpointList.Count; iEndpoint++)
                            {
                                logBox.AppendText(endpointList[iEndpoint].ToString());
                            }
                        }
                    }
                }
            }

            try
            {
                UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(0x0483, 0x5750);
                MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);

                if (MyUsbDevice == null)
                {
                    throw new Exception("Device Not Found.");
                }

                IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                if (!ReferenceEquals(wholeUsbDevice, null))
                {
                    // Select config #1
                    wholeUsbDevice.SetConfiguration(1);

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

                reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01, 8, EndpointType.Interrupt);

                writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01, EndpointType.Interrupt);

                reader.DataReceived       += (OnRxEndPointData);
                reader.DataReceivedEnabled = true;
                USBcmdTimer.Start();
            }
            catch (Exception ex)
            {
                logBox.AppendText("\r\n");
                logBox.AppendText((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message);
            }
        }
コード例 #22
0
        /// <summary>
        /// Gets all USB devices that match the vendor id and product id passed in.
        /// </summary>
        /// <param name="vendorIdFilter"></param>
        /// <param name="productIdFilter"></param>
        /// <returns></returns>
        private List <UsbDevice> GetDevices(ushort vendorIdFilter, ushort productIdFilter)
        {
            List <UsbDevice> matchingDevices = new List <UsbDevice>();

            // get all the devices in the USB Registry
            UsbRegDeviceList devices = UsbDevice.AllDevices;

            // loop through all the devices
            foreach (UsbRegistry usbRegistry in devices)
            {
                // try and open the device to get info
                if (usbRegistry.Open(out UsbDevice device))
                {
                    // Filters
                    // string BS because of [this](https://github.com/LibUsbDotNet/LibUsbDotNet/issues/91) bug.
                    ushort vendorID  = ushort.Parse(device.Info.Descriptor.VendorID.ToString("x"), System.Globalization.NumberStyles.AllowHexSpecifier);
                    ushort productID = ushort.Parse(device.Info.Descriptor.ProductID.ToString("x"), System.Globalization.NumberStyles.AllowHexSpecifier);
                    if (vendorIdFilter != 0 && vendorID != vendorIdFilter)
                    {
                        continue;
                    }
                    if (productIdFilter != 0 && productID != productIdFilter)
                    {
                        continue;
                    }

                    // Check for the DFU descriptor in the

                    // get the configs
                    for (int iConfig = 0; iConfig < device.Configs.Count; iConfig++)
                    {
                        UsbConfigInfo configInfo = device.Configs[iConfig];

                        // get the interfaces
                        ReadOnlyCollection <UsbInterfaceInfo> interfaceList = configInfo.InterfaceInfoList;

                        // loop through the interfaces
                        for (int iInterface = 0; iInterface < interfaceList.Count; iInterface++)
                        {
                            // shortcut
                            UsbInterfaceInfo interfaceInfo = interfaceList[iInterface];

                            // if it's a DFU device, we want to grab the DFU descriptor
                            // have to string compare because 0xfe isn't defined in `ClassCodeType`
                            if (interfaceInfo.Descriptor.Class.ToString("x").ToLower() != "fe" || interfaceInfo.Descriptor.SubClass != 0x1)
                            {
                                // interface doesn't support DFU
                            }

                            // we should also be getting the DFU descriptor
                            // which describes the DFU parameters like speed and
                            // flash size. However, it's missing from LibUsbDotNet
                            // the Dfu descriptor is supposed to be 0x21
                            //// get the custom descriptor
                            //var dfuDescriptor = interfaceInfo.CustomDescriptors[0x21];
                            //if (dfuDescriptor != null) {
                            //    // add the matching device
                            //    matchingDevices.Add(device);
                            //}
                        }
                    }

                    // add the matching device
                    matchingDevices.Add(device);

                    // cleanup
                    device.Close();
                }
            }

            return(matchingDevices);
        }
コード例 #23
0
        /// <summary>
        /// Used for debug, enumerates all USB devices and their info to the console.
        /// </summary>
        public void ConsoleOutUsbInfo()
        {
            UsbRegDeviceList devices = UsbDevice.AllDevices;

            Debug.WriteLine($"Device Count: {devices.Count}");

            // loop through all the devices in the registry
            foreach (UsbRegistry usbRegistry in devices)
            {
                // try and open the device to get info
                if (usbRegistry.Open(out UsbDevice device))
                {
                    //Debug.WriteLine($"Device.Info: {device.Info.ToString()}");

                    Debug.WriteLine("-----------------------------------------------");
                    Debug.WriteLine($"Found device: {device.Info.ProductString}, by {device.Info.ManufacturerString}, serial: {device.Info.SerialString}");
                    Debug.WriteLine($" VendordID: 0x{device.Info.Descriptor.VendorID.ToString("x4")}, ProductID: 0x{device.Info.Descriptor.ProductID.ToString("x4")}");
                    Debug.WriteLine($" Config count: {device.Configs.Count}");

                    for (int iConfig = 0; iConfig < device.Configs.Count; iConfig++)
                    {
                        UsbConfigInfo configInfo = device.Configs[iConfig];

                        // get the interfaces
                        ReadOnlyCollection <UsbInterfaceInfo> interfaceList = configInfo.InterfaceInfoList;



                        // loop through the interfaces
                        for (int iInterface = 0; iInterface < interfaceList.Count; iInterface++)
                        {
                            UsbInterfaceInfo interfaceInfo = interfaceList[iInterface];

                            Debug.WriteLine($"  Found Interface: {interfaceInfo.InterfaceString}, w/following descriptors: {{");
                            Debug.WriteLine($"    Descriptor Type: {interfaceInfo.Descriptor.DescriptorType}");
                            Debug.WriteLine($"    Interface ID: 0x{interfaceInfo.Descriptor.InterfaceID.ToString("x")}");
                            Debug.WriteLine($"    Alternate ID: 0x{interfaceInfo.Descriptor.AlternateID.ToString("x")}");
                            Debug.WriteLine($"    Class: 0x{interfaceInfo.Descriptor.Class.ToString("x")}");
                            Debug.WriteLine($"    SubClass: 0x{interfaceInfo.Descriptor.SubClass.ToString("x")}");
                            Debug.WriteLine($"    Protocol: 0x{interfaceInfo.Descriptor.Protocol.ToString("x")}");
                            Debug.WriteLine($"    String Index: {interfaceInfo.Descriptor.StringIndex}");
                            Debug.WriteLine($"  }}");

                            if (interfaceInfo.Descriptor.Class.ToString("x").ToLower() != "fe" || interfaceInfo.Descriptor.SubClass != 0x1)
                            {
                                Debug.WriteLine("Not a DFU device");
                            }
                            else
                            {
                                Debug.WriteLine("DFU Device");
                            }

                            // TODO: we really should be looking for the DFU descriptor:
                            // (note this code comes from our binding of LibUsb in DFU-sharp, so the API is different.
                            //// get the descriptor for the interface
                            //var dfu_descriptor = FindDescriptor(
                            //    interface_descriptor.Extra,
                            //    interface_descriptor.Extra_length,
                            //    (byte)Consts.USB_DT_DFU);


                            //foreach (var cd in interfaceInfo.CustomDescriptors) {
                            //    Debug.WriteLine($"Custom Descriptor: { System.Text.Encoding.ASCII.GetChars(cd).ToString() }");
                            //}

                            // get the endpoints
                            ReadOnlyCollection <UsbEndpointInfo> endpointList = interfaceInfo.EndpointInfoList;
                            for (int iEndpoint = 0; iEndpoint < endpointList.Count; iEndpoint++)
                            {
                                Debug.WriteLine($"endpointList[{ iEndpoint}]: {endpointList[iEndpoint].ToString()}");
                            }
                        }
                    }

                    device.Close();
                    Debug.WriteLine("-----------------------------------------------");
                }
            }
            //UsbDevice.Exit();
        }
コード例 #24
0
ファイル: fBenchmark.cs プロジェクト: wangbo121/UAV-NET
        private bool openAsTestDevice(UsbRegistry usbRegistry)
        {
            if (!ReferenceEquals(mUsbDevice, null))
            {
                closeTestDevice(mUsbDevice);
            }

            mUsbDevice = null;

            try
            {
                if (usbRegistry.Open(out mUsbDevice))
                {
                    UsbInterfaceInfo readInterfaceInfo;
                    UsbInterfaceInfo writeInterfaceInfo;

                    UsbDevice.UsbErrorEvent += OnUsbError;

                    if (!UsbEndpointBase.LookupEndpointInfo(mUsbDevice.Configs[0],
                                                            0x80,
                                                            out readInterfaceInfo,
                                                            out mReadEndpointInfo))
                    {
                        throw new Exception("failed locating read endpoint.");
                    }

                    mBenchMarkParameters.BufferSize -= (mBenchMarkParameters.BufferSize % (mReadEndpointInfo.Descriptor.MaxPacketSize));

                    if (!UsbEndpointBase.LookupEndpointInfo(mUsbDevice.Configs[0],
                                                            0x00,
                                                            out writeInterfaceInfo,
                                                            out mWriteEndpointInfo))
                    {
                        throw new Exception("failed locating write endpoint.");
                    }

                    if (((mWriteEndpointInfo.Descriptor.Attributes & 3) == (int)EndpointType.Isochronous) ||
                        ((mReadEndpointInfo.Descriptor.Attributes & 3) == (int)EndpointType.Isochronous))
                    {
                        throw new Exception("buenchmark GUI application does not support ISO endpoints. Use BenchmarkCon instead.");
                    }

                    mBenchMarkParameters.BufferSize -= (mBenchMarkParameters.BufferSize % (mWriteEndpointInfo.Descriptor.MaxPacketSize));

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

                    mEP1Reader = mUsbDevice.OpenEndpointReader(
                        (ReadEndpointID)mReadEndpointInfo.Descriptor.EndpointID,
                        mBenchMarkParameters.BufferSize,
                        (EndpointType)(mReadEndpointInfo.Descriptor.Attributes & 3));

                    mEP1Writer = mUsbDevice.OpenEndpointWriter(
                        (WriteEndpointID)mWriteEndpointInfo.Descriptor.EndpointID,
                        (EndpointType)(mWriteEndpointInfo.Descriptor.Attributes & 3));

                    mInterfaceInfo = writeInterfaceInfo;

                    mEP1Reader.ReadThreadPriority = mBenchMarkParameters.Priority;
                    mEP1Reader.DataReceived      += OnDataReceived;

                    makeTestBytes(out loopTestBytes, mBenchMarkParameters.BufferSize);


                    // If this is a "whole" usb device (libusb-win32, linux libusb)
                    // it will have an IUsbDevice interface. If not (WinUSB) the
                    // variable will be null indicating this is an interface of a
                    // device.
                    IUsbDevice wholeUsbDevice = mUsbDevice 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(mInterfaceInfo.Descriptor.InterfaceID);
                    }
                    return(true);
                }
            }
            catch (Exception ex)
            {
                SetStatus(ex.Message, true);
            }

            if (!ReferenceEquals(mUsbDevice, null))
            {
                try
                {
                    closeTestDevice(mUsbDevice);
                }
                finally
                {
                    mUsbDevice = null;
                    mEP1Reader = null;
                    mEP1Writer = null;
                }
            }
            return(false);
        }