Scanning for a Human Interface Device (HID) using C#

后端 未结 2 641
忘了有多久
忘了有多久 2021-01-13 07:31

I am developing a C# .NET 2.0 application wherein I need to scan for an attached HID. How can this be done? Because it is a HID, Windows does not assign a COM port to it.

2条回答
  •  生来不讨喜
    2021-01-13 07:52

    Here is an example of enumerating Hid devices on Windows:

        public static ConnectedDeviceDefinition GetDeviceDefinition(string deviceId, SafeFileHandle safeFileHandle)
        {
            try
            {
                var hidAttributes = GetHidAttributes(safeFileHandle);
                var hidCollectionCapabilities = GetHidCapabilities(safeFileHandle);
                var manufacturer = GetManufacturer(safeFileHandle);
                var serialNumber = GetSerialNumber(safeFileHandle);
                var product = GetProduct(safeFileHandle);
    
                return new ConnectedDeviceDefinition(deviceId)
                {
                    WriteBufferSize = hidCollectionCapabilities.OutputReportByteLength,
                    ReadBufferSize = hidCollectionCapabilities.InputReportByteLength,
                    Manufacturer = manufacturer,
                    ProductName = product,
                    ProductId = (ushort)hidAttributes.ProductId,
                    SerialNumber = serialNumber,
                    Usage = hidCollectionCapabilities.Usage,
                    UsagePage = hidCollectionCapabilities.UsagePage,
                    VendorId = (ushort)hidAttributes.VendorId,
                    VersionNumber = (ushort)hidAttributes.VersionNumber,
                    DeviceType = DeviceType.Hid
                };
            }
            catch (Exception)
            {
                return null;
            }
        }
    

    Full class here: https://github.com/MelbourneDeveloper/Device.Net/blob/master/src/Hid.Net/Windows/WindowsHidDeviceFactory.cs

    API Calls here: https://github.com/MelbourneDeveloper/Device.Net/blob/master/src/Hid.Net/Windows/HidAPICalls.cs

    Here is a similar thing for Windows 10 (UWP):

        public async Task> GetConnectedDeviceDefinitions(FilterDeviceDefinition deviceDefinition)
        {
            var aqsFilter = GetAqsFilter(deviceDefinition.VendorId, deviceDefinition.ProductId);
    
            var deviceInformationCollection = await wde.DeviceInformation.FindAllAsync(aqsFilter).AsTask();
    
            var deviceDefinitions = deviceInformationCollection.Select(d => GetDeviceInformation(d, DeviceType));
    
            var deviceDefinitionList = new List();
    
            foreach (var deviceDef in deviceDefinitions)
            {
                var connectionInformation = await TestConnection(deviceDef.DeviceId);
                if (connectionInformation.CanConnect)
                {
                    await Task.Delay(1000);
    
                    deviceDef.UsagePage = connectionInformation.UsagePage;
    
                    deviceDefinitionList.Add(deviceDef);
                }
            }
    
            return deviceDefinitionList;
        }
    

    Code:https://github.com/MelbourneDeveloper/Device.Net/blob/77439b1ab0f4b3ad97376e4e62c7efac0a749783/src/Device.Net.UWP/UWPDeviceFactoryBase.cs#L47

    Android (https://github.com/MelbourneDeveloper/Device.Net/blob/77439b1ab0f4b3ad97376e4e62c7efac0a749783/src/Usb.Net.Android/AndroidUsbDeviceFactory.cs#L31):

        public Task> GetConnectedDeviceDefinitions(FilterDeviceDefinition deviceDefinition)
        {
            return Task.Run>(() =>
            {
                //TODO: Get more details about the device.
                return UsbManager.DeviceList.Select(kvp => kvp.Value).Where(d => deviceDefinition.VendorId == d.VendorId && deviceDefinition.ProductId == d.ProductId).Select(GetAndroidDeviceDefinition).ToList();
            });
        }
    

    Using Hid.Net, you can enumerate devices in the same way on any platform like below. See the full article.

    var devices = await DeviceManager.Current.GetConnectedDeviceDefinitions(new FilterDeviceDefinition { VendorId = 0x1209, ProductId = 0x53C1 });
    foreach (var device in devices)
    {
        Debug.WriteLine(device.DeviceId);
    }
    

提交回复
热议问题