Unit Conversion¶
You define a conversion rule when you want to convert data for another system of units. This data is executed for a specific order of magnitude and unit of measure.
Conversion rules are defined 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 includes Convert
and Reverse
methods for use as function blocks. Locations where you access a variable, you can link the variable to a conversion rule. The input assistant provides conversion rules in the Functionblocks 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 respective editor opens with a table of the 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 drop-down list.
- ⇒
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 drop-down list.
- ⇒
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 respective editor opens with a table of the 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 drop-down list in the Switchable conversion name column, for example
Conv_AInInch
.In Condition setting, type 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 drop-down list in the Switchable conversion name column, for example
Conv_AInMM
.In Condition setting, type 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 applies 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.
- ⇒
ST sample code:
rConvertedA : REAL;
Use the input assistant to apply the conversion rule with the
Convert
method and then assign the result to the variable.- ⇒
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.
- ⇒
ST sample code:
rReverseA: REAL;
Apply the reverse conversion rule with the
Reverse
method and then assign the result to the variable.- ⇒
rReverseA := ConvRule_A.Reverse(rConvertedA);
Example
Requirement: The conversion rule is Conv_XtoY
.
ST call
PROGRAM A_PRG
VAR
\ rA : REAL;
\ rConvertedA : REAL;
\ rReverseToA : REAL;
END_VAR
rConvertedA := Conv_XtoY.Convert(rA);
rReverseToA := Conv_XtoY.Reverse(rConvertedA);
CFC call
In the CFC editor, define the instance name of the conversion rule via the block. Select the method in the block.
See also