Your first Structured Text program
Structured Text (ST) is the text language of IEC 61131-3: one line can replace a whole rung, and calculations that are awkward in ladder become simple. Learn the shape of an ST program, its data types, operators and the rules of evaluation, and run your code in the live editor.
By the end of this module you can
- Read and write declarations (VAR_INPUT, VAR_OUTPUT, VAR_IN_OUT, VAR) and assignments
- Translate a ladder rung into one ST statement and back
- Use literals for numbers, booleans, times and strings
- Predict the result of an expression from operator precedence and data types
- Write, validate and run an ST routine on the Logivyx Soft-PLC
Ladder is ideal for on/off logic that maintenance staff troubleshoot on screen. ST shines when the logic is mostly data: calculations, comparisons, loops over arrays, strings, recipes and state machines. Most real projects mix both, and every major PLC supports ST:
Ladder
Start_PB Stop_PB_OK Motor --+--| |--+-----| |--------( )-- | Motor| +--| |--+
Structured Text
Motor := (Start_PB OR Motor) AND Stop_PB_OK;
Same logic, one line. Series contacts become AND, a branch becomes OR in brackets, an XIO contact becomes NOT.
An ST routine has declarations first, then statements. Every statement ends with a semicolon, := assigns a value, and comments are // to the end of the line or (* a block *). Keywords are not case-sensitive.
| Section | Meaning | In Logivyx |
|---|---|---|
| VAR_INPUT | Values the routine reads | Tag → variable at the start of each scan |
| VAR_OUTPUT | Values the routine produces | Variable → tag at the end of each scan |
| VAR_IN_OUT | Read and written | Read at start, written back at end |
| VAR | Internal memory, keeps its value between scans | Local, not linked to a tag |
The editor below is a real ST interpreter running at a 50 ms scan, with the variables shown live. Try the challenge, then change the code (for example make it start-dominant, or add a Stopped_Lamp) and click Run this code. Mistakes are reported with a line number.
| Type | Literal examples | Notes |
|---|---|---|
| BOOL | TRUE, FALSE | Result of every comparison |
| INT / DINT | 42, -7, 16#FF, 2#1010 | Whole numbers (hex and binary on most PLCs) |
| REAL / LREAL | 3.14, 0.5, 1.0E-3 | Always write the decimal point: 10.0, not 10 |
| TIME | T#500ms, T#5s, T#2m30s | Timer presets |
| STRING | 'Batch OK' | Single quotes |
7 / 2 = 3 (the fraction is cut off). The simulators on this page follow the standard. The Logivyx Soft-PLC calculates the exact value (3.5), so use TRUNC(7 / 2) there when you need a whole number. Brands also differ in how they convert REAL to integer (truncate or round): check before relying on it.| Priority | Operators |
|---|---|
| 1 (first) | ( ) function calls |
| 2 | -x NOT |
| 3 | * / MOD |
| 4 | + - |
| 5 | < > <= >= = <> |
| 6 | AND (&) |
| 7 | XOR |
| 8 (last) | OR |
So A OR B AND C means A OR (B AND C). When in doubt, add brackets: they cost nothing and make the intent clear.
Calculations with live inputs: move the slider. This is the pressure scaling and alarm from module 02, in ST. Then try changing the alarm limit or adding a Press_Low alarm below 1 bar.
ST is the most portable IEC language: statements look almost identical everywhere. What differs is where variables are declared and how names are written.
VAR_INPUT
Start_PB : BOOL;
Stop_PB_OK : BOOL;
END_VAR
VAR_IN_OUT
Motor : BOOL; // read and written
END_VAR
Motor := (Start_PB OR Motor) AND Stop_PB_OK;
Logivyx: Tools → Logic Editor… → + Add Routine. Variables in VAR_INPUT / VAR_OUTPUT / VAR_IN_OUT are bound to project tags in the table under the code (Auto-bind by Name, Auto-Fill Tags). Declare one name per line; give starting values through the tag's initial value or the STARTUP routine. Validate checks the syntax and shows errors with line numbers.
// SCL, in an FB or FC #Motor := (#Start_PB OR #Motor) AND #Stop_PB_OK; "Lamp_Run" := "Motor_Q"; // global PLC tags in quotes
TIA Portal calls ST SCL. Block interface sections Input, Output, InOut, Static, Temp replace VAR blocks; local names are shown with #, global tags in double quotes. REGION … END_REGION folds code. Use Static for memory that must survive between calls; Temp is lost after each call.
// Structured Text routine in Studio 5000 Motor := (Start_PB OR Motor) AND Stop_PB_OK;
No VAR blocks in the routine: tags are created in the controller or program tag database (or as Add-On Instruction parameters). Comments with //, (* *) or /* */. ST must be enabled in your Studio 5000 edition. Function-block-style instructions are called with their tag as the parameter, e.g. TONR(MyTimer); (module 12).
// ST program in GX Works3 (labels or devices) Motor := (Start_PB OR Motor) AND Stop_PB_OK; Y0 := (X0 OR Y0) AND X1; // same with devices
Labels are declared in the program's local label table or the global label table, with class VAR, VAR_INPUT and so on. Devices (X0, Y0, D100, M10) can be used directly in ST. ST programs and function blocks can be mixed with ladder in the same project.
Use PLC_Trainer. PB1 = START, SW1 = stop circuit OK, Q1 = motor, Q2 = "stopped" lamp, N1 shows a value.
L10_ST_Motor
- Tools → Logic Editor… → + Add Routine, name
L10_ST_Motor. Double-click it to open the editor. - Declare
PB1andSW1in VAR_INPUT,Q1in VAR_IN_OUT (it is read for the seal-in and written), andQ2 : BOOL;andN1 : DINT;in VAR_OUTPUT. One name per line. - Write
Q1 := (PB1 OR Q1) AND SW1;andQ2 := NOT Q1; - Add
N1 := TRUNC(7 / 2);with a comment explaining whyTRUNCis there (Logivyx computes7 / 2exactly as 3.5). - Click Validate and fix any error. Then Auto-bind by Name: every variable should bind to the trainer tag of the same name. Save & Close.
- In
Main, callL10_ST_Motorinstead of the previous lab. Save, F5. - Test the motor: PB1 starts Q1 and Q2 goes off; switching SW1 off stops Q1 and Q2 comes on. N1 shows 3.
- Open 🧮 Logic Diag and find the variables of
L10_ST_Motorwith their live values.