From 8b728bf6bd0faeff841dbc4528503badc7616748 Mon Sep 17 00:00:00 2001 From: maisvendoo Date: Fri, 2 Jun 2023 10:21:32 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BD=D0=B0=D1=8F=20=D1=86=D0=B8=D0=BA=D0=BB?= =?UTF-8?q?=D0=B8=D1=87=D0=B5=D1=81=D0=BA=D0=B0=D1=8F=20=D0=BE=D1=82=D0=BF?= =?UTF-8?q?=D1=80=D0=B0=D0=B2=D0=BA=D0=B0=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE?= =?UTF-8?q?=D1=81=D0=BE=D0=B2=20=D0=BF=D0=BE=20=D1=82=D0=B0=D0=B9=D0=BC?= =?UTF-8?q?=D0=B5=D1=80=D1=83.=20=D0=9F=D0=BE=D1=82=D0=BE=D0=BA=20=D1=83?= =?UTF-8?q?=D0=B1=D1=80=D0=B0=D0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/data-sender.h | 58 ------------------------------ include/mainwindow.h | 16 ++++----- src/data-sender.cpp | 69 ----------------------------------- src/mainwindow.cpp | 84 ++++++++++++------------------------------- 4 files changed, 29 insertions(+), 198 deletions(-) delete mode 100644 include/data-sender.h delete mode 100644 src/data-sender.cpp diff --git a/include/data-sender.h b/include/data-sender.h deleted file mode 100644 index e5fde0d..0000000 --- a/include/data-sender.h +++ /dev/null @@ -1,58 +0,0 @@ -//------------------------------------------------------------------------------ -// -// -// -// -// -//------------------------------------------------------------------------------ -/*! - * \file - * \brief Modbus master data sender - * \copyright maisvendoo - * \author Dmitry Pritykin - * \date 25/11/2017 - */ - -#ifndef DATA_SENDER_H -#define DATA_SENDER_H - -#include -#include "request.h" - -//------------------------------------------------------------------------------ -// -//------------------------------------------------------------------------------ -class DataSender : public QObject -{ - Q_OBJECT - -public: - - explicit DataSender(QObject *parent = Q_NULLPTR); - virtual ~DataSender(); - - void init(bool is_cyclic, int delay, abstract_request_t request); - -private: - - bool is_cyclic; - bool is_send_started; - int delay; - abstract_request_t request; - -signals: - - /// Send request from master to slave - void sendMasterRequest(abstract_request_t *request); - - void quit(); - - /// - void isStarted(bool *started); - -public slots: - - void cyclicDataSend(); -}; - -#endif // DATA_SENDER_H diff --git a/include/mainwindow.h b/include/mainwindow.h index 3d1e0b6..342d7a8 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -18,10 +18,9 @@ #include #include -#include +#include #include "master.h" -#include "data-sender.h" //------------------------------------------------------------------------------ // @@ -86,14 +85,13 @@ class MainWindow : public QMainWindow bool is_send_started; /// Cyclic send flag - bool is_cyclic; - - DataSender dataSender; - QThread threadCyclicSend; + bool is_cyclic; /// Flag of close window event bool is_close_event; + QTimer sendRequestTimer; + signals: /// Send request from master to slave @@ -137,11 +135,11 @@ private slots: /// Check/uncheck cyclic send of data void checkCyclicSend(int state); - /// Actions by finish sender thread - void onFinishSendThread(); - /// Send started flag to data sender void getStartedFlag(bool *started); + + /// Cyclic send request by reference time interval + void slotCyclicRequestSend(); }; #endif // MAINWINDOW_H diff --git a/src/data-sender.cpp b/src/data-sender.cpp deleted file mode 100644 index 69fdede..0000000 --- a/src/data-sender.cpp +++ /dev/null @@ -1,69 +0,0 @@ -//------------------------------------------------------------------------------ -// -// -// -// -// -//------------------------------------------------------------------------------ -/*! - * \file - * \brief Modbus master data sender - * \copyright maisvendoo - * \author Dmitry Pritykin - * \date 25/11/2017 - */ - -#include "data-sender.h" - -#include - -//------------------------------------------------------------------------------ -// -//------------------------------------------------------------------------------ -DataSender::DataSender(QObject *parent) : QObject(parent) -{ - is_cyclic = is_send_started = false; -} - -//------------------------------------------------------------------------------ -// -//------------------------------------------------------------------------------ -DataSender::~DataSender() -{ - -} - -//------------------------------------------------------------------------------ -// -//------------------------------------------------------------------------------ -void DataSender::init(bool is_cyclic, int delay, abstract_request_t request) -{ - this->is_cyclic = is_cyclic; - this->delay = delay; - this->request = request; - - is_send_started = true; -} - -//------------------------------------------------------------------------------ -// -//------------------------------------------------------------------------------ -void DataSender::cyclicDataSend() -{ - do - { - // Send request - emit sendMasterRequest(&request); - - // Sleeping thread, if requed cyclical data send - if (is_cyclic) - QThread::msleep(delay); - - // Get started flag from main window - emit isStarted(&is_send_started); - - } while (is_cyclic && is_send_started); - - // Terminate sender thread - emit quit(); -} diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 1988453..b8c4c93 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -154,17 +154,8 @@ void MainWindow::init() connect(ui->cCyclicSend, &QCheckBox::stateChanged, this, &MainWindow::checkCyclicSend); - connect(&dataSender, &DataSender::sendMasterRequest, - master, &Master::sendRequest); - - connect(&threadCyclicSend, &QThread::finished, - this, &MainWindow::onFinishSendThread); - - connect(&dataSender, &DataSender::quit, - &threadCyclicSend, &QThread::terminate); - - connect(&dataSender, &DataSender::isStarted, - this, &MainWindow::getStartedFlag); + connect(&sendRequestTimer, &QTimer::timeout, + this, &MainWindow::slotCyclicRequestSend); is_close_event = false; } @@ -313,18 +304,7 @@ abstract_request_t *MainWindow::getRequestData() //------------------------------------------------------------------------------ void MainWindow::closeEvent(QCloseEvent *event) { - // Check sender thread state - if (threadCyclicSend.isRunning()) - { - // Stop thread - is_send_started = false; - // Close window after terminating of sender thread - is_close_event = true; - // Ignore close event - event->ignore(); - } - else - event->accept(); + event->accept(); } //------------------------------------------------------------------------------ @@ -433,32 +413,25 @@ void MainWindow::sendButtonRelease() return; } - // Check sender thread state - if (!threadCyclicSend.isRunning()) + if (!is_cyclic) { - // Set started flag - is_send_started = true; - - // Init data sender - dataSender.init(is_cyclic, ui->sbSendInterval->value(), *getRequestData()); - - // Move data sender to thread - dataSender.moveToThread(&threadCyclicSend); - - // Connect start signal with thread function - connect(&threadCyclicSend, &QThread::started, - &dataSender, &DataSender::cyclicDataSend); - - // Start sender thread - threadCyclicSend.start(); - - // Mark button as stop button - ui->bSend->setText("Stop"); + emit sendMasterRequest(getRequestData()); } else { - // Reset started flag - is_send_started = false; + if (!sendRequestTimer.isActive()) + { + sendRequestTimer.setInterval(ui->sbSendInterval->value()); + sendRequestTimer.start(); + ui->bSend->setText("Stop"); + ui->bConnect->setEnabled(false); + } + else + { + sendRequestTimer.stop(); + ui->bSend->setText("Send"); + ui->bConnect->setEnabled(true); + } } } @@ -541,28 +514,15 @@ void MainWindow::checkCyclicSend(int state) //------------------------------------------------------------------------------ // //------------------------------------------------------------------------------ -void MainWindow::onFinishSendThread() +void MainWindow::getStartedFlag(bool *started) { - // Disconnect thread function - disconnect(&threadCyclicSend, &QThread::started, - &dataSender, &DataSender::cyclicDataSend); - - // Mark button as send button - ui->bSend->setText("Send"); - - // Check close flag - if (is_close_event) - { - master->closeConnection(); - QApplication::quit(); - } + *started = is_send_started; } //------------------------------------------------------------------------------ // //------------------------------------------------------------------------------ -void MainWindow::getStartedFlag(bool *started) +void MainWindow::slotCyclicRequestSend() { - *started = is_send_started; + emit sendMasterRequest(getRequestData()); } -