if(hReadHandle!=INVALID_HANDLE_VALUE) //句柄有效
{
//则调用ReadFile函数请求9字节的报告数据
Result=ReadFile(hReadHandle,
ReadReportBuffer,
Capabilities.InputReportByteLength,
&NumberOfBytesRead,
&ReadOverlapped);
}
else
{
return false;
}
//如果函数返回失败,则可能是真的失败,也可能是IO挂起了
if(Result==FALSE)
{
//获取最后错误代码
LastError=GetLastError();
//看是否是真的IO挂起
if((LastError==ERROR_IO_PENDING)||(LastError==ERROR_SUCCESS))
{
//等待事件触发
Result = WaitForSingleObject(ReadOverlapped.hEvent,iTimeOut);//INFINITE
}
//否则,是函数调用时发生错误,显示错误代码
else
{
AfxMessageBox("IO挂起错误!",MB_OK);
}
}
//否则,函数返回成功
else
{
//等待事件触发
Result = WaitForSingleObject(ReadOverlapped.hEvent,iTimeOut);//INFINITE
}
switch (Result)
{
case WAIT_OBJECT_0:
{
break;
}
case WAIT_TIMEOUT:
{
/*API call: CancelIo
Cancels the ReadFile
Requires the device handle.
Returns non-zero on success.
*/
AfxMessageBox("超时!",MB_OK);
Result = CancelIo(hReadHandle);
return false;
break;
}
default:
{
//AfxMessageBox("未知!",MB_OK);
return false;
break;
}
}
来读取HID设备的报告
21.1 DO 02 5a .Z 1.9ms 2298.1.0
22 DO 02 5a 00 00 00 00 .Z.... 2us 2299.1.0
21.1 DI 02 02 04 06 08 01 06 01 09 ......... 58ms 2300.1.0
22 DI 02 02 04 06 08 01 06 01 09 ff 09 09 25 04 01 28 02 00 00 3f ............%..( 16us 2301.1.0
报告ID:02 输入报告
读1000多次!就会有一次读超时!求教这是为什么!
//设置HID设备驱动中的缓冲区大小
Result = HidD_SetNumInputBuffers(hDevHandle,USB_BUFFER_SIZE);
设置为128
|