Structure Text Programming - Codesys 2
Structure Text Programming - Codesys 2
program flow control. A ST program will be run from beginning to end many times each second. A tra-
ditional program should not reach the end until it is completely finished. In the previous example the
loop could lead to a program that (with some modification) might go into an infinite loop. If this were
to happen during a control application the controller would stop responding, the process might become
dangerous, and the controller watchdog timer would force a fault.
ST has been designed to work with the other PLC programming languages. For example, a lad-
der logic program can call a structured text subroutine.
The language is composed of written statements separated by semicolons. The statements use
predefined statements and program subroutines to change variables. The variables can be explicitly
defined values, internally stored variables, or inputs and outputs. Spaces can be used to separate state-
ments and variables, although they are not often necessary. Structured text is not case sensitive, but it
can be useful to make variables lower case, and make statements upper case. Indenting and comments
should also be used to increase readability and documents the program. Consider the example shown in
Figure 262.
FUNCTION sample
GOOD INPUT_VAR
start : BOOL; (* a NO start input *)
stop : BOOL; (* a NC stop input *)
END_VAR
OUTPUT_VAR
motor : BOOL;(* a motor control relay
*)
END_VAR
motor := (motor + start) * stop;(* get the motor output *)
END_FUNCTION
FUNCTION sample
BAD INPUT_VAR
START:BOOL;STOP:BOOL;
END_VAR
OUTPUT_VAR
MOTOR:BOOL;
END_VAR
MOTOR:=(MOTOR+START)*STOP;END_FUNCTION