diff --git a/Servo/include/ModbusMaster.h b/Servo/include/ModbusMaster.h index 2addc9e..3fd9cf5 100644 --- a/Servo/include/ModbusMaster.h +++ b/Servo/include/ModbusMaster.h @@ -48,6 +48,8 @@ public slots: QVector getInputRegister(int startAddress, quint16 readSize); void setSingleCoil(int startAddress, bool coilFlag); void setMultipleCoil(int startAddress, quint16 writeSize, QBitArray coilFlags); + void setSingleRegister(int startAddress, quint16 registerValue); + void setMultipleRegister(int startAddress, quint16 writeSize, QVector registerValues); }; #endif //MODBUSMASTER_H diff --git a/Servo/include/ModbusWrapper.h b/Servo/include/ModbusWrapper.h index c6eb637..bc6ddf1 100644 --- a/Servo/include/ModbusWrapper.h +++ b/Servo/include/ModbusWrapper.h @@ -29,6 +29,8 @@ public: void setSingleCoil(int startAddress, bool coilFlag); void setMultipleCoil(int startAddress, quint16 writeSize, QBitArray coilFlags); + void setSingleRegister(int startAddress, quint16 registerValue); + void setMultipleRegister(int startAddress, quint16 writeSize, QVector registerValues); signals: void connectOrder(ModbusConfig modbusConfig); @@ -41,7 +43,10 @@ signals: void setSingleCoilOrder(int startAddress, bool coilFlag); void setMultipleCoilOrder(int startAddress, quint16 writeSize, QBitArray coilFlags); - + void setSingleRegisterOrder(int startAddress, quint16 registerValue); + void setMultipleRegisterOrder(int startAddress, + quint16 writeSize, + QVector registerValues); //uncrustify off public slots: //uncrustify on diff --git a/Servo/src/ModbusMaster.cpp b/Servo/src/ModbusMaster.cpp index 78aa59f..32abdf9 100644 --- a/Servo/src/ModbusMaster.cpp +++ b/Servo/src/ModbusMaster.cpp @@ -203,6 +203,36 @@ void ModbusMaster::setMultipleCoil(int startAddress, quint16 writeSize, QBitArra } } +void ModbusMaster::setSingleRegister(int startAddress, quint16 registerValue) +{ + try + { + _RegistersToWrite.resize(1); + _RegistersToWrite[0] = registerValue; + writeRequest(QModbusDataUnit::RegisterType::HoldingRegisters, startAddress, 1); + } + catch(const ServoException& ex) + { + qDebug() << ex.what(); + } +} + +/*************************************************************************************************/ +void ModbusMaster::setMultipleRegister(int startAddress, + quint16 writeSize, + QVector registerValues) +{ + try + { + _RegistersToWrite = registerValues; + writeRequest(QModbusDataUnit::RegisterType::HoldingRegisters, 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 46e5438..a58efe3 100644 --- a/Servo/src/ModbusWrapper.cpp +++ b/Servo/src/ModbusWrapper.cpp @@ -64,6 +64,16 @@ void ModbusWrapper::init() &_modbusMaster, &ModbusMaster::setMultipleCoil, Qt::BlockingQueuedConnection); + connect(this, + &ModbusWrapper::setSingleRegisterOrder, + &_modbusMaster, + &ModbusMaster::setSingleRegister, + Qt::BlockingQueuedConnection); + connect(this, + &ModbusWrapper::setMultipleRegisterOrder, + &_modbusMaster, + &ModbusMaster::setMultipleRegister, + Qt::BlockingQueuedConnection); emit initOrder(); @@ -106,6 +116,20 @@ void ModbusWrapper::setMultipleCoil(int startAddress, quint16 writeSize, QBitArr emit setMultipleCoilOrder(startAddress, writeSize, coilFlags); } +/*************************************************************************************************/ +void ModbusWrapper::setSingleRegister(int startAddress, quint16 registerValue) +{ + emit setSingleRegisterOrder(startAddress, registerValue); +} + +/*************************************************************************************************/ +void ModbusWrapper::setMultipleRegister(int startAddress, + quint16 writeSize, + QVector registerValues) +{ + emit setMultipleRegisterOrder(startAddress, writeSize, registerValues); +} + /*************************************************************************************************/ void ModbusWrapper::connectToDevice(ModbusConfig modbusConfig) { diff --git a/Test/MainWindow.cpp b/Test/MainWindow.cpp index 355ccb7..8fd0208 100644 --- a/Test/MainWindow.cpp +++ b/Test/MainWindow.cpp @@ -41,6 +41,7 @@ void MainWindow::on_connect_clicked() } } +/*************************************************************************************************/ void MainWindow::on_readButton_clicked() { int startAddress = ui->startAddress->text().toInt(); @@ -71,6 +72,7 @@ void MainWindow::on_readButton_clicked() } } +/*************************************************************************************************/ void MainWindow::printCoilsDataFromClient(QBitArray uiCoils) { QString readedData; @@ -83,6 +85,7 @@ void MainWindow::printCoilsDataFromClient(QBitArray uiCoils) ui->textEditRead->setText(readedData); } +/*************************************************************************************************/ void MainWindow::printRegisterDataFromClient(QVector uiHoldingRegisters) { QString readedData; @@ -95,14 +98,14 @@ void MainWindow::printRegisterDataFromClient(QVector uiHoldingRegisters ui->textEditRead->setText(readedData); } +/*************************************************************************************************/ void MainWindow::on_writeSingleCoil_clicked() { int startAddress = ui->writeSingleCoilAddress->text().toInt(); - QBitArray uiCoils; - uiCoils.resize(1); modbusWrapper.setSingleCoil(startAddress, ui->coilData->isChecked()); } +/*************************************************************************************************/ void MainWindow::on_writeMultiCoil_clicked() { int startAddress = ui->writeMultiCoilStartAddress->text().toInt(); @@ -121,3 +124,32 @@ void MainWindow::on_writeMultiCoil_clicked() } modbusWrapper.setMultipleCoil(startAddress, static_cast(writeQty), uiCoils); } + +/*************************************************************************************************/ +void MainWindow::on_writeSingleRegister_clicked() +{ + int startAddress = ui->writeSingleRegisterAddress->text().toInt(); + quint16 value = static_cast(ui->writeSingleRegisterValue->text().toInt()); + modbusWrapper.setSingleRegister(startAddress, value); +} + +/*************************************************************************************************/ +void MainWindow::on_writeMultiRegister_clicked() +{ + int startAddress = ui->writeMultiRegisterStartAddress->text().toInt(); + int writeQty = ui->writeMultiRegisterQty->text().toInt(); + QString valueToWrite = ui->writeMultiRegisterValue->text(); + QVector uiHoldingRegisters; + uiHoldingRegisters.resize(writeQty); + + QRegExp rx("(\\ |\\,|\\.|\\:|\\t)"); //RegEx for ' ' or ',' or '.' or ':' or '\t' + QStringList stringListValue = valueToWrite.split(rx); + + for(int i = 0; i < stringListValue.size(); i++) + { + uiHoldingRegisters[i] = static_cast(stringListValue[i].toInt()); + } + modbusWrapper.setMultipleRegister(startAddress, + static_cast(writeQty), + uiHoldingRegisters); +} diff --git a/Test/MainWindow.h b/Test/MainWindow.h index 54994a3..6b63513 100644 --- a/Test/MainWindow.h +++ b/Test/MainWindow.h @@ -33,6 +33,8 @@ private slots: void on_readButton_clicked(); void on_writeSingleCoil_clicked(); void on_writeMultiCoil_clicked(); + void on_writeSingleRegister_clicked(); + void on_writeMultiRegister_clicked(); }; #endif //MAINWINDOW_H diff --git a/Test/MainWindow.ui b/Test/MainWindow.ui index a22c9af..34ac62c 100644 --- a/Test/MainWindow.ui +++ b/Test/MainWindow.ui @@ -343,7 +343,7 @@ address: - + 60