C#与USBHID接口的通讯相对于与串口间的通讯较为复杂,其中需要多次调用到Windows的一些API。其原理编者尚未全部理清,以下提供简单的USBHID通讯流程。(参考网友资料)
一、获取所有连接HID的设备信息。
1.通过一个空的GUID来获取HID的全局GUID。
Guid HIDGuid = Guid.Empty;
///
/// The
HidD_GetHidGuid routine returns the device interface GUID for
HIDClass devices.
///
/// a
caller-allocated GUID buffer that the routine uses to return the
device interface GUID for HIDClass devices.
[DllImport("hid.dll")]
private static extern
void HidD_GetHidGuid(ref Guid
HidGuid);
2.通过获取到的HID全局GUID来获取包含所有HID接口信息集合的句柄。
IntPtr HIDInfoSet= SetupDiGetClassDevs(refHIDGuid,0,IntPtr.Zero,DIGCF.DIGCF_PRESENT|DIGCF.DIGCF_DEVICEINTERFACE);
///
/// The
SetupDiGetClassDevs function returns a handle to a device
information set that contains requested device information elements
for a local machine.
///
/// GUID
for a device setup class or a device interface
class.
/// A
pointer to a NULL-terminated string that supplies the name of a PnP
enumerator or a PnP device instance identifier.
/// A
handle of the top-level window to be used for a user
interface
/// A
variable that specifies control options that
filter the device information elements that are added to the device
information set.
///
/// a
handle to a device information set
[DllImport("setupapi.dll", SetLastError = true)]
private static extern
IntPtr
SetupDiGetClassDevs(ref
Guid ClassGuid, uint Enumerator, IntPtr HwndParent, USBHIDEnum.DIGCF Flags);
相关枚举:
public enum DIGCF
{
DIGCF_DEFAULT = 0x00000001,
DIGCF_PR