Browse Source

multithread test

master
nasi 3 years ago
parent
commit
adb6bcca6b
  1. 42
      serialConnectionManager/ModbusMaster.cpp
  2. 7
      serialConnectionManager/ModbusMaster.h
  3. 4
      serialConnectionManager/mainwindow.cpp

42
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<void> 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]() {

7
serialConnectionManager/ModbusMaster.h

@ -8,8 +8,8 @@
#include <QBitArray>
#include <QSerialPort>
#include "SettingSerialParameters.h"
#include "QWaitCondition"
#include "qmutex.h"
class QModbusClient;
class QModbusReply;
@ -54,6 +54,9 @@ private:
SettingSerialParameters _modbusSettings;
QBitArray _coilFlags;
QVector<quint16> _registerValues;
QWaitCondition modBusAnswerNotReady;
QWaitCondition modBusAnswerConsumed;
QMutex mutex;
public:
QModbusReply *reply;
};

4
serialConnectionManager/mainwindow.cpp

@ -1,6 +1,7 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qdebug.h"
#include <QThread>
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);
}

Loading…
Cancel
Save