当前位置:首页 > Windows程序 > 正文

C#与USBHID间的通信

2021-03-21 Windows程序

标签:

原文: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(ref HIDGuid,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_PRESENT = 0x00000002,

            DIGCF_ALLCLASSES = 0x00000004,

            DIGCF_PROFILE = 0x00000008,

            DIGCF_DEVICEINTERFACE = 0x00000010

        }

 

3.获取接口信息。

        ///

        /// The SetupDiEnumDeviceInterfaces function enumerates the device interfaces that are contained in a device information set.

        ///

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/64908.html