PLC language reference

The simulator's built-in PLC runs IEC 61131-3 Structured Text and Ladder Diagram. Ladder is a second way to write the same programs: each rung is translated to the Structured Text that means it, so both run on one engine.

This page lists what the engine accepts, what it refuses, where it differs from the standard, and which outside tools check it. It is generated from the engine. Every example below was compiled while the page was built, and every refusal shows the message the compiler gave.

Within the subset, a program is meant to behave the way a real IEC runtime would, so code written here carries over to a physical PLC; where it knowingly behaves differently, the difference is listed. There is no compliance statement and no PLCopen certification.

1. What is and is not claimed

Claimed

  • Within the subset, a program behaves the way a real IEC runtime would, scan for scan. For ladder, where IEC leaves a choice open, this page states the engine's own rule (section 3).
  • A program can leave the simulator as a spec-legal .st file, or as PLCopen TC6 XML, the IEC exchange format, with ladder as an LD body.
  • Anything the engine does not accept is refused by name, with the conversion or substitute to write.
  • The deliberate differences we know of are listed in section 7.

Not claimed

  • Compliance. There is no statement against the standard's feature tables and no PLCopen certification.
  • Completeness. Event tasks, 64-bit types, WSTRING and the other gaps in section 4 are missing.
  • Function Block Diagram, Sequential Function Chart and Instruction List. Only Structured Text and Ladder Diagram are read.
  • Vendor extensions. IEC text is read as written, and vendor extensions are not translated.
  • All of 3rd-edition object orientation: function blocks take METHOD, EXTENDS and INTERFACE, and variables take REF_TO, but there is no CLASS or NAMESPACE.

2. Structured Text

The types, blocks, functions and keywords below are the engine's own tables. Each example compiled while this page was built.

Types
BOOLSINTINTDINTUSINTUINTUDINTBYTEWORDDWORDREALLREALTIMEDATETODDTSTRING
Function blocks
TONTOFTPR_TRIGF_TRIGCTUCTDCTUDSRRS
Functions
ABSACOSADDADD_DT_TIMEADD_TIMEADD_TOD_TIMEASINATANBCD_TO_INTCONCATCONCAT_DATE_TODCOSDELETEDIVDIVTIMEDIV_TIMEEQEXPEXPTFINDGEGTINSERTINT_TO_BCDLELEFTLENLIMITLNLOGLTMAXMIDMINMOVEMULMULTIMEMUL_TIMEMUXNEREPLACERIGHTROLRORSELSHLSHRSINSQRTSUBSUB_DATE_DATESUB_DT_DTSUB_DT_TIMESUB_TIMESUB_TOD_TIMESUB_TOD_TODTANTRUNC

Plus the <SRC>_TO_<DST> conversion family over those types.

Keywords
ANDARRAYBOOLBYBYTECASECONSTANTCONTINUEDINTDODWORDELSEELSIFEND_CASEEND_FOREND_FUNCTIONEND_FUNCTION_BLOCKEND_IFEND_REPEATEND_STRUCTEND_TYPEEND_VAREND_WHILEEXITFALSEFORFUNCTIONFUNCTION_BLOCKIFINTLREALMODNOTOFORREALREPEATRETURNSINTSTRINGSTRUCTTHENTIMETOTRUETYPEUDINTUINTUNTILUSINTVARVAR_EXTERNALVAR_GLOBALVAR_INPUTVAR_IN_OUTVAR_OUTPUTVAR_TEMPWHILEWORDXOR

Control flow

IF / ELSIF / ELSE
VAR_INPUT n : INT; END_VAR VAR_OUTPUT q : INT; END_VAR
IF n > 10 THEN q := 2; ELSIF n > 0 THEN q := 1; ELSE q := 0; END_IF;
CASE with lists and lo..hi ranges
VAR_INPUT n : INT; END_VAR VAR_OUTPUT q : INT; END_VAR
CASE n OF
  1, 3: q := 1;
  4..9: q := 2;
  -1: q := 9;
ELSE q := 0;
END_CASE;
FOR with BY
VAR i : INT; sum : INT; END_VAR
FOR i := 10 TO 0 BY -2 DO sum := sum + i; END_FOR;
WHILE and REPEAT ... UNTIL
VAR i : INT; j : INT; END_VAR
WHILE i < 5 DO i := i + 1; END_WHILE;
REPEAT j := j + 1; UNTIL j >= 5 END_REPEAT;
EXIT, CONTINUE, RETURN
VAR i : INT; n : INT; END_VAR
FOR i := 1 TO 10 DO
  IF i = 3 THEN CONTINUE; END_IF;
  IF i = 8 THEN EXIT; END_IF;
  n := n + 1;
END_FOR;
RETURN;

POUs

User FUNCTION_BLOCK, named inputs and => output bindings
FUNCTION_BLOCK Motor
VAR_INPUT Run : BOOL; END_VAR
VAR_OUTPUT Spinning : BOOL; END_VAR
Spinning := Run;
END_FUNCTION_BLOCK

VAR M1 : Motor; lamp : BOOL; END_VAR
M1(Run := TRUE, Spinning => lamp);
Stateless FUNCTION (returns by assigning its own name)
FUNCTION Double : INT
VAR_INPUT x : INT; END_VAR
Double := x * 2;
END_FUNCTION

VAR r : INT; END_VAR
r := Double(21);
A user block may shadow a standard one (TON is not reserved)
FUNCTION_BLOCK TON
VAR_INPUT IN : BOOL; END_VAR
VAR_OUTPUT Q : BOOL; END_VAR
Q := IN;
END_FUNCTION_BLOCK

VAR t : TON; q : BOOL; END_VAR
t(IN := TRUE, Q => q);
Positional arguments, in the block’s own declaration order
VAR t : TON; END_VAR
t(TRUE, T#1s);
VAR_IN_OUT binds a place, so the block writes the caller’s array
FUNCTION_BLOCK Fill
VAR_IN_OUT buf : ARRAY [1..4] OF INT; END_VAR
VAR i : INT; END_VAR
FOR i := 1 TO 4 DO buf[i] := i; END_FOR;
END_FUNCTION_BLOCK

VAR f : Fill; data : ARRAY [1..4] OF INT; END_VAR
f(buf := data);
An array of function-block instances, called by index
VAR eyes : ARRAY [1..4] OF BOOL; delay : ARRAY [1..4] OF TON; jam : ARRAY [1..4] OF BOOL; i : INT; END_VAR
FOR i := 1 TO 4 DO
  delay[i](IN := eyes[i], PT := T#3s);
  jam[i] := delay[i].Q;
END_FOR;
A function-block instance as a struct field
TYPE Merge : STRUCT gap : TON; count : CTU; END_STRUCT; END_TYPE
VAR m : Merge; eye : BOOL; open : BOOL; END_VAR
m.gap(IN := NOT eye, PT := T#500ms);
m.count(CU := eye, PV := 100);
open := m.gap.Q;
Structs in and out: by-value inputs, readable outputs, struct results
TYPE Pkg : STRUCT id : DINT; END_STRUCT; END_TYPE
FUNCTION Make : Pkg
VAR_INPUT id : DINT; END_VAR
Make.id := id;
END_FUNCTION

FUNCTION_BLOCK Latch
VAR_INPUT p : Pkg; END_VAR
VAR_OUTPUT held : Pkg; END_VAR
held := p;
END_FUNCTION_BLOCK

VAR l : Latch; box : Pkg; copy : Pkg; n : DINT; END_VAR
box := Make(7);
l(p := box, held => copy);
n := l.held.id;
A formal FUNCTION call, naming its arguments in any order
FUNCTION Span : INT
VAR_INPUT lo : INT; hi : INT; END_VAR
Span := hi - lo;
END_FUNCTION

VAR n : INT; END_VAR
n := Span(hi := 10, lo := 3);
A formal FUNCTION call that leaves an input at its initial value
FUNCTION Scale : INT
VAR_INPUT v : INT; gain : INT := 2; END_VAR
Scale := v * gain;
END_FUNCTION

VAR n : INT; END_VAR
n := Scale(v := 21);

Declarations

Whole structs and arrays assign as values
TYPE Pkg : STRUCT id : DINT; dest : INT; END_STRUCT; END_TYPE
VAR a : Pkg; b : Pkg; track : ARRAY [0..3] OF Pkg; i : INT; END_VAR
b := a;
FOR i := 3 TO 1 BY -1 DO track[i] := track[i - 1]; END_FOR;
An enumeration value written with its type (Mode#Idle)
TYPE Mode : (Idle, Running); END_TYPE
VAR m : Mode := Mode#Idle; END_VAR
IF m = Mode#Idle THEN m := Mode#Running; END_IF;
Several names on one declaration line
VAR x, y, z : INT := 3; END_VAR
x := y + z;
CONSTANT
VAR CONSTANT Limit : INT := 100; END_VAR VAR n : INT; END_VAR
n := Limit;
VAR_GLOBAL, shared by every POU
VAR_GLOBAL Shared : INT := 5; END_VAR
VAR n : INT; END_VAR
n := Shared;
A function-block instance in VAR_GLOBAL
VAR_GLOBAL t : TON; END_VAR
VAR q : BOOL; END_VAR
t(IN := TRUE, PT := T#1s);
q := t.Q;
TYPE ... STRUCT, nested access paths
TYPE Inner : STRUCT a : INT; END_STRUCT; END_TYPE
TYPE Outer : STRUCT
  inner : Inner;
  data : ARRAY [1..3] OF INT;
END_STRUCT; END_TYPE
VAR p : Outer; n : INT; END_VAR
n := p.inner.a + p.data[2];
Multi-dimensional ARRAY with a repeated initialiser
VAR grid : ARRAY [1..2, 0..3] OF INT := [1, 2, 6(0)]; n : INT; END_VAR
n := grid[2, 1];
Enumerations are their own type
TYPE Mode : (Idle, Running := 10, Fault); END_TYPE
VAR m : Mode := Running; n : INT; END_VAR
CASE m OF
  Idle: n := 0;
  Running: n := 1;
  Fault: n := 2;
END_CASE;
Subranges, enforced on every store
TYPE Percent : INT (0..100); END_TYPE
VAR p : Percent := 50; END_VAR
p := 60;
VAR_EXTERNAL, checked against the VAR_GLOBAL it names
VAR_GLOBAL Shared : INT; END_VAR
VAR_EXTERNAL Shared : INT; END_VAR
Shared := 1;
Located variables at inputs and outputs (AT %IX0.0, AT %QW4)
VAR
  Eye AT %IX100.0 : BOOL;
  Run AT %QX100.0 : BOOL;
  Count AT %IW2 : INT;
END_VAR
Run := NOT Eye;

The address is how the variable meets the world: a scene part whose PLC tag is %IX100.0 feeds it, and an external controller reaches it at that address. Declared in VAR (or VAR_GLOBAL), one variable per address, and the type has to fit the size (a bit holds BOOL, a word INT, UINT or WORD). A function block or function cannot locate a variable.

Located variables in memory (AT %MW0, %MD1, %MX8.0)
VAR
  Count AT %MW0 : INT;
  Total AT %MD1 : DINT;
  Flag AT %MX8.0 : BOOL;
END_VAR
Count := Count + 1;
Flag := Count > 3;

Storage the program keeps, read and written by name: OpenPLC keeps a SCADA’s values at %MW and %MD, so its programs carry these. No scene part binds a memory address; the OPC UA server publishes the variable by name.

A plain type alias (TYPE Count : INT)
TYPE Count : INT; END_TYPE
VAR n : Count; END_VAR
A named array type, one type under its name
TYPE Row : ARRAY [1..3] OF INT; END_TYPE
VAR a : Row; b : Row := [1, 2, 3]; END_VAR
a := b;
Array bounds written as named CONSTANTs
VAR CONSTANT ZONES : INT := 4; END_VAR
VAR eyes : ARRAY [0..ZONES - 1] OF BOOL; END_VAR
eyes[ZONES - 1] := TRUE;
Structured initial values: (field := value), and arrays of them
TYPE Pkg : STRUCT id : DINT; dest : INT; END_STRUCT; END_TYPE
VAR p : Pkg := (id := 7, dest := 3); lane : ARRAY [1..2] OF Pkg := [(id := 1), (id := 2)]; END_VAR
p.dest := 4;

Library

The PLCopen motion blocks on an AXIS_REF (MC_Power, MC_MoveAbsolute, MC_MoveVelocity, MC_Halt, MC_Stop, MC_Home, ...)
VAR Ax : AXIS_REF; pwr : MC_Power; mv : MC_MoveAbsolute; END_VAR
pwr(Axis := Ax, Enable := TRUE);
mv(Axis := Ax, Execute := pwr.Status, Position := 10, Velocity := 20, Acceleration := 100, Deceleration := 100);

A subset of the PLCopen Function Blocks for Motion Control (version 2.0, single axis; not certified), with this engine's choices where PLCopen leaves one written down in docs/motion-control.md: trapezoidal moves (a block's Jerk input is accepted and ignored), an abort, BufferMode Buffered (a queue of moves), the Busy / Done / Active / CommandAborted / Error handshake, software limits. On a real controller the blocks come from its own PLCopen motion library, so an export writes the calls and not the blocks; the Blending buffer modes (a block given one raises Error), gearing, cams and axis groups are not here.

A standard function called with IEC’s parameter names (LIMIT(MN := 0, IN := x, MX := 10))
VAR x : INT := 50; n : INT; s : STRING := 'conveyor'; t : STRING; END_VAR
n := LIMIT(MN := 0, IN := x, MX := 10);
t := MID(IN := s, L := 3, P := 2);
** exponentiation (EXPT)
VAR a : REAL; END_VAR
a := 2.0 ** 10.0;
The time and date functions (ADD_TIME, SUB_DT_DT, CONCAT_DATE_TOD, ...)
VAR a : TIME := T#1s; b : TIME; t : TOD := TOD#12:00:00; END_VAR
b := ADD_TIME(a, T#500ms);
t := ADD_TOD_TIME(t, MULTIME(b, 2));
Timers, counters and edge detectors
VAR
  t : TON; f : TOF; p : TP;
  r : R_TRIG; ft : F_TRIG;
  cu : CTU; cd : CTD; ud : CTUD;
  s : SR; rs : RS;
END_VAR
t(IN := TRUE, PT := T#1s);
cu(CU := t.Q, PV := 10);
The <SRC>_TO_<DST> conversion family
VAR i : INT := 7; r : REAL; w : WORD; s : STRING; END_VAR
r := INT_TO_REAL(i);
w := INT_TO_WORD(i);
s := INT_TO_STRING(i);
i := STRING_TO_INT('42');
Functional operator forms, including comparison chains
VAR
  a : INT := 3; b : INT := 2; c : INT := 1;
  q : BOOL; n : INT;
END_VAR
q := GT(a, b, c);
n := ADD(a, b, c);
The nine IEC string functions, 1-based
VAR s : STRING := 'hello world'; part : STRING; at : INT; END_VAR
part := MID(s, 5, 1);
at := FIND(s, 'world');
part := REPLACE(s, 'X', 1, 1);

Types

Signed, unsigned and bit-string families, kept apart
VAR
  si : SINT; i : INT; di : DINT;
  usi : USINT; ui : UINT; udi : UDINT;
  b : BYTE; w : WORD; dw : DWORD;
END_VAR
i := i + 1;
ui := ui + 1;
w := w AND 16#00FF;
REAL (single precision) and LREAL
VAR r : REAL := 0.4; l : LREAL; END_VAR
l := r;
TIME is a duration, not a number
VAR t : TIME := T#1m30s; u : TIME; n : DINT; END_VAR
u := t + T#500ms;
n := TIME_TO_DINT(u);
DATE, TIME_OF_DAY and DATE_AND_TIME, with IEC’s arithmetic table
VAR d : DATE := D#2024-02-29; t : TOD := TOD#08:00:00; x : DT; gap : TIME; END_VAR
x := CONCAT_DATE_TOD(d, t) + T#90m;
gap := x - DT#2024-02-29-00:00:00;
STRING with a capacity, and IEC $ escapes
VAR s : STRING[20] := 'line$N'; n : INT; END_VAR
s := CONCAT(s, 'more');
n := LEN(s);
STRING compares and sorts, but has no +
VAR
  a : STRING := 'abc';
  b : STRING := 'abd';
  lo : STRING; q : BOOL;
END_VAR
q := a < b;
lo := MIN(a, b);
Based, typed, exponent and separated literals
VAR
  h : WORD := 16#FF;
  bits : BYTE := 2#1010_1010;
  oct : INT := 8#17;
  typed : INT := INT#5;
  r : REAL := 1.0E-3;
END_VAR
h := h OR WORD#16#0F;
0 and 1 are BOOL literals (the standard’s own examples use them)
VAR_OUTPUT Q : BOOL := 0; END_VAR
Q := 1;
& is AND
VAR a : BOOL; b : BOOL; q : BOOL; END_VAR
q := a & b;
Identifiers are case-insensitive
VAR Motor : BOOL; END_VAR
motor := TRUE;
(* ... *) comments, anywhere trivia may go
(* a header comment *)
VAR n : INT; END_VAR
n := 1; (* trailing *)

Tasks and programs

A PROGRAM ... END_PROGRAM, which with no CONFIGURATION runs on the scan time
PROGRAM Main
VAR n : INT; END_VAR
n := 1;
END_PROGRAM
CONFIGURATION / RESOURCE / TASK, each task on its own INTERVAL
PROGRAM Main
VAR n : INT; END_VAR
n := n + 1;
END_PROGRAM

CONFIGURATION Cfg
  RESOURCE Res ON PLC
    TASK Fast(INTERVAL := T#10ms, PRIORITY := 1);
    TASK Slow(INTERVAL := T#100ms, PRIORITY := 2);
    PROGRAM A WITH Fast : Main;
    PROGRAM B WITH Slow : Main;
  END_RESOURCE
END_CONFIGURATION

Tasks run on SIM time: each comes due when its INTERVAL elapses, and tasks due together run by PRIORITY (0 first). A scan takes no sim time, so no task interrupts another. An instance variable is addressed as Instance.Name in the watch table and by scene tags.

A PROGRAM instance with no task, which runs continuously
PROGRAM Main
VAR n : INT; END_VAR
n := n + 1;
END_PROGRAM

CONFIGURATION Cfg
  RESOURCE Res ON PLC
    TASK Fast(INTERVAL := T#10ms, PRIORITY := 1);
    PROGRAM A WITH Fast : Main;
    PROGRAM Always : Main;
  END_RESOURCE
END_CONFIGURATION

IEC gives a program with no task the lowest priority and re-runs it as soon as it finishes; on a sim clock that is once per physics step, after that step's periodic scans.

Code outside every POU beside a CONFIGURATION, run by an instance that names no type
VAR n : INT; END_VAR
n := n + 1;

CONFIGURATION Cfg
  RESOURCE Res ON PLC
    TASK Fast(INTERVAL := T#10ms, PRIORITY := 1);
    PROGRAM Main WITH Fast;
  END_RESOURCE
END_CONFIGURATION

An extension: the variables are the program's top-level variables, seen by every POU under their bare names, and the statements run from the program instance with no type. Export writes that code as a PROGRAM of its own.

Object orientation

METHOD on a function block: a result, outputs, in-outs, temporaries that start over each call
FUNCTION_BLOCK Acc
VAR n : INT; END_VAR
METHOD Put : INT
  VAR_INPUT v : INT; END_VAR
  n := n + v;
  Put := n;
END_METHOD
END_FUNCTION_BLOCK

VAR a : Acc; r1 : INT; END_VAR
r1 := a.Put(v := 3) + a.Put(4);
EXTENDS, overrides, SUPER^.M() and SUPER^(), ABSTRACT, FINAL, OVERRIDE
FUNCTION_BLOCK ABSTRACT Shape
METHOD ABSTRACT Area : REAL
END_METHOD
METHOD Twice : REAL
  Twice := 2.0 * Area();
END_METHOD
END_FUNCTION_BLOCK

FUNCTION_BLOCK Square EXTENDS Shape
VAR_INPUT side : REAL; END_VAR
METHOD OVERRIDE Area : REAL
  Area := side * side;
END_METHOD
END_FUNCTION_BLOCK

VAR sq : Square; a : REAL; END_VAR
sq(side := 3.0);
a := sq.Twice();

A method called on THIS (THIS.M(), or by its bare name) runs the override of the instance's own class. A derived block runs its own body, not its base's, unless it calls SUPER^(). IEC writes THIS with no ^ and SUPER with one; THIS^ is refused by name.

INTERFACE, IMPLEMENTS, and variables of an interface type
INTERFACE IDrive
  METHOD Travel : INT
    VAR_INPUT dist : INT; END_VAR
  END_METHOD
END_INTERFACE

FUNCTION_BLOCK Belt IMPLEMENTS IDrive
VAR p : INT; END_VAR
METHOD Travel : INT
  VAR_INPUT dist : INT; END_VAR
  p := p + dist;
  Travel := p;
END_METHOD
END_FUNCTION_BLOCK

VAR belt : Belt; d : IDrive; x : INT; END_VAR
d := belt;
IF d <> NULL THEN x := d.Travel(dist := 2); END_IF;

An interface variable that refers to no instance is NULL (compared with NULL, or 0); a call through it faults the scan. STruC++ 0.6.2, OpenPLC's compiler, has no interface variables yet, so a program that uses them does not build for OpenPLC.

REF_TO, REF(), ^ and NULL: IEC references
TYPE Pkg : STRUCT id : INT; END_STRUCT; END_TYPE
VAR zones : ARRAY [1..3] OF Pkg; z : REF_TO Pkg; z2 : REF_TO Pkg; ok : BOOL; END_VAR
z := REF(zones[2]);
z^.id := 9;
z2 := z;
ok := z2 <> NULL;

A reference is a variable of a POU (or a block's or method's input, given by the call), never a struct field or an array element here. It is given REF(place), another REF_TO or NULL, compared with NULL, and dereferenced with ^; dereferencing one that is NULL faults the scan.

A METHOD returning a whole structure or named array
TYPE Pkg : STRUCT id : INT; END_STRUCT; END_TYPE
FUNCTION_BLOCK Sorter
METHOD Make : Pkg
  VAR_INPUT id : INT; END_VAR
  Make.id := id;
END_METHOD
END_FUNCTION_BLOCK

VAR s1 : Sorter; box : Pkg; END_VAR
box := s1.Make(id := 7);
An ARRAY OF an interface, each element pointed at an instance
INTERFACE IDrive
  METHOD Travel : INT
    VAR_INPUT dist : INT; END_VAR
  END_METHOD
END_INTERFACE

VAR drives : ARRAY [1..4] OF IDrive; i : INT; x : INT; END_VAR
FOR i := 1 TO 4 DO
  IF drives[i] <> NULL THEN x := x + drives[i].Travel(dist := 1); END_IF;
END_FOR;

Tolerated input forms, exported in IEC form

An enumeration in a declaration: m : (Idle, Run, Stop)
VAR m : (Idle, Run, Stop) := Run; n : INT; END_VAR
IF m = Run THEN n := 1; END_IF;

IEC. It is an enumeration of the variable's own, its members named bare; PLCopen carries it as the variable's type. MATIEC parses it but cannot generate it ("C code generator does not currently support anonymous enumerated data types").

A STRUCT field with a default value
TYPE Axis :
STRUCT
  Speed : REAL := 1.5;
  Mode : INT := 2;
END_STRUCT
END_TYPE

VAR a : Axis; b : Axis := (Speed := 3.0); s : REAL; END_VAR
s := a.Speed + b.Speed;

IEC. Every value of the type starts there: a variable, a field, an array element, a block's or a function's local; a field a variable's own initializer names takes that value instead. A default is a constant (a literal, an enumeration member, a global CONSTANT). MATIEC refuses a NESTED (field := ...) value of a struct type that has defaults, a gap of its own.

3. Ladder Diagram

The ladder editor draws rungs, and the PLC panel's text view shows the same rungs in a text form. IEC gives ladder no text form, so this one is the project's own. A rung is translated to Structured Text and run by the same engine, so everything in section 2 is available to a ladder program, and the export writes a ladder POU as a PLCopen LD body.

The text form

TextElement
|x| |/x|Normally open / normally closed contact
|P x| |N x|Rising / falling edge contact
(x) (/x)Coil / negated coil
(S x) (R x)Set / reset coil
(P x) (N x)Rising / falling edge coil
a bElements in series
[p1, p2]A branch (parallel paths; an empty path is a wire)
T1(IN, PT := t, ET => e)A function-block box; the bare pin is the one on the rung
ADD(IN1 := a, IN2 := b, OUT => c)A function box, run while the rung is on (EN / ENO)
EQ(IN1 := a, IN2 := b, OUT)A function whose BOOL result starts the rung
Next: ...;A rung label, where a jump lands
... >>Next;A jump: while the rung is on, the rungs down to Next are skipped
... RETURN;A return: while the rung is on, the rest of the POU is skipped
(* ... *)A rung comment, above its rung
Contacts, a coil and a branch (a seal-in)
VAR_INPUT Start : BOOL; Stop : BOOL; END_VAR
VAR_OUTPUT Motor : BOOL; END_VAR
LADDER
  [|Start|, |Motor|] |/Stop| (Motor);
END_LADDER
An edge contact and set / reset coils
VAR_INPUT Eye : BOOL; Button : BOOL; Clear : BOOL; END_VAR
VAR_OUTPUT Jam : BOOL; END_VAR
LADDER
  |Eye| |P Button| (S Jam);
  |Clear| (R Jam);
END_LADDER
A function-block box on the rung (a timer)
VAR_INPUT Run : BOOL; END_VAR
VAR_OUTPUT Late : BOOL; END_VAR
VAR T1 : TON; END_VAR
LADDER
  |Run| T1(IN, PT := T#2s) (Late);
END_LADDER

The bare pin sits on the rung; it defaults to the block's first BOOL input and output (a TON's IN and Q).

A function box that runs while the rung is on (EN / ENO)
VAR_INPUT Go : BOOL; END_VAR
VAR n : INT; END_VAR
LADDER
  |Go| ADD(IN1 := n, IN2 := 1, OUT => n);
END_LADDER
A comparison that starts a rung (a step sequencer)
VAR Step : INT; Busy : BOOL; END_VAR
LADDER
  EQ(IN1 := Step, IN2 := 10, OUT) |/Busy| MOVE(IN := 20, OUT => Step);
END_LADDER
AND, OR, XOR, NOT and MOD as boxes
VAR_INPUT Go : BOOL; a : BOOL; b : BOOL; END_VAR
VAR_OUTPUT c : BOOL; END_VAR
LADDER
  |Go| AND(IN1 := a, IN2 := b, OUT => c);
END_LADDER
Labels, forward jumps and RETURN
VAR_INPUT Skip : BOOL; Halt : BOOL; A : BOOL; END_VAR
VAR_OUTPUT B : BOOL; C : BOOL; END_VAR
LADDER
  |Skip| >>Done;
  |A| (B);
  Done: |A| (C);
  |Halt| RETURN;
END_LADDER
A ladder PROGRAM, with a rung comment
PROGRAM Main
VAR Start : BOOL; Motor : BOOL; END_VAR
LADDER
  (* run while Start is held *)
  |Start| (Motor);
END_LADDER
END_PROGRAM

How rungs run

Not supported

WrittenThe engine's message
A jump back up the section (a loop)
VAR A : BOOL; B : BOOL; END_VAR
LADDER
  Top: |A| (B);
  |B| >>Top;
END_LADDER
a jump back up to 'Top' (rung 0) would run rungs again in one scan, a loop; this ladder jumps only down the section
A comparison in series after a contact
VAR A : BOOL; n : INT; Q : BOOL; END_VAR
LADDER
  |A| GT(IN1 := n, IN2 := 5, OUT) (Q);
END_LADDER
GT(..., OUT) starts a rung: nothing feeds it, so it stands at the left rail, first on the rung or first on a path of a branch that is

Valid IEC, with an export caveat

These are valid IEC ladder and run here. Some targets' importers may not accept them, so the PLCopen export warns about each.

Accepted hereOn a target
A transition-sensing coil (P / N coil)
VAR A : BOOL; Q : BOOL; END_VAR
LADDER
  |A| (P Q);
END_LADDER

IEC has it, and it runs here. Some targets' importers may not accept it, so the PLCopen export warns.

A contact after a coil on the same rung
VAR A : BOOL; B : BOOL; X : BOOL; Y : BOOL; END_VAR
LADDER
  |A| (X) |B| (Y);
END_LADDER

A coil passes its power on, so IEC allows more elements after it. Some targets' importers may not accept the rung, so the PLCopen export warns.

4. Not supported

These are all valid Structured Text in some edition or tool. The engine refuses each one by name rather than reporting a syntax error. The right-hand column is the compiler's message, captured when this page was built.

Types the subset lacks

WrittenThe engine's message
LINT (64-bit signed)
VAR n : LINT; END_VAR
LINT is not supported: 64-bit integers need exact arithmetic a JS number cannot provide; use DINT, or LREAL if the range matters more than the last digits
ULINT (64-bit unsigned)
VAR n : ULINT; END_VAR
ULINT is not supported: 64-bit integers need exact arithmetic a JS number cannot provide; use UDINT, or LREAL if the range matters more than the last digits
LWORD (64-bit bit string)
VAR n : LWORD; END_VAR
LWORD is not supported: 64-bit bit strings need exact arithmetic a JS number cannot provide; use DWORD, or two of them
WSTRING
VAR s : WSTRING; END_VAR
WSTRING is not supported: wide (UTF-16) strings are not implemented; STRING is
A double-quoted (WSTRING) literal
VAR s : STRING; END_VAR
s := "wide";
double-quoted literals are WSTRING, which is not implemented; use a single-quoted STRING
LDATE, LTOD, LDT (64-bit calendar types)
VAR x : LDT; END_VAR
LDT is not supported: the 64-bit calendar types are IEC 3rd edition and not implemented; DT is

Tasks and programs the subset lacks

WrittenThe engine's message
An event task (SINGLE)
VAR_GLOBAL go : BOOL; END_VAR
PROGRAM Main
VAR n : INT; END_VAR
n := 1;
END_PROGRAM

CONFIGURATION Cfg
  RESOURCE Res ON PLC
    TASK OnEdge(SINGLE := go, PRIORITY := 1);
    PROGRAM A WITH OnEdge : Main;
  END_RESOURCE
END_CONFIGURATION
an event task (SINGLE) is not implemented; a TASK here runs on its INTERVAL, and a PROGRAM instance with no task runs continuously
Wiring a PROGRAM instance's inputs and outputs in the CONFIGURATION
VAR_GLOBAL go : BOOL; END_VAR
PROGRAM Main
VAR_INPUT start : BOOL; END_VAR
VAR n : INT; END_VAR
n := 1;
END_PROGRAM

CONFIGURATION Cfg
  RESOURCE Res ON PLC
    TASK Fast(INTERVAL := T#10ms, PRIORITY := 1);
    PROGRAM A WITH Fast : Main (start := go);
  END_RESOURCE
END_CONFIGURATION
wiring a PROGRAM instance's inputs and outputs in the CONFIGURATION is not implemented; the scene binds them by name, as Instance.Variable
More than one RESOURCE in a CONFIGURATION
PROGRAM Main
VAR n : INT; END_VAR
n := 1;
END_PROGRAM

CONFIGURATION Cfg
  RESOURCE A ON PLC
    TASK T1(INTERVAL := T#10ms, PRIORITY := 1);
    PROGRAM P1 WITH T1 : Main;
  END_RESOURCE
  RESOURCE B ON PLC
    TASK T2(INTERVAL := T#10ms, PRIORITY := 1);
    PROGRAM P2 WITH T2 : Main;
  END_RESOURCE
END_CONFIGURATION
2 RESOURCEs are declared; a controller here is one RESOURCE, so declare every TASK and PROGRAM instance inside one

Statements the subset lacks

WrittenThe engine's message
A function-block call used as an expression
VAR t : TON; q : BOOL; END_VAR
q := t(IN := TRUE, PT := T#1s);
unknown function 't'
Vendor string extensions (TO_UPPER, TRIM, ...)
VAR s : STRING := 'abc'; END_VAR
s := TO_UPPER(s);
unknown function 'TO_UPPER'

Other IEC languages

WrittenThe engine's message
Sequential Function Chart (STEP / TRANSITION / ACTION)
INITIAL_STEP Idle:
END_STEP
TRANSITION FROM Idle TO Running := start;
END_TRANSITION
INITIAL_STEP is not supported: Sequential Function Chart is a separate IEC language, not implemented here; express the state machine in ST (a CASE on a state variable is the usual translation)

Object orientation the subset lacks

WrittenThe engine's message
CLASS
CLASS Tank
VAR level : INT; END_VAR
END_CLASS
CLASS is not supported: a CLASS is not implemented; use a FUNCTION_BLOCK, which takes METHODs, EXTENDS and IMPLEMENTS here and is what a PLC instantiates
NAMESPACE
NAMESPACE Plant
END_NAMESPACE
NAMESPACE is not supported: IEC 3rd-edition namespaces are not implemented; names are flat in this subset
An ARRAY OF REF_TO
VAR refs : ARRAY [1..3] OF REF_TO INT; END_VAR
an ARRAY OF REF_TO is not implemented: keep the references as variables of their own, or an array of indices into the data

IEC allows it; this engine does not implement it yet. Keep the references as variables of their own.

Vendor extensions, refused by name

WrittenThe engine's message
POINTER TO
VAR p : POINTER TO INT; END_VAR
POINTER TO is not IEC 61131-3 and is not implemented; declare REF_TO, take a reference with REF() and dereference it with ^

Not IEC 61131-3. Declare REF_TO, take a reference with REF() and dereference it with ^.

PROPERTY with GET and SET
FUNCTION_BLOCK Motor
VAR spd : INT; END_VAR
PROPERTY Speed : INT
  GET
    Speed := spd;
  END_GET
END_PROPERTY
END_FUNCTION_BLOCK
PROPERTY is not part of IEC 61131-3; use a METHOD (or a pair of methods) instead

Not IEC 61131-3. Use a METHOD (or a pair of methods).

VAR_INST in a METHOD
FUNCTION_BLOCK M
METHOD Count : INT
  VAR_INST n : INT; END_VAR
  n := n + 1;
  Count := n;
END_METHOD
END_FUNCTION_BLOCK
VAR_INST is not part of IEC 61131-3; declare the variable in the FUNCTION_BLOCK's VAR instead, where it keeps its value between calls

Not IEC 61131-3. Declare the variable in the FUNCTION_BLOCK's VAR, where it keeps its value between calls.

REFERENCE TO, REF= and __ISVALIDREF
VAR a : INT; r : REFERENCE TO INT; END_VAR
r REF= a;
REFERENCE TO is not IEC 61131-3; declare REF_TO and dereference with ^

Not IEC 61131-3. Declare REF_TO, give it REF(place), dereference it with ^, and compare it with NULL.

THIS^
FUNCTION_BLOCK M
VAR n : INT; END_VAR
METHOD Get : INT
  Get := THIS^.n;
END_METHOD
END_FUNCTION_BLOCK
THIS^ is not how IEC 61131-3 writes it: THIS is used as it is, THIS.x, THIS.Method()

IEC 61131-3 writes THIS with no ^: THIS.n, THIS.Method(). (SUPER keeps its ^: SUPER^.Method(), SUPER^().)

AND_THEN and OR_ELSE
VAR d : INT; ok : BOOL; END_VAR
ok := d <> 0 AND_THEN 10 / d > 1;
AND_THEN is not part of IEC 61131-3; use nested IF statements, or AND (which evaluates both sides)

Not IEC 61131-3. Guard with nested IF statements (IF d <> 0 THEN IF 10 / d > 1 THEN ...), or write AND / OR, which evaluate both sides.

STRUCT EXTENDS
TYPE ST_A : STRUCT x : INT; END_STRUCT; END_TYPE
TYPE ST_B EXTENDS ST_A : STRUCT y : INT; END_STRUCT; END_TYPE
a STRUCT cannot EXTEND another: IEC 61131-3 derives only function blocks and interfaces. Declare the base's fields in the struct, or hold the base as one field

Not IEC 61131-3, which derives only function blocks and interfaces. Declare the base's fields in the struct, or hold the base as one field.

5. Refused for portability

These would not build on a real target, so the engine refuses them too. Most were found by the outside checks in section 8, and the test corpus asserts each refusal.

Refused for portability

WrittenThe engine's message
Arithmetic on a bit string
VAR b : BYTE := 255; r : BYTE; END_VAR
r := b + 1;
+ is arithmetic; 'BYTE' is a bit string, which IEC does not do arithmetic on (convert it: BYTE_TO_USINT(...))

Arithmetic is ANY_NUM; BYTE/WORD/DWORD are ANY_BIT. MATIEC refuses it, so a program that built here would build on no target.

A bitwise operation on a number
VAR i : INT := 1; j : INT := 2; r : INT; END_VAR
r := i AND j;
AND is a bit-string operation; 'INT' is a number (convert it: INT_TO_WORD(...), or declare the variable BYTE/WORD/DWORD)

The mirror of the rule above. Write INT_TO_WORD(...) and operate on the bit string.

NOT on a number
VAR i : INT := 1; r : INT; END_VAR
r := NOT i;
NOT is a bit-string operation; 'INT' is a number (convert it: INT_TO_WORD(...), or declare the variable BYTE/WORD/DWORD)

Complement is defined on ANY_BIT. MATIEC refuses it too.

Shifting a signed integer
VAR i : INT := 8; r : INT; END_VAR
r := SHR(i, 1);
'SHR' shifts a bit string; 'INT' is a number (convert it: INT_TO_WORD(...))

SHL/SHR/ROL/ROR take a bit string, where the vacated bits are defined.

A missing statement separator
VAR n : INT; END_VAR
n := 1
n := 2;
expected ';'

Between two statements. The one after END_IF, END_CASE, END_FOR, END_WHILE and END_REPEAT may be left out, a tolerated input form; see "Tolerated input forms".

A narrowing store without the conversion
VAR r : REAL := 1.5; i : INT; END_VAR
i := r;
cannot assign REAL to 'i' of type INT (write REAL_TO_INT(...))

Only IEC’s safe widenings are implicit. The error names the conversion to write.

A STRING taken apart by members (S.LEN, S.DATA[i])
VAR s : STRING; n : DINT; END_VAR
n := s.LEN;
'.LEN': a STRING has no members in IEC 61131-3 - read its length with LEN() and its characters with MID()

IEC gives a STRING no members: LEN() and MID() read it, and the error says so.

A whole structure or array cleared with := 0
TYPE Pt : STRUCT a : INT; b : REAL; END_STRUCT; END_TYPE
VAR p : Pt; END_VAR
p := 0;
cannot assign INT to 'p' of type Pt

IEC has no value for a structure of zeros: clear the fields or elements one by one.

Mixing BOOL and a number
VAR b : BOOL; n : INT; END_VAR
n := b;
cannot assign BOOL to 'n' of type INT
Assigning to a FOR control variable
VAR i : INT; END_VAR
FOR i := 1 TO 10 DO i := 5; END_FOR;
cannot assign to FOR control variable 'i' inside its own loop

IEC forbids it outright and MATIEC refuses it by name.

A typed literal that does not fit its type
VAR n : INT := INT#40000; END_VAR
40000 does not fit in INT
Two names differing only in case
VAR Motor : BOOL; motor : BOOL; END_VAR
duplicate declaration 'motor' ('Motor' is already declared; identifiers are case-insensitive)

Identifiers are case-insensitive, so these are one name declared twice.

Two memory variables that share bytes (%MW0 and %MX1.2)
VAR w AT %MW0 : INT; b AT %MX1.2 : BOOL; END_VAR
'b' at %MX1.2 shares memory with 'w' at %MW0; targets disagree on what overlapping memory addresses mean (some overlay them, OpenPLC keeps each size apart), so give each variable bytes of its own

IEC leaves overlapping memory to the implementation: one target may overlay the bytes, OpenPLC keeps each size in an area of its own. A program relying on either would mean something different on the other, so each memory variable needs bytes of its own.

A duration in microseconds or nanoseconds (T#500us)
VAR t : TIME := T#500us; END_VAR
T#500us: microseconds (us) and nanoseconds (ns) are LTIME units, which TIME here does not take: write T#0.5ms instead

us and ns are IEC 3rd-edition units, for LTIME; MATIEC, and so OpenPLC, read d, h, m, s and ms. TIME here takes fractions of a millisecond, so T#0.5ms says the same thing.

A DATE, TOD or DT converted to a number
VAR d : DATE; n : DINT; END_VAR
n := DATE_TO_DINT(d);
DATE_TO_DINT is not portable: the reference compilers count a DATE in different units (MATIEC in seconds since 1970, STruC++ in days); keep it a DATE and compare it with a DATE literal: d >= D#2024-03-01

The two reference compilers build it and disagree on the number: MATIEC counts every calendar type in seconds, while STruC++ counts a DATE in days and a TOD or DT in milliseconds. No number is portable, TIME_TO_DINT included (MATIEC counts it in whole seconds), so keep the value in its own type: compare a DATE or TOD with a literal of its kind, and subtract two DTs for a TIME to compare with a TIME literal.

6. Accepted with a caveat

These run here but mean something different on a target, or a strict compiler refuses them. The export warns about each in the file it writes.

Accepted hereOn a target
RETAIN / NON_RETAIN
VAR RETAIN count : INT; END_VAR

The qualifier is consumed and does not change what the simulator does: RETAIN asks a variable to survive a warm restart, and there is no restart here: every run starts from the declared initial value, which is what a retentive variable is given on a cold start. A real target does have a restart, so the word still matters there.

A variable named after a standard function (ADD, GT, LT, ...)
VAR ADD : INT; lt : BOOL; END_VAR
ADD := 1;

IEC does not reserve these, so refusing them would invent a rule the standard lacks. But they are Instruction List OPERATORS, and a compiler sharing one lexer between the languages treats them as keywords in ST too. MATIEC refused VAR_OUTPUT lt : BOOL. Export warns about it.

A POU touching a VAR_GLOBAL without declaring VAR_EXTERNAL
VAR_GLOBAL Shared : INT; END_VAR
FUNCTION_BLOCK Reader
VAR_OUTPUT v : INT; END_VAR
v := Shared;
END_FUNCTION_BLOCK

VAR r : Reader; n : INT; END_VAR
r(v => n);

Semantically identical (the global resolves to the same image either way), but a block that touches a global without the declaration will not build on the target. Export warns about it.

END_IF, END_CASE, END_FOR, END_WHILE, END_REPEAT and END_STRUCT without their ;
TYPE Pt :
STRUCT
  x : INT;
END_STRUCT
END_TYPE
TYPE Mode : (Idle, Run) END_TYPE

VAR n : INT; p : Pt; END_VAR
IF n > 0 THEN n := 1; END_IF
FOR n := 1 TO 3 DO p.x := n; END_FOR

Common in code written elsewhere. Strict IEC requires it and MATIEC refuses a program without, so an export writes each one back.

An enumeration member written Mode.Idle
TYPE Mode : (Idle, Run) END_TYPE

VAR m : Mode := Mode.Run; n : INT; END_VAR
CASE m OF
  Mode.Idle: n := 0;
  Mode.Run: n := 1;
END_CASE;

Read as IEC's Mode#Idle, which is what an export writes. A variable called Mode keeps its meaning: then Mode.Idle is its member.

An argument named and given nothing: Q => , or IN := ,
VAR t : TON; et : TIME; END_VAR
t(IN := TRUE, PT := T#1s, Q => , ET => et);

What generated call templates leave behind. It reads as not there at all: an input keeps its value, an output goes nowhere. An export drops it.

An interface base in the __SYSTEM namespace, and PUBLIC on an interface's method
INTERFACE IDrive EXTENDS __SYSTEM.IRoot
  METHOD PUBLIC Travel : INT
    VAR_INPUT dist : INT; END_VAR
  END_METHOD
END_INTERFACE

A base named in the __SYSTEM namespace is a runtime's own root interface, which no program declares. Neither it nor PUBLIC asks anything of an implementation (every method of an interface is public), so both are read as saying nothing, and an export leaves them out.

Any library's namespace on a TYPE: JsonLib.Parser, EXTENDS MyLib.Base
FUNCTION_BLOCK Parser
END_FUNCTION_BLOCK

VAR p : JsonLib.Parser; many : ARRAY [1..2] OF JsonLib.Parser; END_VAR

Where only a type can stand (a declaration, EXTENDS, IMPLEMENTS, ARRAY OF, REF_TO, a result), a dotted name is a library's type, so its namespace is read as saying nothing, whatever the library. An export keeps it: the name exists where the library does. Anywhere else a namespace is no namespace: the engine knows no library by name.

An enumeration with a base type: (Idle, Run) UINT
TYPE Mode :
(
  Idle := 0,
  Run := 10
) UINT;
END_TYPE

VAR m : Mode; END_VAR

A type for an enumeration's values. The values are held as INT here whatever it says, so a value INT cannot hold is refused; an export leaves the word out, since IEC's 2nd edition has no place for it.

VAR PERSISTENT
VAR PERSISTENT count : INT := 7; END_VAR

A value that survives a restart and a download; not an IEC word. Read here as retention: a run here has no restart, so it starts at its initial value. PLCopen carries persistent="true"; IEC text writes RETAIN, the nearest word.

A STRING capacity written as a constant: STRING(MAX_LEN)
VAR_GLOBAL CONSTANT MAX_LEN : INT := 20; END_VAR
VAR s : STRING(MAX_LEN); t : STRING[MAX_LEN * 2]; END_VAR

IEC writes a capacity as an integer. Folded here against the global integer CONSTANTs, the same rule as an array bound, wherever a capacity can be written, and an export writes the integer it folds to, in brackets (STRING(n) goes out as STRING[n] too).

A variable linked in the I/O configuration: AT %I*, AT %Q*
TYPE Io : STRUCT bIn AT %I* : BOOL; END_STRUCT END_TYPE
FUNCTION_BLOCK Cyl
VAR bExtended AT %I* : BOOL; bValve AT %Q* : BOOL; END_VAR
bValve := NOT bExtended;
END_FUNCTION_BLOCK

VAR start AT %I* : BOOL; END_VAR

A PLC project completes the address in its I/O configuration, which here is the scene's wiring: the variable is a plain one, in a program, a global, a block or a struct field alike, and a part wired to it by name drives or reads it. IEC completes one in a VAR_CONFIG, which no target here reads, so an export writes the variable plain.

7. Deliberate differences

Each is exercised by a corpus case carrying a divergence note, and reported by the external oracle as known rather than failed.

  1. same-width signed/unsigned ties resolve unsigned, both ordersIEC does not define mixing signed with unsigned at all; our rule is that the UNSIGNED type wins the same-width tie, in either operand order, so the result is deterministic. (Bit strings no longer take part: arithmetic on ANY_BIT is rejected outright, which is what removed the other half of this divergence.)
  2. both operands of a BOOL AND are evaluated, so a guard does not protect a divideThis engine evaluates both operands of a BOOL AND / OR, so the guarded divide faults. IEC leaves operand evaluation open, so implementations may differ: MATIEC stops at a decided operand, so this program returns FALSE there instead of faulting. A program whose behaviour depends on the difference is non-portable whichever way a simulator answers, so this engine answers the way that SURFACES it: a guard that silently worked here would stop a target that evaluates both operands.
  3. FOR evaluates bounds once and owns its iterationThe loop bound is snapshotted at entry here, and BOTH independent IEC compilers whose behaviour could be inspected re-read it every iteration, so this program runs 3 times here and 10 times there. MATIEC: its generated C puts `if (I <= N)` inside the loop, reading the live variable. RuSTy (PLC-lang): its FOR lowering plants the user-written bound expression in the loop's exit test rather than a temporary, and says so in its own comment. Neither is provably right: IEC constrains FOR to a count "determined in advance", but no edition text reachable from here says WHEN the final-value expression is evaluated, so where to read it is this engine's own rule. Two compilers agreeing is weaker evidence than it looks, since both lower FOR to a C/LLVM-style loop where re-reading is the lazy path. The snapshot is kept anyway: it is what makes the iteration count knowable at entry, which is the property FOR exists for (WHILE is the construct for a condition re-tested each pass), and the only runaway guard here is a count rather than a clock (the interpreter faults a loop after 1,000,000 iterations), so a body that grows its own bound would run a million iterations in one scan before that fault stopped it. Because it cannot be settled, it is not left silent either: exporting a loop whose body writes its own bound now WARNS (export.ts), which is the honest response to a difference nobody can adjudicate.
  4. R_TRIG / F_TRIG single-scan pulsesthe literal IEC F_TRIG body fires when CLK starts false; we suppress that startup pulse, since CLK was never TRUE before it
  5. SEL and MUX select; out-of-range MUX yields 0IEC declares an out-of-range MUX selector an error; we return 0
  6. REAL division by zero yields Infinity, not a faultfloat error handling is vendor-defined; REAL division by zero stays IEEE +/-Infinity here, matching PLC float hardware, which flags but does not fault. Only INTEGER division faults the scan
  7. an array index outside its bounds faultsThis engine faults on an array subscript outside its declared range. IEC requires a subscript to be within range but says nothing about what a runtime does when it is not, so faulting is a choice: this engine is where the mistake is cheapest to find. MATIEC does not check: it emits C, which reads past the array and completes the scan, so the replay reports "expected 0 scan lines, got 1" for this case.
  8. a store outside a subrange faultsThis engine faults on a store outside a subrange. IEC declares the range but leaves the runtime free, so faulting is this engine being the place the mistake is cheapest to find, exactly as with an array bound. MATIEC does not check and completes the scan, which is why the replay reports "expected 0 scan lines, got 1" here.
  9. an out-of-range length or position clamps instead of erroringIEC calls an out-of-range length/position an error. This engine clamps to the string instead - faulting a scan over a short string is not behaviour a controller survives.
  10. REAL_TO_STRING formatting is implementation-definedIEC leaves float-to-text formatting to the implementation, and the vendors disagree (trailing zeros, exponent thresholds). We emit the shortest round-trip form. The VALUE is portable; the exact spelling is not, so do not parse it on the target.
  11. TIME_TO_STRING emits an IEC duration literalThe output is an IEC duration literal (T#1s500ms); IEC does not fix a TIME_TO_STRING format. Chosen so the output round-trips back through STRING_TO_TIME.
  12. a fraction of a second prints in milliseconds, and a literal in a STRING reads backIEC fixes no spelling for a calendar value as text. A fraction of a second prints in milliseconds, three places; MATIEC prints six (TOD#07:05:09.500000). Text that is no date reads as 0 (1970-01-01, midnight), as STRING_TO_INT of text that is no number reads as 0.
  13. a TIME_OF_DAY pushed past midnight faults the scanThis engine faults on a TOD result past 24:00:00, the same choice as for an array subscript or a subrange: the simulator is where the mistake is cheapest to find. MATIEC does not check a TOD result: it goes on past 24:00:00 with a value that is no time of day.
  14. a DATE_AND_TIME before 1970 faults the scanThis engine's DT range is a 32-bit unsigned count of seconds from 1970, so it has no earlier instant, and a result before 1970 faults rather than hand a program a value an unsigned count cannot hold. MATIEC's count is signed and would go on.
  15. TIME_TO_DINT counts millisecondsThis engine counts a TIME converted to a number in milliseconds, the unit TIME carries everywhere else. STruC++ (T#1500ms converts to 1500 in 0.6.2) counts the same. MATIEC counts WHOLE SECONDS (T#1500ms is 1), so a program that converts a TIME to a number reads a different number on a MATIEC target such as OpenPLC v3.
  16. DINT_TO_TIME and REAL_TO_TIME read the number as millisecondsMATIEC reads a number converted to a TIME as SECONDS (DINT_TO_TIME(1500) and REAL_TO_TIME(1500.0) are T#1500s), the mirror of its TIME_TO_DINT. This engine reads milliseconds (STruC++ 0.6.2 agrees), so a preset converted from a number lasts 1000 times longer on a MATIEC target such as OpenPLC v3.
  17. faults when a REF_TO that is NULL is dereferencedIEC 61131-3 makes dereferencing a NULL reference an error and leaves what a controller then does to the implementation. This engine faults the scan, naming it, as it faults a subscript out of range; STruC++'s runtime stops the POU the same way (its NullReference fault). MATIEC has no references
  18. faults a method called through an interface that refers to no instanceIEC 61131-3 makes a call through an interface that refers to no instance (NULL) an error and leaves what a controller then does to the implementation. This engine faults the scan, naming it, as it faults a NULL REF_TO dereferenced; a program tests `itf <> NULL` (or `<> 0`) first

8. Outside checks

Tests written alongside the engine can only show that it agrees with itself, so the test corpus is also run through tools this project did not write. Each is pinned to an exact version, so a result can be reproduced. These checks have found real defects in the engine, such as timers running a scan early and programs accepted here that no real compiler would build. Each was fixed and is kept as a test case.

On every push

MATIEC (iec2c)IEC 61131-3, 2nd edition

Every corpus program is written out by the app’s own exporter and compiled by a real IEC compiler. The generated C is built, run with the corpus inputs, and compared with the expected values scan for scan.

npm run st-oracle
pinned at 7949c0bda1787de9c7cacaa4876ede49f85262dd
STruC++IEC 61131-3, 3rd edition

The edition boundary. Every case MATIEC refuses only because it is a 2nd-edition compiler must build here, so "the standard allows this" is checked rather than assumed.

npm run st-oracle:ed3
pinned at v0.6.2
PLCopen TC6 schemaPLCopen XML interchange

Every exported PLCopen document is validated against PLCopen’s own published schema.

npm run st-oracle:xml
Real-world import corpusBeremiz projects + IEC’s own Annex F examples

Import, checked with files this project did not write: every POU and data type they declare must reach either the program or the import notes, so nothing is dropped without a word.

npm run st-oracle:import
OpenPLC Editorits own reader, regenerator and STruC++

What a user who takes a program to OpenPLC meets: its editor reads each export (PLCopen XML, and a project folder of .st files) into its own model, writes Structured Text back out and builds it. A declaration or statement lost on the way in fails the check.

npm run st-oracle:openplc
pinned at 13fee913d66fb4d93f7528c3359144819da1c275

The corpus samples the subset rather than covering it. A real IEC compiler has built and run 17/17 of the scalar types, 10/10 of the standard function blocks and 54/58 of the named functions, with the results compared with the expected values scan for scan. A further 4 were compiled but never run, because the 2nd-edition compiler refuses them by design and only the 3rd-edition one builds them: BCD_TO_INT, DIV_TIME, INT_TO_BCD, MUL_TIME. Every type, block and function the engine accepts has been through at least one of them.

9. Import and export

Anything a target may not carry comes back as a warning or a note. A program that quietly means something other than what its author wrote is worse than one that refuses.

Export

  • .st IEC 61131-3 source (.st). Read by MATIEC, STruC++, RuSTy, and any vendor ST editor as paste.
  • .xml PLCopen TC6 interchange XML. Read by IEC 61131-3 tools that import PLCopen XML.
  • The CONFIGURATION, RESOURCE and TASK this simulator does not author are generated on the way out, so one program body can be wrapped for several targets. Beyond that wrapper, Structured Text is written as you wrote it.
  • A ladder POU goes into PLCopen XML as an LD body. In a .st file it is written as the Structured Text it runs as, because IEC gives ladder no text form.
  • A program that does not compile is not exported.

Import

  • A spec-legal .st file. The CONFIGURATION, RESOURCE and PROGRAM wrappers are removed, and the TASK interval becomes the scan time (this page's test file came in at 40 ms).
  • A PLCopen TC6 project, as IEC tools export it. A ladder body comes in as ladder, rung for rung.
  • Anything the subset cannot hold is named in a note, never dropped. An incomplete address such as AT %I*, which only a VAR_CONFIG could complete, comes back with a note asking for the full address. A memory address such as AT %MW0 crosses as written. A rung the engine cannot draw (a bridge, for example), or a body in FBD, SFC or IL, leaves that POU out with a note saying why.

10. Command-line tools

This page and /st-capabilities.json come from the same probe run, so an agent writing a program for this simulator reads the same list a person does. In the repository, these commands check a program without opening the app, and exit non-zero on a failure:

npm run catalogwhat parts exist, their parameters, and the PLC signals each expects
npm run capabilitiesthis manifest: what the engine accepts and refuses, in Structured Text and ladder
npm run check-st -- <path...>an ST file compiles
npm run validate-project -- <path>a project document loads
npm run check-wiring -- <path>every scene tag connects to a program variable
npm run test-project -- <folder>the control logic behaves (JSON tests, real exit codes)