writeToSerial():

Always write next message. Set writeCount. Clear() serial line before sending.
	Flush() serial line after sending.
This commit is contained in:
Gerhard Hoffmann 2024-10-16 13:03:20 +02:00
parent ff746a45bc
commit 87bc5b5c1e

View File

@ -1,5 +1,6 @@
#include "com.h"
#include <QDebug>
#include <QDateTime>
//#include "controlBus.h"
//////////////////////////////////////////////////////////////////////////////////
@ -24,11 +25,7 @@ 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->isOpen()) {
if (CatSerial->error() != QSerialPort::NoError) {
qCritical() << __func__ << "" << __LINE__ << "ERROR on serial line" << CatSerial->errorString();
CatSerial->clearError();
@ -42,8 +39,9 @@ void T_com::writeToSerial(const QByteArray &data, uint16_t sendLength)
qCritical() << __func__ << "" << __LINE__ << "read all data from serial line";
}
QByteArray buffer(data);
CatSerial->clear();
QByteArray buffer(data);
int bytesToWrite = buffer.size();
while (bytesToWrite > 0 && !buffer.isEmpty()) {
int bytesWritten = CatSerial->write(buffer);
@ -59,33 +57,17 @@ void T_com::writeToSerial(const QByteArray &data, uint16_t sendLength)
}
}
// save last command sent.
readCmds.append(data.constData()[2]); // 2: index of the last command
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";
}
} 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;
}
} else {
errorCnt = 0;
}
} 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;
}