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

  1. 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.

  2. Double-click the Add new entry field and type a name.

    ⇒

    CODESYS implements the <name>_Impl function block and instances it as <name>.

  3. 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.

  4. Change the conversion rule in the input fields.

    ⇒

    The changes are displayed in the Setting category of the table.

  5. 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.

  6. 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.

  1. Double-click a Unit conversion object in the device tree.

    ⇒

    The respective editor opens with a table of the predefined conversion rules.

  2. Click the Add new entry field and type a name.

    ⇒

    Example: Conv_A_LanguageDependent

  3. 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.

  4. 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.

  5. 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.

  6. 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 the Conv_AInMM conversion rule. The current visualization language is located in the VisuElems.CurrentLanguage variable.

Applying conversion rules

Add a conversion rule to objects that access IEC variables.

  1. 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.

  2. Declare a variable for the conversion result of the IEC variable.

    ⇒

    ST sample code: rConvertedA : REAL;

  3. 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

  1. In the device tree, double-click an object that accesses an IEC variable.

  2. Declare a variable for the result of the conversion rule.

    ⇒

    ST sample code: rReverseA: REAL;

  3. 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.

../_images/647f26477d4d2a19c0a8640e01e1b1a3
../_images/e61ed7ef7d4da884c0a8640e00ac6452

See also