我正在开发一个C#.NET 2.0应用程序,我需要扫描附加的HID。如何才能做到这一点?因为它是HID,Windows不会为其分配COM端口。我只需要以编程方式确定设备是否已连接。谢谢。
其他信息
当我将USB设备连接到计算机时,设备管理器中的人机接口设备下会显示两个条目。单击其属性可在各自的“详细信息”选项卡中生成此信息:
符合HID标准的设备 设备实例ID:HID \ VID_1795& PID_6004 \ 7& 2694D932& 0& 0000
USB人机接口设备 设备实例ID:USB \ VID_1795& PID_6004 \ B973000000EB0D00
答案 0 :(得分:6)
在WMI Code Creator中选择以下选项:
命名空间:root \ WMI
类:MSWmi_PnPInstanceNames
从结果框中选择InstanceNames
以获取以下代码:
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);
}
}
}
}
答案 1 :(得分:0)
以下是枚举Windows上的Hid设备的示例:
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;
}
}
此处的API调用:https://github.com/MelbourneDeveloper/Device.Net/blob/master/src/Hid.Net/Windows/HidAPICalls.cs
这是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;
}
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();
});
}
使用Hid.Net,您可以在以下任何平台上以相同方式枚举设备。参见full article。
var devices = await DeviceManager.Current.GetConnectedDeviceDefinitions(new FilterDeviceDefinition { VendorId = 0x1209, ProductId = 0x53C1 });
foreach (var device in devices)
{
Debug.WriteLine(device.DeviceId);
}