diff --git a/Servo/Servo.pro b/Servo/Servo.pro index 23df4c0..5ffb908 100644 --- a/Servo/Servo.pro +++ b/Servo/Servo.pro @@ -24,7 +24,8 @@ SOURCES += \ $$files(*.cpp, true) HEADERS += \ - $$files(*.h, true) + $$files(*.h, true) \ + include/ExpConfig.h INCLUDEPATH += $$PWD/include diff --git a/Servo/include/ExpConfig.h b/Servo/include/ExpConfig.h index 164c5da..4fa340c 100644 --- a/Servo/include/ExpConfig.h +++ b/Servo/include/ExpConfig.h @@ -1,4 +1,12 @@ #ifndef EXPCONFIG_H #define EXPCONFIG_H -#endif // EXPCONFIG_H +#include + +struct ExpConfig +{ + bool valid = false; + QString message; +}; + +#endif //EXPCONFIG_H diff --git a/Servo/include/ModbusMaster.h b/Servo/include/ModbusMaster.h index 3fd9cf5..e2437d9 100644 --- a/Servo/include/ModbusMaster.h +++ b/Servo/include/ModbusMaster.h @@ -9,6 +9,8 @@ #include "ServoException.h" #include "ModbusConfig.h" +#include "ExpConfig.h" + class QModbusClient; class QModbusReply; @@ -42,10 +44,12 @@ public slots: void connectToDevice(ModbusConfig modbusConfig); void connectionStateChanged(QModbusDevice::State state); - QBitArray getCoil(int startAddress, quint16 readSize); + + QBitArray getCoil(int startAddress, quint16 readSize, ExpConfig& expConfig); QBitArray getInputCoil(int startAddress, quint16 readSize); 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); void setSingleRegister(int startAddress, quint16 registerValue); diff --git a/Servo/include/ModbusWrapper.h b/Servo/include/ModbusWrapper.h index bc6ddf1..3f56b3e 100644 --- a/Servo/include/ModbusWrapper.h +++ b/Servo/include/ModbusWrapper.h @@ -35,7 +35,8 @@ public: signals: void connectOrder(ModbusConfig modbusConfig); void initOrder(); - QBitArray getCoilOrder(int startAddress, quint16 readSize); + + QBitArray getCoilOrder(int startAddress, quint16 readSize, ExpConfig& expConfig); QBitArray getInputCoilOrder(int startAddress, quint16 readSize); QVector getHoldingRegisterOrder(int startAddress, quint16 readSize); @@ -47,9 +48,6 @@ signals: void setMultipleRegisterOrder(int startAddress, quint16 writeSize, QVector registerValues); - //uncrustify off -public slots: - //uncrustify on }; #endif //MODBUSWRAPPER_H diff --git a/Servo/src/ModbusMaster.cpp b/Servo/src/ModbusMaster.cpp index 32abdf9..31671a6 100644 --- a/Servo/src/ModbusMaster.cpp +++ b/Servo/src/ModbusMaster.cpp @@ -67,7 +67,7 @@ void ModbusMaster::connectionStateChanged(QModbusDevice::State state) } /*************************************************************************************************/ -QBitArray ModbusMaster::getCoil(int startAddress, quint16 readSize) +QBitArray ModbusMaster::getCoil(int startAddress, quint16 readSize, ExpConfig& expConfig) { try { @@ -89,7 +89,8 @@ QBitArray ModbusMaster::getCoil(int startAddress, quint16 readSize) } catch(const ServoException& ex) { - qDebug() << ex.what(); + expConfig.valid = true; + expConfig.message = ex.what(); } } diff --git a/Servo/src/ModbusWrapper.cpp b/Servo/src/ModbusWrapper.cpp index a58efe3..756fb3e 100644 --- a/Servo/src/ModbusWrapper.cpp +++ b/Servo/src/ModbusWrapper.cpp @@ -83,7 +83,15 @@ void ModbusWrapper::init() /*************************************************************************************************/ QBitArray ModbusWrapper::getCoil(int startAddress, quint16 readSize) { - return emit getCoilOrder(startAddress, readSize); + ExpConfig exp; + auto ret = emit getCoilOrder(startAddress, readSize, exp); + + if(exp.valid) + { + throw ServoException(exp.message); + } + + return ret; } /*************************************************************************************************/ diff --git a/Test/MainWindow.cpp b/Test/MainWindow.cpp index 8fd0208..1133474 100644 --- a/Test/MainWindow.cpp +++ b/Test/MainWindow.cpp @@ -46,29 +46,38 @@ void MainWindow::on_readButton_clicked() { int startAddress = ui->startAddress->text().toInt(); int numberOfEntries = ui->readSize->text().toInt(); - switch(ui->writeTable->currentData().toInt()) + try + { + switch(ui->writeTable->currentData().toInt()) + { + case 0: + printCoilsDataFromClient(modbusWrapper.getCoil(startAddress, + static_cast(numberOfEntries))); + break; + + case 1: + printCoilsDataFromClient(modbusWrapper.getInputCoil(startAddress, + static_cast( + numberOfEntries))); + break; + + case 2: + printRegisterDataFromClient(modbusWrapper.getInputRegister(startAddress, + static_cast( + numberOfEntries))); + break; + + case 3: + printRegisterDataFromClient(modbusWrapper.getHoldingRegister(startAddress, + static_cast( + numberOfEntries))); + break; + } + } + catch(const ServoException& exp) { - case 0: - printCoilsDataFromClient(modbusWrapper.getCoil(startAddress, - static_cast(numberOfEntries))); - break; - - case 1: - printCoilsDataFromClient(modbusWrapper.getInputCoil(startAddress, - static_cast(numberOfEntries))); - break; - - case 2: - printRegisterDataFromClient(modbusWrapper.getInputRegister(startAddress, - static_cast( - numberOfEntries))); - break; - - case 3: - printRegisterDataFromClient(modbusWrapper.getHoldingRegister(startAddress, - static_cast( - numberOfEntries))); - break; + qDebug() << "aaaa"; + qDebug() << exp.what(); } }