Skip to main content

ConvertTemperature

Short summary

This function can be used to convert temperatures from one temperature scale into another. The value will change corresponding to the conversion. Example:
300 K = 26.85°C = 80.33 °F

Return: CNM_RecipeHandling.Temprature: a copy of the given temperature converted into the desired scale

Parameters

NameTypeCommentKind
temperatureToConvertTemperaturethe temperature that should be convertedinout
convertToTemperatureUnitthe temperature scale unit it should be converted toinput

Code

Declaration

FUNCTION ConvertTemperature : Temperature
VAR_IN_OUT CONSTANT
(* the temperature that should be converted *)
temperatureToConvert :Temperature;
END_VAR
VAR_INPUT
(* the temperature scale unit it should be converted to *)
convertTo :TemperatureUnit := TemperatureUnit.KELVIN;
END_VAR
VAR CONSTANT
CELSIUS_OFFSET :LREAL := 273.15;
FAHRENHEIT_OFFSET :LREAL := 32.0;
FAHRENHEIT_FACTOR :LREAL := 1.8;
END_VAR

Implementation

ConvertTemperature.unit := convertTo;
IF (NOT CheckValueIsANumber(temperatureToConvert.value)) THEN
ConvertTemperature.value := temperatureToConvert.value;
RETURN;
END_IF

CASE temperatureToConvert.unit OF
TemperatureUnit.KELVIN:
CASE convertTo OF
TemperatureUnit.DEGREE_CELSIUS:
ConvertTemperature.value := temperatureToConvert.value - CELSIUS_OFFSET;
TemperatureUnit.DEGREE_FAHRENHEIT:
ConvertTemperature.value := ((temperatureToConvert.value - CELSIUS_OFFSET) * FAHRENHEIT_FACTOR) + FAHRENHEIT_OFFSET;
ELSE
ConvertTemperature.value := temperatureToConvert.value;
END_CASE
TemperatureUnit.DEGREE_CELSIUS:
CASE convertTo OF
TemperatureUnit.KELVIN:
ConvertTemperature.value := temperatureToConvert.value + CELSIUS_OFFSET;
TemperatureUnit.DEGREE_FAHRENHEIT:
ConvertTemperature.value := (temperatureToConvert.value * FAHRENHEIT_FACTOR) + FAHRENHEIT_OFFSET;
ELSE
ConvertTemperature.value := temperatureToConvert.value;
END_CASE
TemperatureUnit.DEGREE_FAHRENHEIT:
CASE convertTo OF
TemperatureUnit.KELVIN:
ConvertTemperature.value := (temperatureToConvert.value - FAHRENHEIT_OFFSET)/FAHRENHEIT_FACTOR + CELSIUS_OFFSET;
TemperatureUnit.DEGREE_CELSIUS:
ConvertTemperature.value := (temperatureToConvert.value - FAHRENHEIT_OFFSET)/FAHRENHEIT_FACTOR;
ELSE
ConvertTemperature.value := temperatureToConvert.value;
END_CASE
ELSE
ConvertTemperature.value := temperatureToConvert.value;
END_CASE