// The CommThread Function. // DWORD _stdcall TSerialPort::CommThread(LPVOID pParam) { // Cast the void pointer passed to the thread back to // a pointer of TSerialPort class TSerialPort *port = (TSerialPort*)pParam;
// Set the status variable in the dialog class to // TRUE to indicate the thread is running. port->m_bThreadAlive = true;
// Clear comm buffers at startup if (port->m_hComm) // check if the port is opened PurgeComm(port->m_hComm, PURGE_RXCLEAR PURGE_TXCLEAR PURGE_R ABO RT PURGE_TXABORT);
// begin forever loop. This loop will run as long as the thread is a live. for (;;) {
// Make a call to WaitCommEvent(). This call will return immedi tly
// because our port was created as an async port (FILE_FLAG_OVER APP ED // and an m_OverlappedStructerlapped structure specified). This cal l will cause the // m_OverlappedStructerlapped element m_OverlappedStruct.hEvent, whi ch is part of the m_hEventArray to // be placed in a non-signeled state if there are no bytes avail ble to be read, // or to a signeled state if there are bytes available. If this eve nt handle // is set to the non-signeled state, it will be set to signeled hen a // character arrives at the port.
if (!bResult) { // If WaitCommEvent() returns FALSE, process the last er or to dete rmin rmin // the reason.. switch (dwError = GetLastError()) { case ERROR_IO_PENDING: { // This is a normal return value if ther are no bytes // to read at the port. // Do nothing and continue break; } case 87: { // Under Windows NT, this value is retur ed for some reason. // I have not investigated why, but it i also a valid reply // Also do nothing and continue. break; } default: { // All other error codes indicate a seri us error has // occured. Process this error. port->ProcessErrorMessage("WaitCommEvent )"); break; } } } else { // If WaitCommEvent() returns TRUE, check to be sure the e are