TwinCAT PLC Control: Languages

Structured Text (ST)

The structured text consists of a series of instructions which, as determined in high level languages, ("IF..THEN..ELSE") or in loops (WHILE..DO) can be executed.


Example:

IF value < 7 THEN
    WHILE value < 8 DO
        value := value + 1;
    END_WHILE;
END_IF;

Expressions


An expression is a construction which returns a value after its evaluation. Expressions are composed of operators and operands. An operand can be a constant, a variable, a function call, or another expression.


Valuation of expressions

The evaluation of expression takes place by means of processing the operators according to certain binding rules. The operator with the strongest binding is processed first, then the operator with the next strongest binding, etc., until all operators have been processed. Operators with equal binding strength are processed from left to right.


Below you find a table of the ST operators in the order of their binding strength:

Operation Symbol Binding strength
Put in parentheses (expression) Strongest binding
Function call Function name (parameter list)
Exponentiation EXPT
Negate
Building of complements
-
NOT
Multiply
Divide
Modulo
*
/
MOD
Add
Subtract
+
-
Compare <,>,<=,>=
Equal to
Not Equal to
=
<>
Boolean AND AND
Boolean XOR XOR
Boolean OR OR Weakest binding

There are the following instructions in ST, arranged in a table together with example:

Instruction  Example
Assignment A:=B; CV := CV + 1; C:=SIN(X);
Calling a function block and use of the FB version CMD_TMR(IN := %IX5, PT := 300);A:=CMD_TMR.Q;
RETURN RETURN;
IF IF D<0.0 

THEN C:=A;
ELSIF D=0.0 
THEN C:=B;
ELSE C:=D;
END_IF;

CASE CASE INT1 OF

1: BOOL1 := TRUE;
2: BOOL2 := TRUE;
ELSE 
BOOL1 := FALSE; 
BOOL2 := FALSE;
END_CASE;

FOR FOR I:=1 TO 100 BY 2 DO 

   IF ARR[I] = 70 
      THEN J:=I; 
      EXIT; 
   END_IF;
END_FOR;

WHILE WHILE J<= 100 AND ARR[J] <> 70 DO 

   J:=J+2;
END_WHILE;

REPEAT REPEAT J:=J+2;

UNTIL J= 101 OR ARR[J] = 70
END_REPEAT;

EXIT EXIT;
Empty instruction ;


Instruction in structured text

The name already indicates, the structured text is designed for structure programming, i.e. ST offers predetermined structures for certain often used constructs such as loops for programming. This offers the advantages of low error probability and increased clarity of the program. 

For example, let us compare two equally significant program sequences in IL and ST:

A loop for calculating powers of two in IL:

Loop :   
LD         Counter
EQ        0
JMPC   end
LD
        Var1
MUL      2
ST         Var1
LD
        Counter
SUB      1
ST         Counter
JMP      Loop
End:    
LD         Var1
ST         ERG


The same loop programmed in ST would produce:

WHILE Counter<>0 DO
    Var1:=Var1*2;
    Counter:=Counter-1;
END_WHILE

Erg:=Var1;


You can see, the loop in ST is not only faster to program, but is also significantly easier to read, especially in view of the convoluted loops in larger constructs.

The different structures in ST have the following significance:


Assignment operator


On the left side of an assignment there is an operand (variable, address) to which is assigned the value of the expression on the right side with the assignment operator :=

Example: Var1 := Var2 * 10;

After completion of this line Var1 has the tenfold value of Var2.

 

Calling function blocks in ST


A function block is called in ST by writing the name of the instance of the function block and then assigning the values of the parameters in parentheses. In the following example a timer is called with assignments for the parameters IN and PT. Then the result variable Q is assigned to the variable A.

The result variable, as in IL, is addressed with the name of the function block, a following point, and the name of the variable:


CMD_TMR(IN := %IX5, PT := 300);
A:=CMD_TMR.Q