wdm bulkusb or wdf usbsamp 问题,我将它改了,有时不会读到数据的大小为0
遇到一个问题,ReadFile的第三个参数(需要读取多长字节)小于设备实际返回长度的话,ReadFile函数返回的实际读取长度将是正确的(等于第三个参数的大小),但是当Usb 第3个参数大于实际返回的第4个参数时,则返回的实际读取长度始终为0,似乎是ReadFile就无法读取usb返回数据了。
/*****************************************************************************************
//函数功能:该函数的功能是通过异步的方式从端点6读取图像数据
*****************************************************************************************/
DWORD USB_Read(HANDLE handle, // Input 实参hPipe
PVOID pData, // Output 实参pBuff
DWORD dwLen, // Input //实参 size =一副图像的大小
PDWORD pLength, // Output //注意:实际传输的大小
DWORD dwMilliseconds) // Input 延迟
{
BOOL bResult;
DWORD nBytesRead;
OVERLAPPED gOverlapped;
DWORD dwResult;
dwResult = E_FALSE;
// set up overlapped structure fields
gOverlapped.Internal = 0;
gOverlapped.InternalHigh = 0;
gOverlapped.Offset = 0;
gOverlapped.OffsetHigh = 0;
gOverlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if(pLength != NULL)*pLength = 0;
// attempt an asynchronous read operation
bResult = ReadFile(handle,pData,dwLen,&nBytesRead,&gOverlapped);
//现在的问题出在这里,由于某种原因,实际返回的字节<=dwlen ,当实际返回的字节=dwlen时,结果是正确的,能够正确读到数据,但是当实际返回的字节<dwlen 时,nBytesRead=0,即读不到任何数据.(此时bResult=1 nBytesRead=0)
//读是通过调用ReadFile函数异步调用的
if(!bResult)
{
// deal with the error code
switch (GetLastError())
{
case ERROR_HANDLE_EOF:
{
// we have reached the end of the file
// during the call to ReadFile
break;
}
case ERROR_IO_PENDING:
{
// asynchronous i/o is still in progress
switch(WaitForSingleObject(gOverlapped.hEvent, dwMilliseconds))
{
case WAIT_OBJECT_0:
// check on the results of the asynchronous read
// and update the nBytesRead...
bResult = GetOverlappedResult(handle, &gOverlapped,
&nBytesRead, FALSE);
if(bResult)
{
if(pLength != NULL)
*pLength = nBytesRead;
dwResult = E_OK;
}//end if else
break;
case WAIT_TIMEOUT:
CancelIo(handle);
break;
default: //switch(WaitForSingleObject(gOverlapped.hEvent, dwMilliseconds))
CancelIo(handle);
break;
}//end switch
break; //switch (GetLastError()) case ERROR_IO_PENDING
}//end case
default:
CancelIo(handle);
break;
}//end switch
}
else
{
if(pLength != NULL)
*pLength = nBytesRead;
dwResult = E_OK;
}//end if else
ResetEvent(gOverlapped.hEvent); //重设事件
CloseHandle(gOverlapped.hEvent);
return dwResult;
}
请高手帮忙看下,理论上,当nBytesRead 应该为实际读到的字节啊,怎么为0,难道当实际读到字节为0时,就不读吗,在这种情况下,如果我吧dwlen 改小,小于实际返回的字节时,就可以读到数据,此时nBytesRead =dwlen
不知道什么原因了,迷茫中,请大家帮忙看看。