Added sanity check if there is a command active but no data can be read on the serial line

This commit is contained in:
Gerhard Hoffmann 2024-10-15 10:51:20 +02:00
parent 0f7ab3c71c
commit 209132054f

View File

@ -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";
}
}
}