It's asking but need to tweek setting values from registers

This commit is contained in:
TraYali 2024-03-16 22:51:32 +01:00
parent d0f8e5b885
commit f3f1aa3903
8 changed files with 309 additions and 109 deletions

View file

@ -4,9 +4,9 @@ project(hoymilesClient VERSION 0.1.0 LANGUAGES C CXX)
include(CTest) include(CTest)
enable_testing() enable_testing()
include_directories(inc inc/libmodbus inc/hoymiles) include_directories(inc inc/libmodbus inc/hoymiles inc/hoymiles/portParameters)
file(GLOB SOURCES src/*.cpp src/libmodbus/*.c src/hoymiles/*.cpp) file(GLOB SOURCES src/*.cpp src/libmodbus/*.c src/hoymiles/*.cpp src/hoymiles/portParameters/*.cpp)
add_executable(hoymilesClient_exec ${SOURCES}) add_executable(hoymilesClient_exec ${SOURCES})

View file

@ -6,59 +6,11 @@
#include <vector> #include <vector>
#include <memory> #include <memory>
#include "portParametersGeneric.h"
struct _modbus; struct _modbus;
typedef _modbus modbus_t; typedef _modbus modbus_t;
class PortParameter {
protected:
uint16_t parameterAddressOffset;
int registerSize;
virtual void setValueFromRegisters(uint16_t *readArray, int registerCount);
public:
PortParameter(std::string name, uint16_t parameterAddressOffset, int registerSize);
virtual ~PortParameter();
enum PortParameterValueType { Int, Float };
union PortParameterValue {
int i;
float f;
};
protected:
PortParameterValueType valueType;
PortParameterValue value;
public:
std::string name;
std::pair<PortParameterValue, PortParameterValueType> getValue();
virtual std::string getOutputValue();
void updateValue(modbus_t *modbus_context, uint16_t portStartAddress);
};
class PortParameterFloat : public PortParameter {
protected:
int decimalPlaces;
void setValueFromRegisters(uint16_t *readArray, int registerCount);
public:
PortParameterFloat(std::string name, int decimalPlaces, uint16_t parameterAddressOffset, int registerSize);
virtual std::string getOutputValue();
};
class PortParameterInt : public PortParameter {
protected:
void setValueFromRegisters()
};
class Port { class Port {
private: private:
modbus_t *modbus_context; modbus_t *modbus_context;

View file

@ -0,0 +1,84 @@
#ifndef PORTPARAMETERS_H
#define PORTPARAMETERS_H
#include "portParametersGeneric.h"
class PortParameterMicroinverterSerialNumber : public PortParameterInt {
public:
PortParameterMicroinverterSerialNumber();
};
class PortParameterPortNumber : public PortParameterInt {
private:
void setValueFromRegisters(uint16_t *readArray, int registerCount);
public:
PortParameterPortNumber();
};
class PortParameterPvVoltage : public PortParameterFloat {
public:
PortParameterPvVoltage();
};
class PortParameterPvCurrentMi : public PortParameterFloat {
public:
PortParameterPvCurrentMi();
};
class PortParameterPvCurrentHm : public PortParameterFloat {
public:
PortParameterPvCurrentHm();
};
class PortParameterGridVoltage : public PortParameterFloat {
public:
PortParameterGridVoltage();
};
class PortParameterGridFrequency : public PortParameterFloat {
public:
PortParameterGridFrequency();
};
class PortParameterPvPower : public PortParameterFloat {
public:
PortParameterPvPower();
};
class PortParameterTodayProduction : public PortParameterInt {
public:
PortParameterTodayProduction();
};
class PortParameterTotalProduction : public PortParameterInt {
public:
PortParameterTotalProduction();
};
class PortParameterTemperature : public PortParameterFloat {
public:
PortParameterTemperature();
};
class PortParameterOperatingStatus : public PortParameterInt {
public:
PortParameterOperatingStatus();
};
class PortParameterAlarmCode : public PortParameterInt {
public:
PortParameterAlarmCode();
};
class PortParameterAlarmCount : public PortParameterInt {
public:
PortParameterAlarmCount();
};
class PortParameterLinkStatus : public PortParameterInt {
public:
PortParameterLinkStatus();
};
#endif

View file

@ -0,0 +1,66 @@
#ifndef PORTPARAMETERSGENERIC_H
#define PORTPARAMETERSGENERIC_H
#include <stdint.h>
#include <string>
struct _modbus;
typedef _modbus modbus_t;
class PortParameter {
protected:
uint16_t parameterAddressOffset;
int registerSize;
virtual void setValueFromRegisters(uint16_t *readArray, int registerCount);
public:
PortParameter(std::string name, uint16_t parameterAddressOffset, int registerSize);
virtual ~PortParameter();
enum PortParameterValueType { Int, Float };
union PortParameterValue {
long i;
float f;
};
protected:
PortParameterValueType valueType;
PortParameterValue value;
public:
std::string name;
int age;
std::pair<PortParameterValue, PortParameterValueType> getValue();
virtual std::string getOutputValue();
void updateValue(modbus_t *modbus_context, uint16_t portStartAddress);
};
class PortParameterFloat : virtual public PortParameter {
protected:
int decimalPlaces;
virtual void setValueFromRegisters(uint16_t *readArray, int registerCount);
public:
PortParameterFloat(std::string name, int decimalPlaces, uint16_t parameterAddressOffset, int registerSize);
std::string getOutputValue();
};
class PortParameterInt : virtual public PortParameter {
protected:
virtual void setValueFromRegisters(uint16_t *readArray, int registerCount);
public:
PortParameterInt(std::string name, uint16_t parameterAddressOffset, int registerSize);
std::string getOutputValue();
};
#endif

View file

@ -7,6 +7,7 @@
#include "modbus.h" #include "modbus.h"
#include "port.h" #include "port.h"
#include "portParameters.h"
Port::Port(modbus_t *modbus_context, uint16_t portStartAddress) { Port::Port(modbus_t *modbus_context, uint16_t portStartAddress) {
this->modbus_context = modbus_context; this->modbus_context = modbus_context;
@ -16,8 +17,35 @@ Port::Port(modbus_t *modbus_context, uint16_t portStartAddress) {
} }
void Port::populateParameters() { void Port::populateParameters() {
PortParameterFloat portParameterFloat{"gridVoltage", 1, 0x001a, 2}; this->parameters.push_back(std::make_shared<PortParameterMicroinverterSerialNumber>());
this->parameters.push_back(std::make_shared<PortParameterFloat>(portParameterFloat));
this->parameters.push_back(std::make_shared<PortParameterPortNumber>());
this->parameters.push_back(std::make_shared<PortParameterPvVoltage>());
this->parameters.push_back(std::make_shared<PortParameterPvCurrentMi>());
this->parameters.push_back(std::make_shared<PortParameterPvCurrentHm>());
this->parameters.push_back(std::make_shared<PortParameterGridVoltage>());
this->parameters.push_back(std::make_shared<PortParameterGridFrequency>());
this->parameters.push_back(std::make_shared<PortParameterPvPower>());
this->parameters.push_back(std::make_shared<PortParameterTodayProduction>());
this->parameters.push_back(std::make_shared<PortParameterTotalProduction>());
this->parameters.push_back(std::make_shared<PortParameterTemperature>());
this->parameters.push_back(std::make_shared<PortParameterOperatingStatus>());
this->parameters.push_back(std::make_shared<PortParameterAlarmCode>());
this->parameters.push_back(std::make_shared<PortParameterAlarmCount>());
this->parameters.push_back(std::make_shared<PortParameterLinkStatus>());
} }
void Port::updateParameters() { void Port::updateParameters() {
@ -27,52 +55,3 @@ void Port::updateParameters() {
parametersIterator++; parametersIterator++;
} }
} }
PortParameter::PortParameter(std::string name, uint16_t parameterAddressOffset, int registerSize) {
this->name = name;
this->parameterAddressOffset = parameterAddressOffset;
this->registerSize = registerSize;
}
PortParameter::~PortParameter() {}
void PortParameter::setValueFromRegisters(uint16_t *readArray, int registerCount) {}
std::pair<PortParameter::PortParameterValue, PortParameter::PortParameterValueType> PortParameter::getValue() {
return std::pair<PortParameter::PortParameterValue, PortParameter::PortParameterValueType>(this->value, this->valueType);
}
std::string PortParameter::getOutputValue() {}
void PortParameter::updateValue(modbus_t *modbus_context, uint16_t portStartAddress) {
uint16_t readArray[this->registerSize];
int registerCount;
registerCount = modbus_read_registers(modbus_context, portStartAddress + this->parameterAddressOffset, this->registerSize, readArray);
registerCount = 2;
readArray[0] = 2312;
readArray[1] = 5432;
if(registerCount == -1){
std::cerr << "read_error";
return;
}
else{
this->setValueFromRegisters(readArray, registerCount);
}
}
PortParameterFloat::PortParameterFloat(std::string name, int decimalPlaces, uint16_t parameterAddressOffset, int registerSize) : PortParameter(name, parameterAddressOffset, registerSize) {
this->decimalPlaces = decimalPlaces;
this->valueType = Float;
}
void PortParameterFloat::setValueFromRegisters(uint16_t *readArray, int registerCount) {
this->value.f = readArray[0] / std::pow(10, this->decimalPlaces);
}
std::string PortParameterFloat::getOutputValue() {
return std::to_string(this->value.f);
}

View file

@ -0,0 +1,39 @@
#include <cmath>
#include "portParameters.h"
PortParameterMicroinverterSerialNumber::PortParameterMicroinverterSerialNumber() : PortParameterInt("microinverterSerialNumber", 0x0001, 6), PortParameter("microinverterSerialNumber", 0x1001, 6) {}
PortParameterPortNumber::PortParameterPortNumber() : PortParameterInt("portNumber", 0x0007, 1), PortParameter("portNumber", 0x007, 1) {}
void PortParameterPortNumber::setValueFromRegisters(uint16_t *readArray, int registerCount) {
if (registerCount > 0) {
this->value.i = readArray[0];
}
}
PortParameterPvVoltage::PortParameterPvVoltage() : PortParameterFloat("pvVoltage", 1, 0x0008, 2), PortParameter("pvVoltage", 0x0008, 2) {}
PortParameterPvCurrentMi::PortParameterPvCurrentMi() : PortParameterFloat("pvCurrent", 1, 0x000a, 2), PortParameter("pvCurrent", 0x000a, 2) {}
PortParameterPvCurrentHm::PortParameterPvCurrentHm() : PortParameterFloat("pvCurrent", 2, 0x000a, 2), PortParameter("pvCurrent", 0x000a, 2) {}
PortParameterGridVoltage::PortParameterGridVoltage() : PortParameterFloat("gridVoltage", 1, 0x000c, 2), PortParameter("gridVoltage", 0x000c, 2) {}
PortParameterGridFrequency::PortParameterGridFrequency() : PortParameterFloat("gridFrequency", 2, 0x000e, 2), PortParameter("gridFrequency", 0x000e, 2) {}
PortParameterPvPower::PortParameterPvPower() : PortParameterFloat("pvPower", 1, 0x0010, 2), PortParameter("pvPower", 0x0010, 2) {}
PortParameterTodayProduction::PortParameterTodayProduction() : PortParameterInt("todayProduction", 0x0012, 2), PortParameter("todayProduction", 0x0012, 2) {}
PortParameterTotalProduction::PortParameterTotalProduction() : PortParameterInt("totalProduction", 0x0014, 4), PortParameter("totalProduction", 0x0014, 4) {}
PortParameterTemperature::PortParameterTemperature() : PortParameterFloat("temperature", 1, 0x0018, 2), PortParameter("temperature", 0x0018, 2) {}
PortParameterOperatingStatus::PortParameterOperatingStatus() : PortParameterInt("operatingStatus", 0x001a, 2), PortParameter("operatingStatus", 0x001a, 2) {}
PortParameterAlarmCode::PortParameterAlarmCode() : PortParameterInt("alarmCode", 0x001c, 2), PortParameter("alarmCode", 0x001c, 2) {}
PortParameterAlarmCount::PortParameterAlarmCount() : PortParameterInt("alarmCount", 0x001e, 2), PortParameter("alarmCount", 0x001e, 2) {}
PortParameterLinkStatus::PortParameterLinkStatus() : PortParameterInt("linkStatus", 0x020, 2), PortParameter("linkStatus", 0x020, 2) {}

View file

@ -0,0 +1,80 @@
#include <cmath>
#include "modbus.h"
#include "portParametersGeneric.h"
struct _modbus;
typedef _modbus modbus_t;
PortParameter::PortParameter(std::string name, uint16_t parameterAddressOffset, int registerSize) {
this->name = name;
this->parameterAddressOffset = parameterAddressOffset;
this->registerSize = registerSize;
this->age = 0;
}
PortParameter::~PortParameter() {}
void PortParameter::setValueFromRegisters(uint16_t *readArray, int registerCount) {}
std::pair<PortParameter::PortParameterValue, PortParameter::PortParameterValueType> PortParameter::getValue() {
return std::pair<PortParameter::PortParameterValue, PortParameter::PortParameterValueType>(this->value, this->valueType);
}
std::string PortParameter::getOutputValue() {}
void PortParameter::updateValue(modbus_t *modbus_context, uint16_t portStartAddress) {
uint16_t readArray[this->registerSize];
int registerCount;
registerCount = modbus_read_registers(modbus_context, portStartAddress + this->parameterAddressOffset, this->registerSize, readArray);
if(registerCount == -1){
this->age++;
}
else{
this->setValueFromRegisters(readArray, registerCount);
this->age = 0;
}
}
PortParameterFloat::PortParameterFloat(std::string name, int decimalPlaces, uint16_t parameterAddressOffset, int registerSize) : PortParameter(name, parameterAddressOffset, registerSize) {
this->decimalPlaces = decimalPlaces;
this->valueType = Float;
this->value.f = 0;
}
void PortParameterFloat::setValueFromRegisters(uint16_t *readArray, int registerCount) {
float temp = readArray[0];
temp = temp / std::pow(10, this->decimalPlaces);
this->value.f = temp;
}
std::string PortParameterFloat::getOutputValue() {
return std::to_string(this->value.f);
}
PortParameterInt::PortParameterInt(std::string name, uint16_t parameterAddressOffset, int registerSize) : PortParameter(name, parameterAddressOffset, registerSize) {
this->valueType = Int;
this->value.i = 0;
}
void PortParameterInt::setValueFromRegisters(uint16_t *readArray, int registerCount) {
uint16_t readValue;
for (int i{0}; i < registerCount; i++) {
readValue = (readArray[i] & 0xFF00);
this->value.i += readValue * std::pow(10, ((registerCount * 2) - ((registerCount - i) * 2)));
readValue = readArray[i] & 0x00FF;
this->value.i += readValue * std::pow(10, ((registerCount * 2) - ((registerCount - i) * 2)));
}
}
std::string PortParameterInt::getOutputValue() {
return std::to_string(this->value.i);
}

View file

@ -12,14 +12,14 @@ int main(){
std::string ip_address {"192.168.31.136"}; std::string ip_address {"192.168.31.136"};
int port {502}; int port {502};
// Dtu dtu {ip_address.c_str(), port}; Dtu dtu {ip_address.c_str(), port};
// while(true) { while(true) {
// dtu.updateMicroinverters(); dtu.updateMicroinverters();
// } }
Port hoymilesPort{modbus_new_tcp(ip_address.c_str(), port), 0x1000}; // Port hoymilesPort{modbus_new_tcp(ip_address.c_str(), port), 0x1000};
hoymilesPort.updateParameters(); // hoymilesPort.updateParameters();
return 0; return 0;
} }