Skip to main content

Structured Text (ST)

Structured Text (ST) is a programming language defined by the IEC 61131-3 standard. It is a textual language, similar to high-level languages such as Pascal and C, which allows programs to be written in a clear and organized manner. It allows for assignments, function block calls, and, depending on the hardware used, conditional and loop statements. Its simple syntax makes it easier to understand and offers the programmer more flexibility than ladder programming, especially in tasks involving detailed logic and mathematical operations.

Structured Text Elements - Expression

Expressions are structures that, when evaluated, result in a value of a specific data type. They are made up of operators and operands. Operands can be literals, variables, function or method calls with returns, function block instances 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 follow specific priority rules.

Structured Text Elements - Assignment

Assignment replaces the current value of a variable with the result of evaluating an expression. It consists of a variable on the left, followed by the assignment operator :=, the expression to be evaluated, and finally, the semicolon ; that ends the statement.

Example

Below, you can see five different examples of assignment in Structured Text.

VAR
A : REAL;
B : REAL;
END_VAR

B := 10; // Atribuição do valor de um literal.
A := B; // Atribuição do valor de uma variável.
A := (B + 5); // Atribuição do valor resultante de uma operação.
A := FB_POW(B, 2); // Atribuição do valor resultante de uma função.

Structured Text Elements - Comparison

Comparison returns a Boolean value as a result. It must consist of a left operand, 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 - Calls

In Structured Text, there are two main types of calls: function calls and function block calls.

1 - Function Call

Functions are called using a statement consisting of the function name followed by a list of parameters 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 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 in parentheses. The block's internal state is maintained between executions, and output values can be accessed through its attributes.

Example

The example below demonstrates the use of an incrementing 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 : UINT;
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;