Compare commits

..

11 Commits

Author SHA1 Message Date
ca2d9e1b5a Minor: added debug output in error case 2024-10-15 12:45:38 +02:00
4c770349bf removed waitForBytesWritten() 2024-10-15 12:44:56 +02:00
209132054f Added sanity check if there is a command active but no data can be read on the serial line 2024-10-15 10:51:20 +02:00
0f7ab3c71c Minor: adding comments 2024-10-15 10:49:30 +02:00
2b2cd25276 Minor: add waitForTestResponse 2024-10-15 10:08:49 +02:00
08bb513c7b Minor: set recBuf to a definite value 2024-10-15 10:08:11 +02:00
36a8f07069 Pass "this" as parent 2024-10-15 10:07:29 +02:00
900b0ef952 loadRecDataFromFrame():
Read last message if only one command to device controller is active.
	Otherwise assume that the data is obsolete. Trigger the sending
	of a known test-response to find new valid data-stream.
2024-10-15 10:04:40 +02:00
26a11e6c56 writeToSerial():
Only write to serial interface if no current command has been
	sent to DC already.
2024-10-15 10:00:34 +02:00
6321f9068f Add helpers getReadSource() getSerialPort(). 2024-10-15 09:57:49 +02:00
9daa5e17cb Introduce a vector of commands currently sent to device controller. 2024-10-15 09:54:09 +02:00
9 changed files with 135 additions and 56 deletions

View File

@@ -7,6 +7,7 @@
//#include <QString> //#include <QString>
#include <QTimer> #include <QTimer>
#include <QSerialPort> #include <QSerialPort>
#include <QVector>
#include "tslib.h" #include "tslib.h"
#include "controlBus.h" #include "controlBus.h"
#include "interfaces.h" #include "interfaces.h"
@@ -32,6 +33,10 @@ class T_com : public QObject //, public QPlainTextEdit
// QSerialPort *CatSerial = nullptr; // QSerialPort *CatSerial = nullptr;
QSerialPort *CatSerial; QSerialPort *CatSerial;
QVector<uint16_t> readCmds; // list of active commands sent to DC
//char oeffneSerialPort(); //char oeffneSerialPort();
char open_Serial_Port(); char open_Serial_Port();
void closeSerialPort(); void closeSerialPort();
@@ -61,6 +66,9 @@ public:
bool readFromSerial(QByteArray &data, uint16_t &sendLength); bool readFromSerial(QByteArray &data, uint16_t &sendLength);
// retval: true: data available // retval: true: data available
QVector<uint16_t> &getReadCmds() { return readCmds; }
QVector<uint16_t> const &getReadCmds() const { return readCmds; }
/* /*
uint8_t getAllPortPins(void); uint8_t getAllPortPins(void);
// rs232pins: all signals bitwise coded in one byte: // rs232pins: all signals bitwise coded in one byte:

View File

@@ -168,6 +168,8 @@ class T_datif : public QObject
QTimer *datif_trigger; QTimer *datif_trigger;
uint8_t selectedSlaveAddr; uint8_t selectedSlaveAddr;
bool waitForTestResponse = false;
private slots: private slots:
char datif_cycleSend(); char datif_cycleSend();
void StoredRecData(); void StoredRecData();

View File

@@ -121,13 +121,8 @@ public:
explicit hwapi(QObject *parent = nullptr); explicit hwapi(QObject *parent = nullptr);
#ifdef THIS_IS_CA_MASTER #ifdef THIS_IS_CA_MASTER
T_datif *myDatif; T_datif *myDatif;
#else // THIS_IS_CA_SLAVE
QString getVersions();
void resetVersions();
bool m_resetVersions = false;
#endif #endif
T_runProc *runProcess; T_runProc *runProcess;
@@ -1406,8 +1401,6 @@ public:
signals: signals:
void hwapi_restoredVersions();
void hwapi_reportDCDownloadStatus(QString const&) const override; void hwapi_reportDCDownloadStatus(QString const&) const override;
void hwapi_reportDCDownloadSuccess(QString const&) const override; void hwapi_reportDCDownloadSuccess(QString const&) const override;
void hwapi_reportDCDownloadFailure(QString const&) const override; void hwapi_reportDCDownloadFailure(QString const&) const override;

View File

@@ -122,6 +122,9 @@ public:
uint8_t *RdDlen, uint8_t *receivedData); uint8_t *RdDlen, uint8_t *receivedData);
// retval: data valid, only one time true // retval: data valid, only one time true
uint16_t getReadSource() { return readSource; } // readSource contains last command sent to device controller
T_com *getSerialPort() { return mySerialPort; } // utility function
signals: signals:
void framerecieved(); //bool gotINdata); void framerecieved(); //bool gotINdata);

View File

@@ -20,13 +20,72 @@ void T_com::writeToSerial(const QByteArray &data, uint16_t sendLength)
{ {
sendBuffer=data; sendBuffer=data;
sendLen=sendLength; sendLen=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())
{ {
//qDebug() << "sending..." << sendBuffer; if (CatSerial->error() != QSerialPort::NoError) {
CatSerial->write(sendBuffer); qCritical() << __func__ << "" << __LINE__ << "ERROR on serial line" << CatSerial->errorString();
} else CatSerial->clearError();
qDebug() << "error sending, port is not open"; 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";
}
} 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";
}
}
} }

View File

@@ -6,6 +6,7 @@ History:
*/ */
#include "datIf.h" #include "datIf.h"
#include "hwapi.h"
#include "sendWRcmd.h" #include "sendWRcmd.h"
#include "controlBus.h" #include "controlBus.h"
#include "storeINdata.h" #include "storeINdata.h"
@@ -513,6 +514,8 @@ char T_datif::loadRecDataFromFrame()
return 0; return 0;
} }
memset(receivedData, 0x00, sizeof(receivedData));
ret=myDCIF->getReceivedInData(&SlaveAdr, &readSource, &readAddress, &RdDleng, receivedData); ret=myDCIF->getReceivedInData(&SlaveAdr, &readSource, &readAddress, &RdDleng, receivedData);
// nur true wenn CommandState OK und readState OK // nur true wenn CommandState OK und readState OK
@@ -545,6 +548,55 @@ char T_datif::loadRecDataFromFrame()
gpi_storeRecPayLoad(RdDleng, receivedData); // save for host (user of hwapi) gpi_storeRecPayLoad(RdDleng, receivedData); // save for host (user of hwapi)
if (myDCIF && myDCIF->getSerialPort()) {
QVector<uint16_t> &readCmds = myDCIF->getSerialPort()->getReadCmds();
if (readCmds.size() == 1) { // there can be only one command been sent to the DC
// the command number active at the moment must be the same as the saved one
if (readSource == myDCIF->getReadSource() && readSource == readCmds[0]) {
// maybe we have sent a explicit request for a test-response
if (waitForTestResponse) {
qCritical() << __func__ << ":" << __LINE__ << "turn on auto-request";
if (readCmds[0] == CMD2DC_TestSerial) {
if (QString(QByteArray((char const *)receivedData, RdDleng)) == "< SlaveResponse") {
waitForTestResponse = false;
qCritical() << __func__ << ":" << __LINE__ << "turn on auto-request";
((hwapi *)parent())->dc_autoRequest(true); // return autorequest to true
} else {
qCritical() << __func__ << ":" << __LINE__ << "ERROR received wrong test-response"
<< QString(QByteArray((char const *)receivedData, RdDleng));
}
}
} else {
// usual handling of response
// qCritical() << __func__ << ":" << __LINE__ << ":" << readSource << myDCIF->getReadSource() << readCmds[0];
// qCritical() << __func__ << ":" << __LINE__ << ":" << QByteArray((char const *)receivedData, RdDleng).toHex(':');
}
readCmds.clear();
} else { // error
qCritical() << __func__ << ":" << __LINE__ << ": ERROR " << readSource << myDCIF->getReadSource() << readCmds[0];
qCritical() << __func__ << ":" << __LINE__ << ": ERROR length" << RdDleng << QString(", ignore data for cmd 0x%1").arg(readCmds[0], 0, 16)
<< QByteArray((char const *)receivedData, RdDleng).toHex(':');
readCmds.clear();
return 0;
}
} else {
// error: read commands has not size 1 as it should be: turn off
// auto requests and send explicit request for test-response:
// once this response has been received, turn on the autmatic requests
// again.
readCmds.clear();
if (parent()) {
qCritical() << __func__ << ":" << __LINE__ << "ERROR turn off auto-request";
qCritical() << __func__ << ":" << __LINE__ << "ERROR send request for test-response";
waitForTestResponse = true;
((hwapi *)parent())->dc_autoRequest(false);
((hwapi *)parent())->dc_requTestResponse();
}
return 0;
}
}
// uint8_t nn; // uint8_t nn;
//qDebug() << "\n datif: got valid data, rdsrc:" << readSource << " rdadd:" << readAddress //qDebug() << "\n datif: got valid data, rdsrc:" << readSource << " rdadd:" << readAddress
// << " rdlen:" << RdDleng; // << " rdlen:" << RdDleng;

View File

@@ -11,7 +11,6 @@
#include "hwapi.h" #include "hwapi.h"
#include "download_thread.h" #include "download_thread.h"
#include "reporting_thread.h" #include "reporting_thread.h"
#include "storeINdata.h" // epi_loadSWVer, epi_loadHWVer
#include <cstring> #include <cstring>
#include <QThread> #include <QThread>
@@ -26,37 +25,6 @@ static uint8_t hwapi_lastDoorState;
static uint8_t bl_startupStep; static uint8_t bl_startupStep;
#ifdef THIS_IS_CA_MASTER
#else // THIS_IS_CA_SLAVE
QString hwapi::getVersions() {
if (m_sharedMem != nullptr) {
// check if sw/hw-versions have been restored (see resetVersions()).
// emit a signal in such a case.
QString const &sw = epi_loadSWver(); // command 11
QString const &hw = epi_loadHWver(); // command 12
if (!sw.isEmpty() && !hw.isEmpty() && m_resetVersions) {
emit hwapi_restoredVersions();
return sw + " " + hw;
}
}
return "";
}
void hwapi::resetVersions() {
if (m_sharedMem != nullptr) {
// command 11 and 12 are included in the requests sent automatically,
// first 11, then 12. The results are used to refresh corresponding
// shared memory locations.
// set these locations to the empty string in the opposite order as
// the commands are sent.
// purpose: only when both locations are not empty again, we can be
// sure that data are not obsolete.
gpi_storeHWver(""); // command 12
gpi_storeSWver(""); // command 11
m_resetVersions = true;
}
}
#endif
hwapi::hwapi(QObject *parent) : QObject(parent) hwapi::hwapi(QObject *parent) : QObject(parent)
{ {
@@ -88,7 +56,7 @@ hwapi::hwapi(QObject *parent) : QObject(parent)
#error "SLAVE LIB COMPILED INTO MASTER" #error "SLAVE LIB COMPILED INTO MASTER"
#endif #endif
myDatif = new T_datif(); // für die CAslave-Lib auskommentieren! myDatif = new T_datif(this); // für die CAslave-Lib auskommentieren!
#endif #endif

View File

@@ -368,6 +368,9 @@ uint8_t recBuffer[FRAME_MAXLEN];
// read from "VCP": // read from "VCP":
mySerialPort->readFromSerial(Indata, recLength); mySerialPort->readFromSerial(Indata, recLength);
//qDebug()<<"prot: got data " << recLength; //qDebug()<<"prot: got data " << recLength;
memset(recBuffer, 0x00, sizeof(recBuffer));
if (recLength>FRAME_MAXLEN) if (recLength>FRAME_MAXLEN)
recLength=FRAME_MAXLEN; recLength=FRAME_MAXLEN;
for (int nn=0; nn<recLength; nn++) for (int nn=0; nn<recLength; nn++)

View File

@@ -6,11 +6,6 @@
#include "shared_mem_buffer.h" #include "shared_mem_buffer.h"
#include "datei.h" #include "datei.h"
#include <QMutex>
#include <QMutexLocker>
static QMutex SWHD_mutex;
// gpi: grafical access to PI: access from external devices over device controller FOR GUI // gpi: grafical access to PI: access from external devices over device controller FOR GUI
// epi: external access from GUI to PI: FOR external devices (DC) // epi: external access from GUI to PI: FOR external devices (DC)
@@ -69,7 +64,6 @@ bool indat_isMdbOn()
void gpi_storeHWver(QString text) void gpi_storeHWver(QString text)
{ {
QMutexLocker locker(&SWHD_mutex);
// change Qstring to array of chars, because shared mem allowes no QString! // change Qstring to array of chars, because shared mem allowes no QString!
int nn, LL = text.length(); int nn, LL = text.length();
if (LL >= versionBufferLen) if (LL >= versionBufferLen)
@@ -85,7 +79,6 @@ void gpi_storeHWver(QString text)
QString epi_loadHWver(void) QString epi_loadHWver(void)
{ {
QMutexLocker locker(&SWHD_mutex);
// load array of chars from SM and change to QString // load array of chars from SM and change to QString
int nn, LL = versionBufferLen; int nn, LL = versionBufferLen;
char cc; char cc;
@@ -103,7 +96,6 @@ QString epi_loadHWver(void)
void gpi_storeSWver(QString text) void gpi_storeSWver(QString text)
{ {
QMutexLocker locker(&SWHD_mutex);
int nn, LL = text.length(); int nn, LL = text.length();
if (LL >= versionBufferLen) if (LL >= versionBufferLen)
LL=versionBufferLen-1; // leave place for termination LL=versionBufferLen-1; // leave place for termination
@@ -119,7 +111,6 @@ void gpi_storeSWver(QString text)
QString epi_loadSWver(void) QString epi_loadSWver(void)
{ {
QMutexLocker locker(&SWHD_mutex);
int nn, LL = versionBufferLen; int nn, LL = versionBufferLen;
char cc; char cc;
QString myStr; QString myStr;