Browse Source

Add struct for Exception

test
nasicurious 3 years ago
parent
commit
52809a445c
  1. 3
      Servo/Servo.pro
  2. 10
      Servo/include/ExpConfig.h
  3. 6
      Servo/include/ModbusMaster.h
  4. 6
      Servo/include/ModbusWrapper.h
  5. 5
      Servo/src/ModbusMaster.cpp
  6. 10
      Servo/src/ModbusWrapper.cpp
  7. 53
      Test/MainWindow.cpp

3
Servo/Servo.pro

@ -24,7 +24,8 @@ SOURCES += \
$$files(*.cpp, true)
HEADERS += \
$$files(*.h, true)
$$files(*.h, true) \
include/ExpConfig.h
INCLUDEPATH += $$PWD/include

10
Servo/include/ExpConfig.h

@ -1,4 +1,12 @@
#ifndef EXPCONFIG_H
#define EXPCONFIG_H
#endif // EXPCONFIG_H
#include <QString>
struct ExpConfig
{
bool valid = false;
QString message;
};
#endif //EXPCONFIG_H

6
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<quint16> getHoldingRegister(int startAddress, quint16 readSize);
QVector<quint16> 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);

6
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<quint16> getHoldingRegisterOrder(int startAddress,
quint16 readSize);
@ -47,9 +48,6 @@ signals:
void setMultipleRegisterOrder(int startAddress,
quint16 writeSize,
QVector<quint16> registerValues);
//uncrustify off
public slots:
//uncrustify on
};
#endif //MODBUSWRAPPER_H

5
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();
}
}

10
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;
}
/*************************************************************************************************/

53
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<quint16>(numberOfEntries)));
break;
case 1:
printCoilsDataFromClient(modbusWrapper.getInputCoil(startAddress,
static_cast<quint16>(
numberOfEntries)));
break;
case 2:
printRegisterDataFromClient(modbusWrapper.getInputRegister(startAddress,
static_cast<quint16>(
numberOfEntries)));
break;
case 3:
printRegisterDataFromClient(modbusWrapper.getHoldingRegister(startAddress,
static_cast<quint16>(
numberOfEntries)));
break;
}
}
catch(const ServoException& exp)
{
case 0:
printCoilsDataFromClient(modbusWrapper.getCoil(startAddress,
static_cast<quint16>(numberOfEntries)));
break;
case 1:
printCoilsDataFromClient(modbusWrapper.getInputCoil(startAddress,
static_cast<quint16>(numberOfEntries)));
break;
case 2:
printRegisterDataFromClient(modbusWrapper.getInputRegister(startAddress,
static_cast<quint16>(
numberOfEntries)));
break;
case 3:
printRegisterDataFromClient(modbusWrapper.getHoldingRegister(startAddress,
static_cast<quint16>(
numberOfEntries)));
break;
qDebug() << "aaaa";
qDebug() << exp.what();
}
}

Loading…
Cancel
Save