LibUsbDotNet 调用 UsbDevice.AllDevices 时未找到设备

2023-11-22

我正在执行 LibUsbDotNet 的示例代码,它将返回所有连接的 USB 设备的信息。您可以在下面找到此代码。

using System;
using LibUsbDotNet;
using LibUsbDotNet.Info;
using LibUsbDotNet.Main;
using System.Collections.ObjectModel;

namespace Examples
{
    internal class ShowInfo
    {
        public static UsbDevice MyUsbDevice;

        public static void Main(string[] args)
        {
            // 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());
                            }
                        }
                    }
                }
            }


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

            // Wait for user input..
            Console.ReadKey();
        }
    }
}

我的问题是代码中执行的第二行:

UsbRegDeviceList allDevices = UsbDevice.AllDevices;

根本不返回任何设备,而我确实有我想要找到连接的设备,我的键盘和鼠标。

以前有人遇到过这个问题吗?和/或有人知道如何解决它吗?

提前致谢! 米兰·范迪克


libusb 支持 HID 设备吗?

在 Windows 上,libusb 支持原生 Windows HID 驱动程序,但有一些限制,例如无法访问 HID 鼠标和键盘,因为它们是系统保留的,以及直接读取 HID 报告描述符。除此之外,您应该像与任何其他 USB 设备一样与 HID 设备进行通信。

如果您的应用程序将围绕 HID 访问,我们鼓励您尝试使用 Signal 11 Software 的 HIDAPI 库,该库也是跨平台的。它在 Windows 和 Mac OS X 下使用原生 HID API,在 Linux 下可以使用 libusb 或 hidraw 作为后端。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

LibUsbDotNet 调用 UsbDevice.AllDevices 时未找到设备 的相关文章

随机推荐