Structured Text (ST)

STClosed"Structured text" A high-level language that is block structured and syntactically resembles Pascal is a structured literal programming language. A ST program is a list of statements. Each statement describes an action and must end with a semi-colon (";").

The presentation of the text has no meaning for a ST program. You can insert blank characters and line breaks where you want in the program text.

Comments

Comment texts can be entered anywhere in a ST program. Comment texts have no meaning for the execution of the program. A comment text must begin with "(*" and end with "*)". Comments can be entered on several lines (i.e. a comment text can include line breaks). Comment texts cannot be nested.

You can also use // to add a comment on a single line as shown below:

//My main comment

(* My comment *)

a := d + e;

(* A comment can also

be on several lines *)

b := d * e;

 

c := d - e; (* My comment *)

 

Expressions

Each statement describes an action and can include evaluation of complex expressions. An expression is evaluated:

- from the left to the right
- according to the default priority order of operators
- the default priority can be changed using parentheses

Arguments of an expression can be:
- declared variables
- constant expressions
- function calls

Statements

Below are available basic statements that can be entered in a ST program:

- assignment
- function block calling

Below are the available conditional statements in ST language:

- IF / THEN / ELSE
   Simple binary switch.
   One or several ELSIF are allowed.

IF a = b THEN

   c := 0;

ELSIF a < b THEN

   c := 1;

ELSE

   c := -1;

END_IF;

 

- CASE
  Switch between enumerated statements according to an expression.
  The selector can be any integer or a STRING.

CASE iChoice OF

0:

MyString := 'Nothing';

1 .. 2,5:

MyString := 'First case';

3,4:

MyString := 'Second case';

ELSE

MyString := 'Other case';

END_CASE;

 

Below are the available statements for describing loops in ST language:


  • Loop instructions can lead to infinite loops that block the target cycle.
    Never test the state of an input in the condition as the input will not be refreshed before the next cycle.

- WHILE
  Repeat a list of statements.
  Condition is evaluated on loop entry before the statements.

iCount := 0;

WHILE iCount < 100 DO

iCount := iCount +1;

MyVar := MyVar + 1;

END_WHILE;

 

- REPEAT
  Repeat a list of statements.
  Condition is evaluated on loop exit after the statements.

iCount := 0;

REPEAT

MyVar := MyVar + 1;

iCount := iCount + 1;

UNTIL iCount < 100 END_REPEAT;

 

- FOR
  Iteration of statement execution.
  The BY statement is optional (default value is 1)

FOR iCount := 0 TO 100 BY 2 DO

MyVar := MyVar + 1;

END_FOR;

 


  • Loops with FOR instructions are slow, so you can optimize your code by replacing such iterations with a WHILE statement.

Below are some other statements in ST language:

- WAIT / WAIT_TIME (suspend the execution)

- ON … DO (conditional execution of statements: provides a simpler syntax for checking the rising edgeClosedA rising edge is the transition of a digital signal from low to high. It is also called positive edge of a Boolean condition)

 

Related Topics

Structured Text (ST) / Instruction List (IL) Editor

ST Editor Keyboard Shortcuts