From: Martin H. <mh...@us...> - 2005-04-29 14:45:13
|
Update of /cvsroot/opengtoolkit/serial/c_source In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16738/c_source Modified Files: lvserial.cpp Log Message: Improved error handling if one of the OS functions for the serial port has failed. In this case we call the ClearCommError function to get additional information. So we are now able to decide whether the function has failed because there was an I/O error or an invalid parameter. Index: lvserial.cpp =================================================================== RCS file: /cvsroot/opengtoolkit/serial/c_source/lvserial.cpp,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** lvserial.cpp 12 Apr 2005 08:31:36 -0000 1.5 --- lvserial.cpp 29 Apr 2005 14:45:01 -0000 1.6 *************** *** 41,44 **** --- 41,47 ---- //function prototypes + inline long __CommErrorFlagToCode(long ulErrFlags); + inline long __CommClearError(PCOMM pComm, long ulDefaultError); + //helper function for the serial port read and write operations. Before these //functions can be used the parameters should be validated, the serial port *************** *** 134,137 **** --- 137,227 ---- } + /* __CommErrorFlagToCode + * + * This function translates the error flags (got from ClearCommError) into an + * error code + * + * parameter + * ulErrFlags error flags + * + * return + * error code + */ + inline long __CommErrorFlagToCode(unsigned long ulErrFlags) + { + long lResult; + + if (ulErrFlags != 0) + { + if (ulErrFlags & COMM_ERRF_FRAME) + lResult = COMM_ERR_FRAMING; + else if (ulErrFlags & COMM_ERRF_RXPARITY) + lResult = COMM_ERR_PARITY; + else if (ulErrFlags & COMM_ERRF_OVERRUN) + lResult = COMM_ERR_OVERRUN; + else if (ulErrFlags & COMM_ERRF_RXOVER) + lResult = COMM_ERR_OVERFLOW; + else if (ulErrFlags & COMM_ERRF_BREAK) + lResult = COMM_ERR_BREAK; + else if (ulErrFlags & COMM_ERR_TXBUFFERFULL) + lResult = COMM_ERR_TXBUFFERFULL; + else if (ulErrFlags & COMM_ERRF_PTO) + lResult = COMM_ERR_PTO; + else if (ulErrFlags & COMM_ERRF_DNS) + lResult = COMM_ERR_DNS; + else if (ulErrFlags & COMM_ERRF_OOP) + lResult = COMM_ERR_OOP; + else if (ulErrFlags & COMM_ERRF_MODE) + lResult = COMM_ERR_MODE; + else if (ulErrFlags & COMM_ERRF_IOE) + lResult = COMM_ERR_IOE; + else + lResult = COMM_ERR_IOE; + } + else + lResult = COMM_SUCCESS; + + return lResult; + } + + /* __CommClearError + * + * This function calls ClearCommError and if there was an error it returns + * the appropriate error code. If there is no error the function returns the + * default error code. + * + * parameter + * pComm pointer to the serial port data structure + * ulDefaultError default error code + * + * return + * error code + */ + inline long __CommClearError(PCOMM pComm, long lDefaultError) + { + unsigned long ulErrFlags; + long lResult; + long lErr; + + lErr = GetLastError(); + + if (!ClearCommError(pComm->hC, &ulErrFlags,NULL)) + { + _ASSERT(0); + return COMM_ERR_SYSTEM; + } + else if (ulErrFlags != 0) + { + lResult = __CommErrorFlagToCode(ulErrFlags); + } + else + { + lResult = lDefaultError; + SetLastError(lErr); + } + + return lResult; + } + /* CommSplitPortAndOptions * *************** *** 472,475 **** --- 562,566 ---- //now, all is done and the serial port can be used. Before we return, //set some additional defaults for this port. + //flush the serial port buffer if ((lResult = CommTimeout(*phComm, 10000)) != 0) { *************** *** 954,958 **** { // WriteFile failed, but isn't delayed. Report pError and abort. ! lFnkRes = COMM_ERR_SYSTEM; } else --- 1045,1049 ---- { // WriteFile failed, but isn't delayed. Report pError and abort. ! lFnkRes = __CommClearError(pComm, COMM_ERR_SYSTEM); } else *************** *** 1017,1021 **** PCOMM pComm; //pointer to the serial port COMM structure OVERLAPPED osWrite = {0}; //overlapped structure - unsigned long ulErrFlags; //error flags (used for ClearCommError) long lFnkRes = COMM_SUCCESS; //function result of this function unsigned long ulBytesWritten = 0; //number of bytes written --- 1108,1111 ---- *************** *** 1088,1100 **** if (lFnkRes == COMM_ERR_SYSTEM) { ! if (!ClearCommError(pComm->hC, &ulErrFlags,NULL)) ! { ! _ASSERT(0); ! } ! else ! { ! if (ulErrFlags & COMM_ERRF_TXFULL) ! lFnkRes = COMM_ERR_TXBUFFERFULL; ! } } --- 1178,1182 ---- if (lFnkRes == COMM_ERR_SYSTEM) { ! lFnkRes = __CommClearError(pComm, COMM_ERR_SYSTEM); } *************** *** 1228,1232 **** OVERLAPPED osWrite = {0}; //overlapped structure for the write operation OVERLAPPED osReader = {0}; //overlapped structure for the read operation - unsigned long ulErrFlags; //error flags (used for ClearCommError) long lResult; //function return code unsigned long ulTxCount = 0; //number of bytes written --- 1310,1313 ---- *************** *** 1320,1332 **** if (lResult == COMM_ERR_SYSTEM) { ! if (!ClearCommError(pComm->hC, &ulErrFlags,NULL)) ! { ! _ASSERT(0); ! } ! else ! { ! if (ulErrFlags & COMM_ERRF_TXFULL) ! lResult = COMM_ERR_TXBUFFERFULL; ! } } --- 1401,1405 ---- if (lResult == COMM_ERR_SYSTEM) { ! lResult = __CommClearError(pComm, COMM_ERR_SYSTEM); } *************** *** 1495,1499 **** PCOMM pComm; //pointer to the serial port COMM structure unsigned long ulResult; //function return code - unsigned long ulErrFlags; //error flags of ClearCommError OVERLAPPED osReader = {0}; //overlapped structure unsigned long ulBytesRead; //number of bytes read --- 1568,1571 ---- *************** *** 1634,1654 **** //can set any of our own error codes. if (ulResult == COMM_ERR_SYSTEM) ! { ! if (!ClearCommError(pComm->hC, &ulErrFlags,NULL)) ! { ! _ASSERT(0); ! } ! else ! { ! if (ulErrFlags & COMM_ERRF_FRAME) ! ulResult = COMM_ERR_FRAMING; ! else if (ulErrFlags & COMM_ERRF_RXPARITY) ! ulResult = COMM_ERR_PARITY; ! else if (ulErrFlags & COMM_ERRF_OVERRUN) ! ulResult = COMM_ERR_OVERRUN; ! else if (ulErrFlags & COMM_ERRF_RXOVER) ! ulResult = COMM_ERR_OVERFLOW; ! } ! } if (!ReleaseSemaphore(pComm->hInternalLock, 1, NULL)) --- 1706,1710 ---- //can set any of our own error codes. if (ulResult == COMM_ERR_SYSTEM) ! ulResult = __CommClearError(pComm, COMM_ERR_SYSTEM); if (!ReleaseSemaphore(pComm->hInternalLock, 1, NULL)) *************** *** 1693,1708 **** //and clear the break condition. if (!SetCommBreak(pComm->hC)) ! { ! _ASSERT(0); ! return COMM_ERR_SYSTEM; ! } Sleep(ulMilliseconds); if (!ClearCommBreak(pComm->hC)) ! { ! _ASSERT(0); ! return COMM_ERR_SYSTEM; ! } return COMM_SUCCESS; --- 1749,1758 ---- //and clear the break condition. if (!SetCommBreak(pComm->hC)) ! return __CommClearError(pComm, COMM_ERR_SYSTEM); Sleep(ulMilliseconds); if (!ClearCommBreak(pComm->hC)) ! return __CommClearError(pComm, COMM_ERR_SYSTEM); return COMM_SUCCESS; *************** *** 1736,1751 **** _ASSERT(pComm); ! //when changing the queue sizes, we also have to adjust the //size of the Xon and Xoff limits. This is necesseray if one ! //of the handshake modes are enables. if (!SetupComm(pComm->hC, ulInQueue, ulOutQueue)) ! return COMM_ERR_ARGUMENT; if (!GetCommState(pComm->hC, &dcb)) ! { ! _ASSERT(0); ! return COMM_ERR_SYSTEM; ! } dcb.XonLim = (WORD)((ulInQueue * 2) / 3); --- 1786,1798 ---- _ASSERT(pComm); ! //when changing the queue sizes, we also have to adjust the //size of the Xon and Xoff limits. This is necesseray if one ! //of the handshake modes are enables. if (!SetupComm(pComm->hC, ulInQueue, ulOutQueue)) ! return __CommClearError(pComm, COMM_ERR_ARGUMENT); if (!GetCommState(pComm->hC, &dcb)) ! return __CommClearError(pComm, COMM_ERR_SYSTEM); dcb.XonLim = (WORD)((ulInQueue * 2) / 3); *************** *** 1753,1757 **** if (!SetCommState(pComm->hC, &dcb)) ! return COMM_ERR_ARGUMENT; return COMM_SUCCESS; --- 1800,1804 ---- if (!SetCommState(pComm->hC, &dcb)) ! return __CommClearError(pComm, COMM_ERR_ARGUMENT); return COMM_SUCCESS; *************** *** 1792,1799 **** //flush the serial port buffer if (!PurgeComm(pComm->hC, ulFlags)) ! { ! _ASSERT(0); ! return COMM_ERR_SYSTEM; ! } return COMM_SUCCESS; --- 1839,1843 ---- //flush the serial port buffer if (!PurgeComm(pComm->hC, ulFlags)) ! return __CommClearError(pComm, COMM_ERR_SYSTEM); return COMM_SUCCESS; *************** *** 1884,1888 **** } ! return COMM_SUCCESS; } --- 1928,1932 ---- } ! return __CommErrorFlagToCode(*pulErrorFlags); } *************** *** 1932,1936 **** *pulBytesAtPort = stat.cbInQue; ! return COMM_SUCCESS; } --- 1976,1980 ---- *pulBytesAtPort = stat.cbInQue; ! return __CommErrorFlagToCode(ulErrorFlags); } *************** *** 1969,1976 **** if (!GetCommModemStatus(pComm->hC, pulModemStatus)) { - _ASSERT(0); if (pulModemStatus != NULL) *pulModemStatus = 0; ! return COMM_ERR_SYSTEM; } --- 2013,2019 ---- if (!GetCommModemStatus(pComm->hC, pulModemStatus)) { if (pulModemStatus != NULL) *pulModemStatus = 0; ! return __CommClearError(pComm,COMM_ERR_SYSTEM); } *************** *** 2012,2019 **** if (!GetCommModemStatus(pComm->hC, &ulModemStatus)) { - _ASSERT(0); if (pfCTS != NULL) pfCTS = FALSE; ! return COMM_ERR_SYSTEM; } --- 2055,2061 ---- if (!GetCommModemStatus(pComm->hC, &ulModemStatus)) { if (pfCTS != NULL) pfCTS = FALSE; ! return __CommClearError(pComm,COMM_ERR_SYSTEM); } *************** *** 2057,2064 **** if (!GetCommModemStatus(pComm->hC, &ulModemStatus)) { - _ASSERT(0); if (pfDSR != NULL) *pfDSR = FALSE; ! return COMM_ERR_SYSTEM; } --- 2099,2105 ---- if (!GetCommModemStatus(pComm->hC, &ulModemStatus)) { if (pfDSR != NULL) *pfDSR = FALSE; ! return __CommClearError(pComm,COMM_ERR_SYSTEM); } *************** *** 2102,2109 **** if (!GetCommModemStatus(pComm->hC, &ulModemStatus)) { - _ASSERT(0); if (pfRING != NULL) *pfRING = FALSE; ! return COMM_ERR_SYSTEM; } --- 2143,2149 ---- if (!GetCommModemStatus(pComm->hC, &ulModemStatus)) { if (pfRING != NULL) *pfRING = FALSE; ! return __CommClearError(pComm,COMM_ERR_SYSTEM); } *************** *** 2147,2154 **** if (!GetCommModemStatus(pComm->hC, &ulModemStatus)) { - _ASSERT(0); if (pfRLSD != NULL) *pfRLSD = FALSE; ! return COMM_ERR_SYSTEM; } --- 2187,2193 ---- if (!GetCommModemStatus(pComm->hC, &ulModemStatus)) { if (pfRLSD != NULL) *pfRLSD = FALSE; ! return __CommClearError(pComm,COMM_ERR_SYSTEM); } *************** *** 2184,2191 **** if (!EscapeCommFunction(pComm->hC, SETXOFF)) ! { ! _ASSERT(0); ! return COMM_ERR_SYSTEM; ! } return COMM_SUCCESS; --- 2223,2227 ---- if (!EscapeCommFunction(pComm->hC, SETXOFF)) ! return __CommClearError(pComm,COMM_ERR_SYSTEM); return COMM_SUCCESS; *************** *** 2218,2225 **** if (!EscapeCommFunction(pComm->hC, SETXON)) ! { ! _ASSERT(0); ! return COMM_ERR_SYSTEM; ! } return COMM_SUCCESS; --- 2254,2258 ---- if (!EscapeCommFunction(pComm->hC, SETXON)) ! return __CommClearError(pComm,COMM_ERR_SYSTEM); return COMM_SUCCESS; *************** *** 2252,2259 **** if (!EscapeCommFunction(pComm->hC, SETRTS)) ! { ! _ASSERT(0); ! return COMM_ERR_SYSTEM; ! } return COMM_SUCCESS; --- 2285,2289 ---- if (!EscapeCommFunction(pComm->hC, SETRTS)) ! return __CommClearError(pComm,COMM_ERR_SYSTEM); return COMM_SUCCESS; *************** *** 2286,2293 **** if (!EscapeCommFunction(pComm->hC, CLRRTS)) ! { ! _ASSERT(0); ! return COMM_ERR_SYSTEM; ! } return COMM_SUCCESS; --- 2316,2320 ---- if (!EscapeCommFunction(pComm->hC, CLRRTS)) ! return __CommClearError(pComm,COMM_ERR_SYSTEM); return COMM_SUCCESS; *************** *** 2320,2327 **** if (!EscapeCommFunction(pComm->hC, SETDTR)) ! { ! _ASSERT(0); ! return COMM_ERR_SYSTEM; ! } return COMM_SUCCESS; --- 2347,2351 ---- if (!EscapeCommFunction(pComm->hC, SETDTR)) ! return __CommClearError(pComm,COMM_ERR_SYSTEM); return COMM_SUCCESS; *************** *** 2354,2361 **** if (!EscapeCommFunction(pComm->hC, CLRDTR)) ! { ! _ASSERT(0); ! return COMM_ERR_SYSTEM; ! } return COMM_SUCCESS; --- 2378,2382 ---- if (!EscapeCommFunction(pComm->hC, CLRDTR)) ! return __CommClearError(pComm,COMM_ERR_SYSTEM); return COMM_SUCCESS; *************** *** 2389,2396 **** if (!EscapeCommFunction(pComm->hC, SETBREAK)) ! { ! _ASSERT(0); ! return COMM_ERR_SYSTEM; ! } return COMM_SUCCESS; --- 2410,2414 ---- if (!EscapeCommFunction(pComm->hC, SETBREAK)) ! return __CommClearError(pComm,COMM_ERR_SYSTEM); return COMM_SUCCESS; *************** *** 2424,2431 **** if (!EscapeCommFunction(pComm->hC, CLRBREAK)) ! { ! _ASSERT(0); ! return COMM_ERR_SYSTEM; ! } return COMM_SUCCESS; --- 2442,2446 ---- if (!EscapeCommFunction(pComm->hC, CLRBREAK)) ! return __CommClearError(pComm,COMM_ERR_SYSTEM); return COMM_SUCCESS; *************** *** 2469,2476 **** if (!GetCommState(pComm->hC, &dcb)) ! { ! _ASSERT(0); ! return COMM_ERR_SYSTEM; ! } dcb.BaudRate = ulBaudRate; --- 2484,2488 ---- if (!GetCommState(pComm->hC, &dcb)) ! return __CommClearError(pComm,COMM_ERR_SYSTEM); dcb.BaudRate = ulBaudRate; *************** *** 2495,2499 **** if (!SetCommState(pComm->hC, &dcb)) ! return COMM_ERR_ARGUMENT; return COMM_SUCCESS; --- 2507,2511 ---- if (!SetCommState(pComm->hC, &dcb)) ! return __CommClearError(pComm,COMM_ERR_ARGUMENT); return COMM_SUCCESS; *************** *** 2534,2541 **** if (!GetCommState(pComm->hC, &dcb)) ! { ! _ASSERT(0); ! return COMM_ERR_SYSTEM; ! } switch (usFlowControl) --- 2546,2550 ---- if (!GetCommState(pComm->hC, &dcb)) ! return __CommClearError(pComm,COMM_ERR_SYSTEM); switch (usFlowControl) *************** *** 2582,2586 **** if (!SetCommState(pComm->hC, &dcb)) ! return COMM_ERR_ARGUMENT; return COMM_SUCCESS; --- 2591,2595 ---- if (!SetCommState(pComm->hC, &dcb)) ! return __CommClearError(pComm,COMM_ERR_ARGUMENT); return COMM_SUCCESS; *************** *** 2638,2645 **** if (!GetCommState(pComm->hC, &dcb)) ! { ! _ASSERT(0); ! return COMM_ERR_SYSTEM; ! } if (ulFlags & COMM_CTRL_OUTXCTSFLOW) --- 2647,2651 ---- if (!GetCommState(pComm->hC, &dcb)) ! return __CommClearError(pComm,COMM_ERR_SYSTEM); if (ulFlags & COMM_CTRL_OUTXCTSFLOW) *************** *** 2711,2715 **** if (!SetCommState(pComm->hC, &dcb)) ! return COMM_ERR_ARGUMENT; return COMM_SUCCESS; --- 2717,2721 ---- if (!SetCommState(pComm->hC, &dcb)) ! return __CommClearError(pComm,COMM_ERR_ARGUMENT); return COMM_SUCCESS; *************** *** 2802,2817 **** { if (!GetCommState(pComm->hC, &dcb)) ! { ! _ASSERT(0); ! return COMM_ERR_SYSTEM; ! } dcb.EofChar = *pcTermCharacter; if (!SetCommState(pComm->hC, &dcb)) ! { ! _ASSERT(0); ! return COMM_ERR_SYSTEM; ! } } --- 2808,2817 ---- { if (!GetCommState(pComm->hC, &dcb)) ! return __CommClearError(pComm,COMM_ERR_SYSTEM); dcb.EofChar = *pcTermCharacter; if (!SetCommState(pComm->hC, &dcb)) ! return __CommClearError(pComm,COMM_ERR_SYSTEM); } *************** *** 2951,2966 **** { if (!GetCommState(pComm->hC, &dcb)) ! { ! _ASSERT(0); ! return COMM_ERR_SYSTEM; ! } dcb.EofChar = *pcTermination; if (!SetCommState(pComm->hC, &dcb)) ! { ! _ASSERT(0); ! return COMM_ERR_SYSTEM; ! } } --- 2951,2960 ---- { if (!GetCommState(pComm->hC, &dcb)) ! return __CommClearError(pComm,COMM_ERR_SYSTEM); dcb.EofChar = *pcTermination; if (!SetCommState(pComm->hC, &dcb)) ! return __CommClearError(pComm,COMM_ERR_SYSTEM); } *************** *** 3156,3160 **** if (!SetCommTimeouts(pComm->hC, &Timeouts)) ! return COMM_ERR_ARGUMENT; return COMM_SUCCESS; --- 3150,3154 ---- if (!SetCommTimeouts(pComm->hC, &Timeouts)) ! return __CommClearError(pComm,COMM_ERR_ARGUMENT); return COMM_SUCCESS; *************** *** 3213,3217 **** if (!SetCommTimeouts(pComm->hC, &Timeouts)) ! return COMM_ERR_ARGUMENT; return COMM_SUCCESS; --- 3207,3211 ---- if (!SetCommTimeouts(pComm->hC, &Timeouts)) ! return __CommClearError(pComm,COMM_ERR_ARGUMENT); return COMM_SUCCESS; *************** *** 3320,3327 **** //the SetCommState function. if (!GetCommState(pComm->hC, &dcb)) ! { ! _ASSERT(0); ! return COMM_ERR_SYSTEM; ! } //At first, we try to detect the simple form with handshake --- 3314,3318 ---- //the SetCommState function. if (!GetCommState(pComm->hC, &dcb)) ! return __CommClearError(pComm,COMM_ERR_SYSTEM); //At first, we try to detect the simple form with handshake *************** *** 3461,3465 **** //finally we set the new configuration if (!SetCommState(pComm->hC, &dcb)) ! return COMM_ERR_ARGUMENT; return COMM_SUCCESS; --- 3452,3456 ---- //finally we set the new configuration if (!SetCommState(pComm->hC, &dcb)) ! return __CommClearError(pComm,COMM_ERR_ARGUMENT); return COMM_SUCCESS; *************** *** 3698,3702 **** //set the serial port configuration if (!SetCommState(pComm->hC, &dcb)) ! return COMM_ERR_ARGUMENT; return lResult; --- 3689,3693 ---- //set the serial port configuration if (!SetCommState(pComm->hC, &dcb)) ! return __CommClearError(pComm,COMM_ERR_ARGUMENT); return lResult; |