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"
|
|
|
|
|
|
|
|
|
|
PortParameter::PortParameter(std::string name, uint16_t parameterAddressOffset, int registerSize) {
|
|
|
|
|
this->name = name;
|
|
|
|
|
|
|
|
|
|
this->parameterAddressOffset = parameterAddressOffset;
|
|
|
|
|
this->registerSize = registerSize;
|
|
|
|
|
|
|
|
|
|
this->age = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PortParameter::~PortParameter() {}
|
|
|
|
|
|
2024-03-18 13:44:25 +01:00
|
|
|
void PortParameter::setValueFromRegisters(uint16_t *readArray, int registerCount) {}
|
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-19 18:13:27 +01:00
|
|
|
void PortParameter::updateValue(std::shared_ptr<class modbus> modbus, uint16_t portStartAddress) {
|
2024-03-18 13:44:25 +01:00
|
|
|
uint16_t readArray[this->registerSize];
|
|
|
|
|
int registerCount;
|
|
|
|
|
|
2024-03-19 13:02:41 +01:00
|
|
|
// modbus_context_mutex->lock();
|
2024-03-19 18:13:27 +01:00
|
|
|
registerCount = modbus.get()->modbus_read_holding_registers(portStartAddress + this->parameterAddressOffset, this->registerSize, readArray);
|
2024-03-19 13:02:41 +01:00
|
|
|
// modbus_context_mutex->unlock();
|
2024-03-18 13:44:25 +01:00
|
|
|
|
2024-03-19 18:13:27 +01:00
|
|
|
if(registerCount != 0){
|
2024-03-16 22:51:32 +01:00
|
|
|
this->age++;
|
|
|
|
|
}
|
|
|
|
|
else{
|
2024-03-19 18:13:27 +01:00
|
|
|
registerCount = this->registerSize;
|
2024-03-16 22:51:32 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-18 13:44:25 +01:00
|
|
|
void PortParameterFloat::setValueFromRegisters(uint16_t *readArray, int registerCount) {
|
2024-03-16 22:51:32 +01:00
|
|
|
float temp = readArray[0];
|
|
|
|
|
temp = temp / std::pow(10, this->decimalPlaces);
|
|
|
|
|
this->value.f = temp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string PortParameterFloat::getOutputValue() {
|
2024-03-18 23:48:37 +01:00
|
|
|
std::string separator{"_age"};
|
2024-03-18 23:53:47 +01:00
|
|
|
std::stringstream valueStringStream;
|
|
|
|
|
valueStringStream << std::fixed << std::setprecision(this->decimalPlaces) << this->value.f;
|
|
|
|
|
return valueStringStream.str().append(separator.append(std::to_string(this->age)));
|
2024-03-16 22:51:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PortParameterInt::PortParameterInt(std::string name, uint16_t parameterAddressOffset, int registerSize) : PortParameter(name, parameterAddressOffset, registerSize) {
|
|
|
|
|
this->valueType = Int;
|
|
|
|
|
|
|
|
|
|
this->value.i = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-18 13:44:25 +01:00
|
|
|
void PortParameterInt::setValueFromRegisters(uint16_t *readArray, int registerCount) {
|
2024-03-16 22:51:32 +01:00
|
|
|
uint16_t readValue;
|
2024-03-17 21:33:21 +01:00
|
|
|
std::string readValueString = "";
|
2024-03-18 13:44:25 +01:00
|
|
|
registerCount = std::ceil(registerCount/2);
|
|
|
|
|
for (int i{0}; i < registerCount; i++) {
|
|
|
|
|
readValue = readArray[i];
|
2024-03-17 21:33:21 +01:00
|
|
|
readValueString.append(std::to_string(readValue));
|
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-18 23:48:37 +01:00
|
|
|
std::string separator{"_age"};
|
|
|
|
|
return std::to_string(this->value.i).append(separator.append(std::to_string(this->age)));
|
2024-03-16 22:51:32 +01:00
|
|
|
}
|