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.
Operations with floating point data types For operations with floating point data types, the result depends on the target system hardware used. |
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. |
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);
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.
Type conversion operators
With type conversions, a distinction is made between implicit and explicit type conversions.
Implicit type conversions
Conversions from a "smaller" type to a "larger" type, such as from BYTE to INT or from WORD to DINT, can be carried out explicitly - but they are also possible implicitly without calling a conversion operator.
Explicit type conversions
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.
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 spellings for the data types TOD and DT.
- TIME_OF_DAY = TOD
- DATE_AND_TIME = DT
TIME_OF_DAY and DATE_AND_TIME are not mapped as type conversion commands. Use the data types TOD and DT instead.
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! |
Loss of information possible If a larger data type is converted to a smaller data type, information may be lost. |
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).