ST Expressions

An expression is a construct that returns a value after it was evaluated.

Expressions are composed of operators and operands. An operand can be a constant, a variable, a function call or another expression.

Examples:

2014

(* constant *)

nVar

(* variable *)

F_Fct (a, b)

(* function call *)

(x*y)/z

(* expression *)

See also:

Evaluation of Expressions

The evaluation of an expression is carried out by processing the operators according to certain binding rules. TwinCAT first processes the operator with the strongest binding. Operators with the same binding strength are processed from left to right.

Operation

Symbol

Binding strength

Put in parentheses

(Expression)

Strongest binding

Function call

Function name (parameter list)

all operators with syntax: <operator> ()

 

Exponentiation

EXPT

 

Negate

Build. complements

-

NOT

 

Multiply

Divide

Modulo

*

/

MOD

 

Add

Subtract

+

-

 

Compare

<,>,<=,>=

 

Equal to

Not Equal to

=

<>

 

Bool AND

AND
AND_THEN

 

Bool XOR

Bool OR

XOR

OR
OR_ELSE

Weakest binding

 

Sample:

In the following sample the operators AND_THEN and OR are used. Note that OR has the weakest binding and that TwinCAT executes expressions at other operands of the AND_THEN operator only if the first operand of the AND_THEN operator is TRUE.

Therefore, the results for the four expressions shown here are as follows:

 

PROGRAM MAIN
VAR
    pSample    : POINTER TO INT;
    bTest      : BOOL := TRUE;
    nCounter1  : INT;
    nCounter2  : INT;
    nCounter3  : INT;
    nCounter4  : INT;
END_VAR
// Expression 1
IF (pSample <> 0) AND_THEN (pSample^ = 250) AND_THEN (pSample^ <> 300) OR bTest THEN
    nCounter1 := nCounter1 + 1;   // increasing if (pSample = 0) and (bTest = TRUE)
END_IF
 
// Expression 2
IF ((pSample <> 0) AND_THEN (pSample^ = 250) AND_THEN (pSample^ <> 300)) OR bTest THEN
    nCounter2 := nCounter2 + 1;   // increasing if (pSample = 0) and (bTest = TRUE)
END_IF
 
// Expression 3
IF (pSample <> 0) AND_THEN ((pSample^ = 250) AND_THEN (pSample^ <> 300) OR bTest) THEN
    nCounter3 := nCounter3 + 1;   // not increasing if (pSample = 0) and (bTest = TRUE)
END_IF
 
// Expression 4
IF (pSample <> 0) AND_THEN ((pSample^ = 250) OR bTest) THEN
    nCounter4 := nCounter4 + 1;   // not increasing if (pSample = 0) and (bTest = TRUE)
END_IF