if (WriteFile(comHandle, buffer, bytesWrite, &written, &ovReadWrite) == 0)
{
if ((error = GetLastError()) == ERROR_IO_PENDING)
ClearCommError(comHandle, &error, &queue);
else
written = bytesWrite;
} /* end of if */
bResult = WriteFile(port->m_hComm, // Handle to COMM Port
port->m_szWriteBuffer, // Pointer to message buffer in calling function
strlen((char*)port->m_szWriteBuffer), // Length of message to send
&BytesSent,// Where to store the number of bytes sent
&port->m_ov);// Overlapped structure
// deal with any error codes
if (!bResult)
{
DWORD dwError = GetLastError();
switch (dwError)
{
case ERROR_IO_PENDING:
{
// continue to GetOverlappedResults()
BytesSent = 0;
bWrite = false;
break;
}
default:
{
// all other error codes
port->ProcessErrorMessage("WriteFile()");
}
}
}
else
{
LeaveCriticalSection(&port->m_csCommunicationSync);
}
}
// end if(bWrite)
if (!bWrite)
{
bWrite = true;
bResult = GetOverlappedResult(port->m_hComm, // Handle to COM port
&port->m_ov, // Overlapped structure
&BytesSent, // Stores number of bytes sent
true);// Wait flag
// deal with the error code
if (!bResult)
{
port->ProcessErrorMessage("GetOverlappedResults() in WriteFile()");
}
} // end if (!bWrite)
// Verify that the data size send equals what we tried to send
if (BytesSent != strlen((char*)port->m_szWriteBuffer))
{
printf("WARNING: WriteFile() error.. Bytes Sent: %d; Message Length:%d\n", BytesSent, strlen((char*)port->m_szWriteBuffer));
}
}