Managed to implement some stuff, need to rewrite reading from DTU
This commit is contained in:
parent
f3f1aa3903
commit
9c3ed916b5
10 changed files with 90 additions and 27 deletions
|
|
@ -2,6 +2,8 @@
|
||||||
#define DTU_H
|
#define DTU_H
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <memory>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
#include "microinverter.h"
|
#include "microinverter.h"
|
||||||
|
|
||||||
|
|
@ -10,7 +12,8 @@ typedef _modbus modbus_t;
|
||||||
|
|
||||||
class Dtu {
|
class Dtu {
|
||||||
private:
|
private:
|
||||||
modbus_t *modbus_context;
|
std::shared_ptr<modbus_t*> modbus_context;
|
||||||
|
std::mutex modbus_context_mutex;
|
||||||
|
|
||||||
std::vector<Microinverter> microinverters;
|
std::vector<Microinverter> microinverters;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@
|
||||||
#define MICROINVERTER_H
|
#define MICROINVERTER_H
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <memory>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
#include "port.h"
|
#include "port.h"
|
||||||
|
|
||||||
|
|
@ -10,14 +12,15 @@ typedef _modbus modbus_t;
|
||||||
|
|
||||||
class Microinverter {
|
class Microinverter {
|
||||||
private:
|
private:
|
||||||
modbus_t *modbus_context;
|
std::shared_ptr<modbus_t*> modbus_context;
|
||||||
|
std::mutex *modbus_context_mutex;
|
||||||
|
|
||||||
std::vector<Port> ports;
|
std::vector<Port> ports;
|
||||||
|
|
||||||
void populatePorts();
|
void populatePorts();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Microinverter(modbus_t *modbus_t);
|
Microinverter(std::shared_ptr<modbus_t*> modbus_context, std::mutex *modbus_context_mutex);
|
||||||
|
|
||||||
void updatePorts();
|
void updatePorts();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
#include "portParametersGeneric.h"
|
#include "portParametersGeneric.h"
|
||||||
|
|
||||||
|
|
@ -13,7 +14,9 @@ typedef _modbus modbus_t;
|
||||||
|
|
||||||
class Port {
|
class Port {
|
||||||
private:
|
private:
|
||||||
modbus_t *modbus_context;
|
std::shared_ptr<modbus_t*> modbus_context;
|
||||||
|
std::mutex *modbus_context_mutex;
|
||||||
|
|
||||||
uint16_t portStartAddress;
|
uint16_t portStartAddress;
|
||||||
|
|
||||||
std::vector<std::shared_ptr<PortParameter>> parameters;
|
std::vector<std::shared_ptr<PortParameter>> parameters;
|
||||||
|
|
@ -21,7 +24,7 @@ class Port {
|
||||||
void populateParameters();
|
void populateParameters();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Port(modbus_t *modbus_context, uint16_t portStartAddress);
|
Port(std::shared_ptr<modbus_t*> modbus_context, std::mutex *modbus_context_mutex, uint16_t portStartAddress);
|
||||||
|
|
||||||
void updateParameters();
|
void updateParameters();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,9 @@
|
||||||
#include "portParametersGeneric.h"
|
#include "portParametersGeneric.h"
|
||||||
|
|
||||||
class PortParameterMicroinverterSerialNumber : public PortParameterInt {
|
class PortParameterMicroinverterSerialNumber : public PortParameterInt {
|
||||||
|
protected:
|
||||||
|
void setValueFromRegisters(uint16_t *readArray, int registerCount);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PortParameterMicroinverterSerialNumber();
|
PortParameterMicroinverterSerialNumber();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <memory>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
struct _modbus;
|
struct _modbus;
|
||||||
typedef _modbus modbus_t;
|
typedef _modbus modbus_t;
|
||||||
|
|
@ -38,7 +40,7 @@ class PortParameter {
|
||||||
|
|
||||||
virtual std::string getOutputValue();
|
virtual std::string getOutputValue();
|
||||||
|
|
||||||
void updateValue(modbus_t *modbus_context, uint16_t portStartAddress);
|
void updateValue(std::shared_ptr<modbus_t*> modbus_context, std::mutex *modbus_context_mutex, uint16_t portStartAddress);
|
||||||
};
|
};
|
||||||
|
|
||||||
class PortParameterFloat : virtual public PortParameter {
|
class PortParameterFloat : virtual public PortParameter {
|
||||||
|
|
|
||||||
|
|
@ -10,10 +10,11 @@ struct _modbus;
|
||||||
typedef _modbus modbus_t;
|
typedef _modbus modbus_t;
|
||||||
|
|
||||||
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 = std::make_shared<modbus_t*>(modbus_new_tcp(ip_address, port));
|
||||||
if (modbus_connect(this->modbus_context) == -1) {
|
|
||||||
|
if (modbus_connect(*this->modbus_context.get()) == -1) {
|
||||||
std::cerr << "conn_error";
|
std::cerr << "conn_error";
|
||||||
modbus_free(this->modbus_context);
|
modbus_free(*this->modbus_context.get());
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -21,13 +22,13 @@ Dtu::Dtu(const char *ip_address, int port) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Dtu::~Dtu() {
|
Dtu::~Dtu() {
|
||||||
modbus_close(this->modbus_context);
|
modbus_close(*this->modbus_context.get());
|
||||||
modbus_free(this->modbus_context);
|
modbus_free(*this->modbus_context.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Dtu::populateMicroinverters() {
|
void Dtu::populateMicroinverters() {
|
||||||
uint16_t address{0x1000};
|
uint16_t address{0x1000};
|
||||||
Microinverter microinverter{this->modbus_context};
|
Microinverter microinverter{this->modbus_context, &this->modbus_context_mutex};
|
||||||
this->microinverters.push_back(microinverter);
|
this->microinverters.push_back(microinverter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,46 @@
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
#include "modbus.h"
|
||||||
|
|
||||||
#include "microinverter.h"
|
#include "microinverter.h"
|
||||||
#include "port.h"
|
#include "port.h"
|
||||||
|
|
||||||
Microinverter::Microinverter(modbus_t *modbus_context) {
|
struct _modbus;
|
||||||
|
typedef _modbus modbus_t;
|
||||||
|
|
||||||
|
Microinverter::Microinverter(std::shared_ptr<modbus_t*> modbus_context, std::mutex *modbus_context_mutex) {
|
||||||
this->modbus_context = modbus_context;
|
this->modbus_context = modbus_context;
|
||||||
|
this->modbus_context_mutex = modbus_context_mutex;
|
||||||
|
|
||||||
this->populatePorts();
|
this->populatePorts();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Microinverter::populatePorts() {
|
void Microinverter::populatePorts() {
|
||||||
Port port{this->modbus_context, 0x1000};
|
uint16_t portStartAddress = 0x1000;
|
||||||
|
uint16_t readArray[1];
|
||||||
|
|
||||||
|
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);
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Microinverter::updatePorts() {
|
void Microinverter::updatePorts() {
|
||||||
|
std::vector<std::thread> updateThreads;
|
||||||
for(Port port : this->ports){
|
for(Port port : this->ports){
|
||||||
port.updateParameters();
|
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++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -9,8 +9,10 @@
|
||||||
#include "port.h"
|
#include "port.h"
|
||||||
#include "portParameters.h"
|
#include "portParameters.h"
|
||||||
|
|
||||||
Port::Port(modbus_t *modbus_context, uint16_t portStartAddress) {
|
Port::Port(std::shared_ptr<modbus_t*> modbus_context, std::mutex *modbus_context_mutex, uint16_t portStartAddress) {
|
||||||
this->modbus_context = modbus_context;
|
this->modbus_context = modbus_context;
|
||||||
|
this->modbus_context_mutex = modbus_context_mutex;
|
||||||
|
|
||||||
this->portStartAddress = portStartAddress;
|
this->portStartAddress = portStartAddress;
|
||||||
|
|
||||||
this->populateParameters();
|
this->populateParameters();
|
||||||
|
|
@ -51,7 +53,7 @@ void Port::populateParameters() {
|
||||||
void Port::updateParameters() {
|
void Port::updateParameters() {
|
||||||
std::vector<std::shared_ptr<PortParameter>>::iterator parametersIterator{this->parameters.begin()};
|
std::vector<std::shared_ptr<PortParameter>>::iterator parametersIterator{this->parameters.begin()};
|
||||||
while (parametersIterator != this->parameters.end()) {
|
while (parametersIterator != this->parameters.end()) {
|
||||||
parametersIterator->get()->updateValue(this->modbus_context, this->portStartAddress);
|
parametersIterator->get()->updateValue(this->modbus_context, this->modbus_context_mutex, this->portStartAddress);
|
||||||
parametersIterator++;
|
parametersIterator++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,10 +1,25 @@
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <string>
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
#include "portParameters.h"
|
#include "portParameters.h"
|
||||||
|
|
||||||
PortParameterMicroinverterSerialNumber::PortParameterMicroinverterSerialNumber() : PortParameterInt("microinverterSerialNumber", 0x0001, 6), PortParameter("microinverterSerialNumber", 0x1001, 6) {}
|
PortParameterMicroinverterSerialNumber::PortParameterMicroinverterSerialNumber() : PortParameterInt("microinverterSerialNumber", 0x0001, 6), PortParameter("microinverterSerialNumber", 0x0001, 6) {}
|
||||||
|
|
||||||
PortParameterPortNumber::PortParameterPortNumber() : PortParameterInt("portNumber", 0x0007, 1), PortParameter("portNumber", 0x007, 1) {}
|
void PortParameterMicroinverterSerialNumber::setValueFromRegisters(uint16_t *readArray, int registerCount) {
|
||||||
|
uint16_t readValue;
|
||||||
|
std::string readValueString = "";
|
||||||
|
registerCount = std::ceil(registerCount/2);
|
||||||
|
for (int i{0}; i < registerCount; i++) {
|
||||||
|
readValue = readArray[i];
|
||||||
|
std::stringstream readValueStringStream;
|
||||||
|
readValueStringStream << std::hex << readValue;
|
||||||
|
readValueString.append(readValueStringStream.str());
|
||||||
|
}
|
||||||
|
this->value.i = std::stol(readValueString);
|
||||||
|
}
|
||||||
|
|
||||||
|
PortParameterPortNumber::PortParameterPortNumber() : PortParameterInt("portNumber", 0x0007, 1), PortParameter("portNumber", 0x0007, 1) {}
|
||||||
|
|
||||||
void PortParameterPortNumber::setValueFromRegisters(uint16_t *readArray, int registerCount) {
|
void PortParameterPortNumber::setValueFromRegisters(uint16_t *readArray, int registerCount) {
|
||||||
if (registerCount > 0) {
|
if (registerCount > 0) {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <memory>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
#include "modbus.h"
|
#include "modbus.h"
|
||||||
|
|
||||||
|
|
@ -26,10 +28,13 @@ std::pair<PortParameter::PortParameterValue, PortParameter::PortParameterValueTy
|
||||||
|
|
||||||
std::string PortParameter::getOutputValue() {}
|
std::string PortParameter::getOutputValue() {}
|
||||||
|
|
||||||
void PortParameter::updateValue(modbus_t *modbus_context, uint16_t portStartAddress) {
|
void PortParameter::updateValue(std::shared_ptr<modbus_t*> modbus_context, std::mutex *modbus_context_mutex, uint16_t portStartAddress) {
|
||||||
uint16_t readArray[this->registerSize];
|
uint16_t readArray[this->registerSize];
|
||||||
int registerCount;
|
int registerCount;
|
||||||
registerCount = modbus_read_registers(modbus_context, portStartAddress + this->parameterAddressOffset, this->registerSize, readArray);
|
|
||||||
|
modbus_context_mutex->lock();
|
||||||
|
registerCount = modbus_read_registers(*modbus_context.get(), portStartAddress + this->parameterAddressOffset, this->registerSize, readArray);
|
||||||
|
modbus_context_mutex->unlock();
|
||||||
|
|
||||||
if(registerCount == -1){
|
if(registerCount == -1){
|
||||||
this->age++;
|
this->age++;
|
||||||
|
|
@ -66,13 +71,13 @@ PortParameterInt::PortParameterInt(std::string name, uint16_t parameterAddressOf
|
||||||
|
|
||||||
void PortParameterInt::setValueFromRegisters(uint16_t *readArray, int registerCount) {
|
void PortParameterInt::setValueFromRegisters(uint16_t *readArray, int registerCount) {
|
||||||
uint16_t readValue;
|
uint16_t readValue;
|
||||||
|
std::string readValueString = "";
|
||||||
|
registerCount = std::ceil(registerCount/2);
|
||||||
for (int i{0}; i < registerCount; i++) {
|
for (int i{0}; i < registerCount; i++) {
|
||||||
readValue = (readArray[i] & 0xFF00);
|
readValue = readArray[i];
|
||||||
this->value.i += readValue * std::pow(10, ((registerCount * 2) - ((registerCount - i) * 2)));
|
readValueString.append(std::to_string(readValue));
|
||||||
|
|
||||||
readValue = readArray[i] & 0x00FF;
|
|
||||||
this->value.i += readValue * std::pow(10, ((registerCount * 2) - ((registerCount - i) * 2)));
|
|
||||||
}
|
}
|
||||||
|
this->value.i = std::stol(readValueString);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string PortParameterInt::getOutputValue() {
|
std::string PortParameterInt::getOutputValue() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue