Introduction to the PLC Language: Structured Text
About Structured Text
Structured text is one of (if not, the most) powerful of the available IEC languages. It allows complex, conditional code to be written much easier than any of the other languages.
The following functions are available in Structured Text:
- FOR - Iterate through nested code a number of times
- WHILE - Execute the same nested code until the while condition is false
- IF - Execute nested code only if the condition is true
- ELSE - Execute an alternative section of code if the IF condition is false
- ELSIF - Conditional Else function, if the IF condition is false, but the ELSIF condition is true, execute the nested code. ELSIF can be stacked multiple times, and combined with an ELSE at the end to ensure code runs in the event of all conditions returning false. More on stacked conditions later!
- CASE Statement - Execute a particular case, depending on the value of a declared variable
Structured Text lends itself heavily to complex algorithms, long mathematical functions, array manipulation and repetitive tasks. I often find myself using ST (structured text) in function blocks that iterate over arrays.
ST is no different to any of the other IEC languages, and every editor displays it slightly differently.
The ST environment is extremely simple, it's essentially a blank text file. In most editors, the lines are numbered for convenience of referencing code in documentation.
IF Start THEN Start:=FALSE; //Remove Start Latch WHILE Initialised = FALSE DO System_Prime:=TRUE; //Set Output On For System Primer END_WHILE System_Prime:=FALSE; //Turn Off Output For System Primer Once Initialised END_IF
Consider the above example, let's analyse this line by line
- IF Statement - If the variable "Start" is TRUE then execute all code between the IF and the END_IF
- Set the variable "Start" to FALSE. This stops "Start" from remaining TRUE once we have started executing the IF statement. Ensuring the IF Statement only executes once
- WHILE Statement - Whilst the variable "Initialised" is FALSE, DO the nested code repeatedly.
- Set the variable "System_Prime" to true
- END_WHILE - Ends the WHILE statement
- Set the variable "System_Prime" to false
- END_IF - Ends the IF Statement
Structured Text is extremely powerful, however it does have some "issues" that need to be understood in order to use it correctly.
WHILE / FOR STATEMENTS ARE EXECUTION BLOCKING!
WHILE statements and FOR statements can cause some dangerous conditions if the above is not remembered or understood. Consider the earlier example, when the WHILE statement is repeating, no other code is executing, before or after the while loop.
This can be demonstrated by updating the code to the following:
Var1:=0; IF Start THEN Start:=FALSE; //Remove Start Latch WHILE Initialised = FALSE DO System_Prime:=TRUE; //Set Output On For System Primer END_WHILE System_Prime:=FALSE; //Turn Off Output For System Primer Once Initialised Var1:=Var1+1; END_IF
"Var1" will be initialised to the value 0 before the IF statement is executed, however when the WHILST statement is running, line 8 will not be executed, even though the IF statement is still executing.
Most PLCs use a single thread per task, meaning the code will execute sequentially one line at a time.
So how do we stop this from happening? If we had some critical outputs, we don't want code to block execution until the WHILE condition is complete? There are two options
- Simply don't use while statements. Replace them with IF statements that do not block execution. In most cases IF statements can reproduce the same desired outcome with a little extra coding, without the side effects of blocking code
- Run the WHILE code in a separate task with a low priority, this would not block execution on other tasks
IF statements are probably the most used function in ST and the most common reason why ST would be chosen for the POU / Function Block in development.
Take a look at the example below
IF S1 = 0 THEN S1:= S1+1; ELSIF S1 = 1 THEN S1:= 2; ELSIF S1 = 2 THEN S1:= 3; ELSE S1:=4; END_IF
This example shows a 4 state IF statement that is guaranteed to execute 1 state only.
- S1 = 0
- S1 = 1
- S1 = 2
- S1 = Anything else
The ELSIF statement will only execute if the IF or ELSIF statement above it has NOT executed.
The ELSE statement will always execute if none of the statements above have executed
CASE Statements are very similar to IF statements, however they define the state to run without having to pass through every ELSIF statement.
Take a look at the code below:
CASE i OF 0: i:=1; 1: i:=2; 2: i:=3; 3: i:=0; END_CASE
This CASE statement provides almost the same functionality as the previous IF example, the only difference is there is not a "Anything" case that captures any undefined conditions. CASE statements are explicit, the case must exist else it will not be actioned.
Common uses of CASE is to enforce a State Machine type architecture
Structured Text is extremely powerful and certainly has it's place in application design. Favoured for complex tasks, it's flexibility is superior to all other IEC languages, it is easily read, but can be confusing to beginners, especially with the caveats that other languages do not suffer with.
ST should be the language that is learnt second to Ladder (As ladder is most common), it will open up possibilities in functions and function blocks that are extremely difficult to achieve in Ladder alone