From 209132054f94d56b9521b620a38b42036cd6863e Mon Sep 17 00:00:00 2001 From: Gerhard Hoffmann Date: Tue, 15 Oct 2024 10:51:20 +0200 Subject: [PATCH] Added sanity check if there is a command active but no data can be read on the serial line --- src/com.cpp | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/com.cpp b/src/com.cpp index d77c303..91f2982 100644 --- a/src/com.cpp +++ b/src/com.cpp @@ -21,7 +21,12 @@ void T_com::writeToSerial(const QByteArray &data, uint16_t sendLength) sendBuffer=data; sendLen=sendLength; - if (readCmds.size() == 0) { // no other read command active + // logic: exactly one command is sent to DC. no other command can be sent + // until the respond has been read from the serial line. + + static int errorCnt = 0; + + if (readCmds.size() == 0) { // no other read command active: ok if (CatSerial->isOpen()) { if (CatSerial->error() != QSerialPort::NoError) { @@ -53,14 +58,32 @@ void T_com::writeToSerial(const QByteArray &data, uint16_t sendLength) return; } } - readCmds.append(data.constData()[2]); + + // save last command sent. + readCmds.append(data.constData()[2]); // 2: index of the last command + } else { qCritical() << __func__ << ":" << __LINE__ << "ERROR sending" << data.toHex(':') << "port is not open"; } } else { - qCritical() << __func__ << "" << __LINE__ << "ERROR detected active read cmd" << readCmds[0]; - readCmds.clear(); + qCritical() << __func__ << "" << __LINE__ << "ERROR detected active read cmd" << readCmds[0]; + if (CatSerial->isOpen()) { + int availableBytes = CatSerial->bytesAvailable(); + qCritical() << __func__ << "" << __LINE__ << "ERROR available bytes" << availableBytes; + if (availableBytes == 0) { + errorCnt += 1; + if (errorCnt > 100) { + readCmds.clear(); + errorCnt = 0; + } + } else { + errorCnt = 0; + } + } else { + qCritical() << __func__ << ":" << __LINE__ + << "ERROR sending" << data.toHex(':') << "port is not open"; + } } }