Removed multithreading for now, corrected connected

This commit is contained in:
TraYali 2024-03-19 12:54:05 +01:00
parent 089c7a77b5
commit e0135b6753
4 changed files with 34 additions and 20 deletions

View file

@ -17,6 +17,8 @@ class Dtu {
std::vector<Microinverter> microinverters;
bool connected;
void populateMicroinverters();
std::pair<bool, Microinverter*> getMicroinverterBySerialNumber(long serialNumber);
@ -24,6 +26,8 @@ class Dtu {
public:
Dtu(const char *ip_address, int port);
bool isConnected();
void updateMicroinverters();
void printMicroinverters();

View file

@ -1,6 +1,6 @@
#include <vector>
#include <iostream>
#include <thread>
// #include <thread>
#include "modbus.h"
@ -18,10 +18,19 @@ Dtu::Dtu(const char *ip_address, int port) {
if (modbus_connect(*this->modbus_context.get()) == -1) {
std::cerr << "conn_error";
modbus_free(*this->modbus_context.get());
abort();
this->connected = false;
}
else {
this->connected = true;
}
this->populateMicroinverters();
if(this->connected) {
this->populateMicroinverters();
}
}
bool Dtu::isConnected() {
return this->connected;
}
Dtu::~Dtu() {
@ -74,19 +83,20 @@ std::pair<bool, Microinverter*> Dtu::getMicroinverterBySerialNumber(long serialN
}
void Dtu::updateMicroinverters() {
std::vector<std::thread> updateThreads;
// std::vector<std::thread> updateThreads;
std::vector<Microinverter>::iterator microinvertersIterator = this->microinverters.begin();
while (microinvertersIterator != this->microinverters.end()) {
updateThreads.push_back(std::thread(&Microinverter::updatePorts, microinvertersIterator));
// updateThreads.push_back(std::thread(&Microinverter::updatePorts, microinvertersIterator));
microinvertersIterator->updatePorts();
microinvertersIterator++;
}
std::vector<std::thread>::iterator updateThreadsIterator = updateThreads.begin();
while(updateThreadsIterator != updateThreads.end()) {
updateThreadsIterator->join(); updateThreadsIterator++;
}
std::cout << std::endl;
// std::vector<std::thread>::iterator updateThreadsIterator = updateThreads.begin();
// while(updateThreadsIterator != updateThreads.end()) {
// updateThreadsIterator->join(); updateThreadsIterator++;
// }
// std::cout << std::endl;
}
void Dtu::printMicroinverters() {

View file

@ -1,4 +1,4 @@
#include <thread>
// #include <thread>
#include <iostream>
#include <memory>
@ -18,16 +18,16 @@ Microinverter::Microinverter(std::shared_ptr<modbus_t*> modbus_context, std::mut
}
void Microinverter::updatePorts() {
std::vector<std::thread> updateThreads;
// std::vector<std::thread> updateThreads;
for(Port port : this->ports){
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++;
// 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++;
// }
}
void Microinverter::printPorts() {

View file

@ -16,7 +16,7 @@ int main(){
auto endTime = std::chrono::high_resolution_clock::now();
std::cout << "Construction time: " << std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count() << "ms" << std::endl;
while(true) {
while(dtu.isConnected()) {
auto startTime = std::chrono::high_resolution_clock::now();
dtu.updateMicroinverters();
auto endTime = std::chrono::high_resolution_clock::now();