From 1059eb2c8b77c3c2bafb8434e76251b9dbb77469 Mon Sep 17 00:00:00 2001 From: nasicurious Date: Wed, 3 Nov 2021 12:36:52 +0330 Subject: [PATCH] Add GetHoldingRegister --- Servo/include/ModbusMaster.h | 1 + Servo/include/ModbusWrapper.h | 7 +++++-- Servo/src/ModbusMaster.cpp | 25 +++++++++++++++++++++++++ Servo/src/ModbusWrapper.cpp | 34 ++++++++++++++++++++++++---------- Test/MainWindow.cpp | 24 ++++++++++++++++++++---- Test/MainWindow.h | 1 + 6 files changed, 76 insertions(+), 16 deletions(-) diff --git a/Servo/include/ModbusMaster.h b/Servo/include/ModbusMaster.h index 0f19fae..edde8fe 100644 --- a/Servo/include/ModbusMaster.h +++ b/Servo/include/ModbusMaster.h @@ -40,6 +40,7 @@ public slots: void connectionStateChanged(QModbusDevice::State state); QBitArray getCoil(int startAddress, quint16 readSize); QBitArray getInputCoil(int startAddress, quint16 readSize); + QVector getHoldingRegister(int startAddress, quint16 readSize); }; #endif //MODBUSMASTER_H diff --git a/Servo/include/ModbusWrapper.h b/Servo/include/ModbusWrapper.h index 4300a1b..553297a 100644 --- a/Servo/include/ModbusWrapper.h +++ b/Servo/include/ModbusWrapper.h @@ -13,8 +13,8 @@ class ModbusWrapper : public QObject Q_OBJECT private: - ModbusMaster modbusMaster; - QThread workerThread; + ModbusMaster _modbusMaster; + QThread _workerThread; public: explicit ModbusWrapper(QObject* parent = nullptr); @@ -23,12 +23,15 @@ public: void init(); QBitArray getCoil(int startAddress, quint16 readSize); QBitArray getInputCoil(int startAddress, quint16 readSize); + QVector getHoldingRegister(int startAddress, quint16 readSize); signals: void connectOrder(ModbusConfig modbusConfig); void initOrder(); QBitArray getCoilOrder(int startAddress, quint16 readSize); QBitArray getInputCoilOrder(int startAddress, quint16 readSize); + QVector getHoldingRegisterOrder(int startAddress, + quint16 readSize); //uncrustify off public slots: diff --git a/Servo/src/ModbusMaster.cpp b/Servo/src/ModbusMaster.cpp index 784187c..9bdc6ce 100644 --- a/Servo/src/ModbusMaster.cpp +++ b/Servo/src/ModbusMaster.cpp @@ -124,6 +124,31 @@ QBitArray ModbusMaster::getInputCoil(int startAddress, quint16 readSize) } } +/*************************************************************************************************/ +QVector ModbusMaster::getHoldingRegister(int startAddress, quint16 readSize) +{ + try + { + readRequest(QModbusDataUnit::RegisterType::HoldingRegisters, startAddress, readSize); + auto unit = _modbusReplyUnit; + + QVector registerValues; + auto num = unit.valueCount(); + registerValues.resize(static_cast(num)); + + for(uint i = 0; i < unit.valueCount(); i++) + { + registerValues[static_cast(i)] = unit.value(static_cast(i)); + } + + return registerValues; + } + catch(const ServoException& ex) + { + qDebug() << ex.what(); + } +} + /*************************************************************************************************/ void ModbusMaster::readRequest(QModbusDataUnit::RegisterType registerType, int startAddress, diff --git a/Servo/src/ModbusWrapper.cpp b/Servo/src/ModbusWrapper.cpp index 50c6df2..1f802cc 100644 --- a/Servo/src/ModbusWrapper.cpp +++ b/Servo/src/ModbusWrapper.cpp @@ -8,41 +8,46 @@ ModbusWrapper::ModbusWrapper(QObject* parent) : QObject(parent) /*************************************************************************************************/ ModbusWrapper::~ModbusWrapper() { - workerThread.quit(); - workerThread.wait(); + _workerThread.quit(); + _workerThread.wait(); } /*************************************************************************************************/ void ModbusWrapper::init() { - modbusMaster.moveToThread(&workerThread); - workerThread.setObjectName("workerThread"); - workerThread.start(); + _modbusMaster.moveToThread(&_workerThread); + _workerThread.setObjectName("workerThread"); + _workerThread.start(); QEventLoop loop; - connect(&workerThread, &QThread::started, &loop, &QEventLoop::quit); + connect(&_workerThread, &QThread::started, &loop, &QEventLoop::quit); loop.exec(); connect(this, &ModbusWrapper::initOrder, - &modbusMaster, + &_modbusMaster, &ModbusMaster::init, Qt::BlockingQueuedConnection); connect(this, &ModbusWrapper::connectOrder, - &modbusMaster, + &_modbusMaster, &ModbusMaster::connectToDevice, Qt::BlockingQueuedConnection); connect(this, &ModbusWrapper::getCoilOrder, - &modbusMaster, + &_modbusMaster, &ModbusMaster::getCoil, Qt::BlockingQueuedConnection); connect(this, &ModbusWrapper::getInputCoilOrder, - &modbusMaster, + &_modbusMaster, &ModbusMaster::getInputCoil, Qt::BlockingQueuedConnection); + connect(this, + &ModbusWrapper::getHoldingRegisterOrder, + &_modbusMaster, + &ModbusMaster::getHoldingRegister, + Qt::BlockingQueuedConnection); emit initOrder(); @@ -67,6 +72,15 @@ QBitArray ModbusWrapper::getInputCoil(int startAddress, quint16 readSize) return result; } +/*************************************************************************************************/ +QVector ModbusWrapper::getHoldingRegister(int startAddress, quint16 readSize) +{ + auto result = emit getHoldingRegisterOrder(startAddress, readSize); + qDebug() << "getHoldingRegister done"; + + return result; +} + /*************************************************************************************************/ void ModbusWrapper::connectToDevice(ModbusConfig modbusConfig) { diff --git a/Test/MainWindow.cpp b/Test/MainWindow.cpp index c7e38d1..5b852bb 100644 --- a/Test/MainWindow.cpp +++ b/Test/MainWindow.cpp @@ -56,10 +56,14 @@ void MainWindow::on_readButton_clicked() printCoilsDataFromClient(modbusWrapper.getInputCoil(startAddress, static_cast(numberOfEntries))); break; - //case 2: printCoilsDataFromClient(modbusWrapper->getInputRegister(startAddress,static_cast(numberOfEntries))); - //break; - //case 3: printCoilsDataFromClient(modbusWrapper->getHoldingRegister(startAddress,static_cast(numberOfEntries))); - //break; + + //case 2: printCoilsDataFromClient(modbusWrapper.(startAddress,static_cast(numberOfEntries))); + //break; + case 3: + printRegisterDataFromClient(modbusWrapper.getHoldingRegister(startAddress, + static_cast( + numberOfEntries))); + break; } } @@ -74,3 +78,15 @@ void MainWindow::printCoilsDataFromClient(QBitArray uiCoils) } ui->textEditRead->setText(readedData); } + +void MainWindow::printRegisterDataFromClient(QVector uiHoldingRegisters) +{ + QString readedData; + for(int i = 0; i < uiHoldingRegisters.count(); i++) + { + const QString entry = tr("Number: %1, Value: %2").arg(i) + .arg(QString::number(uiHoldingRegisters[i])); + readedData += (entry + "\n"); + } + ui->textEditRead->setText(readedData); +} diff --git a/Test/MainWindow.h b/Test/MainWindow.h index cc2c3d8..2eada0a 100644 --- a/Test/MainWindow.h +++ b/Test/MainWindow.h @@ -24,6 +24,7 @@ public: MainWindow(QWidget* parent = nullptr); ~MainWindow(); void printCoilsDataFromClient(QBitArray uiCoils); + void printRegisterDataFromClient(QVector uiHoldingRegisters); //uncrustify off private slots: