|
@ -2,9 +2,10 @@ |
|
|
|
|
|
|
|
|
#include <QModbusRtuSerialMaster> |
|
|
#include <QModbusRtuSerialMaster> |
|
|
#include <QEventLoop> |
|
|
#include <QEventLoop> |
|
|
|
|
|
#include <QThread> |
|
|
ModbusMaster::ModbusMaster(QObject* parent) : QObject(parent) |
|
|
ModbusMaster::ModbusMaster(QObject* parent) : QObject(parent) |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/*************************************************************************************************/ |
|
|
/*************************************************************************************************/ |
|
@ -18,7 +19,8 @@ void ModbusMaster::init(ExpConfig& expConfig) |
|
|
|
|
|
|
|
|
connect(_modbusDevice, &QModbusClient::errorOccurred, [this](QModbusDevice::Error) |
|
|
connect(_modbusDevice, &QModbusClient::errorOccurred, [this](QModbusDevice::Error) |
|
|
{ |
|
|
{ |
|
|
throw ServoException(_modbusDevice->errorString()); |
|
|
_initialized = false; |
|
|
|
|
|
connectionStateChanged(QModbusDevice::UnconnectedState); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
connect(_modbusDevice, |
|
|
connect(_modbusDevice, |
|
@ -44,9 +46,14 @@ void ModbusMaster::init(ExpConfig& expConfig) |
|
|
/*************************************************************************************************/ |
|
|
/*************************************************************************************************/ |
|
|
void ModbusMaster::connectionStateChanged(QModbusDevice::State state) |
|
|
void ModbusMaster::connectionStateChanged(QModbusDevice::State state) |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
if(state == QModbusDevice::UnconnectedState) |
|
|
if(state == QModbusDevice::UnconnectedState) |
|
|
{ |
|
|
{ |
|
|
throw ServoException("Connection wasnt prepared"); |
|
|
_connected = false; |
|
|
|
|
|
} |
|
|
|
|
|
else if (state == QModbusDevice::ConnectedState) |
|
|
|
|
|
{ |
|
|
|
|
|
_connected = true; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -108,6 +115,7 @@ QBitArray ModbusMaster::getCoil(int startAddress, quint16 readSize, ExpConfig& e |
|
|
{ |
|
|
{ |
|
|
expConfig.valid = true; |
|
|
expConfig.valid = true; |
|
|
expConfig.message = ex.getMessage(); |
|
|
expConfig.message = ex.getMessage(); |
|
|
|
|
|
return QBitArray(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -136,6 +144,7 @@ QBitArray ModbusMaster::getInputCoil(int startAddress, quint16 readSize, ExpConf |
|
|
{ |
|
|
{ |
|
|
expConfig.valid = true; |
|
|
expConfig.valid = true; |
|
|
expConfig.message = ex.getMessage(); |
|
|
expConfig.message = ex.getMessage(); |
|
|
|
|
|
return QBitArray(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -164,6 +173,7 @@ QVector<quint16> ModbusMaster::getHoldingRegister(int startAddress, |
|
|
{ |
|
|
{ |
|
|
expConfig.valid = true; |
|
|
expConfig.valid = true; |
|
|
expConfig.message = ex.getMessage(); |
|
|
expConfig.message = ex.getMessage(); |
|
|
|
|
|
return QVector<quint16>(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -192,6 +202,7 @@ QVector<quint16> ModbusMaster::getInputRegister(int startAddress, |
|
|
{ |
|
|
{ |
|
|
expConfig.valid = true; |
|
|
expConfig.valid = true; |
|
|
expConfig.message = ex.getMessage(); |
|
|
expConfig.message = ex.getMessage(); |
|
|
|
|
|
return QVector<quint16>(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -306,16 +317,20 @@ void ModbusMaster::readRequest(QModbusDataUnit::RegisterType registerType, |
|
|
void ModbusMaster::checkForError(QModbusReply* _reply) |
|
|
void ModbusMaster::checkForError(QModbusReply* _reply) |
|
|
{ |
|
|
{ |
|
|
if(_reply->error() == QModbusDevice::NoError) |
|
|
if(_reply->error() == QModbusDevice::NoError) |
|
|
|
|
|
{ |
|
|
|
|
|
_connected = true; |
|
|
return; |
|
|
return; |
|
|
|
|
|
} |
|
|
if(_reply->error() == QModbusDevice::ProtocolError) |
|
|
if(_reply->error() == QModbusDevice::ProtocolError) |
|
|
{ |
|
|
{ |
|
|
throw ServoException(QString("Read response error: %1 (Mobus exception: %2)") |
|
|
_connected = false; |
|
|
|
|
|
throw ServoException(QString("Read response error: %1 (Modbus exception: %2)") |
|
|
.arg(_reply->errorString()) |
|
|
.arg(_reply->errorString()) |
|
|
.arg(_reply->rawResult().exceptionCode())); |
|
|
.arg(_reply->rawResult().exceptionCode())); |
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
|
{ |
|
|
{ |
|
|
|
|
|
_connected = false; |
|
|
throw ServoException(QString("Read response error: %1 (code: %2)") |
|
|
throw ServoException(QString("Read response error: %1 (code: %2)") |
|
|
.arg(_reply->errorString()) |
|
|
.arg(_reply->errorString()) |
|
|
.arg(_reply->error())); |
|
|
.arg(_reply->error())); |
|
@ -361,9 +376,23 @@ void ModbusMaster::writeRequest(QModbusDataUnit::RegisterType registerType, |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************************************/ |
|
|
|
|
|
bool ModbusMaster::getConnectionState() |
|
|
|
|
|
{ |
|
|
|
|
|
return _connected; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************************************/ |
|
|
void ModbusMaster::close() |
|
|
void ModbusMaster::close() |
|
|
{ |
|
|
{ |
|
|
|
|
|
_initialized = false; |
|
|
if(_modbusDevice) |
|
|
if(_modbusDevice) |
|
|
|
|
|
{ |
|
|
|
|
|
if (_modbusDevice->state() == QModbusDevice::ConnectedState || _modbusDevice->state() == QModbusDevice::ConnectingState) |
|
|
|
|
|
{ |
|
|
_modbusDevice->disconnectDevice(); |
|
|
_modbusDevice->disconnectDevice(); |
|
|
|
|
|
} |
|
|
delete _modbusDevice; |
|
|
delete _modbusDevice; |
|
|
|
|
|
_modbusDevice = nullptr; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|