Skip to content

ST editor (Structured Text)

Structured Text (ST) is the high-level IEC 61131-3 language, with a Pascal-like syntax. It is the most convenient for calculations, state machines, loops and complex logic that would take many rungs in Ladder.

ST editor

In ProtoCode an ST file contains statements only: there is no need for PROGRAM … END_PROGRAM or VAR … END_VAR blocks. Variables are declared in tables:

What Where it is declared How it is used in code
Global variables Project tables (e.g. IO) IO.Motor
Local variables Local section of the bottom panel Local.Estado
Function / FB inputs and outputs Input / Output / InOut sections Input.Name, Output.Name, InOut.Name
Function block instances Instance section (name + type, e.g. T1 : TON) T1(IN := …, PT := …); and T1.Q

Local and Instance tables of an ST program

Assignments, comments and literals
(* This is a block comment *)
// This is a line comment
IO.Motor := TRUE; (* assignment with := *)
Local.Counter := Local.Counter + 1;
Local.Temperature := 21.5; (* REAL *)
Local.Delay := T#2s500ms; (* TIME *)
Local.Mask := 16#00FF; (* hexadecimal *)

Every statement ends with ;. Keywords are case-insensitive, but by convention they are written in upper case.

Kind Operators
Arithmetic + - * / MOD ** (power)
Comparison = <> < <= > >=
Logic / bitwise AND (&) OR XOR NOT
Grouping ( )
IF IO.Level < 20 THEN
IO.Pump := TRUE;
ELSIF IO.Level > 80 THEN
IO.Pump := FALSE;
ELSE
(* between 20 and 80: keep the state (hysteresis) *)
END_IF;

Inside a loop, EXIT leaves the loop and CONTINUE skips to the next iteration. RETURN ends the file’s execution for the current scan.

  1. Declare the instance in the Instance table (e.g. T_Mix of type TON).
  2. Call it with its inputs by name using :=.
  3. Read its outputs with instance.output.
T_Mix(IN := Local.State = 2, PT := T#6s);
IF T_Mix.Q THEN
Local.State := 3;
END_IF;
Local.Elapsed := T_Mix.ET; (* elapsed time *)

Functions return a value and are used inside expressions, with the arguments in pin order:

Local.Setpoint := LIMIT(0.0, Local.Input, 100.0); (* MN, IN, MX *)
Local.Larger := MAX(Local.A, Local.B);
Local.Percent := SCALE(IO.Level, 0, 27648, 0.0, 100.0); (* IN, InMin, InMax, OutMin, OutMax *)
Local.Output := SEL(IO.Manual, Local.Auto, Local.Hand); (* G, IN0, IN1 *)

Your own project functions and function blocks are called the same way. See every available block in the block library.

Start/stop with seal-in
(* ST equivalent of the Ladder seal-in rung *)
IO.Motor := (IO.Start OR IO.Motor) AND IO.Stop AND NOT IO.Overload;
IO.RunLamp := IO.Motor;
IO.FaultLamp := IO.Overload;

The Batch mixer template contains a complete state machine ready to simulate.

  • Syntax highlighting: keywords, variables, numbers, times and comments.
  • Line numbers and a red mark on lines with errors.
  • A Problems panel listing the file’s errors and warnings (line, column and message).

ST error shown in the Problems panel

Shortcut Action
Ctrl+/ or Ctrl+K Comment / uncomment lines
Ctrl+Shift+K Delete lines
Alt+↑ / Alt+↓ Move lines up / down
Alt+Shift+↓ Duplicate lines
Alt+Shift+F Format (indent) the code
Ctrl+. / Alt+Enter Declare the variable under the caret (Ctrl+Shift+. for all)
F2 Rename the identifier under the caret throughout the file
F12 Go to declaration
F8 / Shift+F8 Next / previous problem
Ctrl+S Save

With View states on, the editor shows the current value of the variables used on each line to the right of that line. Boolean values are highlighted in green (TRUE) or red (FALSE). The text is locked, but you can still navigate it, use F12/F8 and right-click a variable to force it.

ST during simulation with per-line values