// start forever loop. I use this type of loop because I // do not know at runtime how many loops this will have to // run. My solution is to start a forever loop and to // break out of it when I have processed all of the // data available. Be careful with this approach and // be sure your loop will exit. // My reasons for this are not as clear in this sample // as it is in my production code, but I have found this // solutiion to be the most efficient way to do this.
if (comstat.cbInQue == 0) { // break out when all bytes have been read break; }
if (bRead) { bResult = ReadFile(port->m_hComm, // Handl to COMM port &RXBuff, / RX Buffer Pointer 1, / Read one byte &BytesRead, / Stores number of bytes read &port->m_ov); / pointer to the m_ov structure // deal with the error code if (!bResult) { switch (dwError = GetLastError()) { case ERROR_IO_PENDING: { // asynchronous i/o is s ill in progress // Proceed on to GetOver appedResults(); bRead = false; break; } default: { // Another error has occ red. Process this error. port->ProcessErrorMessag ("ReadFile()"); break; } } } else { // ReadFile() returned complete. It is not neces ary to call GetOv erlappedResults() bRead = true; } } // close if (bRead)
if (!bRead) { bRead = true; bResult = GetOverlappedResult(port->m_hComm, // Handl to COMM port
&port->m_ov, // Overlapped structure
&BytesRead, // Stores number of bytes read
true); // Wait flag
// deal with the error code if (!bResult) { port->ProcessErrorMessage("GetOverlappedResults( in ReadFile()");