From 670ca56b0bea0a917c4641a19b6ac41280e7ec7f Mon Sep 17 00:00:00 2001 From: trabus322 Date: Wed, 13 Mar 2024 16:54:03 +0100 Subject: [PATCH] Starting work on implementing structures --- inc/hoymiles.h | 40 +++++++++++++++++++++++++++++++++++++++- src/hoymiles.cpp | 31 +++++++++++++++++++++++-------- src/main.cpp | 11 ++++++++++- 3 files changed, 72 insertions(+), 10 deletions(-) diff --git a/inc/hoymiles.h b/inc/hoymiles.h index 72be251..dc56e08 100644 --- a/inc/hoymiles.h +++ b/inc/hoymiles.h @@ -1,6 +1,9 @@ #ifndef HOYMILES_H #define HOYMILES_H +#include +#include + struct _modbus; typedef _modbus modbus_t; @@ -8,12 +11,47 @@ class Dtu{ private: modbus_t *modbus_t; + std::vector microinverters; + public: Dtu(const char *ip_address, int port); - void readTest(); + void readTest(uint16_t address, int registers); ~Dtu(); }; +class Microinverter{ + private: + uint16_t readArray[33]; + uint16_t address; + + public: + const int serialNumber; + std::pair portNumber; + + std::pair plantVoltage; + std::pair plantCurrent; + + std::pair gridVoltage; + std::pair gridFrequency; + + std::pair plantPower; + + std::pair todayProduction; + std::pair totalProduction; + + std::pair temperature; + + std::pair operatingStatus; + std::pair alarmCode; + std::pair alarmCount; + + std::pair linkStatus; + + Microinverter(uint16_t address); + + void updateValues(); +}; + #endif \ No newline at end of file diff --git a/src/hoymiles.cpp b/src/hoymiles.cpp index fbb5821..e5bed85 100644 --- a/src/hoymiles.cpp +++ b/src/hoymiles.cpp @@ -1,16 +1,31 @@ #include "hoymiles.h" #include "modbus.h" -Dtu::Dtu(const char *ip_address, int port){ - this->modbus_t = modbus_new_tcp(ip_address, port); - modbus_connect(this->modbus_t); +#include + +Dtu::Dtu(const char *ip_address, int port) { + this->modbus_t = modbus_new_tcp(ip_address, port); + if(modbus_connect(this->modbus_t) == -1){ + std::cerr << "conn_error"; + modbus_free(this->modbus_t); + abort(); + } } -void Dtu::readTest(){ - uint16_t tempArray[32]; - modbus_read_registers(this->modbus_t, 0x13ef, 1, tempArray); +void Dtu::readTest(uint16_t address, int registers) { + uint16_t readArray[registers]; + int registerCount; + registerCount = modbus_read_registers(this->modbus_t, address, registers, readArray); + if(registerCount == -1){ + std::cerr << "read_error"; + return; + } + for (int i{0}; i < registerCount; i++) { + std::clog << readArray[i] << std::endl; + } } -Dtu::~Dtu(){ - delete this->modbus_t; +Dtu::~Dtu() { + modbus_close(this->modbus_t); + modbus_free(this->modbus_t); } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 8b9f87c..20c856c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,9 @@ #include #include +#include +#include + #include "hoymiles.h" #include "modbus.h" @@ -10,7 +13,13 @@ int main(){ int port {502}; Dtu dtu {ip_address.c_str(), port}; - dtu.readTest(); + + bool buttonPressed{false}; + for(int i{0}; i<10; i++){ + dtu.readTest(0x1034, 2); + std::clog << std::endl; + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + } return 0; } \ No newline at end of file