可以使用以下步骤来打开和读取设备:
引用HID
API
库(或者你使用的语言中的HID库)。
打开设备并获取设备句柄。
读取设备数据并将其存储在缓冲区中。
关闭设备。
下面是一个C#的示例代码:
using System;
using System.Runtime.InteropServices;
class Program
static void Main(string[] args)
// Step 1: 引用HID API库
[DllImport("hid.dll", SetLastError = true)]
static extern bool HidD_GetAttributes(IntPtr HidDeviceObject, ref HIDD_ATTRIBUTES Attributes);
[DllImport("hid.dll", SetLastError = true)]
static extern bool HidD_GetSerialNumberString(IntPtr HidDeviceObject, IntPtr Buffer, int BufferLength);
[DllImport("hid.dll", SetLastError = true)]
static extern bool HidD_GetPreparsedData(IntPtr HidDeviceObject, ref IntPtr PreparsedData);
[DllImport("hid.dll", SetLastError = true)]
static extern bool HidD_FreePreparsedData(IntPtr PreparsedData);
[DllImport("hid.dll", SetLastError = true)]
static extern bool HidP_GetCaps(IntPtr PreparsedData, ref HIDP_CAPS Capabilities);
[DllImport("hid.dll", SetLastError = true)]
static extern bool HidD_GetProductString(IntPtr HidDeviceObject, IntPtr Buffer, int BufferLength);
[DllImport("hid.dll", SetLastError = true)]
static extern bool HidD_GetManufacturerString(IntPtr HidDeviceObject, IntPtr Buffer, int BufferLength);
[DllImport("hid.dll", SetLastError = true)]
static extern bool HidD_SetFeature(IntPtr HidDeviceObject, byte[] lpReportBuffer, int ReportBufferLength);
[DllImport("hid.dll", SetLastError = true)]
static extern bool HidD_GetFeature(IntPtr HidDeviceObject, byte[] lpReportBuffer, int ReportBufferLength);
[DllImport("hid.dll", SetLastError = true)]
static extern bool HidD_SetOutputReport(IntPtr HidDeviceObject, byte[] lpReportBuffer, int ReportBufferLength);
[DllImport("hid.dll", SetLastError = true)]
static extern bool HidD_GetInputReport(IntPtr HidDeviceObject, byte[] lpReportBuffer, int ReportBufferLength);
[DllImport("hid.dll", SetLastError = true)]
static extern IntPtr CreateFile(
string fileName,
uint desiredAccess,
uint shareMode,
IntPtr securityAttributes,
uint creationDisposition,
uint flagsAndAttributes,
IntPtr templateFile);
[StructLayout(LayoutKind.Sequential