diff --git a/serialConnectionManager/ModbusMaster.cpp b/serialConnectionManager/ModbusMaster.cpp index 76a979a..f455e41 100644 --- a/serialConnectionManager/ModbusMaster.cpp +++ b/serialConnectionManager/ModbusMaster.cpp @@ -107,8 +107,14 @@ void modBusMaster::setMultipleRegister(int startAddress, quint16 writeSize , QVe QBitArray modBusMaster::getCoil(int startAddress, quint16 readSize) { - QModbusDataUnit unit = readRequest(QModbusDataUnit::RegisterType::Coils, startAddress, readSize); + mutex.lock(); + if (!unit.valueCount()) + modBusAnswerNotReady.wait(&mutex); + mutex.unlock(); + + + QBitArray coils; coils.resize(2); auto num = unit.valueCount(); @@ -117,7 +123,15 @@ QBitArray modBusMaster::getCoil(int startAddress, quint16 readSize) { coils.setBit((unit.value(i))); } + + mutex.lock(); + modBusAnswerConsumed.wait(&mutex); + mutex.unlock(); + return coils ; + + + } QBitArray modBusMaster::getInputCoil(int startAddress, quint16 readSize) @@ -174,19 +188,29 @@ QModbusDataUnit modBusMaster::readRequest(QModbusDataUnit::RegisterType registe if (auto *reply =modbusDevice->sendReadRequest(QModbusDataUnit(table, startAddress, readSize), _slaveAddress)) { QFuture future = QtConcurrent::run([this, reply]() { - qDebug() << "reply->isFinished() " << reply->isFinished(); - while(!reply->isFinished()) - { - qDebug() << "reply->isFinished() " << reply->isFinished(); - qDebug() << "Hello from thread " << QThread::currentThread(); - } + + mutex.lock(); + if (!reply->isFinished()) + modBusAnswerNotReady.wait(&mutex); + mutex.unlock(); + + + // qDebug() << "reply->isFinished() " << reply->isFinished(); + // while(!reply->isFinished()) + // { + // qDebug() << "reply->isFinished() " << reply->isFinished(); + // qDebug() << "Hello from thread " << QThread::currentThread(); + // } if(!checkForError(reply)) { const QModbusDataUnit unit = reply->result(); return unit; } + + mutex.lock(); + modBusAnswerConsumed.wakeAll(); + mutex.unlock(); }); - // future.waitForFinished(); } else @@ -244,7 +268,7 @@ void modBusMaster::writeRequest(QModbusDataUnit::RegisterType registerType, int } }); - // future.waitForFinished(); + // future.waitForFinished(); // if (!reply->isFinished()) { // connect(reply, &QModbusReply::finished, this, [reply]() { diff --git a/serialConnectionManager/ModbusMaster.h b/serialConnectionManager/ModbusMaster.h index e6d215b..985a686 100644 --- a/serialConnectionManager/ModbusMaster.h +++ b/serialConnectionManager/ModbusMaster.h @@ -8,8 +8,8 @@ #include #include #include "SettingSerialParameters.h" - - +#include "QWaitCondition" +#include "qmutex.h" class QModbusClient; class QModbusReply; @@ -54,6 +54,9 @@ private: SettingSerialParameters _modbusSettings; QBitArray _coilFlags; QVector _registerValues; + QWaitCondition modBusAnswerNotReady; + QWaitCondition modBusAnswerConsumed; + QMutex mutex; public: QModbusReply *reply; }; diff --git a/serialConnectionManager/mainwindow.cpp b/serialConnectionManager/mainwindow.cpp index 1d4b555..ad60da2 100644 --- a/serialConnectionManager/mainwindow.cpp +++ b/serialConnectionManager/mainwindow.cpp @@ -1,6 +1,7 @@ #include "mainwindow.h" #include "ui_mainwindow.h" #include "qdebug.h" +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) @@ -10,6 +11,9 @@ MainWindow::MainWindow(QWidget *parent) ui->writeTable->addItem(tr("Discrete Inputs"), 1); ui->writeTable->addItem(tr("Input Registers"), 2); ui->writeTable->addItem(tr("Holding Registers"), 3); + QThread *upperModbusThread = new QThread(); + modBusObj.moveToThread(upperModbusThread); + connect(&modBusObj, &modBusMaster::modbusConnectionState, this, &MainWindow::connectionState); }