From 1d5e0b669d44af0d35c139d82facdc9420409195 Mon Sep 17 00:00:00 2001 From: nasi Date: Tue, 19 Oct 2021 17:03:55 +0330 Subject: [PATCH] multithread 14000727-1 --- serialConnectionManager/ModbusMaster.cpp | 61 ++++++++++++++++-------- serialConnectionManager/ModbusMaster.h | 6 ++- 2 files changed, 45 insertions(+), 22 deletions(-) diff --git a/serialConnectionManager/ModbusMaster.cpp b/serialConnectionManager/ModbusMaster.cpp index fdb0674..cd35629 100644 --- a/serialConnectionManager/ModbusMaster.cpp +++ b/serialConnectionManager/ModbusMaster.cpp @@ -110,7 +110,7 @@ QBitArray modBusMaster::getCoil(int startAddress, quint16 readSize) QModbusDataUnit unit = readRequest(QModbusDataUnit::RegisterType::Coils, startAddress, readSize); mutex.lock(); if(!freeThread) - modBusAnswerNotReady.wait(&mutex); + modBusAnswerNotReady.wait(&mutex); mutex.unlock(); @@ -186,30 +186,35 @@ QModbusDataUnit modBusMaster::readRequest(QModbusDataUnit::RegisterType registe if (auto *reply =modbusDevice->sendReadRequest(QModbusDataUnit(table, startAddress, readSize), _slaveAddress)) { QFuture future = QtConcurrent::run([this, reply]() { + // while(!reply->isFinished()) + // { + // qDebug() << "reply->isFinished() " << reply->isFinished(); + // qDebug() << "Hello from thread " << QThread::currentThread(); + // } - - - - // qDebug() << "reply->isFinished() " << reply->isFinished(); - while(!reply->isFinished()) - { - qDebug() << "reply->isFinished() " << reply->isFinished(); - qDebug() << "Hello from thread " << QThread::currentThread(); - } - freeThread=true; - mutex.lock(); - if (freeThread) - modBusAnswerNotReady.wakeAll(); - mutex.unlock(); - - if(!checkForError(reply)) + if (!freeThread) { - const QModbusDataUnit unit = reply->result(); - return unit; + if (!reply->isFinished()) + { + qDebug() << "1reply->isFinished() " << reply->isFinished(); + connect(reply, &QModbusReply::finished, this, &modBusMaster::modbusReplyRecieved, Qt::ConnectionType::BlockingQueuedConnection); + qDebug() << "2reply->isFinished() " << reply->isFinished(); + } } + }); + // freeThread=true; + mutex.lock(); + if (freeThread) + modBusAnswerNotReady.wakeAll(); + mutex.unlock(); + + if(!checkForError(reply)) + { + const QModbusDataUnit unit = reply->result(); + return unit; + } - }); } else @@ -218,7 +223,7 @@ QModbusDataUnit modBusMaster::readRequest(QModbusDataUnit::RegisterType registe } - //reply->deleteLater(); + reply->deleteLater(); } bool modBusMaster::checkForError(QModbusReply *_reply) { @@ -239,6 +244,20 @@ bool modBusMaster::checkForError(QModbusReply *_reply) } +void modBusMaster::modbusReplyRecieved() +{ + + // auto reply = qobject_cast(sender()); + // if (!reply) + // return; + + // modbusReplyFromClient = reply; + freeThread=true; + + // reply->deleteLater(); + +} + void modBusMaster::writeRequest(QModbusDataUnit::RegisterType registerType, int startAddress, quint16 writeSize) { qDebug() << " ---- setSingleCoil coilFlag writeRequest "; diff --git a/serialConnectionManager/ModbusMaster.h b/serialConnectionManager/ModbusMaster.h index 2874235..1f4dcb6 100644 --- a/serialConnectionManager/ModbusMaster.h +++ b/serialConnectionManager/ModbusMaster.h @@ -45,9 +45,12 @@ public: signals: void dataFromClient(QString readedParameter); void modbusConnectionState(bool state); +private slots: + void modbusReplyRecieved(); private: - QModbusReply *lastRequest; + QModbusReply *modbusReplyFromClient; + // QModbusDataUnit modbusReplyUnit; QModbusClient *modbusDevice = nullptr; QString _serialPort ; int _slaveAddress; @@ -57,6 +60,7 @@ private: QWaitCondition modBusAnswerNotReady; QMutex mutex; bool freeThread=false; + public: QModbusReply *reply; };