2024-03-17 21:33:21 +01:00
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
|
|
#include "modbus.h"
|
|
|
|
|
|
2024-03-16 21:15:15 +01:00
|
|
|
#include "microinverter.h"
|
|
|
|
|
#include "port.h"
|
|
|
|
|
|
2024-03-17 21:33:21 +01:00
|
|
|
struct _modbus;
|
|
|
|
|
typedef _modbus modbus_t;
|
|
|
|
|
|
|
|
|
|
Microinverter::Microinverter(std::shared_ptr<modbus_t*> modbus_context, std::mutex *modbus_context_mutex) {
|
2024-03-16 21:15:15 +01:00
|
|
|
this->modbus_context = modbus_context;
|
2024-03-17 21:33:21 +01:00
|
|
|
this->modbus_context_mutex = modbus_context_mutex;
|
2024-03-16 21:15:15 +01:00
|
|
|
|
|
|
|
|
this->populatePorts();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Microinverter::populatePorts() {
|
2024-03-17 21:33:21 +01:00
|
|
|
uint16_t portStartAddress = 0x1000;
|
|
|
|
|
uint16_t readArray[1];
|
2024-03-16 21:15:15 +01:00
|
|
|
|
2024-03-17 21:33:21 +01:00
|
|
|
int registerCount;
|
|
|
|
|
registerCount = modbus_read_registers(*this->modbus_context.get(), portStartAddress + 0x0021, 1, readArray);
|
|
|
|
|
while(registerCount != -1 && readArray[0] == 0x700) {
|
|
|
|
|
Port port{ this->modbus_context, this->modbus_context_mutex, portStartAddress };
|
|
|
|
|
this->ports.push_back(port);
|
|
|
|
|
portStartAddress += 0x0028;
|
|
|
|
|
|
|
|
|
|
this->modbus_context_mutex->lock();
|
|
|
|
|
registerCount = modbus_read_registers(*this->modbus_context.get(), portStartAddress + 0x0021, 1, readArray);
|
|
|
|
|
this->modbus_context_mutex->unlock();
|
|
|
|
|
}
|
2024-03-16 21:15:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Microinverter::updatePorts() {
|
2024-03-17 21:33:21 +01:00
|
|
|
std::vector<std::thread> updateThreads;
|
2024-03-16 21:15:15 +01:00
|
|
|
for(Port port : this->ports){
|
2024-03-17 21:33:21 +01:00
|
|
|
updateThreads.push_back(std::thread(&Port::updateParameters, port));
|
|
|
|
|
// port.updateParameters();
|
|
|
|
|
}
|
|
|
|
|
std::vector<std::thread>::iterator updateThreadsIterator = updateThreads.begin();
|
|
|
|
|
while(updateThreadsIterator != updateThreads.end()) {
|
|
|
|
|
updateThreadsIterator->join();
|
|
|
|
|
updateThreadsIterator++;
|
2024-03-16 21:15:15 +01:00
|
|
|
}
|
|
|
|
|
}
|