This commit is contained in:
TraYali 2024-04-18 18:27:57 +02:00
parent 79f8ee4944
commit c24e6c7d06
4 changed files with 129 additions and 13 deletions

View file

@ -11,7 +11,8 @@
#include "portParameters.h"
Dtu::Dtu(const char *address, int id, bool rtu, bool tcp) {
Dtu::Dtu(const char *address, int id, bool rtu, bool tcp) : dtuPort(0) {
dtuPort.statusPortStartAddress = 0xd000;
if (tcp) {
this->modbus = modbus_new_tcp(address, id);
}
@ -22,7 +23,7 @@ Dtu::Dtu(const char *address, int id, bool rtu, bool tcp) {
this->connected = false;
if (modbus_connect(this->modbus) == -1) {
std::cerr << "NOT CONNECTED" << std::endl;
std::cerr << "[" << id << "] NOT_CONNECTED" << std::endl;
} else {
this->connected = true;
if (rtu) {
@ -32,6 +33,24 @@ Dtu::Dtu(const char *address, int id, bool rtu, bool tcp) {
}
}
Dtu::Dtu(modbus_t *modbus, int id = -1) : dtuPort(0) {
dtuPort.statusPortStartAddress = 0xd000;
this->modbus = modbus;
this->rtuId = id;
this->connected = false;
if (modbus_connect(this->modbus) == -1) {
std::cerr << "[" << id << "] NOT_CONNECTED" << std::endl;
} else {
this->connected = true;
if (id != -1) {
modbus_set_slave(this->modbus, rtuId);
}
this->populateMicroinverters();
}
}
bool Dtu::isConnected() { return this->connected; }
Dtu::~Dtu() {
@ -40,14 +59,32 @@ Dtu::~Dtu() {
}
void Dtu::populateMicroinverters() {
if(this->rtuId != -1) {
modbus_set_slave(this->modbus, this->rtuId);
}
int portStartAddress = 0x4000;
uint16_t registers[19];
int registersToRead{19};
uint16_t registers[registersToRead];
bool lastSuccesful{true};
int addressToSkip{-1};
while (portStartAddress <= (0x4000 + (0x0019 * 99))) {
int registerCount;
registerCount = modbus_read_registers(this->modbus, portStartAddress, 19, registers);
int registerCount{-1};
int timesTried{0};
while (((timesTried < 3) && (lastSuccesful && registerCount == -1) && portStartAddress != addressToSkip)) {
registerCount = modbus_read_registers(this->modbus, portStartAddress, registersToRead, registers);
timesTried++;
}
portStartAddress += 0x0019;
if (registers[0] == 12) {
if ((registers[0] == 12 && registerCount != -1) && portStartAddress != addressToSkip) {
if (!lastSuccesful) {
addressToSkip = portStartAddress;
portStartAddress -= 2 * 0x0019;
}
lastSuccesful = true;
Port port{portStartAddress};
port.setParametersFromMicroinverterArray(registers, 0);
@ -56,10 +93,23 @@ void Dtu::populateMicroinverters() {
this->microinverters.push_back(microinverter);
}
this->getMicroinverterBySerialNumber(port.getParameterByName("microinverterSerialNumber").first.get()->getValue().first.i).first->ports.push_back(port);
Microinverter *microinverter = this->getMicroinverterBySerialNumber(port.getParameterByName("microinverterSerialNumber").first.get()->getValue().first.i).first;
std::vector<Port>::iterator portsIterator = microinverter->ports.begin();
bool valueExists{false};
while (portsIterator != microinverter->ports.end() && !valueExists) {
if (portsIterator->getParameterByName("portNumber").first.get()->getValue().first.i == port.getParameterByName("portNumber").first.get()->getValue().first.i) {
valueExists = true;
}
portsIterator++;
}
if (!valueExists) {
microinverter->ports.push_back(port);
}
} else {
lastSuccesful = false;
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
std::this_thread::sleep_for(std::chrono::milliseconds(20));
}
}
@ -76,6 +126,9 @@ std::pair<Microinverter *, bool> Dtu::getMicroinverterBySerialNumber(long long s
}
void Dtu::updateMicroinverters(std::vector<std::string> &parametersToGet, bool allParameters, std::vector<long long> &microinvertersToGet) {
if(this->rtuId != -1) {
modbus_set_slave(this->modbus, this->rtuId);
}
if (microinvertersToGet.empty()) {
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
while (microinvertersIterator != this->microinverters.end()) {
@ -155,4 +208,39 @@ void Dtu::listOfMicroinverters() {
std::cout << " " << microinvertersIterator->serialNumber << std::endl;
microinvertersIterator++;
}
}
}
float Dtu::getCurrentPower() {
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
float currentPower{0};
while (microinvertersIterator != this->microinverters.end()) {
currentPower += microinvertersIterator->getCurrentPower();
microinvertersIterator++;
}
return currentPower;
}
int Dtu::getCurrentOnOff() {
if(this->microinverters.size() == 0) {
return 0;
}
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
int currentOn{0};
while(microinvertersIterator != this->microinverters.end()) {
currentOn += microinvertersIterator->getCurrentOnOff();
microinvertersIterator++;
}
float balance = currentOn / this->microinverters.size();
if(balance >= 0.5) {
return 1;
}
else {
return 0;
}
}
void Dtu::turnOffMicroinverters() { this->dtuPort.getStatusByName("onOff").first.get()->writeValue(0, this->modbus, this->dtuPort.statusPortStartAddress); }
void Dtu::turnOnMicroinverters() { this->dtuPort.getStatusByName("onOff").first.get()->writeValue(1, this->modbus, this->dtuPort.statusPortStartAddress); }
void Dtu::limitMicroinverters(uint16_t limit) { this->dtuPort.getStatusByName("limitActivePower").first.get()->writeValue(limit, this->modbus, this->dtuPort.statusPortStartAddress); }