# Ladder diagrams and their generated ST

> How a ladder (LD) POU becomes the Structured Text that runs - who generates it, what the PLC refuses on a save, how data/ladder diagrams and running programs are migrated into the project, and how a diagram uses global variables with VAR_EXTERNAL.

Product: eaxPLC · Type: Reference · Editions: Free, Pro
Source: https://eaxware.com/docs/eaxplc/reference/ladder-diagrams-and-generated-st/
Verified against commit 2171d7c.

An LD POU (a PROGRAM or a FUNCTION_BLOCK drawn as ladder) is stored as its diagram (`ladder`), its declaration (`decl`) and its generated Structured Text (`gen`). The PLC links and runs the generated ST. The PLC guarantees that this ST is the diagram: it generates the same text itself and refuses any other.

## The generated ST

The Studio's ladder editor and the PLC generate the ST with the same rules, byte for byte:

- each rung is read left to right as power flow: contacts AND into the power, a parallel branch ORs in, an empty parallel branch is a wire;
- a block takes the power at its enable input (`IN`, `CU`, `CLK`, ...) and its output (`Q`, `OUT`, ...) carries the power on; a block without an enable input runs while the rung is TRUE;
- a coil writes the power (`:=`), its negation, or sets or resets its variable;
- a P or N contact is a rising or falling edge detector, an `R_TRIG` or `F_TRIG` instance (`_LD_P1`, `_LD_N2`, ...) declared in a `VAR` block at the start of the text;
- every rung starts with a `// Network N: <title>` line and its comment; every control character in a title or comment except the tab becomes a space, so the text stays a comment.

The linked unit is `PROGRAM ` (or `FUNCTION_BLOCK `), the declaration, the generated ST, then `END_PROGRAM`.

## What is refused

A diagram that cannot be generated faithfully is refused, never generated with a part left out or guessed. The editor shows the problem on the diagram; a save is refused with the same reason. The reason names the rung (1-based), its title, the element (`3`, or `3.0.1` for the second contact of the first parallel branch of element 3) and its id:

| Problem | Reason (example) |
|---|---|
| a block the catalogue does not have | `unknown block "PWM": the PLC does not generate ST for it` |
| an element, contact or coil type the editor does not make | `unknown element type "jump"`, `unknown contact type "contact_xx"`, `unknown coil type "coil_pulse"` |
| a missing operand | `contact: an operand is missing`, `GT.IN2: an operand is missing` |
| an operand that is not a variable name, a literal or a direct address | `contact: "???" is not a variable name, a literal or a direct address` |
| a constant on a coil or an output pin | `coil: TRUE is a constant: an output needs a variable` |
| a block instance name that is not a name | `TON: "" is not a valid instance name` |
| something other than a contact in a parallel branch | `only contacts can be in a parallel branch, not "coil_normal"` |

Example: `rung 2 ("Feed valve XV-01"), element 1 (id "l1_r2_e2"): contact: "???" is not a variable name, a literal or a direct address`.

## Saving an LD POU

`POST /api/project/pous` and `PUT /api/project/pous/{name}` with `{ladder, decl, gen}`:

| `gen` sent | The PLC |
|---|---|
| left out, `null` or blank | generates it from the diagram. A diagram it cannot generate: `400 PROJECT_INVALID` *the diagram cannot run: \<reason> (fix the element, then save again)*. |
| the ST the PLC generates (CRLF read as LF) | saves it as sent. |
| any other text | `400 PROJECT_INVALID` *gen is not the ST of the diagram (line N: expected "...", got "...")*, naming the first line that differs. Nothing changes. |

The Studio sends `gen` when its editor generates the diagram, and leaves it out when the editor refuses it: the save is then refused with the PLC's reason, shown in the **Save** message. A draft (`PUT .../draft`) is kept without `gen`; drafts are never linked.

The compatibility route `POST /api/ladder` applies the same rules, except that a diagram the PLC cannot generate is saved without ST (`gen_missing: true`). Such a POU is not linked: a PROGRAM is listed in the link's `excluded` (*LD PROGRAM without generated ST ...*), a FUNCTION_BLOCK stops the link, until it is saved again. A POU saved by an older Studio keeps its stored ST, which still links. The PLC does not regenerate stored POUs.

## Global variables in a diagram

A diagram may use the globals of a global variable list. The ladder editor checks a diagram against its declaration only, so declare each global the diagram uses in a `VAR_EXTERNAL` block, with the type of the global and no address or initial value. Those belong to the global:

```text
VAR_EXTERNAL
    EmergencyStop : BOOL;   (* %IX0.2 *)
    FeedValve     : BOOL;   (* %QX0.0 *)
    Temp          : REAL;
END_VAR
VAR
    HB : BLINK;             (* the diagram's own instance *)
END_VAR
```

Under a contact or coil on a `VAR_EXTERNAL` global, the editor shows the global's address from the running PLC, or else the address stored with the element. A `VAR_EXTERNAL` block is the keyword in use: it is not reported as a name that became a keyword.

## Migration into the project

On the first start of a PLC without a project, the running program and `data/ladder` become the project. Nothing is compiled or deployed:

- each `PROGRAM` and `FUNCTION_BLOCK` of the running text becomes its POU; the last PROGRAM is the main;
- a global variable list as the linker writes it (`{gvl }`, then `VAR_GLOBAL` blocks) becomes a GVL POU, and the project moves to format 2;
- a running PROGRAM or FUNCTION_BLOCK whose text is exactly the unit of its diagram in `data/ladder/.json` (with `.decl.st`), its declaration plus the ST the PLC generates, becomes that LD POU, so the editor shows the ladder that runs. Any difference keeps the running text as an ST POU;
- every other diagram in `data/ladder` becomes an LD POU of its own, with the ST the PLC generates. A diagram the PLC cannot generate is migrated without ST (`gen_missing`), and its note says why.

A running text written this way links back to the same bytes: the project is not detached and nothing waits to be deployed.

## See also

- [Data model](/docs/eaxplc/explanation/data-model/)
- [Create a global variable list and a structure](/docs/eaxplc/how-to/create-global-variables-and-structures/)
- [Make online edits](/docs/eaxplc/how-to/make-online-edits/)
- [Runtime HTTP API routes](/docs/eaxplc/reference/generated/http-api-runtime/)
