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-03-20 20:17:15 +01:00
|
|
|
PortParameter::PortParameter(std::string name, std::string shortName, 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-03-16 22:51:32 +01:00
|
|
|
|
|
|
|
|
this->parameterAddressOffset = parameterAddressOffset;
|
|
|
|
|
this->registerSize = registerSize;
|
|
|
|
|
|
2024-03-28 19:44:54 +01:00
|
|
|
// this->age = 0;
|
2024-03-16 22:51:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PortParameter::~PortParameter() {}
|
|
|
|
|
|
2024-03-28 19:44:54 +01:00
|
|
|
void PortParameter::setValueFromRegisters(uint8_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-03-19 16:41:58 +01:00
|
|
|
std::string PortParameter::getOutputValue() {
|
|
|
|
|
return "yeet";
|
|
|
|
|
}
|
2024-03-16 22:51:32 +01:00
|
|
|
|
2024-03-28 19:44:54 +01:00
|
|
|
// void PortParameter::updateValue(std::shared_ptr<class modbus> modbus, uint16_t portStartAddress) {
|
|
|
|
|
// uint16_t readArray[this->registerSize];
|
|
|
|
|
// int registerCount;
|
2024-03-18 13:44:25 +01:00
|
|
|
|
2024-03-28 19:44:54 +01:00
|
|
|
// registerCount = modbus.get()->modbus_read_holding_registers(portStartAddress + this->parameterAddressOffset, this->registerSize, readArray);
|
|
|
|
|
|
|
|
|
|
// if(registerCount != 0){
|
|
|
|
|
// this->age++;
|
|
|
|
|
// }
|
|
|
|
|
// else{
|
|
|
|
|
// registerCount = this->registerSize;
|
|
|
|
|
// this->setValueFromRegisters(readArray, registerCount);
|
|
|
|
|
// this->age = 0;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2024-03-16 22:51:32 +01:00
|
|
|
|
2024-03-20 20:17:15 +01:00
|
|
|
PortParameterFloat::PortParameterFloat(std::string name, std::string shortName, int decimalPlaces, uint16_t parameterAddressOffset, int registerSize) : PortParameter(name, shortName, parameterAddressOffset, registerSize) {
|
2024-03-16 22:51:32 +01:00
|
|
|
this->decimalPlaces = decimalPlaces;
|
|
|
|
|
|
|
|
|
|
this->valueType = Float;
|
|
|
|
|
|
|
|
|
|
this->value.f = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-28 19:44:54 +01:00
|
|
|
void PortParameterFloat::setValueFromRegisters(uint8_t *registers, int addressOffset) {
|
|
|
|
|
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-03-28 19:44:54 +01:00
|
|
|
return valueStringStream.str();
|
2024-03-16 22:51:32 +01:00
|
|
|
}
|
|
|
|
|
|
2024-03-20 20:17:15 +01:00
|
|
|
PortParameterInt::PortParameterInt(std::string name, std::string shortName, uint16_t parameterAddressOffset, int registerSize) : PortParameter(name, shortName, parameterAddressOffset, registerSize) {
|
2024-03-16 22:51:32 +01:00
|
|
|
this->valueType = Int;
|
|
|
|
|
|
|
|
|
|
this->value.i = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-28 19:44:54 +01:00
|
|
|
void PortParameterInt::setValueFromRegisters(uint8_t *registers, int addressOffset) {
|
|
|
|
|
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-03-28 19:44:54 +01:00
|
|
|
return std::to_string(this->value.i);
|
2024-03-16 22:51:32 +01:00
|
|
|
}
|