Using unit conversion
You can define a conversion rule when you want to adjust data for another system of units. This data is executed for a specific order of magnitude and unit of measure.
You define conversion rules in a Unit Conversion object. CODESYS automatically implements each conversion rule as a function block <name>_Impl
and instances it as <name>
. Each conversion rule has the Convert
and Reverse
methods. As a result, you can use a function block as usual. Locations where you access a variable, you can link the variable to a conversion rule. The Input Assistant provides conversion rules in the Function Blocks and Instance Calls categories. After execution, the result is a converted value according to the conversion rule.
In a visualization, an IEC variable that is configured in an element property can also be linked to a conversion rules.
Defining unit conversions
Double-click a Unit Conversion object in the device tree.
The corresponding editor opens. It contains a table with already defined conversion rules. You edit a rule in Type setting and a respective condition in Condition Setting.
Double-click the Add new entry field and type a name.
CODESYS implements the
<name>_Impl
function block and instances it as<name>
.Double-click the Type field and click a type from the list box.
Input fields are displayed below the table for editing the conversion rule. The input fields vary according to selected type.
Change the conversion rule in the input fields.
The changes are displayed in the Setting category of the table.
Double-click the Condition field and click a condition type from the list box.
Input fields are displayed below the Condition Setting category of the table to edit the condition. The input fields vary according to selected type.
Edit the condition.
The changes are displayed in the Condition Setting category of the table.
Defining switchable unit conversions
You can define which conversion rule is applied to a specific language or condition.
Double-click a Unit Conversion object in the device tree.
The corresponding editor opens. It contains a table with predefined conversion rules.
Click the Add new entry field and type a name.
Example:
Conv_A_LanguageDependent
Double-click the Type field and click Switchable Conversion. Double-click the Condition field and click Language.
Below the main table, the Switchable Conversion table is displayed with Condition Setting.
In the Switchable Conversion table, double-click a predefined conversion rule from the list box in the Switchable conversion name column, for example
Conv_AInInch
.In Condition Setting, type in a value in the For condition 'Language' input field, for example
en
.CODESYS executes the
Conv_AInInch
conversion rule only if the language set in the Visualization Manager is en .In the Switchable Conversion table, double-click a predefined conversion rule from the list box in the Switchable conversion name column, for example
Conv_AInMM
.In Condition Setting, click a value in the For condition 'Language' input field, for example
de
.CODESYS executes the
Conv_AInMM
conversion rule only if the language set in the Visualization Manager is de.Apply the
Conv_A_LanguageDependent
conversion rule in the application or visualization.If the set language in the visualization is English, then the application visualization apply the
Conv_AInInch
conversion rule. If the set language in the visualization is German, then the application visualization apply theConv_AInMM
conversion rule. The current visualization language is located in theVisuElems.CurrentLanguage
variable.
Applying conversion rules
Add a conversion rule to objects that access IEC variables.
In the device tree, double-click an object that accesses IEC variables in order to link an IEC variable to a conversion rule at that location.
Declare a variable for the conversion result of the IEC variable.
Code for example in ST:
rConvertedA : REAL;
Apply the reverse conversion rule with the
Convert
method and then assign the result to the variable. Use the Input Assistant.ST sample code to link the IEC variable to the conversion rule:
rConvertedA := ConvRule_A.Convert(rA);
Applying reverse conversion rules
In the device tree, double-click an object that accesses an IEC variable.
Declare a variable for the result of the conversion rule.
Code for example in ST:
rReverseA: REAL;
Apply the reverse conversion rule with the
Reverse
method and then assign the result to the variable.rReverseA := ConvRule_A.Reverse(rConvertedA);
Examples
Requirement: The conversion rule is Conv_XtoY
.
PROGRAM A_PRG VAR rA : REAL; rConvertedA : REAL; rReverseToA : REAL; END_VAR rConvertedA := Conv_XtoY.Convert(rA); rReverseToA := Conv_XtoY.Reverse(rConvertedA);
In the CFC editor, define the instance name of the conversion rule via the block. Select the method in the block.


Configuring a variable assignment with unit conversion
You can link a variable which has been assigned in a visualization to a unit conversion. This has the effect that the variable value is converted according to a predefined rule and the result is processed in the visualization.
You have already defined the conversion rules in the editor in Unit Conversion.
Linking a variable with unit conversion
Requirement: A project with a visualization is open. In addition, the application contains the UnitConversion
object with the convert_A
rule.
Select an element.
The Properties view opens.
In a variable assignment, link the
iVar_A
variable to a unit conversion rule:convert_A.convert(iVar_A)
Compile, download, and start the application.
The application runs. The visualization opens. The unit conversion is applied.