From 87bc5b5c1e6558bd011f28690651a2f907010e1e Mon Sep 17 00:00:00 2001 From: Gerhard Hoffmann Date: Wed, 16 Oct 2024 13:03:20 +0200 Subject: [PATCH] writeToSerial(): Always write next message. Set writeCount. Clear() serial line before sending. Flush() serial line after sending. --- src/com.cpp | 97 ++++++++++++++++++++++------------------------------- 1 file changed, 40 insertions(+), 57 deletions(-) diff --git a/src/com.cpp b/src/com.cpp index 859717a..4be8ae7 100644 --- a/src/com.cpp +++ b/src/com.cpp @@ -1,5 +1,6 @@ #include "com.h" #include +#include //#include "controlBus.h" ////////////////////////////////////////////////////////////////////////////////// @@ -24,67 +25,48 @@ void T_com::writeToSerial(const QByteArray &data, uint16_t sendLength) // 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) { - qCritical() << __func__ << "" << __LINE__ << "ERROR on serial line" << CatSerial->errorString(); - CatSerial->clearError(); - qCritical() << __func__ << "" << __LINE__ << "cleared error on serial line"; - } - - if (!CatSerial->atEnd()) { - qCritical() << QString("ERROR %1 bytes available on serial line before write").arg(CatSerial->bytesAvailable()); - qCritical() << CatSerial->readAll().toHex(':'); - CatSerial->clear(); - qCritical() << __func__ << "" << __LINE__ << "read all data from serial line"; - } - - QByteArray buffer(data); - - int bytesToWrite = buffer.size(); - while (bytesToWrite > 0 && !buffer.isEmpty()) { - int bytesWritten = CatSerial->write(buffer); - if (bytesWritten != -1) { - bytesToWrite -= bytesWritten; - buffer = buffer.right(bytesWritten); - } else { - qCritical() << __func__ << ":" << __LINE__ - << QString("ERROR %1 for sending %2").arg(CatSerial->errorString()).arg(data.toHex(':').constData()); - CatSerial->clearError(); - qCritical() << __func__ << ":" << __LINE__ << "cleared error on serial line. returning ..."; - return; - } - } - - // 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"; + if (CatSerial->isOpen()) { + if (CatSerial->error() != QSerialPort::NoError) { + qCritical() << __func__ << "" << __LINE__ << "ERROR on serial line" << CatSerial->errorString(); + CatSerial->clearError(); + qCritical() << __func__ << "" << __LINE__ << "cleared error on serial line"; } - } else { - qCritical() << __func__ << "" << __LINE__ << errorCnt << "ERROR about to send cmd" << QString("0x%1").arg((unsigned char)data.constData()[2], 0, 16); - qCritical() << __func__ << "" << __LINE__ << errorCnt << "ERROR detected active read cmds" << readCmds; - if (CatSerial->isOpen()) { - int availableBytes = CatSerial->bytesAvailable(); - qCritical() << __func__ << "" << __LINE__ << "ERROR available bytes" << availableBytes; - if (availableBytes == 0) { - errorCnt += 1; - if (errorCnt > 20) { - readCmds.clear(); - errorCnt = 0; - } + + if (!CatSerial->atEnd()) { + qCritical() << QString("ERROR %1 bytes available on serial line before write").arg(CatSerial->bytesAvailable()); + qCritical() << CatSerial->readAll().toHex(':'); + CatSerial->clear(); + qCritical() << __func__ << "" << __LINE__ << "read all data from serial line"; + } + + CatSerial->clear(); + + QByteArray buffer(data); + int bytesToWrite = buffer.size(); + while (bytesToWrite > 0 && !buffer.isEmpty()) { + int bytesWritten = CatSerial->write(buffer); + if (bytesWritten != -1) { + bytesToWrite -= bytesWritten; + buffer = buffer.right(bytesWritten); } else { - errorCnt = 0; + qCritical() << __func__ << ":" << __LINE__ + << QString("ERROR %1 for sending %2").arg(CatSerial->errorString()).arg(data.toHex(':').constData()); + CatSerial->clearError(); + qCritical() << __func__ << ":" << __LINE__ << "cleared error on serial line. returning ..."; + return; } - } else { - qCritical() << __func__ << ":" << __LINE__ - << "ERROR sending" << data.toHex(':') << "port is not open"; } + + CatSerial->flush(); + writeCount += 1; + + // only for debugging + // qCritical() << __func__ << ":" << __LINE__ << QDateTime::currentDateTime().time().toString(Qt::ISODateWithMs) + // << "write cmd" << (unsigned int)data.constData()[2]; + + } else { + qCritical() << __func__ << ":" << __LINE__ + << "ERROR sending" << data.toHex(':') << "port is not open"; } } @@ -141,6 +123,7 @@ T_com::T_com(QObject *parent) : QObject(parent) ChkConnectTimer->setSingleShot(false); ChkConnectTimer->start(100); // in ms com_want2read=0; + writeCount = 0; }