diff --git a/bgetech/bgetech.pro b/bgetech/bgetech.pro index 080685d..bb12ae8 100644 --- a/bgetech/bgetech.pro +++ b/bgetech/bgetech.pro @@ -1,14 +1,13 @@ include(../plugins.pri) -QT += serialport serialbus +# Generate modbus connection +MODBUS_CONNECTIONS += sdm630-registers.json +#MODBUS_TOOLS_CONFIG += VERBOSE +include(../modbus.pri) HEADERS += \ - integrationpluginbgetech.h \ - sdm630modbusrtuconnection.h \ - ../modbus/modbusdatautils.h + integrationpluginbgetech.h SOURCES += \ - integrationpluginbgetech.cpp \ - sdm630modbusrtuconnection.cpp \ - ../modbus/modbusdatautils.cpp + integrationpluginbgetech.cpp diff --git a/bgetech/sdm630-registers.json b/bgetech/sdm630-registers.json index b4d9f0d..1bdbabf 100644 --- a/bgetech/sdm630-registers.json +++ b/bgetech/sdm630-registers.json @@ -1,4 +1,5 @@ { + "className": "Sdm630", "protocol": "RTU", "endianness": "BigEndian", "registers": [ @@ -257,4 +258,4 @@ ] } ] -} \ No newline at end of file +} diff --git a/bgetech/sdm630modbusrtuconnection.cpp b/bgetech/sdm630modbusrtuconnection.cpp deleted file mode 100644 index 1134d2f..0000000 --- a/bgetech/sdm630modbusrtuconnection.cpp +++ /dev/null @@ -1,455 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -* -* Copyright 2013 - 2021, nymea GmbH -* Contact: contact@nymea.io -* -* This fileDescriptor is part of nymea. -* This project including source code and documentation is protected by -* copyright law, and remains the property of nymea GmbH. All rights, including -* reproduction, publication, editing and translation, are reserved. The use of -* this project is subject to the terms of a license agreement to be concluded -* with nymea GmbH in accordance with the terms of use of nymea GmbH, available -* under https://nymea.io/license -* -* GNU Lesser General Public License Usage -* Alternatively, this project may be redistributed and/or modified under the -* terms of the GNU Lesser General Public License as published by the Free -* Software Foundation; version 3. This project is distributed in the hope that -* it will be useful, but WITHOUT ANY WARRANTY; without even the implied -* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -* Lesser General Public License for more details. -* -* You should have received a copy of the GNU Lesser General Public License -* along with this project. If not, see . -* -* For any further details and any questions please contact us under -* contact@nymea.io or see our FAQ/Licensing Information on -* https://nymea.io/license/faq -* -* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - - -#include "sdm630modbusrtuconnection.h" -#include "loggingcategories.h" - -NYMEA_LOGGING_CATEGORY(dcSdm630ModbusRtuConnection, "Sdm630ModbusRtuConnection") - -Sdm630ModbusRtuConnection::Sdm630ModbusRtuConnection(ModbusRtuMaster *modbusRtuMaster, quint16 slaveId, QObject *parent) : - QObject(parent), - m_modbusRtuMaster(modbusRtuMaster), - m_slaveId(slaveId) -{ - -} - -ModbusRtuMaster *Sdm630ModbusRtuConnection::modbusRtuMaster() const -{ - return m_modbusRtuMaster; -} -quint16 Sdm630ModbusRtuConnection::slaveId() const -{ - return m_slaveId; -} -float Sdm630ModbusRtuConnection::totalCurrentPower() const -{ - return m_totalCurrentPower; -} - -float Sdm630ModbusRtuConnection::voltagePhaseA() const -{ - return m_voltagePhaseA; -} - -float Sdm630ModbusRtuConnection::voltagePhaseB() const -{ - return m_voltagePhaseB; -} - -float Sdm630ModbusRtuConnection::voltagePhaseC() const -{ - return m_voltagePhaseC; -} - -float Sdm630ModbusRtuConnection::currentPhaseA() const -{ - return m_currentPhaseA; -} - -float Sdm630ModbusRtuConnection::currentPhaseB() const -{ - return m_currentPhaseB; -} - -float Sdm630ModbusRtuConnection::currentPhaseC() const -{ - return m_currentPhaseC; -} - -float Sdm630ModbusRtuConnection::powerPhaseA() const -{ - return m_powerPhaseA; -} - -float Sdm630ModbusRtuConnection::powerPhaseB() const -{ - return m_powerPhaseB; -} - -float Sdm630ModbusRtuConnection::powerPhaseC() const -{ - return m_powerPhaseC; -} - -float Sdm630ModbusRtuConnection::frequency() const -{ - return m_frequency; -} - -float Sdm630ModbusRtuConnection::totalEnergyConsumed() const -{ - return m_totalEnergyConsumed; -} - -float Sdm630ModbusRtuConnection::totalEnergyProduced() const -{ - return m_totalEnergyProduced; -} - -float Sdm630ModbusRtuConnection::energyProducedPhaseA() const -{ - return m_energyProducedPhaseA; -} - -float Sdm630ModbusRtuConnection::energyProducedPhaseB() const -{ - return m_energyProducedPhaseB; -} - -float Sdm630ModbusRtuConnection::energyProducedPhaseC() const -{ - return m_energyProducedPhaseC; -} - -float Sdm630ModbusRtuConnection::energyConsumedPhaseA() const -{ - return m_energyConsumedPhaseA; -} - -float Sdm630ModbusRtuConnection::energyConsumedPhaseB() const -{ - return m_energyConsumedPhaseB; -} - -float Sdm630ModbusRtuConnection::energyConsumedPhaseC() const -{ - return m_energyConsumedPhaseC; -} - -void Sdm630ModbusRtuConnection::initialize() -{ - // No init registers defined. Nothing to be done and we are finished. - emit initializationFinished(); -} - -void Sdm630ModbusRtuConnection::update() -{ - updateTotalCurrentPower(); - updatePhaseVoltageAndCurrentBlock(); - updatePhasePowerBlock(); - updateFrequencyAndTotalEnergyBlock(); - updatePhaseEnergyEnergyBlock(); -} - -void Sdm630ModbusRtuConnection::updateTotalCurrentPower() -{ - // Update registers from Total system power - qCDebug(dcSdm630ModbusRtuConnection()) << "--> Read \"Total system power\" register:" << 52 << "size:" << 2; - ModbusRtuReply *reply = readTotalCurrentPower(); - if (reply) { - if (!reply->isFinished()) { - connect(reply, &ModbusRtuReply::finished, this, [this, reply](){ - if (reply->error() == ModbusRtuReply::NoError) { - QVector values = reply->result(); - qCDebug(dcSdm630ModbusRtuConnection()) << "<-- Response from \"Total system power\" register" << 52 << "size:" << 2 << values; - float receivedTotalCurrentPower = ModbusDataUtils::convertToFloat32(values, ModbusDataUtils::ByteOrderBigEndian); - if (m_totalCurrentPower != receivedTotalCurrentPower) { - m_totalCurrentPower = receivedTotalCurrentPower; - emit totalCurrentPowerChanged(m_totalCurrentPower); - } - } - }); - - connect(reply, &ModbusRtuReply::errorOccurred, this, [reply] (ModbusRtuReply::Error error){ - qCWarning(dcSdm630ModbusRtuConnection()) << "ModbusRtu reply error occurred while updating \"Total system power\" registers" << error << reply->errorString(); - emit reply->finished(); - }); - } - } else { - qCWarning(dcSdm630ModbusRtuConnection()) << "Error occurred while reading \"Total system power\" registers"; - } -} - -void Sdm630ModbusRtuConnection::updatePhaseVoltageAndCurrentBlock() -{ - // Update register block "phaseVoltageAndCurrent" - qCDebug(dcSdm630ModbusRtuConnection()) << "--> Read block \"phaseVoltageAndCurrent\" registers from:" << 0 << "size:" << 12; - ModbusRtuReply *reply = m_modbusRtuMaster->readInputRegister(m_slaveId, 0, 12); - if (reply) { - if (!reply->isFinished()) { - connect(reply, &ModbusRtuReply::finished, this, [this, reply](){ - if (reply->error() == ModbusRtuReply::NoError) { - QVector blockValues = reply->result(); - QVector values; - qCDebug(dcSdm630ModbusRtuConnection()) << "<-- Response from reading block \"phaseVoltageAndCurrent\" register" << 0 << "size:" << 12 << blockValues; - values = blockValues.mid(0, 2); - float receivedVoltagePhaseA = ModbusDataUtils::convertToFloat32(values, ModbusDataUtils::ByteOrderBigEndian); - if (m_voltagePhaseA != receivedVoltagePhaseA) { - m_voltagePhaseA = receivedVoltagePhaseA; - emit voltagePhaseAChanged(m_voltagePhaseA); - } - - values = blockValues.mid(2, 2); - float receivedVoltagePhaseB = ModbusDataUtils::convertToFloat32(values, ModbusDataUtils::ByteOrderBigEndian); - if (m_voltagePhaseB != receivedVoltagePhaseB) { - m_voltagePhaseB = receivedVoltagePhaseB; - emit voltagePhaseBChanged(m_voltagePhaseB); - } - - values = blockValues.mid(4, 2); - float receivedVoltagePhaseC = ModbusDataUtils::convertToFloat32(values, ModbusDataUtils::ByteOrderBigEndian); - if (m_voltagePhaseC != receivedVoltagePhaseC) { - m_voltagePhaseC = receivedVoltagePhaseC; - emit voltagePhaseCChanged(m_voltagePhaseC); - } - - values = blockValues.mid(6, 2); - float receivedCurrentPhaseA = ModbusDataUtils::convertToFloat32(values, ModbusDataUtils::ByteOrderBigEndian); - if (m_currentPhaseA != receivedCurrentPhaseA) { - m_currentPhaseA = receivedCurrentPhaseA; - emit currentPhaseAChanged(m_currentPhaseA); - } - - values = blockValues.mid(8, 2); - float receivedCurrentPhaseB = ModbusDataUtils::convertToFloat32(values, ModbusDataUtils::ByteOrderBigEndian); - if (m_currentPhaseB != receivedCurrentPhaseB) { - m_currentPhaseB = receivedCurrentPhaseB; - emit currentPhaseBChanged(m_currentPhaseB); - } - - values = blockValues.mid(10, 2); - float receivedCurrentPhaseC = ModbusDataUtils::convertToFloat32(values, ModbusDataUtils::ByteOrderBigEndian); - if (m_currentPhaseC != receivedCurrentPhaseC) { - m_currentPhaseC = receivedCurrentPhaseC; - emit currentPhaseCChanged(m_currentPhaseC); - } - - } - }); - - connect(reply, &ModbusRtuReply::errorOccurred, this, [reply] (ModbusRtuReply::Error error){ - qCWarning(dcSdm630ModbusRtuConnection()) << "ModbusRtu reply error occurred while updating block \"phaseVoltageAndCurrent\" registers" << error << reply->errorString(); - emit reply->finished(); - }); - } - } else { - qCWarning(dcSdm630ModbusRtuConnection()) << "Error occurred while reading block \"phaseVoltageAndCurrent\" registers"; - } -} - -void Sdm630ModbusRtuConnection::updatePhasePowerBlock() -{ - // Update register block "phasePower" - qCDebug(dcSdm630ModbusRtuConnection()) << "--> Read block \"phasePower\" registers from:" << 12 << "size:" << 6; - ModbusRtuReply *reply = m_modbusRtuMaster->readInputRegister(m_slaveId, 12, 6); - if (reply) { - if (!reply->isFinished()) { - connect(reply, &ModbusRtuReply::finished, this, [this, reply](){ - if (reply->error() == ModbusRtuReply::NoError) { - QVector blockValues = reply->result(); - QVector values; - qCDebug(dcSdm630ModbusRtuConnection()) << "<-- Response from reading block \"phasePower\" register" << 12 << "size:" << 6 << blockValues; - values = blockValues.mid(0, 2); - float receivedPowerPhaseA = ModbusDataUtils::convertToFloat32(values, ModbusDataUtils::ByteOrderBigEndian); - if (m_powerPhaseA != receivedPowerPhaseA) { - m_powerPhaseA = receivedPowerPhaseA; - emit powerPhaseAChanged(m_powerPhaseA); - } - - values = blockValues.mid(2, 2); - float receivedPowerPhaseB = ModbusDataUtils::convertToFloat32(values, ModbusDataUtils::ByteOrderBigEndian); - if (m_powerPhaseB != receivedPowerPhaseB) { - m_powerPhaseB = receivedPowerPhaseB; - emit powerPhaseBChanged(m_powerPhaseB); - } - - values = blockValues.mid(4, 2); - float receivedPowerPhaseC = ModbusDataUtils::convertToFloat32(values, ModbusDataUtils::ByteOrderBigEndian); - if (m_powerPhaseC != receivedPowerPhaseC) { - m_powerPhaseC = receivedPowerPhaseC; - emit powerPhaseCChanged(m_powerPhaseC); - } - - } - }); - - connect(reply, &ModbusRtuReply::errorOccurred, this, [reply] (ModbusRtuReply::Error error){ - qCWarning(dcSdm630ModbusRtuConnection()) << "ModbusRtu reply error occurred while updating block \"phasePower\" registers" << error << reply->errorString(); - emit reply->finished(); - }); - } - } else { - qCWarning(dcSdm630ModbusRtuConnection()) << "Error occurred while reading block \"phasePower\" registers"; - } -} - -void Sdm630ModbusRtuConnection::updateFrequencyAndTotalEnergyBlock() -{ - // Update register block "frequencyAndTotalEnergy" - qCDebug(dcSdm630ModbusRtuConnection()) << "--> Read block \"frequencyAndTotalEnergy\" registers from:" << 70 << "size:" << 6; - ModbusRtuReply *reply = m_modbusRtuMaster->readInputRegister(m_slaveId, 70, 6); - if (reply) { - if (!reply->isFinished()) { - connect(reply, &ModbusRtuReply::finished, this, [this, reply](){ - if (reply->error() == ModbusRtuReply::NoError) { - QVector blockValues = reply->result(); - QVector values; - qCDebug(dcSdm630ModbusRtuConnection()) << "<-- Response from reading block \"frequencyAndTotalEnergy\" register" << 70 << "size:" << 6 << blockValues; - values = blockValues.mid(0, 2); - float receivedFrequency = ModbusDataUtils::convertToFloat32(values, ModbusDataUtils::ByteOrderBigEndian); - if (m_frequency != receivedFrequency) { - m_frequency = receivedFrequency; - emit frequencyChanged(m_frequency); - } - - values = blockValues.mid(2, 2); - float receivedTotalEnergyConsumed = ModbusDataUtils::convertToFloat32(values, ModbusDataUtils::ByteOrderBigEndian); - if (m_totalEnergyConsumed != receivedTotalEnergyConsumed) { - m_totalEnergyConsumed = receivedTotalEnergyConsumed; - emit totalEnergyConsumedChanged(m_totalEnergyConsumed); - } - - values = blockValues.mid(4, 2); - float receivedTotalEnergyProduced = ModbusDataUtils::convertToFloat32(values, ModbusDataUtils::ByteOrderBigEndian); - if (m_totalEnergyProduced != receivedTotalEnergyProduced) { - m_totalEnergyProduced = receivedTotalEnergyProduced; - emit totalEnergyProducedChanged(m_totalEnergyProduced); - } - - } - }); - - connect(reply, &ModbusRtuReply::errorOccurred, this, [reply] (ModbusRtuReply::Error error){ - qCWarning(dcSdm630ModbusRtuConnection()) << "ModbusRtu reply error occurred while updating block \"frequencyAndTotalEnergy\" registers" << error << reply->errorString(); - emit reply->finished(); - }); - } - } else { - qCWarning(dcSdm630ModbusRtuConnection()) << "Error occurred while reading block \"frequencyAndTotalEnergy\" registers"; - } -} - -void Sdm630ModbusRtuConnection::updatePhaseEnergyEnergyBlock() -{ - // Update register block "phaseEnergyEnergy" - qCDebug(dcSdm630ModbusRtuConnection()) << "--> Read block \"phaseEnergyEnergy\" registers from:" << 346 << "size:" << 12; - ModbusRtuReply *reply = m_modbusRtuMaster->readInputRegister(m_slaveId, 346, 12); - if (reply) { - if (!reply->isFinished()) { - connect(reply, &ModbusRtuReply::finished, this, [this, reply](){ - if (reply->error() == ModbusRtuReply::NoError) { - QVector blockValues = reply->result(); - QVector values; - qCDebug(dcSdm630ModbusRtuConnection()) << "<-- Response from reading block \"phaseEnergyEnergy\" register" << 346 << "size:" << 12 << blockValues; - values = blockValues.mid(0, 2); - float receivedEnergyProducedPhaseA = ModbusDataUtils::convertToFloat32(values, ModbusDataUtils::ByteOrderBigEndian); - if (m_energyProducedPhaseA != receivedEnergyProducedPhaseA) { - m_energyProducedPhaseA = receivedEnergyProducedPhaseA; - emit energyProducedPhaseAChanged(m_energyProducedPhaseA); - } - - values = blockValues.mid(2, 2); - float receivedEnergyProducedPhaseB = ModbusDataUtils::convertToFloat32(values, ModbusDataUtils::ByteOrderBigEndian); - if (m_energyProducedPhaseB != receivedEnergyProducedPhaseB) { - m_energyProducedPhaseB = receivedEnergyProducedPhaseB; - emit energyProducedPhaseBChanged(m_energyProducedPhaseB); - } - - values = blockValues.mid(4, 2); - float receivedEnergyProducedPhaseC = ModbusDataUtils::convertToFloat32(values, ModbusDataUtils::ByteOrderBigEndian); - if (m_energyProducedPhaseC != receivedEnergyProducedPhaseC) { - m_energyProducedPhaseC = receivedEnergyProducedPhaseC; - emit energyProducedPhaseCChanged(m_energyProducedPhaseC); - } - - values = blockValues.mid(6, 2); - float receivedEnergyConsumedPhaseA = ModbusDataUtils::convertToFloat32(values, ModbusDataUtils::ByteOrderBigEndian); - if (m_energyConsumedPhaseA != receivedEnergyConsumedPhaseA) { - m_energyConsumedPhaseA = receivedEnergyConsumedPhaseA; - emit energyConsumedPhaseAChanged(m_energyConsumedPhaseA); - } - - values = blockValues.mid(8, 2); - float receivedEnergyConsumedPhaseB = ModbusDataUtils::convertToFloat32(values, ModbusDataUtils::ByteOrderBigEndian); - if (m_energyConsumedPhaseB != receivedEnergyConsumedPhaseB) { - m_energyConsumedPhaseB = receivedEnergyConsumedPhaseB; - emit energyConsumedPhaseBChanged(m_energyConsumedPhaseB); - } - - values = blockValues.mid(10, 2); - float receivedEnergyConsumedPhaseC = ModbusDataUtils::convertToFloat32(values, ModbusDataUtils::ByteOrderBigEndian); - if (m_energyConsumedPhaseC != receivedEnergyConsumedPhaseC) { - m_energyConsumedPhaseC = receivedEnergyConsumedPhaseC; - emit energyConsumedPhaseCChanged(m_energyConsumedPhaseC); - } - - } - }); - - connect(reply, &ModbusRtuReply::errorOccurred, this, [reply] (ModbusRtuReply::Error error){ - qCWarning(dcSdm630ModbusRtuConnection()) << "ModbusRtu reply error occurred while updating block \"phaseEnergyEnergy\" registers" << error << reply->errorString(); - emit reply->finished(); - }); - } - } else { - qCWarning(dcSdm630ModbusRtuConnection()) << "Error occurred while reading block \"phaseEnergyEnergy\" registers"; - } -} - -ModbusRtuReply *Sdm630ModbusRtuConnection::readTotalCurrentPower() -{ - return m_modbusRtuMaster->readInputRegister(m_slaveId, 52, 2); -} - -void Sdm630ModbusRtuConnection::verifyInitFinished() -{ - if (m_pendingInitReplies.isEmpty()) { - qCDebug(dcSdm630ModbusRtuConnection()) << "Initialization finished of Sdm630ModbusRtuConnection"; - emit initializationFinished(); - } -} - -QDebug operator<<(QDebug debug, Sdm630ModbusRtuConnection *sdm630ModbusRtuConnection) -{ - debug.nospace().noquote() << "Sdm630ModbusRtuConnection(" << sdm630ModbusRtuConnection->modbusRtuMaster()->modbusUuid().toString() << ", " << sdm630ModbusRtuConnection->modbusRtuMaster()->serialPort() << ", slave ID:" << sdm630ModbusRtuConnection->slaveId() << ")" << "\n"; - debug.nospace().noquote() << " - Total system power:" << sdm630ModbusRtuConnection->totalCurrentPower() << " [W]" << "\n"; - debug.nospace().noquote() << " - Voltage phase L1:" << sdm630ModbusRtuConnection->voltagePhaseA() << " [V]" << "\n"; - debug.nospace().noquote() << " - Voltage phase L2:" << sdm630ModbusRtuConnection->voltagePhaseB() << " [V]" << "\n"; - debug.nospace().noquote() << " - Voltage phase L3:" << sdm630ModbusRtuConnection->voltagePhaseC() << " [V]" << "\n"; - debug.nospace().noquote() << " - Current phase L1:" << sdm630ModbusRtuConnection->currentPhaseA() << " [A]" << "\n"; - debug.nospace().noquote() << " - Current phase L2:" << sdm630ModbusRtuConnection->currentPhaseB() << " [A]" << "\n"; - debug.nospace().noquote() << " - Current phase L3:" << sdm630ModbusRtuConnection->currentPhaseC() << " [A]" << "\n"; - debug.nospace().noquote() << " - Power phase L1:" << sdm630ModbusRtuConnection->powerPhaseA() << " [W]" << "\n"; - debug.nospace().noquote() << " - Power phase L2:" << sdm630ModbusRtuConnection->powerPhaseB() << " [W]" << "\n"; - debug.nospace().noquote() << " - Power phase L3:" << sdm630ModbusRtuConnection->powerPhaseC() << " [W]" << "\n"; - debug.nospace().noquote() << " - Frequency:" << sdm630ModbusRtuConnection->frequency() << " [Hz]" << "\n"; - debug.nospace().noquote() << " - Total energy consumed:" << sdm630ModbusRtuConnection->totalEnergyConsumed() << " [kWh]" << "\n"; - debug.nospace().noquote() << " - Total energy produced:" << sdm630ModbusRtuConnection->totalEnergyProduced() << " [kWh]" << "\n"; - debug.nospace().noquote() << " - Energy produced phase A:" << sdm630ModbusRtuConnection->energyProducedPhaseA() << " [kWh]" << "\n"; - debug.nospace().noquote() << " - Energy produced phase B:" << sdm630ModbusRtuConnection->energyProducedPhaseB() << " [kWh]" << "\n"; - debug.nospace().noquote() << " - Energy produced phase C:" << sdm630ModbusRtuConnection->energyProducedPhaseC() << " [kWh]" << "\n"; - debug.nospace().noquote() << " - Energy consumed phase A:" << sdm630ModbusRtuConnection->energyConsumedPhaseA() << " [kWh]" << "\n"; - debug.nospace().noquote() << " - Energy consumed phase B:" << sdm630ModbusRtuConnection->energyConsumedPhaseB() << " [kWh]" << "\n"; - debug.nospace().noquote() << " - Energy consumed phase C:" << sdm630ModbusRtuConnection->energyConsumedPhaseC() << " [kWh]" << "\n"; - return debug.quote().space(); -} - diff --git a/bgetech/sdm630modbusrtuconnection.h b/bgetech/sdm630modbusrtuconnection.h deleted file mode 100644 index 649a667..0000000 --- a/bgetech/sdm630modbusrtuconnection.h +++ /dev/null @@ -1,199 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -* -* Copyright 2013 - 2021, nymea GmbH -* Contact: contact@nymea.io -* -* This fileDescriptor is part of nymea. -* This project including source code and documentation is protected by -* copyright law, and remains the property of nymea GmbH. All rights, including -* reproduction, publication, editing and translation, are reserved. The use of -* this project is subject to the terms of a license agreement to be concluded -* with nymea GmbH in accordance with the terms of use of nymea GmbH, available -* under https://nymea.io/license -* -* GNU Lesser General Public License Usage -* Alternatively, this project may be redistributed and/or modified under the -* terms of the GNU Lesser General Public License as published by the Free -* Software Foundation; version 3. This project is distributed in the hope that -* it will be useful, but WITHOUT ANY WARRANTY; without even the implied -* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -* Lesser General Public License for more details. -* -* You should have received a copy of the GNU Lesser General Public License -* along with this project. If not, see . -* -* For any further details and any questions please contact us under -* contact@nymea.io or see our FAQ/Licensing Information on -* https://nymea.io/license/faq -* -* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -#ifndef SDM630MODBUSRTUCONNECTION_H -#define SDM630MODBUSRTUCONNECTION_H - -#include - -#include "../modbus/modbusdatautils.h" -#include - -class Sdm630ModbusRtuConnection : public QObject -{ - Q_OBJECT -public: - explicit Sdm630ModbusRtuConnection(ModbusRtuMaster *modbusRtuMaster, quint16 slaveId, QObject *parent = nullptr); - ~Sdm630ModbusRtuConnection() = default; - - ModbusRtuMaster *modbusRtuMaster() const; - quint16 slaveId() const; - - /* Total system power [W] - Address: 52, Size: 2 */ - float totalCurrentPower() const; - - /* Voltage phase L1 [V] - Address: 0, Size: 2 */ - float voltagePhaseA() const; - - /* Voltage phase L2 [V] - Address: 2, Size: 2 */ - float voltagePhaseB() const; - - /* Voltage phase L3 [V] - Address: 4, Size: 2 */ - float voltagePhaseC() const; - - /* Current phase L1 [A] - Address: 6, Size: 2 */ - float currentPhaseA() const; - - /* Current phase L2 [A] - Address: 8, Size: 2 */ - float currentPhaseB() const; - - /* Current phase L3 [A] - Address: 10, Size: 2 */ - float currentPhaseC() const; - - /* Read block from start addess 0 with size of 12 registers containing following 6 properties: - - Voltage phase L1 [V] - Address: 0, Size: 2 - - Voltage phase L2 [V] - Address: 2, Size: 2 - - Voltage phase L3 [V] - Address: 4, Size: 2 - - Current phase L1 [A] - Address: 6, Size: 2 - - Current phase L2 [A] - Address: 8, Size: 2 - - Current phase L3 [A] - Address: 10, Size: 2 - */ - void updatePhaseVoltageAndCurrentBlock(); - /* Power phase L1 [W] - Address: 12, Size: 2 */ - float powerPhaseA() const; - - /* Power phase L2 [W] - Address: 14, Size: 2 */ - float powerPhaseB() const; - - /* Power phase L3 [W] - Address: 16, Size: 2 */ - float powerPhaseC() const; - - /* Read block from start addess 12 with size of 6 registers containing following 3 properties: - - Power phase L1 [W] - Address: 12, Size: 2 - - Power phase L2 [W] - Address: 14, Size: 2 - - Power phase L3 [W] - Address: 16, Size: 2 - */ - void updatePhasePowerBlock(); - /* Frequency [Hz] - Address: 70, Size: 2 */ - float frequency() const; - - /* Total energy consumed [kWh] - Address: 72, Size: 2 */ - float totalEnergyConsumed() const; - - /* Total energy produced [kWh] - Address: 74, Size: 2 */ - float totalEnergyProduced() const; - - /* Read block from start addess 70 with size of 6 registers containing following 3 properties: - - Frequency [Hz] - Address: 70, Size: 2 - - Total energy consumed [kWh] - Address: 72, Size: 2 - - Total energy produced [kWh] - Address: 74, Size: 2 - */ - void updateFrequencyAndTotalEnergyBlock(); - /* Energy produced phase A [kWh] - Address: 346, Size: 2 */ - float energyProducedPhaseA() const; - - /* Energy produced phase B [kWh] - Address: 348, Size: 2 */ - float energyProducedPhaseB() const; - - /* Energy produced phase C [kWh] - Address: 350, Size: 2 */ - float energyProducedPhaseC() const; - - /* Energy consumed phase A [kWh] - Address: 352, Size: 2 */ - float energyConsumedPhaseA() const; - - /* Energy consumed phase B [kWh] - Address: 354, Size: 2 */ - float energyConsumedPhaseB() const; - - /* Energy consumed phase C [kWh] - Address: 356, Size: 2 */ - float energyConsumedPhaseC() const; - - /* Read block from start addess 346 with size of 12 registers containing following 6 properties: - - Energy produced phase A [kWh] - Address: 346, Size: 2 - - Energy produced phase B [kWh] - Address: 348, Size: 2 - - Energy produced phase C [kWh] - Address: 350, Size: 2 - - Energy consumed phase A [kWh] - Address: 352, Size: 2 - - Energy consumed phase B [kWh] - Address: 354, Size: 2 - - Energy consumed phase C [kWh] - Address: 356, Size: 2 - */ - void updatePhaseEnergyEnergyBlock(); - - void updateTotalCurrentPower(); - - virtual void initialize(); - virtual void update(); - -signals: - void initializationFinished(); - - void totalCurrentPowerChanged(float totalCurrentPower); - void voltagePhaseAChanged(float voltagePhaseA); - void voltagePhaseBChanged(float voltagePhaseB); - void voltagePhaseCChanged(float voltagePhaseC); - void currentPhaseAChanged(float currentPhaseA); - void currentPhaseBChanged(float currentPhaseB); - void currentPhaseCChanged(float currentPhaseC); - void powerPhaseAChanged(float powerPhaseA); - void powerPhaseBChanged(float powerPhaseB); - void powerPhaseCChanged(float powerPhaseC); - void frequencyChanged(float frequency); - void totalEnergyConsumedChanged(float totalEnergyConsumed); - void totalEnergyProducedChanged(float totalEnergyProduced); - void energyProducedPhaseAChanged(float energyProducedPhaseA); - void energyProducedPhaseBChanged(float energyProducedPhaseB); - void energyProducedPhaseCChanged(float energyProducedPhaseC); - void energyConsumedPhaseAChanged(float energyConsumedPhaseA); - void energyConsumedPhaseBChanged(float energyConsumedPhaseB); - void energyConsumedPhaseCChanged(float energyConsumedPhaseC); - -private: - ModbusRtuMaster *m_modbusRtuMaster = nullptr; - quint16 m_slaveId = 1; - QVector m_pendingInitReplies; - - float m_totalCurrentPower = 0; - float m_voltagePhaseA = 0; - float m_voltagePhaseB = 0; - float m_voltagePhaseC = 0; - float m_currentPhaseA = 0; - float m_currentPhaseB = 0; - float m_currentPhaseC = 0; - float m_powerPhaseA = 0; - float m_powerPhaseB = 0; - float m_powerPhaseC = 0; - float m_frequency = 0; - float m_totalEnergyConsumed = 0; - float m_totalEnergyProduced = 0; - float m_energyProducedPhaseA = 0; - float m_energyProducedPhaseB = 0; - float m_energyProducedPhaseC = 0; - float m_energyConsumedPhaseA = 0; - float m_energyConsumedPhaseB = 0; - float m_energyConsumedPhaseC = 0; - - void verifyInitFinished(); - - ModbusRtuReply *readTotalCurrentPower(); - - -}; - -QDebug operator<<(QDebug debug, Sdm630ModbusRtuConnection *sdm630ModbusRtuConnection); - -#endif // SDM630MODBUSRTUCONNECTION_H