示例#1
0
        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();
        }
示例#2
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());
                            }
                        }
                    }
                }
            }
        }
        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);//设置光标的位置到文本尾
        }
        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();
        }
示例#5
0
        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();
        }
示例#6
0
        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();
        }
示例#7
0
        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();
        }
示例#8
0
        private void GetConfigValue_Click(object sender, EventArgs e)
        {
            byte bConfig;

            if (mUsbDevice.GetConfiguration(out bConfig))
            {
                ReadOnlyCollection <UsbConfigInfo> configProfiles = mUsbDevice.Configs;
                if (bConfig == 0 || bConfig > configProfiles.Count)
                {
                    tInfo.AppendText("[ERROR] Invalid configuration data received.");
                    return;
                }
                UsbConfigInfo currentConfig = configProfiles[bConfig - 1];
                SetStatus(string.Format("Config Value:{0} Size:{1}", bConfig, currentConfig.Descriptor.TotalLength), false);

                tInfo.AppendText(currentConfig.ToString());
            }
            else
            {
                SetStatus("GetConfiguration Failed.", true);
            }
        }
示例#9
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);
        }
示例#10
0
        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);
            }
        }
示例#11
0
        public bool DiscoveryUsbDevice(Boolean assignFlag = false)
        {
            UsbRegDeviceList allDevices = UsbDevice.AllDevices;

            if (assignFlag)
            {
                profileList   = new MonoUsbProfileList();
                sessionHandle = new MonoUsbSessionHandle();

                if (sessionHandle.IsInvalid)
                {
                    throw new Exception(String.Format("libusb {0}:{1}", MonoUsbSessionHandle.LastErrorCode,
                                                      MonoUsbSessionHandle.LastErrorString));
                }
                MyUsbDeviceArray = new UsbDevice[MAX_USB_DEVICE_COUNT];
            }
            else
            {
            }

            foreach (UsbRegistry usbRegistry in allDevices)
            {
                System.Console.WriteLine("Open one more ");
                if (usbRegistry.Open(out MyUsbDevice))
                {
                    if ((bt_vid == MyUsbDevice.Info.Descriptor.VendorID) && (bt_pid == MyUsbDevice.Info.Descriptor.ProductID))
                    {
                        MyUsbDeviceArray[UsbDeviceCount] = MyUsbDevice;
                        UsbDeviceCount++;
                    }
                    else
                    {
                        System.Console.WriteLine(String.Format("device vid {0} pid {1}", MyUsbDevice.Info.Descriptor.VendorID, MyUsbDevice.Info.Descriptor.ProductID));
                    }

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

            System.Console.WriteLine(String.Format("Open  {0}", UsbDeviceCount));

            System.Console.WriteLine("begin");
            if (profileList != null)
            {
                profileList.Refresh(sessionHandle);
            }

            MonoUsbProfile myProfile = profileList.GetList().Find(MyVidPidPredicate);

            MatchingUsbDeviceList = profileList.GetList().FindAll(MyVidPidPredicate);

            if (myProfile == null)
            {
                Console.WriteLine("myProfile is 0");
                return(false);
            }

            // Open the device handle to perfom I/O
            myDeviceHandle = myProfile.OpenDeviceHandle();
            if (myDeviceHandle.IsInvalid)
            {
                throw new Exception(String.Format("{0}, {1}", MonoUsbDeviceHandle.LastErrorCode,
                                                  MonoUsbDeviceHandle.LastErrorString));
            }

            for (int i = 0; i < UsbDeviceCount; i++)
            {
                object thisHubPort;
                int    thisPortNum     = -1;
                int    thisHubNum      = -1;
                char[] separatingChars = { '_', '.', '#' };

                MyUsbDeviceArray[i].UsbRegistryInfo.DeviceProperties.TryGetValue("LocationInformation", out thisHubPort);

                System.Console.WriteLine(String.Format("thisHubPort {0}", thisHubPort));
                string[] words = thisHubPort.ToString().Split(separatingChars, System.StringSplitOptions.RemoveEmptyEntries);

                if (words[0].Equals("Port"))
                {
                    thisPortNum = Convert.ToInt32(words[1]);
                }
                if (words[2].Equals("Hub"))
                {
                    thisHubNum = Convert.ToInt32(words[3]);
                }

                if (assignFlag)
                {
                    usbDeviceInfo             = new UsbDeviceInfo();
                    usbDeviceInfo.MyUsbDevice = MyUsbDeviceArray[i];
                    usbDeviceInfo.Port        = thisPortNum;
                    usbDeviceInfo.Hub         = thisHubNum;
                    Console.WriteLine(String.Format("info {0},{1}", thisPortNum, thisHubNum));
                    FinalDeviceIndex = (short)i;
                }
                else
                {
                    if ((usb_port == thisPortNum) && (usb_hub == thisHubNum))
                    {
                        System.Console.WriteLine(String.Format("usb_port {0}, usb_hub {1}", usb_port, usb_hub));
                        FinalDeviceIndex = (short)i;
                        break;
                    }
                }
            }

            return(true);
        }