c# hid usb 卡死_C#与USB HID间的通信

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值