Trying to make inheritance work, not worky :c
This commit is contained in:
parent
39194f9e67
commit
216ceca3e1
3 changed files with 148 additions and 71 deletions
102
inc/hoymiles.h
102
inc/hoymiles.h
|
|
@ -2,64 +2,98 @@
|
|||
#define HOYMILES_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
struct _modbus;
|
||||
typedef _modbus modbus_t;
|
||||
|
||||
class MicroinverterParameter {
|
||||
public:
|
||||
std::string name;
|
||||
class PortParameter {
|
||||
protected:
|
||||
uint16_t addressOffset;
|
||||
|
||||
int valueInt;
|
||||
float valueFloat;
|
||||
int registerSize;
|
||||
|
||||
int age;
|
||||
void setValue(uint16_t *readArray, int registerCount);
|
||||
|
||||
uint16_t addressOffset;
|
||||
int registerSize;
|
||||
public:
|
||||
PortParameter(std::string name, uint16_t addressOffset, int registerSize);
|
||||
|
||||
MicroinverterParameter(std::string name, uint16_t addressOffset, int registerSize);
|
||||
std::string name;
|
||||
int age;
|
||||
int value;
|
||||
|
||||
void updateValue(modbus_t *modbus_context, uint16_t microinverterAddress);
|
||||
virtual void updateValue(modbus_t *modbus_context, uint16_t portStartAddress);
|
||||
};
|
||||
|
||||
class Microinverter{
|
||||
private:
|
||||
modbus_t *modbus_context;
|
||||
uint16_t address;
|
||||
class PortParameterSerialNumber : public PortParameter {
|
||||
private:
|
||||
void setValue(uint16_t *readArray, int registerCount);
|
||||
|
||||
std::vector<MicroinverterParameter> parameters;
|
||||
public:
|
||||
PortParameterSerialNumber();
|
||||
};
|
||||
|
||||
class PortParameterFloat : public PortParameter {
|
||||
private:
|
||||
int decimalPlaces;
|
||||
|
||||
void setValue(uint16_t *readArray, int registerCount);
|
||||
|
||||
public:
|
||||
PortParameterFloat(std::string name, uint16_t addressOffset, int registerSize, int decimalPlaces);
|
||||
|
||||
float value;
|
||||
};
|
||||
|
||||
class Port {
|
||||
private:
|
||||
modbus_t *modbus_context;
|
||||
uint16_t portStartAddress;
|
||||
|
||||
std::vector<std::shared_ptr<PortParameter>> parameters;
|
||||
|
||||
void populateParameters();
|
||||
|
||||
public:
|
||||
Microinverter(modbus_t *modbus_t, uint16_t address);
|
||||
Port(modbus_t *modbus_context, uint16_t portStartAddress);
|
||||
|
||||
void updateParameters();
|
||||
|
||||
void updateParameterByIndex(int i);
|
||||
|
||||
void updateParameterByName(std::string name);
|
||||
|
||||
MicroinverterParameter getParameterByIndex(int i);
|
||||
|
||||
MicroinverterParameter getParameterByName(std::string name);
|
||||
};
|
||||
|
||||
class Dtu{
|
||||
private:
|
||||
modbus_t *modbus_context;
|
||||
class Microinverter {
|
||||
private:
|
||||
modbus_t *modbus_context;
|
||||
|
||||
std::vector<Microinverter> microinverters;
|
||||
std::vector<Port> ports;
|
||||
|
||||
void populateMicroinverters();
|
||||
void populatePorts();
|
||||
|
||||
public:
|
||||
Dtu(const char *ip_address, int port);
|
||||
public:
|
||||
Microinverter(modbus_t *modbus_t);
|
||||
|
||||
void updateMicroinverters();
|
||||
void updatePorts();
|
||||
|
||||
~Dtu();
|
||||
void updatePort(int i);
|
||||
|
||||
Port getPort(int i);
|
||||
};
|
||||
|
||||
class Dtu {
|
||||
private:
|
||||
modbus_t *modbus_context;
|
||||
|
||||
std::vector<Microinverter> microinverters;
|
||||
|
||||
void populateMicroinverters();
|
||||
|
||||
public:
|
||||
Dtu(const char *ip_address, int port);
|
||||
|
||||
void updateMicroinverters();
|
||||
|
||||
~Dtu();
|
||||
};
|
||||
|
||||
#endif
|
||||
105
src/hoymiles.cpp
105
src/hoymiles.cpp
|
|
@ -1,8 +1,8 @@
|
|||
#include "hoymiles.h"
|
||||
#include "modbus.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
Dtu::Dtu(const char *ip_address, int port) {
|
||||
this->modbus_context = modbus_new_tcp(ip_address, port);
|
||||
|
|
@ -22,67 +22,106 @@ Dtu::~Dtu() {
|
|||
|
||||
void Dtu::populateMicroinverters() {
|
||||
uint16_t address{0x1000};
|
||||
Microinverter microinverter{this->modbus_context, address};
|
||||
Microinverter microinverter{this->modbus_context};
|
||||
this->microinverters.push_back(microinverter);
|
||||
}
|
||||
|
||||
void Dtu::updateMicroinverters() {
|
||||
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
|
||||
while(microinvertersIterator != this->microinverters.end()){
|
||||
microinvertersIterator->updateParameters();
|
||||
while (microinvertersIterator != this->microinverters.end()) {
|
||||
microinvertersIterator->updatePorts();
|
||||
microinvertersIterator++;
|
||||
}
|
||||
}
|
||||
|
||||
MicroinverterParameter::MicroinverterParameter(std::string name, uint16_t addressOffset, int registerSize) {
|
||||
void PortParameter::setValue(uint16_t *readArray, int registerCount) {
|
||||
this->value = readArray[0];
|
||||
}
|
||||
|
||||
PortParameter::PortParameter(std::string name, uint16_t addressOffset, int registerSize) {
|
||||
this->name = name;
|
||||
|
||||
this->addressOffset = addressOffset;
|
||||
this->registerSize = registerSize;
|
||||
|
||||
this->value = 0;
|
||||
}
|
||||
|
||||
void MicroinverterParameter::updateValue(modbus_t *modbus_context, uint16_t microinverterAddress) {
|
||||
void PortParameter::updateValue(modbus_t *modbus_context, uint16_t microinverterAddress) {
|
||||
uint16_t readArray[this->registerSize];
|
||||
int registerCount;
|
||||
registerCount = modbus_read_registers(modbus_context, microinverterAddress + this->addressOffset, this->registerSize, readArray);
|
||||
if(registerCount == -1){
|
||||
registerCount = 2;
|
||||
readArray[0] = 2309;
|
||||
readArray[1] = 4354;
|
||||
if (registerCount == -1) {
|
||||
this->age++;
|
||||
}
|
||||
else{
|
||||
for(int i{0}; i<this->registerSize; i++){
|
||||
this->valueInt = readArray[i];
|
||||
}
|
||||
} else {
|
||||
this->setValue(readArray, registerCount);
|
||||
this->age = 0;
|
||||
}
|
||||
}
|
||||
|
||||
Microinverter::Microinverter(modbus_t *modbus_t, uint16_t address) {
|
||||
this->modbus_context = modbus_t;
|
||||
this->address = address;
|
||||
void Microinverter::populatePorts() {
|
||||
Port port{this->modbus_context, 0x1000};
|
||||
|
||||
MicroinverterParameter microinverterParameter{"gridVoltage", 0x0001, 6};
|
||||
|
||||
this->parameters.push_back(microinverterParameter);
|
||||
this->ports.push_back(port);
|
||||
}
|
||||
|
||||
void Microinverter::updateParameters() {
|
||||
std::vector<MicroinverterParameter>::iterator parametersIterator = this->parameters.begin();
|
||||
while(parametersIterator != this->parameters.end()) {
|
||||
parametersIterator->updateValue(this->modbus_context, this->address);
|
||||
void Microinverter::updatePorts() {
|
||||
for(Port port : this->ports){
|
||||
port.updateParameters();
|
||||
}
|
||||
}
|
||||
|
||||
Microinverter::Microinverter(modbus_t *modbus_context) {
|
||||
this->modbus_context = modbus_context;
|
||||
|
||||
this->populatePorts();
|
||||
}
|
||||
|
||||
void Port::populateParameters() {
|
||||
PortParameterFloat portParameter{"gridVoltage", 0x0034, 2, 1};
|
||||
|
||||
this->parameters.push_back(std::make_shared<PortParameterFloat>(portParameter));
|
||||
}
|
||||
|
||||
void Port::updateParameters() {
|
||||
std::vector<std::shared_ptr<PortParameter>>::iterator parametersIterator{this->parameters.begin()};
|
||||
while(parametersIterator != this->parameters.end()){
|
||||
parametersIterator->get()->updateValue(this->modbus_context, this->portStartAddress);
|
||||
parametersIterator++;
|
||||
}
|
||||
}
|
||||
|
||||
void Microinverter::updateParameterByIndex(int i){
|
||||
uint16_t readArray[2];
|
||||
int registerCount;
|
||||
registerCount = modbus_read_registers(this->modbus_context, this->address + this->parameters.at(i).addressOffset, this->parameters.at(i).registerSize, readArray);
|
||||
if(registerCount == -1){
|
||||
this->parameters.at(i).age++;
|
||||
}
|
||||
else{
|
||||
this->parameters.at(i).valueInt = readArray[0];
|
||||
this->parameters.at(i).valueInt = readArray[1];
|
||||
this->parameters.at(i).age = 0;
|
||||
Port::Port(modbus_t *modbus_context, uint16_t portStartAddress) {
|
||||
this->modbus_context = modbus_context;
|
||||
this->portStartAddress = portStartAddress;
|
||||
|
||||
this->populateParameters();
|
||||
}
|
||||
|
||||
PortParameterFloat::PortParameterFloat(std::string name, uint16_t addressOffset, int registerSize, int decimalPlaces) : PortParameter(name, addressOffset, registerSize) {
|
||||
this->decimalPlaces = decimalPlaces;
|
||||
this->value = 0;
|
||||
}
|
||||
|
||||
void PortParameterFloat::setValue(uint16_t *readArray, int registerCount) {
|
||||
uint16_t readValue{readArray[0]};
|
||||
|
||||
this->value = readValue / std::pow(10, this->decimalPlaces);
|
||||
}
|
||||
|
||||
PortParameterSerialNumber::PortParameterSerialNumber() : PortParameter("serialNumber", 0x0001, 6){};
|
||||
|
||||
void PortParameterSerialNumber::setValue(uint16_t *readArray, int registerCount) {
|
||||
uint16_t readValue;
|
||||
int registerCountCorrected = std::min(3, registerCount);
|
||||
for(int i{0}; i<registerCountCorrected; i++){
|
||||
readValue = readArray[i] & 0xFF00;
|
||||
this->value+= readValue * std::pow(10, (registerCountCorrected-i)*2);
|
||||
|
||||
readValue = readArray[i] & 0x00FF;
|
||||
this->value+= readValue * std::pow(10, ((registerCountCorrected-i)*2)-1);
|
||||
}
|
||||
}
|
||||
12
src/main.cpp
12
src/main.cpp
|
|
@ -12,10 +12,14 @@ int main(){
|
|||
std::string ip_address {"192.168.31.136"};
|
||||
int port {502};
|
||||
|
||||
Dtu dtu {ip_address.c_str(), port};
|
||||
while(true) {
|
||||
dtu.updateMicroinverters();
|
||||
}
|
||||
// Dtu dtu {ip_address.c_str(), port};
|
||||
// while(true) {
|
||||
// dtu.updateMicroinverters();
|
||||
// }
|
||||
|
||||
Port hoymilesPort{modbus_new_tcp(ip_address.c_str(), port), 0x1000};
|
||||
|
||||
hoymilesPort.updateParameters();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue