关闭设备
关闭设备比较简单,只需要直接使用函数CloseHandle即可
- void DeviceClose(HANDLE &handle)
-
-
{
-
-
CloseHandle(handle);
-
-
handle = INVALID_HANDLE_VALUE;
-
-
}
写数据
假设HID的Report大小为8字节,且第一字节为ID
- BOOL DeviceWrite(HANDLE handle, LPCVOID lpBuffer, DWORD dwSize)
-
-
{
-
-
BYTE wBuffer[8] = {0};
-
-
DWORD dwRet;
-
-
BOOL bRet;
-
-
wBuffer[0] = 0x01;
-
-
wBuffer[1] = 0x00;
-
-
memcpy(&wBuffer[2], lpBuffer, min(6, dwSize));
-
-
bRet = WriteFile(handle, wBuffer, 8, &dwRet, NULL);
-
-
return bRet;
-
-
}
读数据
- BOOL DeviceRead(HANDLE handle, LPVOID lpBuffer, DWORD dwSize)
-
-
{
-
-
BYTE rBuffer[8] = {0};
-
-
DWORD dwRet;
-
-
BOOL bRet;
-
-
rBuffer[0] = 0x01;
-
-
rBuffer[1] = 0xff;
-
-
bRet = WriteFile(handle, rBuffer, 8, &dwRet, NULL);
-
-
bRet &= ReadFile(handle, rBuffer, 8, &dwRet, NULL);
-
-
memcpy(lpBuffer, &rBuffer[1], min(7, dwSize));
-
-
return bRet;
-
-
}