From 1ab62ee3493ebf78a7f1eab735aca7bb6130db8b Mon Sep 17 00:00:00 2001 From: nasicurious Date: Wed, 3 Nov 2021 08:55:21 +0330 Subject: [PATCH] Add GetInputCoil --- Servo/include/ModbusMaster.h | 1 + Servo/include/ModbusWrapper.h | 2 ++ Servo/src/ModbusMaster.cpp | 31 +++++++++++++++++++++++++++++-- Servo/src/ModbusWrapper.cpp | 14 ++++++++++++++ Test/MainWindow.cpp | 11 +++++++---- 5 files changed, 53 insertions(+), 6 deletions(-) diff --git a/Servo/include/ModbusMaster.h b/Servo/include/ModbusMaster.h index 5f9e6a9..0f19fae 100644 --- a/Servo/include/ModbusMaster.h +++ b/Servo/include/ModbusMaster.h @@ -39,6 +39,7 @@ public slots: void connectToDevice(ModbusConfig modbusConfig); void connectionStateChanged(QModbusDevice::State state); QBitArray getCoil(int startAddress, quint16 readSize); + QBitArray getInputCoil(int startAddress, quint16 readSize); }; #endif //MODBUSMASTER_H diff --git a/Servo/include/ModbusWrapper.h b/Servo/include/ModbusWrapper.h index 2980a7a..4300a1b 100644 --- a/Servo/include/ModbusWrapper.h +++ b/Servo/include/ModbusWrapper.h @@ -22,11 +22,13 @@ public: void connectToDevice(ModbusConfig modbusConfig); void init(); QBitArray getCoil(int startAddress, quint16 readSize); + QBitArray getInputCoil(int startAddress, quint16 readSize); signals: void connectOrder(ModbusConfig modbusConfig); void initOrder(); QBitArray getCoilOrder(int startAddress, quint16 readSize); + QBitArray getInputCoilOrder(int startAddress, quint16 readSize); //uncrustify off public slots: diff --git a/Servo/src/ModbusMaster.cpp b/Servo/src/ModbusMaster.cpp index 657e507..784187c 100644 --- a/Servo/src/ModbusMaster.cpp +++ b/Servo/src/ModbusMaster.cpp @@ -84,9 +84,36 @@ QBitArray ModbusMaster::getCoil(int startAddress, quint16 readSize) for(uint i = 0; i < num; i++) { if(unit.value(static_cast(i)) == 1) - coils.setBit(i); + coils.setBit(static_cast(i)); else - coils.clearBit(i); + coils.clearBit(static_cast(i)); + } + + return coils; + } + catch(const ServoException& ex) + { + qDebug() << ex.what(); + } +} + +/*************************************************************************************************/ +QBitArray ModbusMaster::getInputCoil(int startAddress, quint16 readSize) +{ + try + { + readRequest(QModbusDataUnit::RegisterType::DiscreteInputs, startAddress, readSize); + auto unit = _modbusReplyUnit; + + QBitArray coils; + auto num = unit.valueCount(); + coils.resize(static_cast(num)); + for(uint i = 0; i < num; i++) + { + if(unit.value(static_cast(i)) == 1) + coils.setBit(static_cast(i)); + else + coils.clearBit(static_cast(i)); } return coils; diff --git a/Servo/src/ModbusWrapper.cpp b/Servo/src/ModbusWrapper.cpp index 84ad915..50c6df2 100644 --- a/Servo/src/ModbusWrapper.cpp +++ b/Servo/src/ModbusWrapper.cpp @@ -38,6 +38,11 @@ void ModbusWrapper::init() &modbusMaster, &ModbusMaster::getCoil, Qt::BlockingQueuedConnection); + connect(this, + &ModbusWrapper::getInputCoilOrder, + &modbusMaster, + &ModbusMaster::getInputCoil, + Qt::BlockingQueuedConnection); emit initOrder(); @@ -53,6 +58,15 @@ QBitArray ModbusWrapper::getCoil(int startAddress, quint16 readSize) return result; } +/*************************************************************************************************/ +QBitArray ModbusWrapper::getInputCoil(int startAddress, quint16 readSize) +{ + auto result = emit getInputCoilOrder(startAddress, readSize); + qDebug() << "getInputCoilOrder done"; + + return result; +} + /*************************************************************************************************/ void ModbusWrapper::connectToDevice(ModbusConfig modbusConfig) { diff --git a/Test/MainWindow.cpp b/Test/MainWindow.cpp index f60ab69..c7e38d1 100644 --- a/Test/MainWindow.cpp +++ b/Test/MainWindow.cpp @@ -51,11 +51,14 @@ void MainWindow::on_readButton_clicked() printCoilsDataFromClient(modbusWrapper.getCoil(startAddress, static_cast(numberOfEntries))); break; - //case 1: printCoilsDataFromSlave(modbusWrapper->getInputCoil(startAddress,static_cast(numberOfEntries))); - //break; - //case 2: printRegisterDataFromSlave(modbusWrapper->getInputRegister(startAddress,static_cast(numberOfEntries))); + + case 1: + printCoilsDataFromClient(modbusWrapper.getInputCoil(startAddress, + static_cast(numberOfEntries))); + break; + //case 2: printCoilsDataFromClient(modbusWrapper->getInputRegister(startAddress,static_cast(numberOfEntries))); //break; - //case 3: printRegisterDataFromSlave(modbusWrapper->getHoldingRegister(startAddress,static_cast(numberOfEntries))); + //case 3: printCoilsDataFromClient(modbusWrapper->getHoldingRegister(startAddress,static_cast(numberOfEntries))); //break; } }