2024-03-16 22:51:32 +01:00
|
|
|
#include <cmath>
|
2024-03-17 21:33:21 +01:00
|
|
|
#include <memory>
|
|
|
|
|
#include <mutex>
|
2024-03-18 23:53:47 +01:00
|
|
|
#include <sstream>
|
|
|
|
|
#include <iomanip>
|
2024-03-16 22:51:32 +01:00
|
|
|
|
|
|
|
|
#include "modbus.h"
|
|
|
|
|
|
|
|
|
|
#include "portParametersGeneric.h"
|
|
|
|
|
|
2024-04-05 16:48:06 +02:00
|
|
|
PortParameter::PortParameter(std::string name, std::string shortName, std::string unit, bool r, bool w, uint16_t parameterAddressOffset, int registerSize) {
|
2024-03-16 22:51:32 +01:00
|
|
|
this->name = name;
|
2024-03-20 20:17:15 +01:00
|
|
|
this->shortName = shortName;
|
2024-04-05 15:54:36 +02:00
|
|
|
this->unit = unit;
|
2024-03-16 22:51:32 +01:00
|
|
|
|
2024-04-05 16:48:06 +02:00
|
|
|
this->r = r;
|
|
|
|
|
this->w = w;
|
|
|
|
|
|
2024-03-16 22:51:32 +01:00
|
|
|
this->parameterAddressOffset = parameterAddressOffset;
|
|
|
|
|
this->registerSize = registerSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PortParameter::~PortParameter() {}
|
|
|
|
|
|
2024-04-06 00:32:49 +02:00
|
|
|
void PortParameter::getValueFromRegisters(uint16_t *readArray, int portOffset) {}
|
2024-03-16 22:51:32 +01:00
|
|
|
|
|
|
|
|
std::pair<PortParameter::PortParameterValue, PortParameter::PortParameterValueType> PortParameter::getValue() {
|
|
|
|
|
return std::pair<PortParameter::PortParameterValue, PortParameter::PortParameterValueType>(this->value, this->valueType);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-06 00:32:49 +02:00
|
|
|
PortParameter& PortParameter::writeValue(uint16_t value, class modbus& modbus, int portStartAddress) {
|
2024-04-06 16:29:02 +02:00
|
|
|
int writeCount;
|
|
|
|
|
writeCount = modbus.modbus_write_register(this->parameterAddressOffset + portStartAddress, value);
|
2024-04-06 00:32:49 +02:00
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-19 16:41:58 +01:00
|
|
|
std::string PortParameter::getOutputValue() {
|
|
|
|
|
return "yeet";
|
|
|
|
|
}
|
2024-03-16 22:51:32 +01:00
|
|
|
|
2024-04-05 16:48:06 +02:00
|
|
|
PortParameterFloat::PortParameterFloat(std::string name, std::string shortName, std::string unit, bool r, bool w, int decimalPlaces, uint16_t parameterAddressOffset, int registerSize) : PortParameter(name, shortName, unit, r, w, parameterAddressOffset, registerSize) {
|
2024-03-16 22:51:32 +01:00
|
|
|
this->decimalPlaces = decimalPlaces;
|
|
|
|
|
|
|
|
|
|
this->valueType = Float;
|
|
|
|
|
|
|
|
|
|
this->value.f = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-06 00:32:49 +02:00
|
|
|
void PortParameterFloat::getValueFromRegisters(uint16_t *registers, int addressOffset) {
|
2024-03-28 19:44:54 +01:00
|
|
|
std::string readValueString{""};
|
|
|
|
|
for(int i{0}; i<this->registerSize; i++) {
|
|
|
|
|
std::stringstream readValueStringStream;
|
|
|
|
|
readValueStringStream << (int) registers[addressOffset + this->parameterAddressOffset + i];
|
|
|
|
|
readValueString.append(readValueStringStream.str().c_str());
|
|
|
|
|
}
|
|
|
|
|
this->value.f = std::stoll(readValueString) / std::pow(10, this->decimalPlaces);
|
2024-03-16 22:51:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string PortParameterFloat::getOutputValue() {
|
2024-03-18 23:53:47 +01:00
|
|
|
std::stringstream valueStringStream;
|
|
|
|
|
valueStringStream << std::fixed << std::setprecision(this->decimalPlaces) << this->value.f;
|
2024-04-05 15:54:36 +02:00
|
|
|
return valueStringStream.str().append(this->unit.c_str());
|
2024-03-16 22:51:32 +01:00
|
|
|
}
|
|
|
|
|
|
2024-04-05 16:48:06 +02:00
|
|
|
PortParameterInt::PortParameterInt(std::string name, std::string shortName, std::string unit, bool r, bool w, uint16_t parameterAddressOffset, int registerSize) : PortParameter(name, shortName, unit, r, w, parameterAddressOffset, registerSize) {
|
2024-03-16 22:51:32 +01:00
|
|
|
this->valueType = Int;
|
|
|
|
|
|
|
|
|
|
this->value.i = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-06 00:32:49 +02:00
|
|
|
void PortParameterInt::getValueFromRegisters(uint16_t *registers, int addressOffset) {
|
2024-03-28 19:44:54 +01:00
|
|
|
std::string readValueString{""};
|
|
|
|
|
for (int i{0}; i < this->registerSize; i++) {
|
|
|
|
|
std::stringstream readValueStringStream;
|
|
|
|
|
readValueStringStream << (int) registers[addressOffset + this->parameterAddressOffset + i];
|
|
|
|
|
readValueString.append(readValueStringStream.str().c_str());
|
2024-03-16 22:51:32 +01:00
|
|
|
}
|
2024-03-20 16:39:25 +01:00
|
|
|
this->value.i = std::stoll(readValueString);
|
2024-03-16 22:51:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string PortParameterInt::getOutputValue() {
|
2024-04-05 15:54:36 +02:00
|
|
|
return std::to_string(this->value.i).append(this->unit.c_str());
|
2024-03-16 22:51:32 +01:00
|
|
|
}
|