Browse Source

multithread 14000727-2

master
nasi 3 years ago
parent
commit
eda3bacc22
  1. 33
      serialConnectionManager/ModbusMaster.cpp
  2. 33
      serialConnectionManager/mainwindow.cpp
  3. 5
      serialConnectionManager/mainwindow.h

33
serialConnectionManager/ModbusMaster.cpp

@ -107,7 +107,9 @@ void modBusMaster::setMultipleRegister(int startAddress, quint16 writeSize , QVe
QBitArray modBusMaster::getCoil(int startAddress, quint16 readSize) QBitArray modBusMaster::getCoil(int startAddress, quint16 readSize)
{ {
qDebug() << " QThread::currentThreadId() getCoil befor" <<QThread::currentThreadId() ;
QModbusDataUnit unit = readRequest(QModbusDataUnit::RegisterType::Coils, startAddress, readSize); QModbusDataUnit unit = readRequest(QModbusDataUnit::RegisterType::Coils, startAddress, readSize);
qDebug() << " QThread::currentThreadId() getCoil after" <<QThread::currentThreadId() ;
mutex.lock(); mutex.lock();
if(!freeThread) if(!freeThread)
modBusAnswerNotReady.wait(&mutex); modBusAnswerNotReady.wait(&mutex);
@ -181,33 +183,32 @@ QVector<quint16> modBusMaster::getHoldingRegister(int startAddress, quint16 rea
QModbusDataUnit modBusMaster::readRequest(QModbusDataUnit::RegisterType registerType, int startAddress, quint16 readSize) QModbusDataUnit modBusMaster::readRequest(QModbusDataUnit::RegisterType registerType, int startAddress, quint16 readSize)
{ {
qDebug() << " QThread::currentThreadId() readRequest1" <<QThread::currentThreadId() ;
auto table =registerType; auto table =registerType;
if (auto *reply =modbusDevice->sendReadRequest(QModbusDataUnit(table, startAddress, readSize), _slaveAddress)) if (auto *reply =modbusDevice->sendReadRequest(QModbusDataUnit(table, startAddress, readSize), _slaveAddress))
{ {
QFuture<void> future = QtConcurrent::run([this, reply]() { QFuture<void> future = QtConcurrent::run([this, reply]() {
qDebug() << " QThread::currentThreadId() inside worker thread" <<QThread::currentThreadId() ;
// while(!reply->isFinished()) // while(!reply->isFinished())
// { // {
// qDebug() << "reply->isFinished() " << reply->isFinished(); // qDebug() << "reply->isFinished() " << reply->isFinished();
// qDebug() << "Hello from thread " << QThread::currentThread(); // qDebug() << "Hello from thread " << QThread::currentThread();
// } // }
if (!freeThread)
if (!reply->isFinished())
{ {
if (!reply->isFinished()) qDebug() << " QThread::currentThreadId() inside worker thread isFinished" <<QThread::currentThreadId() ;
{ qDebug() << "1reply->isFinished() " << reply->isFinished();
qDebug() << "1reply->isFinished() " << reply->isFinished(); connect(reply, &QModbusReply::finished, this, &modBusMaster::modbusReplyRecieved);
connect(reply, &QModbusReply::finished, this, &modBusMaster::modbusReplyRecieved, Qt::ConnectionType::BlockingQueuedConnection); qDebug() << "2reply->isFinished() " << reply->isFinished();
qDebug() << "2reply->isFinished() " << reply->isFinished();
}
} }
});
});
qDebug() << " QThread::currentThreadId() readRequest3" <<QThread::currentThreadId() ;
// freeThread=true; // freeThread=true;
mutex.lock();
if (freeThread)
modBusAnswerNotReady.wakeAll();
mutex.unlock();
if(!checkForError(reply)) if(!checkForError(reply))
{ {
@ -246,13 +247,17 @@ bool modBusMaster::checkForError(QModbusReply *_reply)
void modBusMaster::modbusReplyRecieved() void modBusMaster::modbusReplyRecieved()
{ {
qDebug() << " QThread::currentThreadId() inside slot" <<QThread::currentThreadId() ;
// auto reply = qobject_cast<QModbusReply *>(sender()); // auto reply = qobject_cast<QModbusReply *>(sender());
// if (!reply) // if (!reply)
// return; // return;
// modbusReplyFromClient = reply; // modbusReplyFromClient = reply;
freeThread=true; freeThread=true;
mutex.lock();
if (freeThread)
modBusAnswerNotReady.wakeAll();
mutex.unlock();
// reply->deleteLater(); // reply->deleteLater();

33
serialConnectionManager/mainwindow.cpp

@ -2,6 +2,7 @@
#include "ui_mainwindow.h" #include "ui_mainwindow.h"
#include "qdebug.h" #include "qdebug.h"
#include <QThread> #include <QThread>
#include "QtConcurrent/QtConcurrent"
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
, ui(new Ui::MainWindow) , ui(new Ui::MainWindow)
@ -11,13 +12,21 @@ MainWindow::MainWindow(QWidget *parent)
ui->writeTable->addItem(tr("Discrete Inputs"), 1); ui->writeTable->addItem(tr("Discrete Inputs"), 1);
ui->writeTable->addItem(tr("Input Registers"), 2); ui->writeTable->addItem(tr("Input Registers"), 2);
ui->writeTable->addItem(tr("Holding Registers"), 3); ui->writeTable->addItem(tr("Holding Registers"), 3);
connect(&modBusObj, &modBusMaster::modbusConnectionState, this, &MainWindow::connectionState); modBusObj = new modBusMaster();
modBusObj->moveToThread(&workerThread);
workerThread.setObjectName("workerThread");
workerThread.start();
// the thread starts the event loop and blocks waiting for events
connect(modBusObj, &modBusMaster::modbusConnectionState, this, &MainWindow::connectionState);
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
{ {
delete ui; delete ui;
workerThread.quit();
workerThread.wait();
} }
@ -25,12 +34,12 @@ void MainWindow::on_connectButton_clicked()
{ {
if( ui->connectButton->text()==tr("Disconnect")) if( ui->connectButton->text()==tr("Disconnect"))
{ {
modBusObj.stop(); modBusObj->stop();
} }
else else
{ {
modBusObj.init(); modBusObj->init();
modBusObj.open(ui->portEdit->text(),ui->serverEdit->value()); modBusObj->open(ui->portEdit->text(),ui->serverEdit->value());
} }
@ -42,13 +51,13 @@ void MainWindow::on_readButton_clicked()
// do not go beyond 10 entries // do not go beyond 10 entries
int numberOfEntries = qMin(ui->readSize->currentText().toInt(), 10 - startAddress); int numberOfEntries = qMin(ui->readSize->currentText().toInt(), 10 - startAddress);
switch (ui->writeTable->currentData().toInt()) { switch (ui->writeTable->currentData().toInt()) {
case 0: printCoilsDataFromSlave(modBusObj.getCoil(startAddress,static_cast<quint16>(numberOfEntries))); case 0: printCoilsDataFromSlave(modBusObj->getCoil(startAddress,static_cast<quint16>(numberOfEntries)));
break; break;
case 1: printCoilsDataFromSlave(modBusObj.getInputCoil(startAddress,static_cast<quint16>(numberOfEntries))); case 1: printCoilsDataFromSlave(modBusObj->getInputCoil(startAddress,static_cast<quint16>(numberOfEntries)));
break; break;
case 2: printRegisterDataFromSlave(modBusObj.getInputRegister(startAddress,static_cast<quint16>(numberOfEntries))); case 2: printRegisterDataFromSlave(modBusObj->getInputRegister(startAddress,static_cast<quint16>(numberOfEntries)));
break; break;
case 3: printRegisterDataFromSlave(modBusObj.getHoldingRegister(startAddress,static_cast<quint16>(numberOfEntries))); case 3: printRegisterDataFromSlave(modBusObj->getHoldingRegister(startAddress,static_cast<quint16>(numberOfEntries)));
break; break;
} }
@ -97,11 +106,11 @@ void MainWindow::on_writeButton_clicked()
{ {
uiCoils.setBit(0,ui->coilData1->isChecked()); uiCoils.setBit(0,ui->coilData1->isChecked());
uiCoils.setBit(1,ui->coilData2->isChecked()); uiCoils.setBit(1,ui->coilData2->isChecked());
modBusObj.setMultipleCoil(startAddress, numberOfEntries, uiCoils); modBusObj->setMultipleCoil(startAddress, numberOfEntries, uiCoils);
} }
else if (numberOfEntries==1) else if (numberOfEntries==1)
{ {
modBusObj.setSingleCoil(startAddress, ui->coilData1->isChecked()); modBusObj->setSingleCoil(startAddress, ui->coilData1->isChecked());
} }
break; break;
@ -110,11 +119,11 @@ void MainWindow::on_writeButton_clicked()
{ {
uiholdingRegisters[0]=ui->textEdit1->toPlainText().toUInt(); uiholdingRegisters[0]=ui->textEdit1->toPlainText().toUInt();
uiholdingRegisters[1]=ui->textEdit2->toPlainText().toUInt(); uiholdingRegisters[1]=ui->textEdit2->toPlainText().toUInt();
modBusObj.setMultipleRegister(startAddress, numberOfEntries, uiholdingRegisters); modBusObj->setMultipleRegister(startAddress, numberOfEntries, uiholdingRegisters);
} }
else if (numberOfEntries==1) else if (numberOfEntries==1)
{ {
modBusObj.setSingleRegister(startAddress, ui->textEdit1->toPlainText().toUInt()); modBusObj->setSingleRegister(startAddress, ui->textEdit1->toPlainText().toUInt());
} }
break; break;
} }

5
serialConnectionManager/mainwindow.h

@ -1,6 +1,6 @@
#ifndef MAINWINDOW_H #ifndef MAINWINDOW_H
#define MAINWINDOW_H #define MAINWINDOW_H
#include <QThread>
#include <QMainWindow> #include <QMainWindow>
#include "ModbusMaster.h" #include "ModbusMaster.h"
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -26,8 +26,9 @@ private slots:
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
modBusMaster modBusObj; modBusMaster *modBusObj;
QBitArray uiCoils; QBitArray uiCoils;
QVector<quint16> uiholdingRegisters; QVector<quint16> uiholdingRegisters;
QThread workerThread;
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

Loading…
Cancel
Save