From 32d9dfff98ca9a3e36bc712559dc1c749e3d9817 Mon Sep 17 00:00:00 2001 From: trabus322 Date: Fri, 5 Apr 2024 16:48:06 +0200 Subject: [PATCH] MAde some small corrections, fixed up some values, preparing for writing --- .../portParameters/portParametersGeneric.h | 9 ++++-- src/hoymiles/dtu.cpp | 4 +-- src/hoymiles/microinverter.cpp | 2 +- src/hoymiles/port.cpp | 3 +- .../portParameters/portParameters.cpp | 32 +++++++++---------- .../portParameters/portParametersGeneric.cpp | 9 ++++-- src/main.cpp | 19 +++++++---- 7 files changed, 46 insertions(+), 32 deletions(-) diff --git a/inc/hoymiles/portParameters/portParametersGeneric.h b/inc/hoymiles/portParameters/portParametersGeneric.h index b91952b..b9ba714 100644 --- a/inc/hoymiles/portParameters/portParametersGeneric.h +++ b/inc/hoymiles/portParameters/portParametersGeneric.h @@ -15,8 +15,11 @@ class PortParameter { std::string unit; + bool r; + bool w; + public: - PortParameter(std::string name, std::string shortName, std::string unit, uint16_t parameterAddressOffset, int registerSize); + PortParameter(std::string name, std::string shortName, std::string unit, bool r, bool w, uint16_t parameterAddressOffset, int registerSize); virtual ~PortParameter(); @@ -51,7 +54,7 @@ class PortParameterFloat : public PortParameter { int decimalPlaces; public: - PortParameterFloat(std::string name, std::string shortName, std::string unit, int decimalPlaces, uint16_t parameterAddressOffset, int registerSize); + PortParameterFloat(std::string name, std::string shortName, std::string unit, bool r, bool w, int decimalPlaces, uint16_t parameterAddressOffset, int registerSize); std::string getOutputValue(); @@ -62,7 +65,7 @@ class PortParameterInt : public PortParameter { protected: public: - PortParameterInt(std::string name, std::string shortName, std::string unit, uint16_t parameterAddressOffset, int registerSize); + PortParameterInt(std::string name, std::string shortName, std::string unit, bool r, bool w, uint16_t parameterAddressOffset, int registerSize); std::string getOutputValue(); diff --git a/src/hoymiles/dtu.cpp b/src/hoymiles/dtu.cpp index 96cdd5c..4f2f9bc 100644 --- a/src/hoymiles/dtu.cpp +++ b/src/hoymiles/dtu.cpp @@ -111,10 +111,10 @@ void Dtu::printMicroinverters(std::vector ¶metersToGet, bool al std::cout << " " << "Microinverter: " << microinverterPair.first->serialNumber << std::endl; std::cout << " " << "Microinverter Data Age: " << microinverterPair.first->age << std::endl; if (printTodayProduction) { - std::cout << " " << "TodayProduction: " << microinverterPair.first->getTodayProduction() << std::endl; + std::cout << " " << "TodayProduction: " << microinverterPair.first->getTodayProduction() << "Wh" << std::endl; } if (printTotalProduction) { - std::cout << " " << "TotalProduction: " << microinverterPair.first->getTotalProduction() << std::endl; + std::cout << " " << "TotalProduction: " << microinverterPair.first->getTotalProduction() << "Wh" << std::endl; } microinverterPair.first->printPorts(parametersToGet, allParameters, shortNames); std::cout << std::endl; diff --git a/src/hoymiles/microinverter.cpp b/src/hoymiles/microinverter.cpp index 993599e..c36cc40 100644 --- a/src/hoymiles/microinverter.cpp +++ b/src/hoymiles/microinverter.cpp @@ -43,7 +43,7 @@ void Microinverter::updateParameters(std::vector ¶metersToGet, } for (int i{0}; i < portsToRead; i++) { - this->ports.at(i).setParametersFromMicroinverterArray(registers, i * 0x0019); + this->ports.at(i + portsRead).setParametersFromMicroinverterArray(registers, i * 0x0019); } portsRead += portsToRead; diff --git a/src/hoymiles/port.cpp b/src/hoymiles/port.cpp index 7b1a518..069acf6 100644 --- a/src/hoymiles/port.cpp +++ b/src/hoymiles/port.cpp @@ -80,7 +80,7 @@ void Port::fixCurrent() { if (this->getParameterByName("pvPower").first->getValue().first.f > this->getParameterByName("pvVoltage").first->getValue().first.f * this->getParameterByName("pvCurrentMI").first->getValue().first.f) { this->parameters.erase(std::find(this->parameters.begin(), this->parameters.end(), this->getParameterByName("pvCurrentHM").first)); } else { - this->parameters.erase(std::find(this->parameters.begin(), this->parameters.end(), this->getParameterByName("pvCurrentM").first)); + this->parameters.erase(std::find(this->parameters.begin(), this->parameters.end(), this->getParameterByName("pvCurrentMI").first)); } this->currentFixed = true; } @@ -129,6 +129,7 @@ void Port::setParametersFromMicroinverterArray(uint16_t *registers, int addressO parametersIterator->get()->setValueFromRegisters(registers, addressOffset); parametersIterator++; } + this->fixCurrent(); } void Port::printParameters(std::vector ¶metersToGet, bool allParameters, bool shortNames) { diff --git a/src/hoymiles/portParameters/portParameters.cpp b/src/hoymiles/portParameters/portParameters.cpp index 39237cb..478f6b8 100644 --- a/src/hoymiles/portParameters/portParameters.cpp +++ b/src/hoymiles/portParameters/portParameters.cpp @@ -5,43 +5,43 @@ #include "portParameters.h" -PortParameterMicroinverterSerialNumber::PortParameterMicroinverterSerialNumber() : PortParameterInt("microinverterSerialNumber", "mSN", "", 0x0001, 3) {} +PortParameterMicroinverterSerialNumber::PortParameterMicroinverterSerialNumber() : PortParameterInt("microinverterSerialNumber", "mSN", "", false, false, 0x0001, 3) {} void PortParameterMicroinverterSerialNumber::setValueFromRegisters(uint16_t *registers, int addressOffset) { std::string readValueString = ""; for (int i{0}; i < this->registerSize; i++) { std::stringstream readValueStringStream; - readValueStringStream << std::hex << (int) ((registers[addressOffset + this->parameterAddressOffset + i] & 0xff00) > 8); + readValueStringStream << std::hex << (int) ((registers[addressOffset + this->parameterAddressOffset + i] & 0xff00) >> 8); readValueStringStream << std::hex << (int) (registers[addressOffset + this->parameterAddressOffset + i] & 0x00ff); readValueString.append(readValueStringStream.str()); } this->value.i = std::stoll(readValueString); } -PortParameterPortNumber::PortParameterPortNumber() : PortParameterInt("portNumber", "pN", "", 0x0004, 1) {} +PortParameterPortNumber::PortParameterPortNumber() : PortParameterInt("portNumber", "pN", "", false, false, 0x0004, 1) {} -PortParameterPvVoltage::PortParameterPvVoltage() : PortParameterFloat("pvVoltage", "pvU", "V", 1, 0x0005, 1) {} +PortParameterPvVoltage::PortParameterPvVoltage() : PortParameterFloat("pvVoltage", "pvU", "V", false, false, 1, 0x0005, 1) {} -PortParameterPvCurrentMi::PortParameterPvCurrentMi() : PortParameterFloat("pvCurrentMI", "pvIMI", "A", 1, 0x0006, 1) {} +PortParameterPvCurrentMi::PortParameterPvCurrentMi() : PortParameterFloat("pvCurrentMI", "pvIMI", "A", false, false, 1, 0x0006, 1) {} -PortParameterPvCurrentHm::PortParameterPvCurrentHm() : PortParameterFloat("pvCurrentHM", "pvIHM", "A", 2, 0x0006, 1) {} +PortParameterPvCurrentHm::PortParameterPvCurrentHm() : PortParameterFloat("pvCurrentHM", "pvIHM", "A", false, false, 2, 0x0006, 1) {} -PortParameterGridVoltage::PortParameterGridVoltage() : PortParameterFloat("gridVoltage", "gU", "V", 1, 0x0007, 1) {} +PortParameterGridVoltage::PortParameterGridVoltage() : PortParameterFloat("gridVoltage", "gU", "V", false, false, 1, 0x0007, 1) {} -PortParameterGridFrequency::PortParameterGridFrequency() : PortParameterFloat("gridFrequency", "gF", "Hz", 2, 0x0008, 1) {} +PortParameterGridFrequency::PortParameterGridFrequency() : PortParameterFloat("gridFrequency", "gF", "Hz", false, false, 2, 0x0008, 1) {} -PortParameterPvPower::PortParameterPvPower() : PortParameterFloat("pvPower", "pvP", "W", 1, 0x0009, 1) {} +PortParameterPvPower::PortParameterPvPower() : PortParameterFloat("pvPower", "pvP", "W", false, false, 1, 0x0009, 1) {} -PortParameterTodayProduction::PortParameterTodayProduction() : PortParameterInt("todayProduction", "tdP", "Wh", 0x000A, 1) {} +PortParameterTodayProduction::PortParameterTodayProduction() : PortParameterInt("todayProduction", "tdP", "Wh", false, false, 0x000A, 1) {} -PortParameterTotalProduction::PortParameterTotalProduction() : PortParameterInt("totalProduction", "ttP", "Wh", 0x000B, 2) {} +PortParameterTotalProduction::PortParameterTotalProduction() : PortParameterInt("totalProduction", "ttP", "Wh", false, false, 0x000B, 2) {} -PortParameterTemperature::PortParameterTemperature() : PortParameterFloat("temperature", "t", "C", 1, 0x000D, 1) {} +PortParameterTemperature::PortParameterTemperature() : PortParameterFloat("temperature", "t", "C", false, false, 1, 0x000D, 1) {} -PortParameterOperatingStatus::PortParameterOperatingStatus() : PortParameterInt("operatingStatus", "oS", "", 0x000e, 1) {} +PortParameterOperatingStatus::PortParameterOperatingStatus() : PortParameterInt("operatingStatus", "oS", "", false, false, 0x000e, 1) {} -PortParameterAlarmCode::PortParameterAlarmCode() : PortParameterInt("alarmCode", "aC", "", 0x000f, 1) {} +PortParameterAlarmCode::PortParameterAlarmCode() : PortParameterInt("alarmCode", "aC", "", false, false, 0x000f, 1) {} -PortParameterAlarmCount::PortParameterAlarmCount() : PortParameterInt("alarmCount", "aCnt", "", 0x0010, 1) {} +PortParameterAlarmCount::PortParameterAlarmCount() : PortParameterInt("alarmCount", "aCnt", "", false, false, 0x0010, 1) {} -PortParameterLinkStatus::PortParameterLinkStatus() : PortParameterInt("linkStatus", "lS", "", 0x011, 1) {} \ No newline at end of file +PortParameterLinkStatus::PortParameterLinkStatus() : PortParameterInt("linkStatus", "lS", "", false, false, 0x011, 1) {} \ No newline at end of file diff --git a/src/hoymiles/portParameters/portParametersGeneric.cpp b/src/hoymiles/portParameters/portParametersGeneric.cpp index bdea96f..c8b0a81 100644 --- a/src/hoymiles/portParameters/portParametersGeneric.cpp +++ b/src/hoymiles/portParameters/portParametersGeneric.cpp @@ -8,11 +8,14 @@ #include "portParametersGeneric.h" -PortParameter::PortParameter(std::string name, std::string shortName, std::string unit, uint16_t parameterAddressOffset, int registerSize) { +PortParameter::PortParameter(std::string name, std::string shortName, std::string unit, bool r, bool w, uint16_t parameterAddressOffset, int registerSize) { this->name = name; this->shortName = shortName; this->unit = unit; + this->r = r; + this->w = w; + this->parameterAddressOffset = parameterAddressOffset; this->registerSize = registerSize; @@ -47,7 +50,7 @@ std::string PortParameter::getOutputValue() { // } // } -PortParameterFloat::PortParameterFloat(std::string name, std::string shortName, std::string unit, int decimalPlaces, uint16_t parameterAddressOffset, int registerSize) : PortParameter(name, shortName, unit, parameterAddressOffset, registerSize) { +PortParameterFloat::PortParameterFloat(std::string name, std::string shortName, std::string unit, bool r, bool w, int decimalPlaces, uint16_t parameterAddressOffset, int registerSize) : PortParameter(name, shortName, unit, r, w, parameterAddressOffset, registerSize) { this->decimalPlaces = decimalPlaces; this->valueType = Float; @@ -71,7 +74,7 @@ std::string PortParameterFloat::getOutputValue() { return valueStringStream.str().append(this->unit.c_str()); } -PortParameterInt::PortParameterInt(std::string name, std::string shortName, std::string unit, uint16_t parameterAddressOffset, int registerSize) : PortParameter(name, shortName, unit, parameterAddressOffset, registerSize) { +PortParameterInt::PortParameterInt(std::string name, std::string shortName, std::string unit, bool r, bool w, uint16_t parameterAddressOffset, int registerSize) : PortParameter(name, shortName, unit, r, w, parameterAddressOffset, registerSize) { this->valueType = Int; this->value.i = 0; diff --git a/src/main.cpp b/src/main.cpp index 0b22ab6..5052c0d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,11 +1,11 @@ #include #include +#include +#include +#include #include #include #include -#include -#include -#include #include "CLI11.hpp" #include "modbus.h" @@ -19,11 +19,13 @@ int main(int argc, char **argv) { signal(SIGTERM, sigHandler); signal(SIGABRT, sigHandler); - std::string version{"v2.1"}; + std::string version{"v2.2w"}; std::cout << version << std::endl; CLI::App hoymilesClient{"Client for DTU-Pro/DTU-ProS"}; + hoymilesClient.set_version_flag("-v,--version", version); + std::string ipAddress{"127.0.0.1"}; std::string ipAddressHelp{"ipv4 address of DTU {default: " + ipAddress + "}"}; hoymilesClient.add_option("-i,--ip_address", ipAddress, ipAddressHelp)->required()->group("Networking"); @@ -33,7 +35,8 @@ int main(int argc, char **argv) { hoymilesClient.add_option("-p,--port", port, portHelp)->group("Networking"); std::vector parametersToGet{}; - std::string parametersToGetHelp{"List of parameters to fetch, delimited by ','; possible parameters:\n - pvVoltage [pvU]\n - pvCurrentMI [pvIMI]\n - pvCurrentHM [pvIHM]\n - gridVoltage [gU]\n - gridFrequency [gF]\n - pvPower [gP]\n - todayProduction [tdP]\n - totalProduction [ttP]\n - temperature [t]\n - operatingStatus [oS]\n - alarmCode [aC]\n - alarmCount [aCnt]\n - linkStatus [lS]"}; + std::string parametersToGetHelp{"List of parameters to fetch, delimited by ','; possible parameters:\n - pvVoltage [pvU]\n - pvCurrentMI [pvIMI]\n - pvCurrentHM [pvIHM]\n - gridVoltage [gU]\n - gridFrequency [gF]\n - pvPower [gP]\n - " + "todayProduction [tdP]\n - totalProduction [ttP]\n - temperature [t]\n - operatingStatus [oS]\n - alarmCode [aC]\n - alarmCount [aCnt]\n - linkStatus [lS]"}; hoymilesClient.add_option>("-P,--parameters", parametersToGet, parametersToGetHelp)->delimiter(',')->group("Parameters"); bool allParameters = false; @@ -60,7 +63,6 @@ int main(int argc, char **argv) { std::string ignoreNotConnectedHelp{"Ignore conn_error"}; hoymilesClient.add_flag("-I,--ignore_conn_error", ignoreNotConnected, ignoreNotConnectedHelp)->group("Debug"); - try { hoymilesClient.parse(argc, argv); } catch (const CLI::ParseError &e) { @@ -72,11 +74,16 @@ int main(int argc, char **argv) { Dtu dtu{ipAddress.c_str(), port}; auto endTime = std::chrono::high_resolution_clock::now(); std::cout << "DTU construction time: " << std::chrono::duration_cast(endTime - startTime).count() << "ms" << std::endl; + std::cout << std::endl << std::endl; while ((dtu.isConnected() || ignoreNotConnected) && (!parametersToGet.empty() || allParameters)) { + time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); + startTime = std::chrono::high_resolution_clock::now(); dtu.updateMicroinverters(parametersToGet, allParameters, microinvertersToGet); endTime = std::chrono::high_resolution_clock::now(); + + std::cout << "Pass time: " << std::put_time(localtime(&now), "%F %T") << std::endl; std::cout << "DTU update time: " << std::chrono::duration_cast(endTime - startTime).count() << "ms" << std::endl; dtu.printMicroinverters(parametersToGet, allParameters, microinvertersToGet, shortNames, microinvertersGetTodayProduction, microinvertersGetTotalProduction);