MotorControlModuleSDFM_TMS3.../Projects/EFC_Communication/UMLibrary/driver/ModbusAnalogWideInput.cpp
2024-06-07 11:12:56 +03:00

95 lines
2.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* ModbusAnalogWideInput.cpp
*
* Created on: 16 апр. 2023 г.
* Author: titov
*/
#include "ModbusAnalogWideInput.hh"
#include <cstring>
#include <math.h>
bool driver::modbus::ModbusAnalogWideInput::extract() {
using namespace std;
float value = signal;
if ( isnan(value) )
memcpy( data, &nan_out_of_range, sizeof(uint32_t) );
else {
float temp = value * coeff + offset;
if ( temp < down_limit || temp > up_limit )
memcpy( data, &nan_out_of_range, sizeof(uint32_t) );
else {
if( format_signed ) {
int32_t signed_reg = rintf(temp);
memcpy( data, &signed_reg, sizeof(int32_t) );
} else {
uint32_t unsigned_reg = rintf(temp);
memcpy( data, &unsigned_reg, sizeof(uint32_t) );
}
}
}
return true;
}
driver::modbus::ModbusAnalogWideInput::Part::Status driver::modbus::ModbusAnalogWideInput::Part::read( uint16_t & reg ) {
reg = *data;
return Done;
}
driver::modbus::ModbusAnalogWideInput::Part::Status driver::modbus::ModbusAnalogWideInput::Part::write( uint16_t reg ) {
return Failure;
}
driver::modbus::ModbusAnalogWideInput::ModbusAnalogWideInput(
systemic::ISignal & value, bool format_signed, uint32_t nan_out_of_range,
float coeff, float offset, float up_limit, float down_limit,
WordSequence sequnece ) : signal(value), format_signed(format_signed),
nan_out_of_range(nan_out_of_range),
coeff(coeff), offset(offset), up_limit(up_limit), down_limit(down_limit),
one( *this, sequnece == LowFirstHighSecond ? &data[1] : &data[0] ),
two( sequnece == HighFirstLowSecond ? &data[1] : &data[0] ) {}
driver::modbus::IModBusDispatch & driver::modbus::ModbusAnalogWideInput::first() {
return one;
}
driver::modbus::IModBusDispatch & driver::modbus::ModbusAnalogWideInput::second() {
return two;
}
driver::modbus::ModbusAnalogWideInput::Status driver::modbus::ModbusAnalogWideInput::PartShower::read(
uint16_t & reg ) {
if( self.extract() ) {
reg = *data;
return Done;
} else
return Failure;
}