I've edited this question after gathering more information. I'm trying to use IOCP for communication through a serial port.
I open the serial port with the overlapped flag:
HANDLE hComm = CreateFile(strPortName,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
Then I associate "hComm" with the IOCP port.
I'm using ReadFile() to start an overlapped request to read from the serial port. I use ReadFile() like this:
bool bQueued = false;
DWORD dwRead;
BOOL bResult = ReadFile(GetCommHandle(), lpOverlapped->pbBufferData, lpOverlapped->dwBufferSize, &dwRead, (OVERLAPPED*)lpOverlapped);
if (bResult)
{
// It completed, but will still trigger the completion routine, so don't need to queue another one here.
bQueued = true;
}
else
{
DWORD dwError = GetLastError();
if (ERROR_IO_PENDING == dwError)
{
bQueued = true;
}
else
{
LogQueueReceiveError(lpOverlapped, dwError);
ResetConnection();
}
}
The call to ReadFile() always returns immediately with a 'true' result so the IOCP request is queued. However, the operation only completes when the specified number of bytes have arrived at the serial port. In my code, the ReadFile() is called with the size of the receive buffer as the number of bytes to read (which is the way it's done when working with sockets).
If I change the number of bytes to read to a value of 1, then the operation completes as soon as data arrives at the port. Likewise, if I change the number of bytes to read to a value of 8, then the operation completes when the eighth bytes arrives at the port, etc.
If I don't know the number of bytes expected, how can I use IOCP with a serial port without reading a single byte at a time which seems really inefficient?