From 0d9591930a3229b2b72134c5a10b02b383f34af7 Mon Sep 17 00:00:00 2001 From: nasicurious Date: Wed, 3 Nov 2021 15:37:22 +0330 Subject: [PATCH] Add SetMultipleCoil --- Servo/include/ModbusMaster.h | 1 + Servo/include/ModbusWrapper.h | 2 + Servo/src/ModbusMaster.cpp | 13 ++ Servo/src/ModbusWrapper.cpp | 11 ++ Test/MainWindow.cpp | 19 +++ Test/MainWindow.h | 1 + Test/MainWindow.ui | 298 +++++++++++++++++++++++++++++++++- 7 files changed, 339 insertions(+), 6 deletions(-) diff --git a/Servo/include/ModbusMaster.h b/Servo/include/ModbusMaster.h index 51fea7e..2addc9e 100644 --- a/Servo/include/ModbusMaster.h +++ b/Servo/include/ModbusMaster.h @@ -47,6 +47,7 @@ public slots: QVector getHoldingRegister(int startAddress, quint16 readSize); QVector getInputRegister(int startAddress, quint16 readSize); void setSingleCoil(int startAddress, bool coilFlag); + void setMultipleCoil(int startAddress, quint16 writeSize, QBitArray coilFlags); }; #endif //MODBUSMASTER_H diff --git a/Servo/include/ModbusWrapper.h b/Servo/include/ModbusWrapper.h index 3cc1d25..c6eb637 100644 --- a/Servo/include/ModbusWrapper.h +++ b/Servo/include/ModbusWrapper.h @@ -28,6 +28,7 @@ public: QVector getInputRegister(int startAddress, quint16 readSize); void setSingleCoil(int startAddress, bool coilFlag); + void setMultipleCoil(int startAddress, quint16 writeSize, QBitArray coilFlags); signals: void connectOrder(ModbusConfig modbusConfig); @@ -39,6 +40,7 @@ signals: QVector getInputRegisterOrder(int startAddress, quint16 readSize); void setSingleCoilOrder(int startAddress, bool coilFlag); + void setMultipleCoilOrder(int startAddress, quint16 writeSize, QBitArray coilFlags); //uncrustify off public slots: diff --git a/Servo/src/ModbusMaster.cpp b/Servo/src/ModbusMaster.cpp index 5717818..78aa59f 100644 --- a/Servo/src/ModbusMaster.cpp +++ b/Servo/src/ModbusMaster.cpp @@ -190,6 +190,19 @@ void ModbusMaster::setSingleCoil(int startAddress, bool coilFlag) } } +void ModbusMaster::setMultipleCoil(int startAddress, quint16 writeSize, QBitArray coilFlags) +{ + try + { + _coilsToWrite = coilFlags; + writeRequest(QModbusDataUnit::RegisterType::Coils, startAddress, writeSize); + } + 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 93ca357..46e5438 100644 --- a/Servo/src/ModbusWrapper.cpp +++ b/Servo/src/ModbusWrapper.cpp @@ -59,6 +59,11 @@ void ModbusWrapper::init() &_modbusMaster, &ModbusMaster::setSingleCoil, Qt::BlockingQueuedConnection); + connect(this, + &ModbusWrapper::setMultipleCoilOrder, + &_modbusMaster, + &ModbusMaster::setMultipleCoil, + Qt::BlockingQueuedConnection); emit initOrder(); @@ -95,6 +100,12 @@ void ModbusWrapper::setSingleCoil(int startAddress, bool coilFlag) emit setSingleCoilOrder(startAddress, coilFlag); } +/*************************************************************************************************/ +void ModbusWrapper::setMultipleCoil(int startAddress, quint16 writeSize, QBitArray coilFlags) +{ + emit setMultipleCoilOrder(startAddress, writeSize, coilFlags); +} + /*************************************************************************************************/ void ModbusWrapper::connectToDevice(ModbusConfig modbusConfig) { diff --git a/Test/MainWindow.cpp b/Test/MainWindow.cpp index e082fc6..355ccb7 100644 --- a/Test/MainWindow.cpp +++ b/Test/MainWindow.cpp @@ -102,3 +102,22 @@ void MainWindow::on_writeSingleCoil_clicked() uiCoils.resize(1); modbusWrapper.setSingleCoil(startAddress, ui->coilData->isChecked()); } + +void MainWindow::on_writeMultiCoil_clicked() +{ + int startAddress = ui->writeMultiCoilStartAddress->text().toInt(); + int writeQty = ui->writeMultiCoilQty->text().toInt(); + QString valueToWrite = ui->writeMultiCoilValue->text(); + + QBitArray uiCoils; + uiCoils.resize(writeQty); + + for(int i = 0; i < valueToWrite.size(); i++) + { + if(valueToWrite.at(i) == '0') + uiCoils[i] = false; + else + uiCoils[i] = true; + } + modbusWrapper.setMultipleCoil(startAddress, static_cast(writeQty), uiCoils); +} diff --git a/Test/MainWindow.h b/Test/MainWindow.h index c8013ab..54994a3 100644 --- a/Test/MainWindow.h +++ b/Test/MainWindow.h @@ -32,6 +32,7 @@ private slots: //uncrustify on void on_readButton_clicked(); void on_writeSingleCoil_clicked(); + void on_writeMultiCoil_clicked(); }; #endif //MAINWINDOW_H diff --git a/Test/MainWindow.ui b/Test/MainWindow.ui index 0b600f4..a22c9af 100644 --- a/Test/MainWindow.ui +++ b/Test/MainWindow.ui @@ -7,7 +7,7 @@ 0 0 800 - 600 + 507 @@ -157,7 +157,7 @@ 340 70 191 - 111 + 161 @@ -167,7 +167,7 @@ 10 - 20 + 30 61 23 @@ -180,7 +180,7 @@ 70 - 40 + 60 113 25 @@ -190,7 +190,7 @@ 10 - 40 + 60 71 26 @@ -203,7 +203,7 @@ 20 - 70 + 120 141 25 @@ -213,6 +213,289 @@ + + + + 540 + 70 + 241 + 161 + + + + Write Multiple Coil + + + + + 10 + 60 + 126 + 25 + + + + Number of values: + + + + + + 110 + 30 + 121 + 25 + + + + + + + 10 + 30 + 126 + 26 + + + + Start address: + + + + + + 140 + 60 + 91 + 25 + + + + + + + 60 + 90 + 171 + 25 + + + + + + + 10 + 90 + 51 + 25 + + + + Values + + + + + + 50 + 130 + 141 + 25 + + + + Write Multi Coil + + + + + + + 340 + 250 + 181 + 201 + + + + Write Single Register + + + + + 70 + 30 + 101 + 25 + + + + + + + 10 + 30 + 71 + 26 + + + + address: + + + + + + 60 + 70 + 111 + 25 + + + + + + + 10 + 70 + 51 + 25 + + + + Value + + + + + + 10 + 110 + 161 + 25 + + + + Write Single Register + + + + + + + 530 + 250 + 261 + 201 + + + + Write Multiple Register + + + + + 50 + 170 + 141 + 25 + + + + Write Multi Register + + + + + + 10 + 110 + 51 + 25 + + + + Values + + + + + + 10 + 60 + 126 + 25 + + + + Number of values: + + + + + + 110 + 30 + 121 + 25 + + + + + + + 140 + 60 + 91 + 25 + + + + + + + 60 + 110 + 171 + 51 + + + + + + + 10 + 30 + 126 + 26 + + + + Start address: + + + + + + 90 + 90 + 91 + 21 + + + + + Ubuntu Condensed + 8 + 75 + true + + + + Comma Delimiter + + + groupBox connect label_5 @@ -223,6 +506,9 @@ writeTable label_9 groupBox_2 + groupBox_3 + groupBox_4 + groupBox_5