Operators

TwinCAT 3 PLC supports all operators of the IEC 61131-3 standard. These operators are implicitly known throughout the project. In addition to the IEC operators, TwinCAT 3 PLC supports some operators which are not described in the IEC 61131-3 standard.

Operators are used in a function block like functions.

Operators 1:

Operations with floating point data types

For operations with floating point data types, the result depends on the target system hardware used.

Operators 2:

Operations with overflow or underflow

For operations with overflow or underflow in the data type, the calculation result depends on the target system hardware used.

Operators 3:

Information about the processing order (binding strength) of the ST operators can be found in section "ST Expressions".

Overflow/underflow in the data type

The TwinCAT 3 compiler generates code for the respective target device and always calculates intermediate results with the native size specified by the target system. For example, on x86 and ARM systems at least 32-bit intermediate values are used, while on x64 systems 64-bit intermediate values are always used. This offers significant advantages in computing speed and often produces the expected result. However, it also means that an overflow or underflow in the data type may not be truncated.

Sample 1

The result of this addition is not truncated and the result in dwVar is 65536.

VAR
    nVarWORD  : WORD;
    nVarDWORD : DWORD;
END_VAR
nVarWORD  := 65535;
nVarDWORD := nVarWORD + 1;

Sample 2

The overflow and underflow in the data type is not truncated, and the results (bVar1, bVar2) of both comparisons are FALSE on 32-bit and on 64-bit hardware.

VAR
    nVar1 : WORD;
    nVar2 : WORD;
    bVar1 : BOOL;
    bVar2 : BOOL;
END_VAR
nVar1 := 65535;
nVar2 := 0;
bVar1 := (nVar1 + 1) = nVar2;
bVar2 := (nVar2 - 1) = nVar1;

Sample 3

The assignment to nVar3 truncates the value to the target data type WORD, and the result bVar1 is TRUE.

VAR
    nVar1 : WORD;
    nVar2 : WORD;
    nVar3 : WORD;
    bVar1 : BOOL;
END_VAR
nVar1 := 65535;
nVar2 := 0;
nVar3 := (nVar1 + 1);
bVar1 := (nVar3 = nVar2);

Sample 4

A conversion can be added to force the compiler to truncate the intermediate result.

The type conversion ensures that both comparisons only compare 16 bits and that the results (bVar1, bVar2) of both comparisons are TRUE.

VAR
    nVar1 : WORD;
    nVar2 : WORD;
    bVar1 : BOOL;
    bVar2 : BOOL;
END_VAR
nVar1 := 65535;
nVar2 := 0;
bVar1 := (TO_WORD(nVar1 + 1) = nVar2);
bVar2 := (TO_WORD(nVar2 - 1) = nVar1);

Address operators

Arithmetic operators

Call operators

Selection operators

Bitshift Operators

Bitstring operators

Namespace operators

The namespace operators are an extension of the IEC 61131-3 operators. They offer options for making the access to variables or modules unique, even if you use the same variable or module name several times in the project.

Numeric operators

Type conversion operators

You can explicitly call type conversion operators. For typed conversions from one elementary type to another elementary type and also for overloads, the type conversion operators described below are available. Conversions from a "smaller" type to a "larger" type, such as from BYTE to INT or from WORD to DINT, are also possible implicitly.

Typed conversion: <elementary data type>_TO_<another elementary data type>

Overloaded conversion: TO_<elementary data type>

Elementary data types:

<elementary data type> =
__UXINT | __XINT | __XWORD | BIT | BOOL | BYTE | DATE | DINT | DT | DWORD | INT | LDATE | LDT | LINT | LREAL | LTIME | LTOD | LWORD | REAL | SINT | TIME | TOD | UDINT | UINT | ULINT | USINT | WORD

The keywords TIME_OF_DAY and DATE_AND_TIME are alternative notations for the data types TOD and DT. TIME_OF_DAY and DATE_AND_TIME are not mapped as a type conversion command.

Operators 4:

For a type conversion operator, if the operand value is outside the value range of the target data type, the result output is undefined. This is the case, for example, when a negative operand value is converted from LREAL to the target data type UINT.

Information may be lost during type conversion from larger to smaller types.

Operators 5:

String manipulation when converting to STRING or WSTRING

With a type conversion to STRING or WSTRING, the typed value is stored as a left aligned string and truncated if it is overlong. Therefore, declare the return variables for the type conversion operators <type>_TO_STRING and <type>_TO_WSTRING long enough to accommodate the string without manipulation.

Comparison operators

The comparison operators are Boolean operators, which compare two inputs (first and second operand).

Further operators