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
|
|
@ -2,51 +2,85 @@
|
||||||
#define HOYMILES_H
|
#define HOYMILES_H
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <vector>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
struct _modbus;
|
struct _modbus;
|
||||||
typedef _modbus modbus_t;
|
typedef _modbus modbus_t;
|
||||||
|
|
||||||
class MicroinverterParameter {
|
class PortParameter {
|
||||||
public:
|
protected:
|
||||||
std::string name;
|
|
||||||
|
|
||||||
int valueInt;
|
|
||||||
float valueFloat;
|
|
||||||
|
|
||||||
int age;
|
|
||||||
|
|
||||||
uint16_t addressOffset;
|
uint16_t addressOffset;
|
||||||
|
|
||||||
int registerSize;
|
int registerSize;
|
||||||
|
|
||||||
MicroinverterParameter(std::string name, uint16_t addressOffset, int registerSize);
|
void setValue(uint16_t *readArray, int registerCount);
|
||||||
|
|
||||||
void updateValue(modbus_t *modbus_context, uint16_t microinverterAddress);
|
|
||||||
};
|
|
||||||
|
|
||||||
class Microinverter{
|
|
||||||
private:
|
|
||||||
modbus_t *modbus_context;
|
|
||||||
uint16_t address;
|
|
||||||
|
|
||||||
std::vector<MicroinverterParameter> parameters;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Microinverter(modbus_t *modbus_t, uint16_t address);
|
PortParameter(std::string name, uint16_t addressOffset, int registerSize);
|
||||||
|
|
||||||
void updateParameters();
|
std::string name;
|
||||||
|
int age;
|
||||||
|
int value;
|
||||||
|
|
||||||
void updateParameterByIndex(int i);
|
virtual void updateValue(modbus_t *modbus_context, uint16_t portStartAddress);
|
||||||
|
|
||||||
void updateParameterByName(std::string name);
|
|
||||||
|
|
||||||
MicroinverterParameter getParameterByIndex(int i);
|
|
||||||
|
|
||||||
MicroinverterParameter getParameterByName(std::string name);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class Dtu{
|
class PortParameterSerialNumber : public PortParameter {
|
||||||
|
private:
|
||||||
|
void setValue(uint16_t *readArray, int registerCount);
|
||||||
|
|
||||||
|
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:
|
||||||
|
Port(modbus_t *modbus_context, uint16_t portStartAddress);
|
||||||
|
|
||||||
|
void updateParameters();
|
||||||
|
};
|
||||||
|
|
||||||
|
class Microinverter {
|
||||||
|
private:
|
||||||
|
modbus_t *modbus_context;
|
||||||
|
|
||||||
|
std::vector<Port> ports;
|
||||||
|
|
||||||
|
void populatePorts();
|
||||||
|
|
||||||
|
public:
|
||||||
|
Microinverter(modbus_t *modbus_t);
|
||||||
|
|
||||||
|
void updatePorts();
|
||||||
|
|
||||||
|
void updatePort(int i);
|
||||||
|
|
||||||
|
Port getPort(int i);
|
||||||
|
};
|
||||||
|
|
||||||
|
class Dtu {
|
||||||
private:
|
private:
|
||||||
modbus_t *modbus_context;
|
modbus_t *modbus_context;
|
||||||
|
|
||||||
|
|
|
||||||
105
src/hoymiles.cpp
105
src/hoymiles.cpp
|
|
@ -1,8 +1,8 @@
|
||||||
#include "hoymiles.h"
|
#include "hoymiles.h"
|
||||||
#include "modbus.h"
|
#include "modbus.h"
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <thread>
|
|
||||||
|
|
||||||
Dtu::Dtu(const char *ip_address, int port) {
|
Dtu::Dtu(const char *ip_address, int port) {
|
||||||
this->modbus_context = modbus_new_tcp(ip_address, port);
|
this->modbus_context = modbus_new_tcp(ip_address, port);
|
||||||
|
|
@ -22,67 +22,106 @@ Dtu::~Dtu() {
|
||||||
|
|
||||||
void Dtu::populateMicroinverters() {
|
void Dtu::populateMicroinverters() {
|
||||||
uint16_t address{0x1000};
|
uint16_t address{0x1000};
|
||||||
Microinverter microinverter{this->modbus_context, address};
|
Microinverter microinverter{this->modbus_context};
|
||||||
this->microinverters.push_back(microinverter);
|
this->microinverters.push_back(microinverter);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Dtu::updateMicroinverters() {
|
void Dtu::updateMicroinverters() {
|
||||||
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
|
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
|
||||||
while(microinvertersIterator != this->microinverters.end()){
|
while (microinvertersIterator != this->microinverters.end()) {
|
||||||
microinvertersIterator->updateParameters();
|
microinvertersIterator->updatePorts();
|
||||||
microinvertersIterator++;
|
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->name = name;
|
||||||
|
|
||||||
this->addressOffset = addressOffset;
|
this->addressOffset = addressOffset;
|
||||||
this->registerSize = registerSize;
|
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];
|
uint16_t readArray[this->registerSize];
|
||||||
int registerCount;
|
int registerCount;
|
||||||
registerCount = modbus_read_registers(modbus_context, microinverterAddress + this->addressOffset, this->registerSize, readArray);
|
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++;
|
this->age++;
|
||||||
}
|
} else {
|
||||||
else{
|
this->setValue(readArray, registerCount);
|
||||||
for(int i{0}; i<this->registerSize; i++){
|
|
||||||
this->valueInt = readArray[i];
|
|
||||||
}
|
|
||||||
this->age = 0;
|
this->age = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Microinverter::Microinverter(modbus_t *modbus_t, uint16_t address) {
|
void Microinverter::populatePorts() {
|
||||||
this->modbus_context = modbus_t;
|
Port port{this->modbus_context, 0x1000};
|
||||||
this->address = address;
|
|
||||||
|
|
||||||
MicroinverterParameter microinverterParameter{"gridVoltage", 0x0001, 6};
|
this->ports.push_back(port);
|
||||||
|
|
||||||
this->parameters.push_back(microinverterParameter);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Microinverter::updateParameters() {
|
void Microinverter::updatePorts() {
|
||||||
std::vector<MicroinverterParameter>::iterator parametersIterator = this->parameters.begin();
|
for(Port port : this->ports){
|
||||||
while(parametersIterator != this->parameters.end()) {
|
port.updateParameters();
|
||||||
parametersIterator->updateValue(this->modbus_context, this->address);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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++;
|
parametersIterator++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Microinverter::updateParameterByIndex(int i){
|
Port::Port(modbus_t *modbus_context, uint16_t portStartAddress) {
|
||||||
uint16_t readArray[2];
|
this->modbus_context = modbus_context;
|
||||||
int registerCount;
|
this->portStartAddress = portStartAddress;
|
||||||
registerCount = modbus_read_registers(this->modbus_context, this->address + this->parameters.at(i).addressOffset, this->parameters.at(i).registerSize, readArray);
|
|
||||||
if(registerCount == -1){
|
this->populateParameters();
|
||||||
this->parameters.at(i).age++;
|
}
|
||||||
}
|
|
||||||
else{
|
PortParameterFloat::PortParameterFloat(std::string name, uint16_t addressOffset, int registerSize, int decimalPlaces) : PortParameter(name, addressOffset, registerSize) {
|
||||||
this->parameters.at(i).valueInt = readArray[0];
|
this->decimalPlaces = decimalPlaces;
|
||||||
this->parameters.at(i).valueInt = readArray[1];
|
this->value = 0;
|
||||||
this->parameters.at(i).age = 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"};
|
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};
|
||||||
|
|
||||||
|
hoymilesPort.updateParameters();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue