Browse Source

update Exception

test
nasicurious 3 years ago
parent
commit
08c958f91d
  1. 28
      Servo/include/ModbusMaster.h
  2. 24
      Servo/include/ModbusWrapper.h
  3. 5
      Servo/include/ServoException.h
  4. 157
      Servo/src/ModbusMaster.cpp
  5. 71
      Servo/src/ModbusWrapper.cpp
  6. 91
      Test/MainWindow.cpp
  7. 1000
      Test/MainWindow.ui
  8. 1
      Test/main.cpp

28
Servo/include/ModbusMaster.h

@ -36,24 +36,30 @@ public:
void writeRequest(QModbusDataUnit::RegisterType registerType,
int startAddress,
quint16 writeSize);
void close();
//uncrustify off
public slots:
//uncrustify on
void init();
void connectToDevice(ModbusConfig modbusConfig);
void init(ExpConfig& expConfig);
void connectToDevice(ModbusConfig modbusConfig, ExpConfig& expConfig);
void connectionStateChanged(QModbusDevice::State state);
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);
void setMultipleRegister(int startAddress, quint16 writeSize, QVector<quint16> registerValues);
QBitArray getInputCoil(int startAddress, quint16 readSize, ExpConfig& expConfig);
QVector<quint16> getHoldingRegister(int startAddress, quint16 readSize, ExpConfig& expConfig);
QVector<quint16> getInputRegister(int startAddress, quint16 readSize, ExpConfig& expConfig);
void setSingleCoil(int startAddress, bool coilFlag, ExpConfig& expConfig);
void setMultipleCoil(int startAddress,
quint16 writeSize,
QBitArray coilFlags,
ExpConfig& expConfig);
void setSingleRegister(int startAddress, quint16 registerValue, ExpConfig& expConfig);
void setMultipleRegister(int startAddress,
quint16 writeSize,
QVector<quint16> registerValues,
ExpConfig& expConfig);
};
#endif //MODBUSMASTER_H

24
Servo/include/ModbusWrapper.h

@ -33,21 +33,25 @@ public:
void setMultipleRegister(int startAddress, quint16 writeSize, QVector<quint16> registerValues);
signals:
void connectOrder(ModbusConfig modbusConfig);
void initOrder();
void connectOrder(ModbusConfig modbusConfig, ExpConfig& expConfig);
void initOrder(ExpConfig& expConfig);
QBitArray getCoilOrder(int startAddress, quint16 readSize, ExpConfig& expConfig);
QBitArray getInputCoilOrder(int startAddress, quint16 readSize);
QBitArray getInputCoilOrder(int startAddress, quint16 readSize, ExpConfig& expConfig);
QVector<quint16> getHoldingRegisterOrder(int startAddress,
quint16 readSize);
QVector<quint16> getInputRegisterOrder(int startAddress, quint16 readSize);
void setSingleCoilOrder(int startAddress, bool coilFlag);
void setMultipleCoilOrder(int startAddress, quint16 writeSize, QBitArray coilFlags);
void setSingleRegisterOrder(int startAddress, quint16 registerValue);
quint16 readSize, ExpConfig& expConfig);
QVector<quint16> getInputRegisterOrder(int startAddress, quint16 readSize,
ExpConfig& expConfig);
void setSingleCoilOrder(int startAddress, bool coilFlag, ExpConfig& expConfig);
void setMultipleCoilOrder(int startAddress,
quint16 writeSize,
QBitArray coilFlags,
ExpConfig& expConfig);
void setSingleRegisterOrder(int startAddress, quint16 registerValue, ExpConfig& expConfig);
void setMultipleRegisterOrder(int startAddress,
quint16 writeSize,
QVector<quint16> registerValues);
QVector<quint16> registerValues, ExpConfig& expConfig);
};
#endif //MODBUSWRAPPER_H

5
Servo/include/ServoException.h

@ -19,6 +19,11 @@ public:
{
return _str.toStdString().c_str();
}
QString getMessage() const
{
return _str;
}
};
#endif //SERVOEXCEPTION_H

157
Servo/src/ModbusMaster.cpp

@ -8,61 +8,79 @@ ModbusMaster::ModbusMaster(QObject* parent) : QObject(parent)
}
/*************************************************************************************************/
void ModbusMaster::init()
void ModbusMaster::init(ExpConfig& expConfig)
{
if(!_initialized)
{
_modbusDevice = nullptr;
_modbusDevice = new QModbusRtuSerialMaster(this);
connect(_modbusDevice, &QModbusClient::errorOccurred, [this](QModbusDevice::Error)
try {
if(!_initialized)
{
throw ServoException(_modbusDevice->errorString());
});
_modbusDevice = nullptr;
_modbusDevice = new QModbusRtuSerialMaster(this);
connect(_modbusDevice, &QModbusClient::errorOccurred, [this](QModbusDevice::Error)
{
throw ServoException(_modbusDevice->errorString());
});
connect(_modbusDevice, &QModbusDevice::stateChanged, this,
&ModbusMaster::connectionStateChanged);
connect(_modbusDevice,
&QModbusClient::stateChanged,
this,
&ModbusMaster::connectionStateChanged);
_initialized = true;
_initialized = true;
}
else
{
throw ServoException(
"Modbus Device Object Created Before First Delete it Then Make New One");
}
}
else
catch(const ServoException& ex)
{
throw ServoException("Modbus Device Created Before Handle it");
expConfig.valid = true;
expConfig.message = ex.getMessage();
}
}
/*************************************************************************************************/
void ModbusMaster::connectToDevice(ModbusConfig modbusConfig)
void ModbusMaster::connectionStateChanged(QModbusDevice::State state)
{
if(_modbusDevice->state() != QModbusDevice::ConnectedState)
if(state == QModbusDevice::UnconnectedState)
{
_clientAddress = modbusConfig.clientAddress;
_modbusDevice->setConnectionParameter(QModbusDevice::SerialPortNameParameter,
modbusConfig.serialPort);
_modbusDevice->setConnectionParameter(QModbusDevice::SerialParityParameter,
modbusConfig.parity);
_modbusDevice->setConnectionParameter(QModbusDevice::SerialBaudRateParameter,
modbusConfig.baud);
_modbusDevice->setConnectionParameter(QModbusDevice::SerialDataBitsParameter,
modbusConfig.dataBits);
_modbusDevice->setConnectionParameter(QModbusDevice::SerialStopBitsParameter,
modbusConfig.stopBits);
_modbusDevice->setTimeout(modbusConfig.responseTime);
_modbusDevice->setNumberOfRetries(modbusConfig.numberOfRetries);
if(!_modbusDevice->connectDevice())
{
throw ServoException(_modbusDevice->errorString());
}
throw ServoException("Connection wasnt prepared");
}
}
/*************************************************************************************************/
void ModbusMaster::connectionStateChanged(QModbusDevice::State state)
void ModbusMaster::connectToDevice(ModbusConfig modbusConfig, ExpConfig& expConfig)
{
if(state == QModbusDevice::UnconnectedState)
try {
if(_modbusDevice->state() != QModbusDevice::ConnectedState)
{
_clientAddress = modbusConfig.clientAddress;
_modbusDevice->setConnectionParameter(QModbusDevice::SerialPortNameParameter,
modbusConfig.serialPort);
_modbusDevice->setConnectionParameter(QModbusDevice::SerialParityParameter,
modbusConfig.parity);
_modbusDevice->setConnectionParameter(QModbusDevice::SerialBaudRateParameter,
modbusConfig.baud);
_modbusDevice->setConnectionParameter(QModbusDevice::SerialDataBitsParameter,
modbusConfig.dataBits);
_modbusDevice->setConnectionParameter(QModbusDevice::SerialStopBitsParameter,
modbusConfig.stopBits);
_modbusDevice->setTimeout(modbusConfig.responseTime);
_modbusDevice->setNumberOfRetries(modbusConfig.numberOfRetries);
if(!_modbusDevice->connectDevice())
{
throw ServoException(_modbusDevice->errorString());
}
}
}
catch(const ServoException& ex)
{
throw ServoException("Connection wasnt prepared");
expConfig.valid = true;
expConfig.message = ex.getMessage();
}
}
@ -90,12 +108,12 @@ QBitArray ModbusMaster::getCoil(int startAddress, quint16 readSize, ExpConfig& e
catch(const ServoException& ex)
{
expConfig.valid = true;
expConfig.message = ex.what();
expConfig.message = ex.getMessage();
}
}
/*************************************************************************************************/
QBitArray ModbusMaster::getInputCoil(int startAddress, quint16 readSize)
QBitArray ModbusMaster::getInputCoil(int startAddress, quint16 readSize, ExpConfig& expConfig)
{
try
{
@ -117,12 +135,15 @@ QBitArray ModbusMaster::getInputCoil(int startAddress, quint16 readSize)
}
catch(const ServoException& ex)
{
qDebug() << ex.what();
expConfig.valid = true;
expConfig.message = ex.getMessage();
}
}
/*************************************************************************************************/
QVector<quint16> ModbusMaster::getHoldingRegister(int startAddress, quint16 readSize)
QVector<quint16> ModbusMaster::getHoldingRegister(int startAddress,
quint16 readSize,
ExpConfig& expConfig)
{
try
{
@ -142,12 +163,15 @@ QVector<quint16> ModbusMaster::getHoldingRegister(int startAddress, quint16 read
}
catch(const ServoException& ex)
{
qDebug() << ex.what();
expConfig.valid = true;
expConfig.message = ex.getMessage();
}
}
/*************************************************************************************************/
QVector<quint16> ModbusMaster::getInputRegister(int startAddress, quint16 readSize)
QVector<quint16> ModbusMaster::getInputRegister(int startAddress,
quint16 readSize,
ExpConfig& expConfig)
{
try
{
@ -167,12 +191,13 @@ QVector<quint16> ModbusMaster::getInputRegister(int startAddress, quint16 readSi
}
catch(const ServoException& ex)
{
qDebug() << ex.what();
expConfig.valid = true;
expConfig.message = ex.getMessage();
}
}
/*************************************************************************************************/
void ModbusMaster::setSingleCoil(int startAddress, bool coilFlag)
void ModbusMaster::setSingleCoil(int startAddress, bool coilFlag, ExpConfig& expConfig)
{
try
{
@ -187,11 +212,16 @@ void ModbusMaster::setSingleCoil(int startAddress, bool coilFlag)
}
catch(const ServoException& ex)
{
qDebug() << ex.what();
expConfig.valid = true;
expConfig.message = ex.getMessage();
}
}
void ModbusMaster::setMultipleCoil(int startAddress, quint16 writeSize, QBitArray coilFlags)
/*************************************************************************************************/
void ModbusMaster::setMultipleCoil(int startAddress,
quint16 writeSize,
QBitArray coilFlags,
ExpConfig& expConfig)
{
try
{
@ -200,11 +230,13 @@ void ModbusMaster::setMultipleCoil(int startAddress, quint16 writeSize, QBitArra
}
catch(const ServoException& ex)
{
qDebug() << ex.what();
expConfig.valid = true;
expConfig.message = ex.getMessage();
}
}
void ModbusMaster::setSingleRegister(int startAddress, quint16 registerValue)
/*************************************************************************************************/
void ModbusMaster::setSingleRegister(int startAddress, quint16 registerValue, ExpConfig& expConfig)
{
try
{
@ -214,14 +246,15 @@ void ModbusMaster::setSingleRegister(int startAddress, quint16 registerValue)
}
catch(const ServoException& ex)
{
qDebug() << ex.what();
expConfig.valid = true;
expConfig.message = ex.getMessage();
}
}
/*************************************************************************************************/
void ModbusMaster::setMultipleRegister(int startAddress,
quint16 writeSize,
QVector<quint16> registerValues)
QVector<quint16> registerValues, ExpConfig& expConfig)
{
try
{
@ -230,7 +263,8 @@ void ModbusMaster::setMultipleRegister(int startAddress,
}
catch(const ServoException& ex)
{
qDebug() << ex.what();
expConfig.valid = true;
expConfig.message = ex.getMessage();
}
}
@ -277,15 +311,15 @@ void ModbusMaster::checkForError(QModbusReply* _reply)
if(_reply->error() == QModbusDevice::ProtocolError)
{
throw ServoException(tr("Read response error: %1 (Mobus exception: 0x%2)").
arg(_reply->errorString()).
arg(_reply->rawResult().exceptionCode(), -1, 16));
throw ServoException(QString("Read response error: %1 (Mobus exception: %2)")
.arg(_reply->errorString())
.arg(_reply->rawResult().exceptionCode()));
}
else
{
throw ServoException(tr("Read response error: %1 (code: 0x%2)").
arg(_reply->errorString()).
arg(_reply->error(), -1, 16));
throw ServoException(QString("Read response error: %1 (code: %2)")
.arg(_reply->errorString())
.arg(_reply->error()));
}
}
@ -327,3 +361,10 @@ void ModbusMaster::writeRequest(QModbusDataUnit::RegisterType registerType,
throw ServoException(_modbusDevice->errorString());
}
}
void ModbusMaster::close()
{
if(_modbusDevice)
_modbusDevice->disconnectDevice();
delete _modbusDevice;
}

71
Servo/src/ModbusWrapper.cpp

@ -75,9 +75,12 @@ void ModbusWrapper::init()
&ModbusMaster::setMultipleRegister,
Qt::BlockingQueuedConnection);
emit initOrder();
qDebug() << "init done";
ExpConfig exp;
emit initOrder(exp);
if(exp.valid)
{
throw ServoException(exp.message);
}
}
/*************************************************************************************************/
@ -97,37 +100,73 @@ QBitArray ModbusWrapper::getCoil(int startAddress, quint16 readSize)
/*************************************************************************************************/
QBitArray ModbusWrapper::getInputCoil(int startAddress, quint16 readSize)
{
return emit getInputCoilOrder(startAddress, readSize);
ExpConfig exp;
auto ret = emit getInputCoilOrder(startAddress, readSize, exp);
if(exp.valid)
{
throw ServoException(exp.message);
}
return ret;
}
/*************************************************************************************************/
QVector<quint16> ModbusWrapper::getHoldingRegister(int startAddress, quint16 readSize)
{
return emit getHoldingRegisterOrder(startAddress, readSize);
ExpConfig exp;
auto ret = emit getHoldingRegisterOrder(startAddress, readSize, exp);
if(exp.valid)
{
throw ServoException(exp.message);
}
return ret;
}
/*************************************************************************************************/
QVector<quint16> ModbusWrapper::getInputRegister(int startAddress, quint16 readSize)
{
return emit getInputRegisterOrder(startAddress, readSize);
ExpConfig exp;
auto ret = emit getInputRegisterOrder(startAddress, readSize, exp);
if(exp.valid)
{
throw ServoException(exp.message);
}
return ret;
}
/*************************************************************************************************/
void ModbusWrapper::setSingleCoil(int startAddress, bool coilFlag)
{
emit setSingleCoilOrder(startAddress, coilFlag);
ExpConfig exp;
emit setSingleCoilOrder(startAddress, coilFlag, exp);
if(exp.valid)
{
throw ServoException(exp.message);
}
}
/*************************************************************************************************/
void ModbusWrapper::setMultipleCoil(int startAddress, quint16 writeSize, QBitArray coilFlags)
{
emit setMultipleCoilOrder(startAddress, writeSize, coilFlags);
ExpConfig exp;
emit setMultipleCoilOrder(startAddress, writeSize, coilFlags, exp);
if(exp.valid)
{
throw ServoException(exp.message);
}
}
/*************************************************************************************************/
void ModbusWrapper::setSingleRegister(int startAddress, quint16 registerValue)
{
emit setSingleRegisterOrder(startAddress, registerValue);
ExpConfig exp;
emit setSingleRegisterOrder(startAddress, registerValue, exp);
if(exp.valid)
{
throw ServoException(exp.message);
}
}
/*************************************************************************************************/
@ -135,11 +174,21 @@ void ModbusWrapper::setMultipleRegister(int startAddress,
quint16 writeSize,
QVector<quint16> registerValues)
{
emit setMultipleRegisterOrder(startAddress, writeSize, registerValues);
ExpConfig exp;
emit setMultipleRegisterOrder(startAddress, writeSize, registerValues, exp);
if(exp.valid)
{
throw ServoException(exp.message);
}
}
/*************************************************************************************************/
void ModbusWrapper::connectToDevice(ModbusConfig modbusConfig)
{
emit connectOrder(modbusConfig);
ExpConfig exp;
emit connectOrder(modbusConfig, exp);
if(exp.valid)
{
throw ServoException(exp.message);
}
}

91
Test/MainWindow.cpp

@ -34,10 +34,11 @@ void MainWindow::on_connect_clicked()
qDebug() << "before connect";
modbusWrapper.connectToDevice(configDevice);
qDebug() << "after connect";
ui->errorMonitoring->setText("No Error");
}
catch(ServoException ex)
{
qDebug() << ex.what();
ui->errorMonitoring->setText(ex.getMessage());
}
}
@ -73,11 +74,11 @@ void MainWindow::on_readButton_clicked()
numberOfEntries)));
break;
}
ui->errorMonitoring->setText("No Error");
}
catch(const ServoException& exp)
{
qDebug() << "aaaa";
qDebug() << exp.what();
ui->errorMonitoring->setText(exp.getMessage());
}
}
@ -110,55 +111,83 @@ void MainWindow::printRegisterDataFromClient(QVector<quint16> uiHoldingRegisters
/*************************************************************************************************/
void MainWindow::on_writeSingleCoil_clicked()
{
int startAddress = ui->writeSingleCoilAddress->text().toInt();
modbusWrapper.setSingleCoil(startAddress, ui->coilData->isChecked());
try {
int startAddress = ui->writeSingleCoilAddress->text().toInt();
modbusWrapper.setSingleCoil(startAddress, ui->coilData->isChecked());
ui->errorMonitoring->setText("No Error");
}
catch(const ServoException& exp)
{
ui->errorMonitoring->setText(exp.getMessage());
}
}
/*************************************************************************************************/
void MainWindow::on_writeMultiCoil_clicked()
{
int startAddress = ui->writeMultiCoilStartAddress->text().toInt();
int writeQty = ui->writeMultiCoilQty->text().toInt();
QString valueToWrite = ui->writeMultiCoilValue->text();
try {
int startAddress = ui->writeMultiCoilStartAddress->text().toInt();
int writeQty = ui->writeMultiCoilQty->text().toInt();
QString valueToWrite = ui->writeMultiCoilValue->text();
QBitArray uiCoils;
uiCoils.resize(writeQty);
QBitArray uiCoils;
uiCoils.resize(writeQty);
for(int i = 0; i < valueToWrite.size(); i++)
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<quint16>(writeQty), uiCoils);
ui->errorMonitoring->setText("No Error");
}
catch(const ServoException& exp)
{
if(valueToWrite.at(i) == '0')
uiCoils[i] = false;
else
uiCoils[i] = true;
ui->errorMonitoring->setText(exp.getMessage());
}
modbusWrapper.setMultipleCoil(startAddress, static_cast<quint16>(writeQty), uiCoils);
}
/*************************************************************************************************/
void MainWindow::on_writeSingleRegister_clicked()
{
int startAddress = ui->writeSingleRegisterAddress->text().toInt();
quint16 value = static_cast<quint16>(ui->writeSingleRegisterValue->text().toInt());
modbusWrapper.setSingleRegister(startAddress, value);
try {
int startAddress = ui->writeSingleRegisterAddress->text().toInt();
quint16 value = static_cast<quint16>(ui->writeSingleRegisterValue->text().toInt());
modbusWrapper.setSingleRegister(startAddress, value);
ui->errorMonitoring->setText("No Error");
}
catch(const ServoException& exp)
{
ui->errorMonitoring->setText(exp.getMessage());
}
}
/*************************************************************************************************/
void MainWindow::on_writeMultiRegister_clicked()
{
int startAddress = ui->writeMultiRegisterStartAddress->text().toInt();
int writeQty = ui->writeMultiRegisterQty->text().toInt();
QString valueToWrite = ui->writeMultiRegisterValue->text();
QVector<quint16> uiHoldingRegisters;
uiHoldingRegisters.resize(writeQty);
try {
int startAddress = ui->writeMultiRegisterStartAddress->text().toInt();
int writeQty = ui->writeMultiRegisterQty->text().toInt();
QString valueToWrite = ui->writeMultiRegisterValue->text();
QVector<quint16> uiHoldingRegisters;
uiHoldingRegisters.resize(writeQty);
QRegExp rx("(\\ |\\,|\\.|\\:|\\t)"); //RegEx for ' ' or ',' or '.' or ':' or '\t'
QStringList stringListValue = valueToWrite.split(rx);
QRegExp rx("(\\ |\\,|\\.|\\:|\\t)"); //RegEx for ' ' or ',' or '.' or ':' or '\t'
QStringList stringListValue = valueToWrite.split(rx);
for(int i = 0; i < stringListValue.size(); i++)
for(int i = 0; i < stringListValue.size(); i++)
{
uiHoldingRegisters[i] = static_cast<quint16>(stringListValue[i].toInt());
}
modbusWrapper.setMultipleRegister(startAddress,
static_cast<quint16>(writeQty),
uiHoldingRegisters);
ui->errorMonitoring->setText("No Error");
}
catch(const ServoException& exp)
{
uiHoldingRegisters[i] = static_cast<quint16>(stringListValue[i].toInt());
ui->errorMonitoring->setText(exp.getMessage());
}
modbusWrapper.setMultipleRegister(startAddress,
static_cast<quint16>(writeQty),
uiHoldingRegisters);
}

1000
Test/MainWindow.ui

File diff suppressed because it is too large

1
Test/main.cpp

@ -21,6 +21,7 @@ public:
}
catch(std::exception& e)
{
qDebug() << "e exception " << e.what();
;
}

Loading…
Cancel
Save