Scanning for a user interface device (HID) using C #

I am developing a C # .NET 2.0 application in which I need to scan an attached HID. How can this be done? Since this is a HID, Windows does not assign a COM port to it. I only need to programmatically determine if the device is connected. Thanks.

ADDITIONAL INFORMATION

When I connect the USB device to the computer, two entries are displayed in the Devices section of the user interface in the device manager. By clicking on their properties, you will receive this information on the corresponding Details tabs:

HID-compatible device Device instance identifier: HID \ VID_1795 & PID_6004 \ 7 & 2694D932 & 0 & 0000

USB device for user interface Device instance identifier: USB \ VID_1795 & PID_6004 \ B973000000EB0D00

+4
source share
2 answers

In the WMI code creator, select the following options:

Namespace: root \ WMI

Class: MSWmi_PnPInstanceNames

Select InstanceNames in the Results field for the following code:

 using System; using System.Management; using System.Windows.Forms; namespace WMISample { public class MyWMIQuery { public static void Main() { try { ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSWmi_PnPInstanceNames"); foreach (ManagementObject queryObj in searcher.Get()) { Console.WriteLine("-----------------------------------"); Console.WriteLine("MSWmi_PnPInstanceNames instance"); Console.WriteLine("-----------------------------------"); Console.WriteLine("InstanceName: {0}", queryObj["InstanceName"]); } } catch (ManagementException e) { MessageBox.Show("An error occurred while querying for WMI data: " + e.Message); } } } } 
+5
source

Here is an example of a listing of 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; } } 

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

The API call is 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<IEnumerable<ConnectedDeviceDefinition>> 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<ConnectedDeviceDefinition>(); 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<IEnumerable<ConnectedDeviceDefinition>> GetConnectedDeviceDefinitions(FilterDeviceDefinition deviceDefinition) { return Task.Run<IEnumerable<ConnectedDeviceDefinition>>(() => { //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 list devices in the same way on any platform, as shown 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); } 
0
source

Source: https://habr.com/ru/post/1384296/


All Articles