Readfile on HID sometimes gives wrong values
Question
I have a connection for a WiiMote
handle = CreateFile(didetail->DevicePath, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if(handle != INVALID_HANDLE_VALUE) {
opened = true;
readReportEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
memset(&readOverlapped, 0, sizeof(readOverlapped));
readOverlapped.hEvent = readReportEvent;
readOverlapped.Offset = 0;
readOverlapped.OffsetHigh = 0;
writeReportEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
memset(&writeOverlapped, 0, sizeof(writeOverlapped));
writeOverlapped.hEvent = readReportEvent;
writeOverlapped.Offset = 0;
writeOverlapped.OffsetHigh = 0;
}
I have a thread which always read this handle for new messages:
while(opened && readThreadNextStatus){
memset (readBuff, 0, 22);
BYTE* ptrbuff = new BYTE[22];
int readfile = ReadFile(handle, readBuff, reportLength, NULL, &readOverlapped);
if(readfile == 0 && GetLastError() == ERROR_IO_PENDING){
DWORD waitError;
do
{
waitError = WaitForSingleObject(readReportEvent, timeout);
} while (waitError == WAIT_TIMEOUT && opened && readThreadNextStatus);
if(opened && readThreadNextStatus){
DWORD read = 0;
if(waitError == WAIT_OBJECT_0){
GetOverlappedResult(handle, &readOverlapped, &read, TRUE);
}
ResetEvent(readReportEvent);
memcpy(ptrbuff, readBuff, 22);
cout << "Read: ";
coutHex(ptrbuff);
}
}
}
My write function:
if(opened){
if(!WriteFile(handle, buff, reportLength, NULL, &writeOverlapped)){
if(GetLastError() != ERROR_IO_PENDING){
close();
}
}
WaitForSingleObject(writeReportEvent, timeout);
DWORD write = 0;
GetOverlappedResult(handle, &writeOverlapped, &write, TRUE);
ResetEvent(writeReportEvent);
}
cout << "Write: ";
coutHex(buff);
Console output:
Connection established
Write: 15- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0-
Read: 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0-
Read: 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0-
Write: 15- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0-
Read: 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0-
Write: 15- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0-
Read: 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0-
Read: 20- 0- 0-10- 0- 0-49-ff-ff-ff-ff-ff-ff-ff-ff-ff-ff-ff-ff-ff-ff-ff-
Write: 15- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0-
Read: 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0-
Write: 15- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0
Read: 0- 0-
0- 0-
- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0- 0-
coutHex always prints the received data in hex format. Sometimes I get the right data, but sometimes the array is loaded only with 00 00 00 00 00 00 00 00 00 ... 00 00
I experienced that, when I make a write, I always get back a report, which contains just 00's and this appears before my write function make the write on the console output.
I was desperate, so I tried out this:
do
{
waitError = WaitForSingleObject(readReportEvent, timeout);
Sleep(500);
} while (waitError == WAIT_TIMEOUT && opened && readThreadNextStatus);
I don't know why, but now it works(not fine, because it has 500ms delay).
What do you think? Maybe ReadFile and WriteFile don't work concurrently?
What causes this? Did I miss something?
Solution
There are some additional problems:
Create manual reset event (CreateEvent(NULL, TRUE, FALSE, NULL)) instead auto-reset event (CreateEvent(NULL, FALSE, FALSE, NULL)).
Check if ReadFile returned FALSE and the value of GetLastError is ERROR_IO_PENDING, and in this case wait for the event (WaitForSingleObject).
If WaitForSingleObject returns WAIT_OBJECT_0 then you call GetOverlappedResult.
Pass lpNumberOfBytesRead is not necessary if you use GetOverlappedResult because this function returns this value too.
OTHER TIPS
You are not supposed to repeat ReadFile
after timeout, but to WaitForSingleObject
again. You still have pending read. Poor man's loop (you probably should refine it, so that user can abort it):
DWORD waitError;
do
{
waitError = WaitForSingleObject(ReportEvent, timeout);
}
while (waitError == WAIT_TIMEOUT);