Re: problem with ReadFile using Overlapped I/O in VC++
I suppose you call SetCommTimeouts, though you ommitted it in your code.
Zero bytes read doesn't mean error. It's successful ReadFile call with zero
bytes read. GetLastError won't return a valid result in this case.
If you read the SetCommTimeouts documentation further, you'll see:
"A value of MAXDWORD, combined with zero values for both the
ReadTotalTimeoutConstant and ReadTotalTimeoutMultiplier members, specifies
that the read operation is to return immediately with the characters that
have already been received, even if no characters have been received."
<vanisathish@gmail.com> wrote in message
news:dd06d1ac-112b-4e09-b41d-3ed38e4b0fc8@e25g2000prg.googlegroups.com...
Hi,
I'm opening the com port using overlapped I/O, setting the Comm read
timeouts as MAX_DWORD,0,0.
And my Asynchronous read file logic is something like this,
1) Call ReadFile
2) If error == ERROR_IO_PENDING
::WaitForSingleObject(lpOverlapped->hEvent,300);
I'm trying to wait for the event to get signalled or timeout after 300
mSec. But i noticed that this function is returning 0x0 which means
WAIT_OBJECT_0 i.e. the event is signalled. But when i do
GetOverlappedResult to read the number of bytes read it is returning
as 0.
How can this happen?. The Event should get signalled only when the
ReadFile has some data in it?. What is the problem here.
I dont want the ReadIntervalTimeouts to be set to some values in order
to get this overlapped I/O working. If i had to use the
ReadIntervalTimeouts , then i can use the normal non-overlapped I/O.
Appreciate your help
Below is my sample code
CreateFile(arrPort, GENERIC_READ | GENERIC_WRITE, 0, //
exclusive access,NULL, // no security
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED, NULL);
CommTimeouts.ReadIntervalTimeout = MAX_DWORD
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
if((!::ReadFile(hCom,pBuf,256,pdwRead,lpOverlapped)) || (!*pdwRead))
{
lLastError = ::GetLastError();
// Overlapped operation in progress is not an actual error
if (lLastError != ERROR_IO_PENDING)
{
return -1;
}
// Wait for the overlapped operation to complete
dwovstatus= ::WaitForSingleObject(lpOverlapped->hEvent,dwTimeout);
switch (dwovstatus)
{
case WAIT_OBJECT_0:
if (!::GetOverlappedResult(hCom,lpOverlapped,pdwRead,FALSE))
{
return -1;
}
}