It's asking but need to tweek setting values from registers

This commit is contained in:
TraYali 2024-03-16 22:51:32 +01:00
parent d0f8e5b885
commit f3f1aa3903
8 changed files with 309 additions and 109 deletions

View file

@ -0,0 +1,39 @@
#include <cmath>
#include "portParameters.h"
PortParameterMicroinverterSerialNumber::PortParameterMicroinverterSerialNumber() : PortParameterInt("microinverterSerialNumber", 0x0001, 6), PortParameter("microinverterSerialNumber", 0x1001, 6) {}
PortParameterPortNumber::PortParameterPortNumber() : PortParameterInt("portNumber", 0x0007, 1), PortParameter("portNumber", 0x007, 1) {}
void PortParameterPortNumber::setValueFromRegisters(uint16_t *readArray, int registerCount) {
if (registerCount > 0) {
this->value.i = readArray[0];
}
}
PortParameterPvVoltage::PortParameterPvVoltage() : PortParameterFloat("pvVoltage", 1, 0x0008, 2), PortParameter("pvVoltage", 0x0008, 2) {}
PortParameterPvCurrentMi::PortParameterPvCurrentMi() : PortParameterFloat("pvCurrent", 1, 0x000a, 2), PortParameter("pvCurrent", 0x000a, 2) {}
PortParameterPvCurrentHm::PortParameterPvCurrentHm() : PortParameterFloat("pvCurrent", 2, 0x000a, 2), PortParameter("pvCurrent", 0x000a, 2) {}
PortParameterGridVoltage::PortParameterGridVoltage() : PortParameterFloat("gridVoltage", 1, 0x000c, 2), PortParameter("gridVoltage", 0x000c, 2) {}
PortParameterGridFrequency::PortParameterGridFrequency() : PortParameterFloat("gridFrequency", 2, 0x000e, 2), PortParameter("gridFrequency", 0x000e, 2) {}
PortParameterPvPower::PortParameterPvPower() : PortParameterFloat("pvPower", 1, 0x0010, 2), PortParameter("pvPower", 0x0010, 2) {}
PortParameterTodayProduction::PortParameterTodayProduction() : PortParameterInt("todayProduction", 0x0012, 2), PortParameter("todayProduction", 0x0012, 2) {}
PortParameterTotalProduction::PortParameterTotalProduction() : PortParameterInt("totalProduction", 0x0014, 4), PortParameter("totalProduction", 0x0014, 4) {}
PortParameterTemperature::PortParameterTemperature() : PortParameterFloat("temperature", 1, 0x0018, 2), PortParameter("temperature", 0x0018, 2) {}
PortParameterOperatingStatus::PortParameterOperatingStatus() : PortParameterInt("operatingStatus", 0x001a, 2), PortParameter("operatingStatus", 0x001a, 2) {}
PortParameterAlarmCode::PortParameterAlarmCode() : PortParameterInt("alarmCode", 0x001c, 2), PortParameter("alarmCode", 0x001c, 2) {}
PortParameterAlarmCount::PortParameterAlarmCount() : PortParameterInt("alarmCount", 0x001e, 2), PortParameter("alarmCount", 0x001e, 2) {}
PortParameterLinkStatus::PortParameterLinkStatus() : PortParameterInt("linkStatus", 0x020, 2), PortParameter("linkStatus", 0x020, 2) {}

View file

@ -0,0 +1,80 @@
#include <cmath>
#include "modbus.h"
#include "portParametersGeneric.h"
struct _modbus;
typedef _modbus modbus_t;
PortParameter::PortParameter(std::string name, uint16_t parameterAddressOffset, int registerSize) {
this->name = name;
this->parameterAddressOffset = parameterAddressOffset;
this->registerSize = registerSize;
this->age = 0;
}
PortParameter::~PortParameter() {}
void PortParameter::setValueFromRegisters(uint16_t *readArray, int registerCount) {}
std::pair<PortParameter::PortParameterValue, PortParameter::PortParameterValueType> PortParameter::getValue() {
return std::pair<PortParameter::PortParameterValue, PortParameter::PortParameterValueType>(this->value, this->valueType);
}
std::string PortParameter::getOutputValue() {}
void PortParameter::updateValue(modbus_t *modbus_context, uint16_t portStartAddress) {
uint16_t readArray[this->registerSize];
int registerCount;
registerCount = modbus_read_registers(modbus_context, portStartAddress + this->parameterAddressOffset, this->registerSize, readArray);
if(registerCount == -1){
this->age++;
}
else{
this->setValueFromRegisters(readArray, registerCount);
this->age = 0;
}
}
PortParameterFloat::PortParameterFloat(std::string name, int decimalPlaces, uint16_t parameterAddressOffset, int registerSize) : PortParameter(name, parameterAddressOffset, registerSize) {
this->decimalPlaces = decimalPlaces;
this->valueType = Float;
this->value.f = 0;
}
void PortParameterFloat::setValueFromRegisters(uint16_t *readArray, int registerCount) {
float temp = readArray[0];
temp = temp / std::pow(10, this->decimalPlaces);
this->value.f = temp;
}
std::string PortParameterFloat::getOutputValue() {
return std::to_string(this->value.f);
}
PortParameterInt::PortParameterInt(std::string name, uint16_t parameterAddressOffset, int registerSize) : PortParameter(name, parameterAddressOffset, registerSize) {
this->valueType = Int;
this->value.i = 0;
}
void PortParameterInt::setValueFromRegisters(uint16_t *readArray, int registerCount) {
uint16_t readValue;
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)));
}
}
std::string PortParameterInt::getOutputValue() {
return std::to_string(this->value.i);
}