自定义HID的异步操作示例
2021-09-14
867
0
自定义HID的异步操作和同步类似,这里有两个关键的区别:
- 第一:CreateFile时wFlagsAndAttributes参数包括FILE_FLAG_OVERLAPPED标识。
- 第二:ReadFile,WriteFile必须的OVERLAPPED参数;
异步CreateFile打开HID示例:
HANDLE tmp_DeviceHandle = CreateFile( strDevicePath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPEDm//和同步的FILE_ATTRIBUTE_NORMAL参数不一样
NULL);
if (tmp_DeviceHandle == INVALID_HANDLE_VALUE)
{
CloseHandle(tmp_DeviceHandle);
continue;
}
异步ReadFile从自定义HID读数据
以下内容为截取并整理github hidapi的代码片段,详见:HidApi库来进行开发,下载地址是https://github.com/yigityuce/HidApi
OVERLAPPED ol;
memset(&ol, 0, sizeof(ol));
ol.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
isok = ReadFile(tmp_DeviceHandle, data, 64, &rtn, NULL);
if (!isok)
{
if (GetLastError() != ERROR_IO_PENDING)
{
/* ReadFile() has failed.
Clean up and return error. */
CancelIo(dev->device_handle);
dev->read_pending = FALSE;
goto end_of_function;
}
else
{
res = WaitForSingleObject(ol.hEvent, 500);
if (res != WAIT_OBJECT_0)
{
/* There was no data this time. Return zero bytes available,
but leave the Overlapped I/O running. */
return 0;
}
res = GetOverlappedResult(tmp_DeviceHandle, &ol, &rtn, TRUE/*wait*/);
}
}
异步WritelFile写数据到自定义HID
以下内容为截取并整理github hidapi的代码片段,详见:HidApi库来进行开发,下载地址是https://github.com/yigityuce/HidApi
OVERLAPPED ol;
memset(&ol, 0, sizeof(ol));
ol.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
isok = WritleFile(tmp_DeviceHandle, data, 64, &rtn, NULL);
if (!isok)
{
if (GetLastError() != ERROR_IO_PENDING)
{
}
else
{
res = WaitForSingleObject(ol.hEvent, 500);
if (res != WAIT_OBJECT_0)
{
/* There was no data this time. Return zero bytes available,
but leave the Overlapped I/O running. */
return 0;
}
}
}
HID人机交互QQ群:564808376
UAC音频QQ群:218581009
UVC相机QQ群:331552032
BOT&UASP大容量存储QQ群:258159197
STC-USB单片机QQ群:315457461
USB技术交流QQ群2:580684376
USB技术交流QQ群:952873936