Structured Text (ST)
Structured Text is a programming language defined by the IEC 61131-3 standard, similar to high-level options like Pascal and C, allowing programs to be written clearly and in an organized manner. With it, it is possible to perform assignments, call function blocks and, depending on the equipment used, execute conditional and repetition instructions. Its simple syntax makes it easier to understand and offers the programmer more flexibility compared to Ladder, especially in tasks involving detailed logic and mathematical operations.
Elements of Structured Text - Expression
Expressions are structures that, when evaluated, result in a value of a specific data type. They are formed by operators and operands. Operands can be literals, variables, function or method calls with return, instances of function blocks with results or even other expressions. Operators are symbols that represent arithmetic, logical or functional operations, and are applied according to the data type and following specific priority rules.
Structured Text Elements - Assignment
Assignment updates the value of a variable with the result obtained from evaluating an expression. The syntax consists of a variable on the left side, followed by the assignment operator ":=", the expression to be evaluated, and a semicolon ";" to terminate the statement.
Example
Below, five different examples of assignment in Structured Text can be observed.
VAR A : REAL; B : REAL; END_VAR
B := 10; //Assigning the value of a literal.
A := B; //Assigning the value of a variable.
A := (B + 5); //Assignment of the value resulting from an operation.
A := FB_POW(B, 2); //Assignment of the resulting value of a function.
|
|---|
Structured Text Elements - Comparison
A comparison returns a boolean value as its result. It should consist of an operand on the left, followed by a comparison operator, and then a second operand on the right. The result of the comparison can be assigned to a variable.
Example
The example below shows a comparison that checks whether one operand is greater than or equal to another.
VAR A : INT; B : INT; END_VAR
B := A >= 0;
|
|---|
Structured Text Elements - Call
In Structured Text, there are two main types of calls: function calls and function block calls.
1 - Function Call
Functions are called using an instruction composed of the function name followed by a list of parameters enclosed in parentheses. They return a value directly, which can be assigned to a variable.
Example
The example below shows the use of the function to calculate the square root of an operand.
VAR A : REAL; B : REAL := 81; END_VAR
A := FB_SQRT(B, 2);
|
At the end of the code execution, the value of A will be 9.0.
2 - Function Block Call
Function blocks are called using the name of the block instance, followed by a list of parameters enclosed in parentheses. The internal state of the block is preserved between executions, and output values can be accessed through its attributes.
Example
The example below demonstrates the use of an increasing counter function block (FB_CTU) in Structured Text. The counter is used to count pulses from a digital input (DI1) and trigger an output (DO1) when a predefined value (30) is reached.
VAR PRESET : UINT := 30; COUNT : UNIT; CTU_INST_0 : FB_CTU; END_VAR
CTU_INST_0.CU := DI1; CTU_INST_0(R := DI2, PV := PRESET); COUNT := CTU_INST_0.CV; DO1 := CTU_INST_0.Q;
|