Structured Text (ST) is a high-level, PASCAL-like textual programming language designed for implementing complex control algorithms, mathematical calculations, and state machines within a controller.
It is the preferred choice for software developers because it uses familiar programming constructs, including conditional statements, iterative loops, and explicit variable assignments. ST allows you to express complex functions in just a handful of lines of code, making it ideal for extensive projects. It is also the primary language used to create custom function blocks.
The following examples demonstrate how ST uses control structures for logic and calculations.
This is used to execute a block of code only if a specified Boolean condition is TRUE.
IF Temperature > 100.0 THEN
Heater_Status := TRUE;
Alarm_Light := TRUE;
END_IF;
The `CASE` statement executes a specific block of code based on the value of an integer expression.
CASE Input_Mode OF
1: // Mode 1: Manual
Motor_Speed := 50;
2: // Mode 2: Automatic
Motor_Speed := Auto_Setpoint;
ELSE // Default for any other value
Motor_Speed := 0;
END_CASE;
The `FOR` loop repeats a block of code a fixed number of times, using a counter variable.
FOR i := 1 TO 10 DO
Result_Array[i] := Result_Array[i] * 2;
END_FOR;