Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 37 ยท Function Blocks and Structured Text

Structured Text Basics

Learn Structured Text, the Pascal-like PLC language: program layout, variable declarations, assignment :=, operators, precedence, type conversion and comments, with C stand-ins.

Structured Text (ST) is the text language of IEC 61131-3. It looks like Pascal: readable keywords, := for assignment, ; at the end of statements. It is the best choice for math, loops, arrays, strings and complex decisions, where the equivalent ladder would sprawl over many rungs.

This lesson covers the building blocks: declaring variables, assigning, operators, and type conversion. The real ST compilers cannot run on this site, so ST code is shown read-only and each concept is mirrored by a runnable C stand-in. Where the two differ, a note says so.

A first program

TEXT
PROGRAM Tank
VAR
    Level      : REAL := 0.0;      (* percent *)
    FillValve  : BOOL;
    HighLevel  : BOOL;
END_VAR

HighLevel := Level >= 95.0;
FillValve := (Level < 90.0) AND NOT HighLevel;
END_PROGRAM

Parts of the program:

  • PROGRAM ... END_PROGRAM wraps the code.
  • VAR ... END_VAR declares variables as Name : TYPE := initial value;.
  • Statements end with ;.
  • (* comment *) is a block comment; many IDEs also accept // line comment.
  • Keywords are case-insensitive, but upper case is customary.

Variables and types

Every variable has a type (see number systems and data types). Variables are grouped by role:

Section Meaning
VAR Local to the program or block
VAR_INPUT Passed in by the caller
VAR_OUTPUT Returned to the caller
VAR_GLOBAL Visible to the whole project
VAR CONSTANT A named constant
VAR RETAIN Keeps its value after power loss
TEXT
VAR CONSTANT
    MaxTemp : REAL := 85.0;
END_VAR

Assignment is :=, comparison is =

This is the most common beginner mistake coming from C-like languages. In ST:

  • := assigns.
  • = compares for equality. <> means not equal.
TEXT
Counter := Counter + 1;         (* assignment *)
IF Counter = 10 THEN ... END_IF;   (* comparison *)
C Stand-in: ST-style assignment and comparison in C

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Operators and precedence

Kind ST operators C equivalent
Arithmetic + - * / and MOD + - * / %
Comparison = <> < > <= >= == != < > <= >=
Boolean AND OR XOR NOT (also &) && || ^ !
Exponent ** pow()

Precedence from high to low: parentheses, function calls, **, unary - and NOT, * / MOD, + -, comparisons, AND, XOR, OR. When in doubt, add parentheses. Note that AND binds tighter than OR, as in most languages.

C Stand-in: precedence of AND and OR (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Integer vs real division and conversion

IEC compilers are strongly typed: you cannot assign a REAL to an INT without an explicit conversion, and dividing two INTs gives an INT (truncated).

TEXT
Percent := DINT_TO_REAL(Count) / DINT_TO_REAL(Total) * 100.0;
Rounded := REAL_TO_INT(Percent);          (* rounds to nearest *)
Truncated := TRUNC(Percent);

Common conversion functions: INT_TO_REAL, REAL_TO_INT, BOOL_TO_INT, TIME_TO_DINT, STRING_TO_INT.

C Stand-in: integer vs real division, rounding vs truncation (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Calling functions and function blocks

Functions are called in expressions. Function blocks are called as statements on an instance, with named parameters (:= for inputs, => for outputs):

TEXT
Temp_C := Scale(Raw := AI_Temp, RawMin := 0, RawMax := 27648, EuMin := 0.0, EuMax := 150.0);
Delay(IN := Start, PT := T#2s, Q => DelayDone);

Named parameters make calls self-documenting, so prefer them over positional ones for blocks with many inputs.

ST quick facts

:= Assign a value
= Compare for equal
; Ends each statement
Strong types Convert explicitly

Keywords end with END_

1 / 2

Style tips

  • One statement per line; indent consistently.
  • Declare variables with initial values and units in comments.
  • Prefer clear names over comments.
  • Avoid very long expressions; assign intermediate results to named variables.

You will use these ingredients in the next lesson: Structured Text control flow.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which operator assigns a value in Structured Text?

    := assigns; = compares for equality.
  2. 2. What does 7 / 2 give when both are INT?

    Integer division truncates, giving 3.
  3. 3. What does a VAR RETAIN variable do?

    RETAIN marks data that survives a power cycle.
  4. 4. Which is the not-equal operator?

    ST uses <> for not equal.

Summary

  • ST uses := to assign, = to compare and ; to end statements.
  • Variables are declared in VAR sections with explicit types.
  • Convert types explicitly; INT division truncates.
  • Call functions in expressions and function blocks with named parameters.

Next: Structured Text Control Flow