From c73248048ca5e9c9fb10df09172661a7ca24a0c3 Mon Sep 17 00:00:00 2001 From: Boernsman Date: Wed, 2 Dec 2020 09:37:57 +0100 Subject: [PATCH] improved RTU client setup --- modbus/modbusrtumaster.cpp | 5 +- .../integrationpluginmodbuscommander.cpp | 53 +++++++++---------- .../integrationpluginmodbuscommander.h | 5 -- 3 files changed, 27 insertions(+), 36 deletions(-) diff --git a/modbus/modbusrtumaster.cpp b/modbus/modbusrtumaster.cpp index 8a7aa5a..4329a0d 100644 --- a/modbus/modbusrtumaster.cpp +++ b/modbus/modbusrtumaster.cpp @@ -385,10 +385,9 @@ void ModbusRTUMaster::onModbusErrorOccurred(QModbusDevice::Error error) void ModbusRTUMaster::onModbusStateChanged(QModbusDevice::State state) { - bool connected = (state != QModbusDevice::UnconnectedState); - if (!connected) { + if (state == QModbusDevice::UnconnectedState) { //try to reconnect in 10 seconds m_reconnectTimer->start(10000); } - emit connectionStateChanged(connected); + emit connectionStateChanged(state == QModbusDevice::ConnectedState); } diff --git a/modbuscommander/integrationpluginmodbuscommander.cpp b/modbuscommander/integrationpluginmodbuscommander.cpp index 7621fc1..7afcf96 100644 --- a/modbuscommander/integrationpluginmodbuscommander.cpp +++ b/modbuscommander/integrationpluginmodbuscommander.cpp @@ -87,9 +87,13 @@ void IntegrationPluginModbusCommander::setupThing(ThingSetupInfo *info) connect(modbusTCPMaster, &ModbusTCPMaster::receivedDiscreteInput, this, &IntegrationPluginModbusCommander::onReceivedDiscreteInput); connect(modbusTCPMaster, &ModbusTCPMaster::receivedHoldingRegister, this, &IntegrationPluginModbusCommander::onReceivedHoldingRegister); 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(); - m_modbusTCPMasters.insert(thing, modbusTCPMaster); - m_asyncTCPSetup.insert(modbusTCPMaster, info); } else if (thing->thingClassId() == modbusRTUClientThingClassId) { @@ -98,7 +102,8 @@ void IntegrationPluginModbusCommander::setupThing(ThingSetupInfo *info) uint stopBits = thing->paramValue(modbusRTUClientThingStopBitsParamTypeId).toUInt(); uint dataBits = thing->paramValue(modbusRTUClientThingDataBitsParamTypeId).toUInt(); 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; } else if (thing->paramValue(modbusRTUClientThingParityParamTypeId).toString().contains("Even")) { parity = QSerialPort::Parity::EvenParity; @@ -114,9 +119,13 @@ void IntegrationPluginModbusCommander::setupThing(ThingSetupInfo *info) connect(modbusRTUMaster, &ModbusRTUMaster::receivedDiscreteInput, this, &IntegrationPluginModbusCommander::onReceivedDiscreteInput); connect(modbusRTUMaster, &ModbusRTUMaster::receivedHoldingRegister, this, &IntegrationPluginModbusCommander::onReceivedHoldingRegister); 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(); - m_modbusRTUMasters.insert(thing, modbusRTUMaster); - m_asyncRTUSetup.insert(modbusRTUMaster, info); } else if ((thing->thingClassId() == coilThingClassId) || (thing->thingClassId() == discreteInputThingClassId) @@ -138,7 +147,7 @@ void IntegrationPluginModbusCommander::discoverThings(ThingDiscoveryInfo *info) //Serial port is not yet used, create now a new one qCDebug(dcModbusCommander()) << "Found serial port:" << port.systemLocation(); QString description = port.manufacturer() + " " + port.description(); - ThingDescriptor thingDescriptor(thingClassId, port.portName(), description); + ThingDescriptor thingDescriptor(thingClassId, "Modbus RTU interface "+port.portName(), description); ParamList parameters; QString serialPort = port.systemLocation(); foreach (Thing *existingThing, myThings()) { @@ -226,7 +235,16 @@ void IntegrationPluginModbusCommander::postSetupThing(Thing *info) // Refresh timer for TCP read int refreshTime = configValue(modbusCommanderPluginUpdateIntervalParamTypeId).toInt(); 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) || @@ -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) { // Check refresh schedule @@ -304,15 +310,6 @@ void IntegrationPluginModbusCommander::onConnectionStateChanged(bool status) { auto modbus = sender(); - if (m_asyncRTUSetup.contains(static_cast(modbus))) { - ThingSetupInfo *info = m_asyncRTUSetup.take(static_cast(modbus)); - info->finish(Thing::ThingErrorNoError); - - } else if (m_asyncTCPSetup.contains(static_cast(modbus))) { - ThingSetupInfo *info = m_asyncTCPSetup.take(static_cast(modbus)); - info->finish(Thing::ThingErrorNoError); - } - if (m_modbusRTUMasters.values().contains(static_cast(modbus))) { Thing *thing = m_modbusRTUMasters.key(static_cast(modbus)); thing->setStateValue(modbusRTUClientConnectedStateTypeId, status); diff --git a/modbuscommander/integrationpluginmodbuscommander.h b/modbuscommander/integrationpluginmodbuscommander.h index 7fed22f..4c7b5c0 100644 --- a/modbuscommander/integrationpluginmodbuscommander.h +++ b/modbuscommander/integrationpluginmodbuscommander.h @@ -65,9 +65,6 @@ private: QHash m_asyncActions; QHash m_readRequests; - QHash m_asyncRTUSetup; - QHash m_asyncTCPSetup; - void readRegister(Thing *thing); void writeRegister(Thing *thing, ThingActionInfo *info); @@ -77,8 +74,6 @@ private: QHash m_valueStateTypeId; private slots: - void onRefreshTimer(); - void onPluginConfigurationChanged(const ParamTypeId ¶mTypeId, const QVariant &value); void onConnectionStateChanged(bool status);