ST Language (Structured Text)
What Is ST?
ST (Structured Text) is a programming language defined in IEC 61131-3 that can be mixed with ladder
Writing calculations, FOR statements and IF statements in ST makes them easier to write and read
What you can do with ST
| Functions | Example |
|---|---|
| Conditional branching | IF |
| Repetition | FOR |
| Loops | WHILE |
| CASE branching | CASE |
| Formulas | + - * / |
| Function calls | Function |
Differences between ladder and ST
| Item | Ladder logic | ST |
|---|---|---|
| Readability | ○ | ○ |
| Complex calculations | △ | ◎ |
| FOR statement | × | ◎ |
| CASE statement | × | ◎ |
| Maintainability | ○ | ○ |
Example
Calculating a triangle

Writing this in ladder requires variables to temporarily hold intermediate values, making it harder to read

Basic ST syntax
IF statement
IF Value > 10 THEN
Judge := TRUE;
ELSE
Judge := FALSE;
END_IF;
FOR statement
FOR i := 0 TO 9 DO
Sum := Sum + Data[i];
END_FOR;
CASE statement
CASE Step OF
0:
Start();
1:
Run();
2:
Stop();
END_CASE;
Notes
Points to note when handling flags with IF statements
Consider turning Judge ON when the area of a triangle is 10 or more

It is tempting to write it like this, but then once Judge turns ON it stays ON even if the area is less than 10, so always add ELSE as below to turn Judge OFF when it is less than 10

Inline ST is skipped when its start condition is OFF
When flg, the start condition of the inline ST, turns OFF, the inline ST is not executed
Therefore variables inside the inline ST keep their previous values, so Judge stays at its previous value; note that if it was ON, it will never change to OFF
If you want Judge to turn OFF when flg is OFF, the simple way seems to be doing only the calculation in ST and moving the IF statement outside the inline ST (adjust the approach to the situation)

