Overloading

Overloading 1:

Undefined result if the value range is exceeded

If the input value of a type conversion operator is outside the value range of the output data type, the result of the operation is not defined and depends on the platform. This is the case, for example, when a negative operand value is converted from LREAL to the target data type UINT. An exception error is also possible!

Overloading 2:

Loss of information possible

If a larger data type is converted to a smaller data type, information may be lost.

Overloading 3:

Rounding logic

The rounding logic for borderline cases depends on the target system or the FPU (Floating Point Unit) of the target system. For example, a value like -1.5 may be converted differently on different controllers.

Intercept value range overflows through the application to program target system-independent code.

The operators convert values to other data types, explicitly specifying only a target data type and no initial data type (data type of the operand) ("overloaded conversion").

The rules for typed conversions also apply to overloading.

Overcharges are not part of IEC 61131-3. If you want to program strictly according to IEC61131-3, please use the operators of the <type> _TO_ <another type> scheme described in the following sections.

Syntax:

<Variablenname> := <TO Operator> ( <Operand> );
<Operand> = <Variablenname> | <Literal>

Operators:

TO___UXINT
TO___XINT
TO___XWORD
TO_BIT
TO_BYTE
TO_BOOL
TO_DATE
TO_DINT
TO_DT
TO_DWORD
TO_INT
TO_LDATE
TO_LDT
TO_LINT
TO_LREAL
TO_LTIME
TO_LTOD
TO_LWORD
TO_REAL
TO_SINT
TO_STRING
TO_TIME
TO_TOD
TO_UDINT
TO_UINT
TO_ULINT
TO_USINT
TO_WORD
TO_WSTRING

Examples:

Implementation language ST:

VAR
    nNumber1 : INT;
    nNumber2 : INT;
    fNumber3 : REAL     := 123.456;
    bTruth   : BOOL;
    sText1   : STRING;
    sText2   : STRING   := 'Hello World!';
    wsText   : WSTRING;
    dEvent   : DATE     := D#2019-9-3;
    nEvent   : UINT;
    nData    : __UXINT;
END_VAR
nNumber1 := TO_INT(4.22);          // Result: 4
nNumber2 := TO_INT(fNumber1);      // Result: 123
bTruth   := TO_BOOL(1);            // Result: TRUE
sText1   := TO_STRING(342);        // Result: '342'
wsText   := TO_WSTRING(sText2);    // Result: "Hello World!"
nEvent   := TO_UINT(dEvent);       // Result: 44288
dEvent   := TO___UXINT(nNumber2);  // Result: 123

See also: