|
|
@ -19,8 +19,8 @@ void ModbusMaster::init(ExpConfig& expConfig) |
|
|
|
|
|
|
|
connect(_modbusDevice, &QModbusClient::errorOccurred, [this](QModbusDevice::Error) |
|
|
|
{ |
|
|
|
_initialized = false; |
|
|
|
throw ServoException(_modbusDevice->errorString()); |
|
|
|
_initialized = false; |
|
|
|
throw ServoException(_modbusDevice->errorString()); |
|
|
|
}); |
|
|
|
|
|
|
|
connect(_modbusDevice, |
|
|
@ -48,7 +48,11 @@ void ModbusMaster::connectionStateChanged(QModbusDevice::State state) |
|
|
|
{ |
|
|
|
if(state == QModbusDevice::UnconnectedState) |
|
|
|
{ |
|
|
|
throw ServoException("Connection wasnt prepared"); |
|
|
|
_connected = false; |
|
|
|
} |
|
|
|
else if (state == QModbusDevice::ConnectedState) |
|
|
|
{ |
|
|
|
_connected = true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -110,6 +114,7 @@ QBitArray ModbusMaster::getCoil(int startAddress, quint16 readSize, ExpConfig& e |
|
|
|
{ |
|
|
|
expConfig.valid = true; |
|
|
|
expConfig.message = ex.getMessage(); |
|
|
|
return QBitArray(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -138,6 +143,7 @@ QBitArray ModbusMaster::getInputCoil(int startAddress, quint16 readSize, ExpConf |
|
|
|
{ |
|
|
|
expConfig.valid = true; |
|
|
|
expConfig.message = ex.getMessage(); |
|
|
|
return QBitArray(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -166,6 +172,7 @@ QVector<quint16> ModbusMaster::getHoldingRegister(int startAddress, |
|
|
|
{ |
|
|
|
expConfig.valid = true; |
|
|
|
expConfig.message = ex.getMessage(); |
|
|
|
return QVector<quint16>(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -194,6 +201,7 @@ QVector<quint16> ModbusMaster::getInputRegister(int startAddress, |
|
|
|
{ |
|
|
|
expConfig.valid = true; |
|
|
|
expConfig.message = ex.getMessage(); |
|
|
|
return QVector<quint16>(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -308,16 +316,20 @@ void ModbusMaster::readRequest(QModbusDataUnit::RegisterType registerType, |
|
|
|
void ModbusMaster::checkForError(QModbusReply* _reply) |
|
|
|
{ |
|
|
|
if(_reply->error() == QModbusDevice::NoError) |
|
|
|
{ |
|
|
|
_connected = true; |
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
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->rawResult().exceptionCode())); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
_connected = false; |
|
|
|
throw ServoException(QString("Read response error: %1 (code: %2)") |
|
|
|
.arg(_reply->errorString()) |
|
|
|
.arg(_reply->error())); |
|
|
@ -363,9 +375,16 @@ void ModbusMaster::writeRequest(QModbusDataUnit::RegisterType registerType, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/*************************************************************************************************/ |
|
|
|
bool ModbusMaster::getConnectionState() |
|
|
|
{ |
|
|
|
return _connected; |
|
|
|
} |
|
|
|
|
|
|
|
/*************************************************************************************************/ |
|
|
|
void ModbusMaster::close() |
|
|
|
{ |
|
|
|
qDebug() << " ModbusMaster::close " <<QThread::currentThreadId() ; |
|
|
|
_initialized = false; |
|
|
|
if(_modbusDevice) |
|
|
|
{ |
|
|
|
if (_modbusDevice->state() == QModbusDevice::ConnectedState || _modbusDevice->state() == QModbusDevice::ConnectingState) |
|
|
|