From 3bbd3e4e195da1d86228a9e325ddcc085896747b Mon Sep 17 00:00:00 2001 From: Siegfried Siegert Date: Sat, 2 Mar 2024 11:04:01 +0100 Subject: [PATCH] Proposal: final version for new CArun class --- dCArun/CArun.cpp | 255 +++++++++++++++++++++-------------------------- dCArun/CArun.h | 24 ++++- 2 files changed, 132 insertions(+), 147 deletions(-) diff --git a/dCArun/CArun.cpp b/dCArun/CArun.cpp index 1d6287c..29a636a 100644 --- a/dCArun/CArun.cpp +++ b/dCArun/CArun.cpp @@ -1,24 +1,22 @@ #include "CArun.h" - #include "datei.h" -#define UPDATE_PERIOD_MS 100 - // period to call chain steps - -#define VENDINGTIMEOUT_MS 30000 - - - CArun::CArun(QObject *parent) - : QObject{parent} + : QObject(parent) + , setupStep(SETUP_STEP::INIT) { + this->timerChainCtrl = new QTimer(this); + this->connect(timerChainCtrl, SIGNAL(timeout()), this, SLOT(chainControl())); + this->timerChainCtrl->setSingleShot(true); + this->timerChainCtrl->setInterval(100); + loadPlugIn(1); - timerChainCtrl = new QTimer(this); - connect(timerChainCtrl, SIGNAL(timeout()), this, SLOT(chainControl())); - timerChainCtrl->setSingleShot(0); - timerChainCtrl->start(UPDATE_PERIOD_MS); // 1000: call every 1000ms + // Start setup-steps: + qCritical() << "CArun: start setup..."; + + this->timerChainCtrl->start(); } @@ -37,13 +35,13 @@ char CArun::loadPlugIn(char lade1_entlade2) if (!pluginLoader->load()) { - qDebug()<<"cannot load plugin"; + qCritical() << "CArun: cannot load plugin"; } else - qDebug() <<"loaded plugin: " << pluginLoader->fileName(); + qCritical() << "CArun: plugin loaded: " << pluginLoader->fileName(); if (!pluginLoader->isLoaded()) { - qDebug()<errorString(); + qCritical() << "CArun: ERROR loading plugin: " << pluginLoader->errorString();; return 0; } @@ -52,148 +50,121 @@ char CArun::loadPlugIn(char lade1_entlade2) { // make instance of the root component (which can hold more then one clases) // also loads the lib if not yet done - qDebug()<<"cannot start instance"; + qCritical() << "CArun: ERROR cannot start instance"; return 0; } - HWaccess= qobject_cast(plugin); + this->HWaccess = qobject_cast(plugin); // make instance to class "hwinf" in dll_HWapi.h over "interfaces.h" - qDebug()<<"loadPlugIn, HWAccess: " << HWaccess; return 0; } +void CArun::openSerialPort() +{ + #define FILENAME_COMPORT "../comport.csv" // TODO: use absolute path + // use settings (ini-file) + // comport.csv wird mit installiert in: /opt/app/ + // k.A., ob "../comport.csv" gefunden wird. + qCritical() << "CArun: open serial port..."; + + QString bs, cn; + int br, ci; + + // load and use last settings: -------------------- + QByteArray myBA; + myBA=datei_readFromFile(FILENAME_COMPORT); + if (myBA.length()>0) + { + bs=csv_getEntryAsString(myBA,0); // read the 's' war 2!?? + br=csv_getEntryAsInt(myBA,1); // z.B. 5 (5.Eintrag in der Baud-Liste) + bs=csv_getEntryAsString(myBA,2); // z.B 115200 + cn=csv_getEntryAsString(myBA,3); // z.B. COM9 + ci=csv_getEntryAsInt(myBA,4); // Eintragsnummer in COM-Fenster + this->HWaccess->dc_openSerial(br,bs,cn,1); + } else + { + // vermutlich wird dies hier ausgeführt? + + // open with default settings + qCritical() << "CArun: open serial with default values"; + + bs="115200"; + br=5; + //cn="COM14"; // Windows + cn="ttymxc2"; // PTU5 + ci=2; + HWaccess->dc_openSerial(br,bs,cn,1); + } +} void CArun::chainControl(void) { - #define FILENAME_COMPORT "../comport.csv" // TODO: use absolute path - // use settings (ini-file) - - QString bs, cn; - int br, ci; - - - // load and use last settings: -------------------- - QByteArray myBA; - myBA=datei_readFromFile(FILENAME_COMPORT); - if (myBA.length()>0) - { - bs=csv_getEntryAsString(myBA,0); // read the 's' war 2!?? - br=csv_getEntryAsInt(myBA,1); // z.B. 5 (5.Eintrag in der Baud-Liste) - bs=csv_getEntryAsString(myBA,2); // z.B 115200 - cn=csv_getEntryAsString(myBA,3); // z.B. COM9 - ci=csv_getEntryAsInt(myBA,4); // Eintragsnummer in COM-Fenster - HWaccess->dc_openSerial(br,bs,cn,1); - } else - { - // vermutlich wird dies hier ausgeführt, weil eine csv-Datei wird nicht installiert - - // open with default settings - qDebug()<<"CArunGui: open serial with default values"; - - bs="115200"; - br=5; - //cn="COM14"; // Windows - cn="ttymxc2"; // PTU5 - ci=2; - HWaccess->dc_openSerial(br,bs,cn,1); + switch (this->setupStep) { + case SETUP_STEP::INIT: + qCritical() << "CArun: SETUP_STEP::INIT"; + this->setupStep = SETUP_STEP::OPEN_SERIAL_PORT; + this->timerChainCtrl->start(); + break; + case SETUP_STEP::OPEN_SERIAL_PORT: + qCritical() << "CArun: SETUP_STEP::OPEN_SERIAL_PORT"; + this->openSerialPort(); + this->setupStep = SETUP_STEP::CHECK_VALID_DATA; + this->timerChainCtrl->start(); + break; + case SETUP_STEP::TEST_OPEN_PORT: + qCritical() << "CArun: SETUP_STEP::TEST_OPEN_PORT"; + if (this->HWaccess->dc_isPortOpen()) { + this->setupStep = SETUP_STEP::TEST_RESPONSE_REQUEST; } - - // TIMER myTO: hat keinen timeout-slot! - // wird nur verwendet, um isActive() abzufragen, d.h. ~x zu warten - - // pruefen, of Port nach x s open: - if (HWaccess->dc_isPortOpen()) - { - - } else - { - // wenn nicht: step 6 - } - myTO->start(100); - } - } else - - if (myStep==2) - { - if (!myTO->isActive()) - { - HWaccess->dc_requTestResponse(); - myStep++; - myTO->start(100); - } - } else - - if (myStep==3) - { - if (!myTO->isActive()) - { - if (HWaccess->dc_readAnswTestResponse()) - myStep++; // response was correct - else - { - myStep=6; // 13.12.23: start Autoconnect cycle - qDebug()<<"CArunGui: got no answer from DC, retry.."; - } - myTO->start(100); - } - - } else - - if (myStep==4) - { - HWaccess->dc_autoRequest(1); - AutSendButton->setChecked(true); // taste "druecken" - myStep++; - myTO->start(2000); - } else - - if (myStep==5) - { - if (!myTO->isActive()) - { - if (HWaccess->sys_areDCdataValid()) - { - qDebug()<<"CArunGui: DC is connected"; - myStep=7; // OK, connection is up and running - } else - { - qDebug()<<"CArunGui: auto request is not running, retry..."; - myStep++; - myTO->start(100); - } - } - - } else - - if (myStep==6) - { - // restart autoconnect cycle - myTO->start(100); // restart - myStep=0; - } else - - if (myStep==7) - { - // stay here, DC connection is up and running - - - } else - { - + else { + qCritical() << "CArun: port is still closed, restarting.."; + this->setupStep = SETUP_STEP::INIT; } - - if (myNextStep) - { - *nextScreen=myNextStep; - myNextStep=0; + this->timerChainCtrl->start(); + break; + case SETUP_STEP::TEST_RESPONSE_REQUEST: + qCritical() << "CArun: SETUP_STEP::TEST_RESPONSE_REQUEST"; + this->HWaccess->dc_requTestResponse(); + this->setupStep = SETUP_STEP::TEST_RESPONSE_CHECK; + this->timerChainCtrl->start(); + break; + case SETUP_STEP::TEST_RESPONSE_CHECK: + qCritical() << "CArun: SETUP_STEP::TEST_RESPONSE_CHECK"; + if (this->HWaccess->dc_readAnswTestResponse()) { + // response was correct + this->setupStep = SETUP_STEP::SETUP_AUTOREQEUST; } - return false; - - - + else { + qCritical() << "CArun: got no answer from DC, retry.."; + this->setupStep = SETUP_STEP::INIT; + } + this->timerChainCtrl->start(); + break; + case SETUP_STEP::SETUP_AUTOREQEUST: + qCritical() << "CArun: SETUP_STEP::SETUP_AUTOREQEUST"; + this->HWaccess->dc_autoRequest(1); + this->setupStep = SETUP_STEP::CHECK_VALID_DATA; + this->timerChainCtrl->start(); + break; + case SETUP_STEP::CHECK_VALID_DATA: + qCritical() << "CArun: SETUP_STEP::CHECK_VALID_DATA"; + if (this->HWaccess->sys_areDCdataValid()) { + qCritical() << "CArun: DC is connected"; + // do not start timer again + } + else { + qCritical() << "CArun: auto request is not running, retry..."; + this->setupStep = SETUP_STEP::INIT; + this->timerChainCtrl->start(); + } + break; + default: + qCritical() << "CArun: invalid setup step"; + break; + } } diff --git a/dCArun/CArun.h b/dCArun/CArun.h index f9d031e..cb44554 100644 --- a/dCArun/CArun.h +++ b/dCArun/CArun.h @@ -9,8 +9,19 @@ #include #include #include "plugin.h" -#include "stepList.h" -//#include "stepList.h" // define all working chain steps here + + +enum class SETUP_STEP { + INIT, + OPEN_SERIAL_PORT, + TEST_OPEN_PORT, + TEST_RESPONSE_REQUEST, + TEST_RESPONSE_CHECK, + SETUP_AUTOREQEUST, + CHECK_VALID_DATA +}; + + class CArun : public QObject @@ -20,8 +31,7 @@ class CArun : public QObject public: explicit CArun(QObject *parent = nullptr); - QTimer *timerChainCtrl; - QTimer *timerVendingTimeout; + QTimer *timerChainCtrl; char loadPlugIn(char lade1_entlade2); @@ -31,11 +41,15 @@ private: hwinf *HWaccess=nullptr; // global pointer to plugin-class + SETUP_STEP setupStep; + void openSerialPort(); signals: + + +private slots: void chainControl(); - void vendingTimeout(); };