Re: ReadFille error caused by USB sniffer
Specifically, we need to know if you're using overlapped I/O, completion
routines, anything like that.
Thanks for posting. Here's a rough example of the code, as you can see
it uses overlapped reads and WaitForSingleObject.
Thanks,
jh
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int UsbHidDevice::Read(char * outBuf, int timeout)
{
const int IDTechInputReportSize = 0x1fe; // This is a bit of a cheat,
but it is a constant
// determined by examining
// Capabilities.InputReportByteLength.
DWORD bytesRead;
DWORD Result;
HANDLE hEventObject;
OVERLAPPED HIDOverlapped;
hEventObject = CreateEvent((LPSECURITY_ATTRIBUTES)NULL, FALSE, TRUE,
"");
HIDOverlapped.hEvent = hEventObject;
HIDOverlapped.Offset = 0;
HIDOverlapped.OffsetHigh = 0;
unsigned char inBuf[DTechInputReportSize+1];
inBuf[0] = '\0';
outBuf[0] = '\0';
Result = ReadFile(m_usbHandle, inBuf, IDTechInputReportSize,
&bytesRead,
(LPOVERLAPPED) &HIDOverlapped);
Result = WaitForSingleObject(hEventObject, 1000);
int i = 0;
int j = 0;
switch (Result) {
case WAIT_OBJECT_0:
{
printf("\n\nWait_Object_0\n"); //####
int getOLRResult = GetOverlappedResult(m_usbHandle, &HIDOverlapped,
&bytesRead, TRUE);
if(!getOLRResult) {
printf("\n!get\n");
return 0;
}
else {
for(i = 0; i < IDTechInputReportSize; i++) {
outBuf[i] = 0;
}
i = 0;
while(inBuf[i] != '%') {
i++; //Throw all before the start sentinel.
}
while(i < IDTechInputReportSize)
{
switch(inBuf[i]) {
case 0: case 0xd0: case '%': case '?': case 0x0d:
break;
default:
outBuf[j] = inBuf[i];
j++;
break;
}
i++;
}
}
outBuf[j] = '\0';
break;
}
case WAIT_TIMEOUT:
CancelIo(m_usbHandle);
Sleep(1);
printf(" TO "); //####
break;
default:
CancelIo(m_usbHandle);
printf("default"); //####
break;
}
return strlen(outBuf);
}