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
|
|
@ -10,10 +10,11 @@ struct _modbus;
|
|||
typedef _modbus modbus_t;
|
||||
|
||||
Dtu::Dtu(const char *ip_address, int port) {
|
||||
this->modbus_context = modbus_new_tcp(ip_address, port);
|
||||
if (modbus_connect(this->modbus_context) == -1) {
|
||||
this->modbus_context = std::make_shared<modbus_t*>(modbus_new_tcp(ip_address, port));
|
||||
|
||||
if (modbus_connect(*this->modbus_context.get()) == -1) {
|
||||
std::cerr << "conn_error";
|
||||
modbus_free(this->modbus_context);
|
||||
modbus_free(*this->modbus_context.get());
|
||||
abort();
|
||||
}
|
||||
|
||||
|
|
@ -21,13 +22,13 @@ Dtu::Dtu(const char *ip_address, int port) {
|
|||
}
|
||||
|
||||
Dtu::~Dtu() {
|
||||
modbus_close(this->modbus_context);
|
||||
modbus_free(this->modbus_context);
|
||||
modbus_close(*this->modbus_context.get());
|
||||
modbus_free(*this->modbus_context.get());
|
||||
}
|
||||
|
||||
void Dtu::populateMicroinverters() {
|
||||
uint16_t address{0x1000};
|
||||
Microinverter microinverter{this->modbus_context};
|
||||
Microinverter microinverter{this->modbus_context, &this->modbus_context_mutex};
|
||||
this->microinverters.push_back(microinverter);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,20 +1,46 @@
|
|||
#include <thread>
|
||||
|
||||
#include "modbus.h"
|
||||
|
||||
#include "microinverter.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_mutex = modbus_context_mutex;
|
||||
|
||||
this->populatePorts();
|
||||
}
|
||||
|
||||
void Microinverter::populatePorts() {
|
||||
Port port{this->modbus_context, 0x1000};
|
||||
uint16_t portStartAddress = 0x1000;
|
||||
uint16_t readArray[1];
|
||||
|
||||
this->ports.push_back(port);
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
void Microinverter::updatePorts() {
|
||||
std::vector<std::thread> updateThreads;
|
||||
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 "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_mutex = modbus_context_mutex;
|
||||
|
||||
this->portStartAddress = portStartAddress;
|
||||
|
||||
this->populateParameters();
|
||||
|
|
@ -51,7 +53,7 @@ void Port::populateParameters() {
|
|||
void Port::updateParameters() {
|
||||
std::vector<std::shared_ptr<PortParameter>>::iterator parametersIterator{this->parameters.begin()};
|
||||
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++;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,10 +1,25 @@
|
|||
#include <cmath>
|
||||
#include <string>
|
||||
#include <iomanip>
|
||||
|
||||
#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) {
|
||||
if (registerCount > 0) {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
#include <cmath>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
#include "modbus.h"
|
||||
|
||||
|
|
@ -26,10 +28,13 @@ std::pair<PortParameter::PortParameterValue, PortParameter::PortParameterValueTy
|
|||
|
||||
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];
|
||||
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){
|
||||
this->age++;
|
||||
|
|
@ -66,13 +71,13 @@ PortParameterInt::PortParameterInt(std::string name, uint16_t parameterAddressOf
|
|||
|
||||
void PortParameterInt::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] & 0xFF00);
|
||||
this->value.i += readValue * std::pow(10, ((registerCount * 2) - ((registerCount - i) * 2)));
|
||||
|
||||
readValue = readArray[i] & 0x00FF;
|
||||
this->value.i += readValue * std::pow(10, ((registerCount * 2) - ((registerCount - i) * 2)));
|
||||
readValue = readArray[i];
|
||||
readValueString.append(std::to_string(readValue));
|
||||
}
|
||||
this->value.i = std::stol(readValueString);
|
||||
}
|
||||
|
||||
std::string PortParameterInt::getOutputValue() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue