improved RTU client setup
parent
60711ba6b5
commit
c73248048c
|
|
@ -385,10 +385,9 @@ void ModbusRTUMaster::onModbusErrorOccurred(QModbusDevice::Error error)
|
||||||
|
|
||||||
void ModbusRTUMaster::onModbusStateChanged(QModbusDevice::State state)
|
void ModbusRTUMaster::onModbusStateChanged(QModbusDevice::State state)
|
||||||
{
|
{
|
||||||
bool connected = (state != QModbusDevice::UnconnectedState);
|
if (state == QModbusDevice::UnconnectedState) {
|
||||||
if (!connected) {
|
|
||||||
//try to reconnect in 10 seconds
|
//try to reconnect in 10 seconds
|
||||||
m_reconnectTimer->start(10000);
|
m_reconnectTimer->start(10000);
|
||||||
}
|
}
|
||||||
emit connectionStateChanged(connected);
|
emit connectionStateChanged(state == QModbusDevice::ConnectedState);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -87,9 +87,13 @@ void IntegrationPluginModbusCommander::setupThing(ThingSetupInfo *info)
|
||||||
connect(modbusTCPMaster, &ModbusTCPMaster::receivedDiscreteInput, this, &IntegrationPluginModbusCommander::onReceivedDiscreteInput);
|
connect(modbusTCPMaster, &ModbusTCPMaster::receivedDiscreteInput, this, &IntegrationPluginModbusCommander::onReceivedDiscreteInput);
|
||||||
connect(modbusTCPMaster, &ModbusTCPMaster::receivedHoldingRegister, this, &IntegrationPluginModbusCommander::onReceivedHoldingRegister);
|
connect(modbusTCPMaster, &ModbusTCPMaster::receivedHoldingRegister, this, &IntegrationPluginModbusCommander::onReceivedHoldingRegister);
|
||||||
connect(modbusTCPMaster, &ModbusTCPMaster::receivedInputRegister, this, &IntegrationPluginModbusCommander::onReceivedInputRegister);
|
connect(modbusTCPMaster, &ModbusTCPMaster::receivedInputRegister, this, &IntegrationPluginModbusCommander::onReceivedInputRegister);
|
||||||
|
connect(modbusTCPMaster, &ModbusTCPMaster::connectionStateChanged, info, [info, modbusTCPMaster, this] (bool connected) {
|
||||||
|
if (connected) {
|
||||||
|
info->finish(Thing::ThingErrorNoError);
|
||||||
|
m_modbusTCPMasters.insert(info->thing(), modbusTCPMaster);
|
||||||
|
}
|
||||||
|
});
|
||||||
modbusTCPMaster->connectDevice();
|
modbusTCPMaster->connectDevice();
|
||||||
m_modbusTCPMasters.insert(thing, modbusTCPMaster);
|
|
||||||
m_asyncTCPSetup.insert(modbusTCPMaster, info);
|
|
||||||
|
|
||||||
} else if (thing->thingClassId() == modbusRTUClientThingClassId) {
|
} else if (thing->thingClassId() == modbusRTUClientThingClassId) {
|
||||||
|
|
||||||
|
|
@ -98,7 +102,8 @@ void IntegrationPluginModbusCommander::setupThing(ThingSetupInfo *info)
|
||||||
uint stopBits = thing->paramValue(modbusRTUClientThingStopBitsParamTypeId).toUInt();
|
uint stopBits = thing->paramValue(modbusRTUClientThingStopBitsParamTypeId).toUInt();
|
||||||
uint dataBits = thing->paramValue(modbusRTUClientThingDataBitsParamTypeId).toUInt();
|
uint dataBits = thing->paramValue(modbusRTUClientThingDataBitsParamTypeId).toUInt();
|
||||||
QSerialPort::Parity parity = QSerialPort::Parity::NoParity;
|
QSerialPort::Parity parity = QSerialPort::Parity::NoParity;
|
||||||
if (thing->paramValue(modbusRTUClientThingParityParamTypeId).toString().contains("No")) {
|
QString parityString = thing->paramValue(modbusRTUClientThingParityParamTypeId).toString();
|
||||||
|
if (parityString.contains("No")) {
|
||||||
parity = QSerialPort::Parity::NoParity;
|
parity = QSerialPort::Parity::NoParity;
|
||||||
} else if (thing->paramValue(modbusRTUClientThingParityParamTypeId).toString().contains("Even")) {
|
} else if (thing->paramValue(modbusRTUClientThingParityParamTypeId).toString().contains("Even")) {
|
||||||
parity = QSerialPort::Parity::EvenParity;
|
parity = QSerialPort::Parity::EvenParity;
|
||||||
|
|
@ -114,9 +119,13 @@ void IntegrationPluginModbusCommander::setupThing(ThingSetupInfo *info)
|
||||||
connect(modbusRTUMaster, &ModbusRTUMaster::receivedDiscreteInput, this, &IntegrationPluginModbusCommander::onReceivedDiscreteInput);
|
connect(modbusRTUMaster, &ModbusRTUMaster::receivedDiscreteInput, this, &IntegrationPluginModbusCommander::onReceivedDiscreteInput);
|
||||||
connect(modbusRTUMaster, &ModbusRTUMaster::receivedHoldingRegister, this, &IntegrationPluginModbusCommander::onReceivedHoldingRegister);
|
connect(modbusRTUMaster, &ModbusRTUMaster::receivedHoldingRegister, this, &IntegrationPluginModbusCommander::onReceivedHoldingRegister);
|
||||||
connect(modbusRTUMaster, &ModbusRTUMaster::receivedInputRegister, this, &IntegrationPluginModbusCommander::onReceivedInputRegister);
|
connect(modbusRTUMaster, &ModbusRTUMaster::receivedInputRegister, this, &IntegrationPluginModbusCommander::onReceivedInputRegister);
|
||||||
|
connect(modbusRTUMaster, &ModbusRTUMaster::connectionStateChanged, info, [info, modbusRTUMaster, this] (bool connected) {
|
||||||
|
if (connected) {
|
||||||
|
info->finish(Thing::ThingErrorNoError);
|
||||||
|
m_modbusRTUMasters.insert(info->thing(), modbusRTUMaster);
|
||||||
|
}
|
||||||
|
});
|
||||||
modbusRTUMaster->connectDevice();
|
modbusRTUMaster->connectDevice();
|
||||||
m_modbusRTUMasters.insert(thing, modbusRTUMaster);
|
|
||||||
m_asyncRTUSetup.insert(modbusRTUMaster, info);
|
|
||||||
|
|
||||||
} else if ((thing->thingClassId() == coilThingClassId)
|
} else if ((thing->thingClassId() == coilThingClassId)
|
||||||
|| (thing->thingClassId() == discreteInputThingClassId)
|
|| (thing->thingClassId() == discreteInputThingClassId)
|
||||||
|
|
@ -138,7 +147,7 @@ void IntegrationPluginModbusCommander::discoverThings(ThingDiscoveryInfo *info)
|
||||||
//Serial port is not yet used, create now a new one
|
//Serial port is not yet used, create now a new one
|
||||||
qCDebug(dcModbusCommander()) << "Found serial port:" << port.systemLocation();
|
qCDebug(dcModbusCommander()) << "Found serial port:" << port.systemLocation();
|
||||||
QString description = port.manufacturer() + " " + port.description();
|
QString description = port.manufacturer() + " " + port.description();
|
||||||
ThingDescriptor thingDescriptor(thingClassId, port.portName(), description);
|
ThingDescriptor thingDescriptor(thingClassId, "Modbus RTU interface "+port.portName(), description);
|
||||||
ParamList parameters;
|
ParamList parameters;
|
||||||
QString serialPort = port.systemLocation();
|
QString serialPort = port.systemLocation();
|
||||||
foreach (Thing *existingThing, myThings()) {
|
foreach (Thing *existingThing, myThings()) {
|
||||||
|
|
@ -226,7 +235,16 @@ void IntegrationPluginModbusCommander::postSetupThing(Thing *info)
|
||||||
// Refresh timer for TCP read
|
// Refresh timer for TCP read
|
||||||
int refreshTime = configValue(modbusCommanderPluginUpdateIntervalParamTypeId).toInt();
|
int refreshTime = configValue(modbusCommanderPluginUpdateIntervalParamTypeId).toInt();
|
||||||
m_refreshTimer = hardwareManager()->pluginTimerManager()->registerTimer(refreshTime);
|
m_refreshTimer = hardwareManager()->pluginTimerManager()->registerTimer(refreshTime);
|
||||||
connect(m_refreshTimer, &PluginTimer::timeout, this, &IntegrationPluginModbusCommander::onRefreshTimer);
|
connect(m_refreshTimer, &PluginTimer::timeout, this, [this] {
|
||||||
|
foreach (Thing *thing, myThings()) {
|
||||||
|
if ((thing->thingClassId() == coilThingClassId) ||
|
||||||
|
(thing->thingClassId() == discreteInputThingClassId) ||
|
||||||
|
(thing->thingClassId() == holdingRegisterThingClassId) ||
|
||||||
|
(thing->thingClassId() == inputRegisterThingClassId)) {
|
||||||
|
readRegister(thing);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((info->thingClassId() == coilThingClassId) ||
|
if ((info->thingClassId() == coilThingClassId) ||
|
||||||
|
|
@ -276,18 +294,6 @@ void IntegrationPluginModbusCommander::thingRemoved(Thing *thing)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void IntegrationPluginModbusCommander::onRefreshTimer()
|
|
||||||
{
|
|
||||||
foreach (Thing *thing, myThings()) {
|
|
||||||
if ((thing->thingClassId() == coilThingClassId) ||
|
|
||||||
(thing->thingClassId() == discreteInputThingClassId) ||
|
|
||||||
(thing->thingClassId() == holdingRegisterThingClassId) ||
|
|
||||||
(thing->thingClassId() == inputRegisterThingClassId)) {
|
|
||||||
readRegister(thing);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void IntegrationPluginModbusCommander::onPluginConfigurationChanged(const ParamTypeId ¶mTypeId, const QVariant &value)
|
void IntegrationPluginModbusCommander::onPluginConfigurationChanged(const ParamTypeId ¶mTypeId, const QVariant &value)
|
||||||
{
|
{
|
||||||
// Check refresh schedule
|
// Check refresh schedule
|
||||||
|
|
@ -304,15 +310,6 @@ void IntegrationPluginModbusCommander::onConnectionStateChanged(bool status)
|
||||||
{
|
{
|
||||||
auto modbus = sender();
|
auto modbus = sender();
|
||||||
|
|
||||||
if (m_asyncRTUSetup.contains(static_cast<ModbusRTUMaster *>(modbus))) {
|
|
||||||
ThingSetupInfo *info = m_asyncRTUSetup.take(static_cast<ModbusRTUMaster *>(modbus));
|
|
||||||
info->finish(Thing::ThingErrorNoError);
|
|
||||||
|
|
||||||
} else if (m_asyncTCPSetup.contains(static_cast<ModbusTCPMaster *>(modbus))) {
|
|
||||||
ThingSetupInfo *info = m_asyncTCPSetup.take(static_cast<ModbusTCPMaster *>(modbus));
|
|
||||||
info->finish(Thing::ThingErrorNoError);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_modbusRTUMasters.values().contains(static_cast<ModbusRTUMaster *>(modbus))) {
|
if (m_modbusRTUMasters.values().contains(static_cast<ModbusRTUMaster *>(modbus))) {
|
||||||
Thing *thing = m_modbusRTUMasters.key(static_cast<ModbusRTUMaster *>(modbus));
|
Thing *thing = m_modbusRTUMasters.key(static_cast<ModbusRTUMaster *>(modbus));
|
||||||
thing->setStateValue(modbusRTUClientConnectedStateTypeId, status);
|
thing->setStateValue(modbusRTUClientConnectedStateTypeId, status);
|
||||||
|
|
|
||||||
|
|
@ -65,9 +65,6 @@ private:
|
||||||
QHash<QUuid, ThingActionInfo*> m_asyncActions;
|
QHash<QUuid, ThingActionInfo*> m_asyncActions;
|
||||||
QHash<QUuid, Thing*> m_readRequests;
|
QHash<QUuid, Thing*> m_readRequests;
|
||||||
|
|
||||||
QHash<ModbusRTUMaster*, ThingSetupInfo *> m_asyncRTUSetup;
|
|
||||||
QHash<ModbusTCPMaster*, ThingSetupInfo *> m_asyncTCPSetup;
|
|
||||||
|
|
||||||
void readRegister(Thing *thing);
|
void readRegister(Thing *thing);
|
||||||
void writeRegister(Thing *thing, ThingActionInfo *info);
|
void writeRegister(Thing *thing, ThingActionInfo *info);
|
||||||
|
|
||||||
|
|
@ -77,8 +74,6 @@ private:
|
||||||
QHash<ThingClassId, StateTypeId> m_valueStateTypeId;
|
QHash<ThingClassId, StateTypeId> m_valueStateTypeId;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void onRefreshTimer();
|
|
||||||
|
|
||||||
void onPluginConfigurationChanged(const ParamTypeId ¶mTypeId, const QVariant &value);
|
void onPluginConfigurationChanged(const ParamTypeId ¶mTypeId, const QVariant &value);
|
||||||
|
|
||||||
void onConnectionStateChanged(bool status);
|
void onConnectionStateChanged(bool status);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue