|
@ -2,368 +2,377 @@ |
|
|
|
|
|
|
|
|
#include <QModbusRtuSerialMaster> |
|
|
#include <QModbusRtuSerialMaster> |
|
|
#include <QEventLoop> |
|
|
#include <QEventLoop> |
|
|
|
|
|
#include <QThread> |
|
|
ModbusMaster::ModbusMaster(QObject* parent) : QObject(parent) |
|
|
ModbusMaster::ModbusMaster(QObject* parent) : QObject(parent) |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/*************************************************************************************************/ |
|
|
/*************************************************************************************************/ |
|
|
void ModbusMaster::init(ExpConfig& expConfig) |
|
|
void ModbusMaster::init(ExpConfig& expConfig) |
|
|
{ |
|
|
{ |
|
|
try { |
|
|
try { |
|
|
if(!_initialized) |
|
|
if(!_initialized) |
|
|
{ |
|
|
{ |
|
|
_modbusDevice = nullptr; |
|
|
_modbusDevice = nullptr; |
|
|
_modbusDevice = new QModbusRtuSerialMaster(this); |
|
|
_modbusDevice = new QModbusRtuSerialMaster(this); |
|
|
|
|
|
|
|
|
connect(_modbusDevice, &QModbusClient::errorOccurred, [this](QModbusDevice::Error) |
|
|
connect(_modbusDevice, &QModbusClient::errorOccurred, [this](QModbusDevice::Error) |
|
|
{ |
|
|
{ |
|
|
throw ServoException(_modbusDevice->errorString()); |
|
|
_initialized = false; |
|
|
}); |
|
|
throw ServoException(_modbusDevice->errorString()); |
|
|
|
|
|
}); |
|
|
connect(_modbusDevice, |
|
|
|
|
|
&QModbusClient::stateChanged, |
|
|
connect(_modbusDevice, |
|
|
this, |
|
|
&QModbusClient::stateChanged, |
|
|
&ModbusMaster::connectionStateChanged); |
|
|
this, |
|
|
|
|
|
&ModbusMaster::connectionStateChanged); |
|
|
_initialized = true; |
|
|
|
|
|
} |
|
|
_initialized = true; |
|
|
else |
|
|
} |
|
|
{ |
|
|
else |
|
|
throw ServoException( |
|
|
{ |
|
|
"Modbus Device Object Created Before First Delete it Then Make New One"); |
|
|
throw ServoException( |
|
|
} |
|
|
"Modbus Device Object Created Before First Delete it Then Make New One"); |
|
|
} |
|
|
} |
|
|
catch(const ServoException& ex) |
|
|
} |
|
|
{ |
|
|
catch(const ServoException& ex) |
|
|
expConfig.valid = true; |
|
|
{ |
|
|
expConfig.message = ex.getMessage(); |
|
|
expConfig.valid = true; |
|
|
} |
|
|
expConfig.message = ex.getMessage(); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/*************************************************************************************************/ |
|
|
/*************************************************************************************************/ |
|
|
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"); |
|
|
throw ServoException("Connection wasnt prepared"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/*************************************************************************************************/ |
|
|
/*************************************************************************************************/ |
|
|
void ModbusMaster::connectToDevice(ModbusConfig modbusConfig, ExpConfig& expConfig) |
|
|
void ModbusMaster::connectToDevice(ModbusConfig modbusConfig, ExpConfig& expConfig) |
|
|
{ |
|
|
{ |
|
|
try { |
|
|
try { |
|
|
if(_modbusDevice->state() != QModbusDevice::ConnectedState) |
|
|
if(_modbusDevice->state() != QModbusDevice::ConnectedState) |
|
|
{ |
|
|
{ |
|
|
_clientAddress = modbusConfig.clientAddress; |
|
|
_clientAddress = modbusConfig.clientAddress; |
|
|
_modbusDevice->setConnectionParameter(QModbusDevice::SerialPortNameParameter, |
|
|
_modbusDevice->setConnectionParameter(QModbusDevice::SerialPortNameParameter, |
|
|
modbusConfig.serialPort); |
|
|
modbusConfig.serialPort); |
|
|
_modbusDevice->setConnectionParameter(QModbusDevice::SerialParityParameter, |
|
|
_modbusDevice->setConnectionParameter(QModbusDevice::SerialParityParameter, |
|
|
modbusConfig.parity); |
|
|
modbusConfig.parity); |
|
|
_modbusDevice->setConnectionParameter(QModbusDevice::SerialBaudRateParameter, |
|
|
_modbusDevice->setConnectionParameter(QModbusDevice::SerialBaudRateParameter, |
|
|
modbusConfig.baud); |
|
|
modbusConfig.baud); |
|
|
_modbusDevice->setConnectionParameter(QModbusDevice::SerialDataBitsParameter, |
|
|
_modbusDevice->setConnectionParameter(QModbusDevice::SerialDataBitsParameter, |
|
|
modbusConfig.dataBits); |
|
|
modbusConfig.dataBits); |
|
|
_modbusDevice->setConnectionParameter(QModbusDevice::SerialStopBitsParameter, |
|
|
_modbusDevice->setConnectionParameter(QModbusDevice::SerialStopBitsParameter, |
|
|
modbusConfig.stopBits); |
|
|
modbusConfig.stopBits); |
|
|
_modbusDevice->setTimeout(modbusConfig.responseTime); |
|
|
_modbusDevice->setTimeout(modbusConfig.responseTime); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!_modbusDevice->connectDevice()) |
|
|
if(!_modbusDevice->connectDevice()) |
|
|
{ |
|
|
{ |
|
|
throw ServoException(_modbusDevice->errorString()); |
|
|
throw ServoException(_modbusDevice->errorString()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
catch(const ServoException& ex) |
|
|
catch(const ServoException& ex) |
|
|
{ |
|
|
{ |
|
|
expConfig.valid = true; |
|
|
expConfig.valid = true; |
|
|
expConfig.message = ex.getMessage(); |
|
|
expConfig.message = ex.getMessage(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/*************************************************************************************************/ |
|
|
/*************************************************************************************************/ |
|
|
QBitArray ModbusMaster::getCoil(int startAddress, quint16 readSize, ExpConfig& expConfig) |
|
|
QBitArray ModbusMaster::getCoil(int startAddress, quint16 readSize, ExpConfig& expConfig) |
|
|
{ |
|
|
{ |
|
|
try |
|
|
try |
|
|
{ |
|
|
{ |
|
|
readRequest(QModbusDataUnit::RegisterType::Coils, startAddress, readSize); |
|
|
readRequest(QModbusDataUnit::RegisterType::Coils, startAddress, readSize); |
|
|
auto unit = _modbusReplyUnit; |
|
|
auto unit = _modbusReplyUnit; |
|
|
|
|
|
|
|
|
QBitArray coils; |
|
|
QBitArray coils; |
|
|
auto num = unit.valueCount(); |
|
|
auto num = unit.valueCount(); |
|
|
coils.resize(static_cast<int>(num)); |
|
|
coils.resize(static_cast<int>(num)); |
|
|
for(uint i = 0; i < num; i++) |
|
|
for(uint i = 0; i < num; i++) |
|
|
{ |
|
|
{ |
|
|
if(unit.value(static_cast<int>(i)) == 1) |
|
|
if(unit.value(static_cast<int>(i)) == 1) |
|
|
coils.setBit(static_cast<int>(i)); |
|
|
coils.setBit(static_cast<int>(i)); |
|
|
else |
|
|
else |
|
|
coils.clearBit(static_cast<int>(i)); |
|
|
coils.clearBit(static_cast<int>(i)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return coils; |
|
|
return coils; |
|
|
} |
|
|
} |
|
|
catch(const ServoException& ex) |
|
|
catch(const ServoException& ex) |
|
|
{ |
|
|
{ |
|
|
expConfig.valid = true; |
|
|
expConfig.valid = true; |
|
|
expConfig.message = ex.getMessage(); |
|
|
expConfig.message = ex.getMessage(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/*************************************************************************************************/ |
|
|
/*************************************************************************************************/ |
|
|
QBitArray ModbusMaster::getInputCoil(int startAddress, quint16 readSize, ExpConfig& expConfig) |
|
|
QBitArray ModbusMaster::getInputCoil(int startAddress, quint16 readSize, ExpConfig& expConfig) |
|
|
{ |
|
|
{ |
|
|
try |
|
|
try |
|
|
{ |
|
|
{ |
|
|
readRequest(QModbusDataUnit::RegisterType::DiscreteInputs, startAddress, readSize); |
|
|
readRequest(QModbusDataUnit::RegisterType::DiscreteInputs, startAddress, readSize); |
|
|
auto unit = _modbusReplyUnit; |
|
|
auto unit = _modbusReplyUnit; |
|
|
|
|
|
|
|
|
QBitArray coils; |
|
|
QBitArray coils; |
|
|
auto num = unit.valueCount(); |
|
|
auto num = unit.valueCount(); |
|
|
coils.resize(static_cast<int>(num)); |
|
|
coils.resize(static_cast<int>(num)); |
|
|
for(uint i = 0; i < num; i++) |
|
|
for(uint i = 0; i < num; i++) |
|
|
{ |
|
|
{ |
|
|
if(unit.value(static_cast<int>(i)) == 1) |
|
|
if(unit.value(static_cast<int>(i)) == 1) |
|
|
coils.setBit(static_cast<int>(i)); |
|
|
coils.setBit(static_cast<int>(i)); |
|
|
else |
|
|
else |
|
|
coils.clearBit(static_cast<int>(i)); |
|
|
coils.clearBit(static_cast<int>(i)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return coils; |
|
|
return coils; |
|
|
} |
|
|
} |
|
|
catch(const ServoException& ex) |
|
|
catch(const ServoException& ex) |
|
|
{ |
|
|
{ |
|
|
expConfig.valid = true; |
|
|
expConfig.valid = true; |
|
|
expConfig.message = ex.getMessage(); |
|
|
expConfig.message = ex.getMessage(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/*************************************************************************************************/ |
|
|
/*************************************************************************************************/ |
|
|
QVector<quint16> ModbusMaster::getHoldingRegister(int startAddress, |
|
|
QVector<quint16> ModbusMaster::getHoldingRegister(int startAddress, |
|
|
quint16 readSize, |
|
|
quint16 readSize, |
|
|
ExpConfig& expConfig) |
|
|
ExpConfig& expConfig) |
|
|
{ |
|
|
{ |
|
|
try |
|
|
try |
|
|
{ |
|
|
{ |
|
|
readRequest(QModbusDataUnit::RegisterType::HoldingRegisters, startAddress, readSize); |
|
|
readRequest(QModbusDataUnit::RegisterType::HoldingRegisters, startAddress, readSize); |
|
|
auto unit = _modbusReplyUnit; |
|
|
auto unit = _modbusReplyUnit; |
|
|
|
|
|
|
|
|
QVector<quint16> registerValues; |
|
|
QVector<quint16> registerValues; |
|
|
auto num = unit.valueCount(); |
|
|
auto num = unit.valueCount(); |
|
|
registerValues.resize(static_cast<int>(num)); |
|
|
registerValues.resize(static_cast<int>(num)); |
|
|
|
|
|
|
|
|
for(uint i = 0; i < unit.valueCount(); i++) |
|
|
for(uint i = 0; i < unit.valueCount(); i++) |
|
|
{ |
|
|
{ |
|
|
registerValues[static_cast<int>(i)] = unit.value(static_cast<int>(i)); |
|
|
registerValues[static_cast<int>(i)] = unit.value(static_cast<int>(i)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return registerValues; |
|
|
return registerValues; |
|
|
} |
|
|
} |
|
|
catch(const ServoException& ex) |
|
|
catch(const ServoException& ex) |
|
|
{ |
|
|
{ |
|
|
expConfig.valid = true; |
|
|
expConfig.valid = true; |
|
|
expConfig.message = ex.getMessage(); |
|
|
expConfig.message = ex.getMessage(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/*************************************************************************************************/ |
|
|
/*************************************************************************************************/ |
|
|
QVector<quint16> ModbusMaster::getInputRegister(int startAddress, |
|
|
QVector<quint16> ModbusMaster::getInputRegister(int startAddress, |
|
|
quint16 readSize, |
|
|
quint16 readSize, |
|
|
ExpConfig& expConfig) |
|
|
ExpConfig& expConfig) |
|
|
{ |
|
|
{ |
|
|
try |
|
|
try |
|
|
{ |
|
|
{ |
|
|
readRequest(QModbusDataUnit::RegisterType::InputRegisters, startAddress, readSize); |
|
|
readRequest(QModbusDataUnit::RegisterType::InputRegisters, startAddress, readSize); |
|
|
auto unit = _modbusReplyUnit; |
|
|
auto unit = _modbusReplyUnit; |
|
|
|
|
|
|
|
|
QVector<quint16> registerValues; |
|
|
QVector<quint16> registerValues; |
|
|
auto num = unit.valueCount(); |
|
|
auto num = unit.valueCount(); |
|
|
registerValues.resize(static_cast<int>(num)); |
|
|
registerValues.resize(static_cast<int>(num)); |
|
|
|
|
|
|
|
|
for(uint i = 0; i < unit.valueCount(); i++) |
|
|
for(uint i = 0; i < unit.valueCount(); i++) |
|
|
{ |
|
|
{ |
|
|
registerValues[static_cast<int>(i)] = unit.value(static_cast<int>(i)); |
|
|
registerValues[static_cast<int>(i)] = unit.value(static_cast<int>(i)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return registerValues; |
|
|
return registerValues; |
|
|
} |
|
|
} |
|
|
catch(const ServoException& ex) |
|
|
catch(const ServoException& ex) |
|
|
{ |
|
|
{ |
|
|
expConfig.valid = true; |
|
|
expConfig.valid = true; |
|
|
expConfig.message = ex.getMessage(); |
|
|
expConfig.message = ex.getMessage(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/*************************************************************************************************/ |
|
|
/*************************************************************************************************/ |
|
|
void ModbusMaster::setSingleCoil(int startAddress, bool coilFlag, ExpConfig& expConfig) |
|
|
void ModbusMaster::setSingleCoil(int startAddress, bool coilFlag, ExpConfig& expConfig) |
|
|
{ |
|
|
{ |
|
|
try |
|
|
try |
|
|
{ |
|
|
{ |
|
|
_coilsToWrite.resize(1); |
|
|
_coilsToWrite.resize(1); |
|
|
|
|
|
|
|
|
if(coilFlag) |
|
|
if(coilFlag) |
|
|
_coilsToWrite.setBit(0); |
|
|
_coilsToWrite.setBit(0); |
|
|
else |
|
|
else |
|
|
_coilsToWrite.clearBit(0); |
|
|
_coilsToWrite.clearBit(0); |
|
|
|
|
|
|
|
|
writeRequest(QModbusDataUnit::RegisterType::Coils, startAddress, 1); |
|
|
writeRequest(QModbusDataUnit::RegisterType::Coils, startAddress, 1); |
|
|
} |
|
|
} |
|
|
catch(const ServoException& ex) |
|
|
catch(const ServoException& ex) |
|
|
{ |
|
|
{ |
|
|
expConfig.valid = true; |
|
|
expConfig.valid = true; |
|
|
expConfig.message = ex.getMessage(); |
|
|
expConfig.message = ex.getMessage(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/*************************************************************************************************/ |
|
|
/*************************************************************************************************/ |
|
|
void ModbusMaster::setMultipleCoil(int startAddress, |
|
|
void ModbusMaster::setMultipleCoil(int startAddress, |
|
|
quint16 writeSize, |
|
|
quint16 writeSize, |
|
|
QBitArray coilFlags, |
|
|
QBitArray coilFlags, |
|
|
ExpConfig& expConfig) |
|
|
ExpConfig& expConfig) |
|
|
{ |
|
|
{ |
|
|
try |
|
|
try |
|
|
{ |
|
|
{ |
|
|
_coilsToWrite = coilFlags; |
|
|
_coilsToWrite = coilFlags; |
|
|
writeRequest(QModbusDataUnit::RegisterType::Coils, startAddress, writeSize); |
|
|
writeRequest(QModbusDataUnit::RegisterType::Coils, startAddress, writeSize); |
|
|
} |
|
|
} |
|
|
catch(const ServoException& ex) |
|
|
catch(const ServoException& ex) |
|
|
{ |
|
|
{ |
|
|
expConfig.valid = true; |
|
|
expConfig.valid = true; |
|
|
expConfig.message = ex.getMessage(); |
|
|
expConfig.message = ex.getMessage(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/*************************************************************************************************/ |
|
|
/*************************************************************************************************/ |
|
|
void ModbusMaster::setSingleRegister(int startAddress, quint16 registerValue, ExpConfig& expConfig) |
|
|
void ModbusMaster::setSingleRegister(int startAddress, quint16 registerValue, ExpConfig& expConfig) |
|
|
{ |
|
|
{ |
|
|
try |
|
|
try |
|
|
{ |
|
|
{ |
|
|
_RegistersToWrite.resize(1); |
|
|
_RegistersToWrite.resize(1); |
|
|
_RegistersToWrite[0] = registerValue; |
|
|
_RegistersToWrite[0] = registerValue; |
|
|
writeRequest(QModbusDataUnit::RegisterType::HoldingRegisters, startAddress, 1); |
|
|
writeRequest(QModbusDataUnit::RegisterType::HoldingRegisters, startAddress, 1); |
|
|
} |
|
|
} |
|
|
catch(const ServoException& ex) |
|
|
catch(const ServoException& ex) |
|
|
{ |
|
|
{ |
|
|
expConfig.valid = true; |
|
|
expConfig.valid = true; |
|
|
expConfig.message = ex.getMessage(); |
|
|
expConfig.message = ex.getMessage(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/*************************************************************************************************/ |
|
|
/*************************************************************************************************/ |
|
|
void ModbusMaster::setMultipleRegister(int startAddress, |
|
|
void ModbusMaster::setMultipleRegister(int startAddress, |
|
|
quint16 writeSize, |
|
|
quint16 writeSize, |
|
|
QVector<quint16> registerValues, ExpConfig& expConfig) |
|
|
QVector<quint16> registerValues, ExpConfig& expConfig) |
|
|
{ |
|
|
{ |
|
|
try |
|
|
try |
|
|
{ |
|
|
{ |
|
|
_RegistersToWrite = registerValues; |
|
|
_RegistersToWrite = registerValues; |
|
|
writeRequest(QModbusDataUnit::RegisterType::HoldingRegisters, startAddress, writeSize); |
|
|
writeRequest(QModbusDataUnit::RegisterType::HoldingRegisters, startAddress, writeSize); |
|
|
} |
|
|
} |
|
|
catch(const ServoException& ex) |
|
|
catch(const ServoException& ex) |
|
|
{ |
|
|
{ |
|
|
expConfig.valid = true; |
|
|
expConfig.valid = true; |
|
|
expConfig.message = ex.getMessage(); |
|
|
expConfig.message = ex.getMessage(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/*************************************************************************************************/ |
|
|
/*************************************************************************************************/ |
|
|
void ModbusMaster::readRequest(QModbusDataUnit::RegisterType registerType, |
|
|
void ModbusMaster::readRequest(QModbusDataUnit::RegisterType registerType, |
|
|
int startAddress, |
|
|
int startAddress, |
|
|
quint16 readSize) |
|
|
quint16 readSize) |
|
|
{ |
|
|
{ |
|
|
auto table = registerType; |
|
|
auto table = registerType; |
|
|
|
|
|
|
|
|
auto reply = _modbusDevice->sendReadRequest(QModbusDataUnit(table, startAddress, readSize), |
|
|
auto reply = _modbusDevice->sendReadRequest(QModbusDataUnit(table, startAddress, readSize), |
|
|
_clientAddress); |
|
|
_clientAddress); |
|
|
if(reply) |
|
|
if(reply) |
|
|
{ |
|
|
{ |
|
|
if(!reply->isFinished()) |
|
|
if(!reply->isFinished()) |
|
|
{ |
|
|
{ |
|
|
QEventLoop loop; |
|
|
QEventLoop loop; |
|
|
connect(reply, |
|
|
connect(reply, |
|
|
&QModbusReply::finished, |
|
|
&QModbusReply::finished, |
|
|
&loop, |
|
|
&loop, |
|
|
&QEventLoop::quit |
|
|
&QEventLoop::quit |
|
|
); |
|
|
); |
|
|
loop.exec(); |
|
|
loop.exec(); |
|
|
|
|
|
|
|
|
checkForError(reply); |
|
|
checkForError(reply); |
|
|
_modbusReplyUnit = reply->result(); |
|
|
_modbusReplyUnit = reply->result(); |
|
|
|
|
|
|
|
|
reply->deleteLater(); |
|
|
reply->deleteLater(); |
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
|
delete reply; //broadcast replies return immediately
|
|
|
delete reply; //broadcast replies return immediately
|
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
|
{ |
|
|
{ |
|
|
throw ServoException(_modbusDevice->errorString()); |
|
|
throw ServoException(_modbusDevice->errorString()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/*************************************************************************************************/ |
|
|
/*************************************************************************************************/ |
|
|
void ModbusMaster::checkForError(QModbusReply* _reply) |
|
|
void ModbusMaster::checkForError(QModbusReply* _reply) |
|
|
{ |
|
|
{ |
|
|
if(_reply->error() == QModbusDevice::NoError) |
|
|
if(_reply->error() == QModbusDevice::NoError) |
|
|
return; |
|
|
return; |
|
|
|
|
|
|
|
|
if(_reply->error() == QModbusDevice::ProtocolError) |
|
|
if(_reply->error() == QModbusDevice::ProtocolError) |
|
|
{ |
|
|
{ |
|
|
throw ServoException(QString("Read response error: %1 (Mobus exception: %2)") |
|
|
throw ServoException(QString("Read response error: %1 (Mobus exception: %2)") |
|
|
.arg(_reply->errorString()) |
|
|
.arg(_reply->errorString()) |
|
|
.arg(_reply->rawResult().exceptionCode())); |
|
|
.arg(_reply->rawResult().exceptionCode())); |
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
|
{ |
|
|
{ |
|
|
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())); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/*************************************************************************************************/ |
|
|
/*************************************************************************************************/ |
|
|
void ModbusMaster::writeRequest(QModbusDataUnit::RegisterType registerType, |
|
|
void ModbusMaster::writeRequest(QModbusDataUnit::RegisterType registerType, |
|
|
int startAddress, |
|
|
int startAddress, |
|
|
quint16 writeSize) |
|
|
quint16 writeSize) |
|
|
{ |
|
|
{ |
|
|
auto table = registerType; |
|
|
auto table = registerType; |
|
|
QModbusDataUnit writeUnit = QModbusDataUnit(table, startAddress, writeSize); |
|
|
QModbusDataUnit writeUnit = QModbusDataUnit(table, startAddress, writeSize); |
|
|
for(int i = 0; i < writeUnit.valueCount(); i++) |
|
|
for(int i = 0; i < writeUnit.valueCount(); i++) |
|
|
{ |
|
|
{ |
|
|
if(table == QModbusDataUnit::Coils) |
|
|
if(table == QModbusDataUnit::Coils) |
|
|
writeUnit.setValue(i, _coilsToWrite[i]); |
|
|
writeUnit.setValue(i, _coilsToWrite[i]); |
|
|
else |
|
|
else |
|
|
writeUnit.setValue(i, _RegistersToWrite[i]); |
|
|
writeUnit.setValue(i, _RegistersToWrite[i]); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if(auto* reply = _modbusDevice->sendWriteRequest(writeUnit, _clientAddress)) |
|
|
if(auto* reply = _modbusDevice->sendWriteRequest(writeUnit, _clientAddress)) |
|
|
{ |
|
|
{ |
|
|
if(!reply->isFinished()) |
|
|
if(!reply->isFinished()) |
|
|
{ |
|
|
{ |
|
|
QEventLoop loop; |
|
|
QEventLoop loop; |
|
|
connect(reply, &QModbusReply::finished, &loop, |
|
|
connect(reply, &QModbusReply::finished, &loop, |
|
|
&QEventLoop::quit |
|
|
&QEventLoop::quit |
|
|
); |
|
|
); |
|
|
loop.exec(); |
|
|
loop.exec(); |
|
|
|
|
|
|
|
|
checkForError(reply); |
|
|
checkForError(reply); |
|
|
_modbusReplyUnit = reply->result(); |
|
|
_modbusReplyUnit = reply->result(); |
|
|
|
|
|
|
|
|
reply->deleteLater(); |
|
|
reply->deleteLater(); |
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
|
delete reply; //broadcast replies return immediately
|
|
|
delete reply; //broadcast replies return immediately
|
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
|
{ |
|
|
{ |
|
|
throw ServoException(_modbusDevice->errorString()); |
|
|
throw ServoException(_modbusDevice->errorString()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void ModbusMaster::close() |
|
|
void ModbusMaster::close() |
|
|
{ |
|
|
{ |
|
|
if(_modbusDevice) |
|
|
qDebug() << " ModbusMaster::close " <<QThread::currentThreadId() ; |
|
|
_modbusDevice->disconnectDevice(); |
|
|
if(_modbusDevice) |
|
|
delete _modbusDevice; |
|
|
{ |
|
|
|
|
|
if (_modbusDevice->state() == QModbusDevice::ConnectedState || _modbusDevice->state() == QModbusDevice::ConnectingState) |
|
|
|
|
|
{ |
|
|
|
|
|
_modbusDevice->disconnectDevice(); |
|
|
|
|
|
} |
|
|
|
|
|
delete _modbusDevice; |
|
|
|
|
|
_modbusDevice = nullptr; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|