MAde some small corrections, fixed up some values, preparing for writing

This commit is contained in:
TraYali 2024-04-05 16:48:06 +02:00
parent 35c4c661c7
commit 32d9dfff98
7 changed files with 46 additions and 32 deletions

View file

@ -15,8 +15,11 @@ class PortParameter {
std::string unit; std::string unit;
bool r;
bool w;
public: 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(); virtual ~PortParameter();
@ -51,7 +54,7 @@ class PortParameterFloat : public PortParameter {
int decimalPlaces; int decimalPlaces;
public: 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(); std::string getOutputValue();
@ -62,7 +65,7 @@ class PortParameterInt : public PortParameter {
protected: protected:
public: 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(); std::string getOutputValue();

View file

@ -111,10 +111,10 @@ void Dtu::printMicroinverters(std::vector<std::string> &parametersToGet, bool al
std::cout << " " << "Microinverter: " << microinverterPair.first->serialNumber << std::endl; std::cout << " " << "Microinverter: " << microinverterPair.first->serialNumber << std::endl;
std::cout << " " << "Microinverter Data Age: " << microinverterPair.first->age << std::endl; std::cout << " " << "Microinverter Data Age: " << microinverterPair.first->age << std::endl;
if (printTodayProduction) { if (printTodayProduction) {
std::cout << " " << "TodayProduction: " << microinverterPair.first->getTodayProduction() << std::endl; std::cout << " " << "TodayProduction: " << microinverterPair.first->getTodayProduction() << "Wh" << std::endl;
} }
if (printTotalProduction) { 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); microinverterPair.first->printPorts(parametersToGet, allParameters, shortNames);
std::cout << std::endl; std::cout << std::endl;

View file

@ -43,7 +43,7 @@ void Microinverter::updateParameters(std::vector<std::string> &parametersToGet,
} }
for (int i{0}; i < portsToRead; i++) { 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; portsRead += portsToRead;

View file

@ -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) { 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)); this->parameters.erase(std::find(this->parameters.begin(), this->parameters.end(), this->getParameterByName("pvCurrentHM").first));
} else { } 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; this->currentFixed = true;
} }
@ -129,6 +129,7 @@ void Port::setParametersFromMicroinverterArray(uint16_t *registers, int addressO
parametersIterator->get()->setValueFromRegisters(registers, addressOffset); parametersIterator->get()->setValueFromRegisters(registers, addressOffset);
parametersIterator++; parametersIterator++;
} }
this->fixCurrent();
} }
void Port::printParameters(std::vector<std::string> &parametersToGet, bool allParameters, bool shortNames) { void Port::printParameters(std::vector<std::string> &parametersToGet, bool allParameters, bool shortNames) {

View file

@ -5,43 +5,43 @@
#include "portParameters.h" #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) { void PortParameterMicroinverterSerialNumber::setValueFromRegisters(uint16_t *registers, int addressOffset) {
std::string readValueString = ""; std::string readValueString = "";
for (int i{0}; i < this->registerSize; i++) { for (int i{0}; i < this->registerSize; i++) {
std::stringstream readValueStringStream; 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); readValueStringStream << std::hex << (int) (registers[addressOffset + this->parameterAddressOffset + i] & 0x00ff);
readValueString.append(readValueStringStream.str()); readValueString.append(readValueStringStream.str());
} }
this->value.i = std::stoll(readValueString); 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) {} PortParameterLinkStatus::PortParameterLinkStatus() : PortParameterInt("linkStatus", "lS", "", false, false, 0x011, 1) {}

View file

@ -8,11 +8,14 @@
#include "portParametersGeneric.h" #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->name = name;
this->shortName = shortName; this->shortName = shortName;
this->unit = unit; this->unit = unit;
this->r = r;
this->w = w;
this->parameterAddressOffset = parameterAddressOffset; this->parameterAddressOffset = parameterAddressOffset;
this->registerSize = registerSize; 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->decimalPlaces = decimalPlaces;
this->valueType = Float; this->valueType = Float;
@ -71,7 +74,7 @@ std::string PortParameterFloat::getOutputValue() {
return valueStringStream.str().append(this->unit.c_str()); 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->valueType = Int;
this->value.i = 0; this->value.i = 0;

View file

@ -1,11 +1,11 @@
#include <chrono> #include <chrono>
#include <iostream> #include <iostream>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string> #include <string>
#include <thread> #include <thread>
#include <vector> #include <vector>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include "CLI11.hpp" #include "CLI11.hpp"
#include "modbus.h" #include "modbus.h"
@ -19,11 +19,13 @@ int main(int argc, char **argv) {
signal(SIGTERM, sigHandler); signal(SIGTERM, sigHandler);
signal(SIGABRT, sigHandler); signal(SIGABRT, sigHandler);
std::string version{"v2.1"}; std::string version{"v2.2w"};
std::cout << version << std::endl; std::cout << version << std::endl;
CLI::App hoymilesClient{"Client for DTU-Pro/DTU-ProS"}; 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 ipAddress{"127.0.0.1"};
std::string ipAddressHelp{"ipv4 address of DTU {default: " + ipAddress + "}"}; std::string ipAddressHelp{"ipv4 address of DTU {default: " + ipAddress + "}"};
hoymilesClient.add_option<std::string>("-i,--ip_address", ipAddress, ipAddressHelp)->required()->group("Networking"); hoymilesClient.add_option<std::string>("-i,--ip_address", ipAddress, ipAddressHelp)->required()->group("Networking");
@ -33,7 +35,8 @@ int main(int argc, char **argv) {
hoymilesClient.add_option<int>("-p,--port", port, portHelp)->group("Networking"); hoymilesClient.add_option<int>("-p,--port", port, portHelp)->group("Networking");
std::vector<std::string> parametersToGet{}; std::vector<std::string> 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<std::vector<std::string>>("-P,--parameters", parametersToGet, parametersToGetHelp)->delimiter(',')->group("Parameters"); hoymilesClient.add_option<std::vector<std::string>>("-P,--parameters", parametersToGet, parametersToGetHelp)->delimiter(',')->group("Parameters");
bool allParameters = false; bool allParameters = false;
@ -60,7 +63,6 @@ int main(int argc, char **argv) {
std::string ignoreNotConnectedHelp{"Ignore conn_error"}; std::string ignoreNotConnectedHelp{"Ignore conn_error"};
hoymilesClient.add_flag<bool>("-I,--ignore_conn_error", ignoreNotConnected, ignoreNotConnectedHelp)->group("Debug"); hoymilesClient.add_flag<bool>("-I,--ignore_conn_error", ignoreNotConnected, ignoreNotConnectedHelp)->group("Debug");
try { try {
hoymilesClient.parse(argc, argv); hoymilesClient.parse(argc, argv);
} catch (const CLI::ParseError &e) { } catch (const CLI::ParseError &e) {
@ -72,11 +74,16 @@ int main(int argc, char **argv) {
Dtu dtu{ipAddress.c_str(), port}; Dtu dtu{ipAddress.c_str(), port};
auto endTime = std::chrono::high_resolution_clock::now(); auto endTime = std::chrono::high_resolution_clock::now();
std::cout << "DTU construction time: " << std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count() << "ms" << std::endl; std::cout << "DTU construction time: " << std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count() << "ms" << std::endl;
std::cout << std::endl << std::endl;
while ((dtu.isConnected() || ignoreNotConnected) && (!parametersToGet.empty() || allParameters)) { 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(); startTime = std::chrono::high_resolution_clock::now();
dtu.updateMicroinverters(parametersToGet, allParameters, microinvertersToGet); dtu.updateMicroinverters(parametersToGet, allParameters, microinvertersToGet);
endTime = std::chrono::high_resolution_clock::now(); 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<std::chrono::milliseconds>(endTime - startTime).count() << "ms" << std::endl; std::cout << "DTU update time: " << std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count() << "ms" << std::endl;
dtu.printMicroinverters(parametersToGet, allParameters, microinvertersToGet, shortNames, microinvertersGetTodayProduction, microinvertersGetTotalProduction); dtu.printMicroinverters(parametersToGet, allParameters, microinvertersToGet, shortNames, microinvertersGetTodayProduction, microinvertersGetTotalProduction);