# Constants, outside writes and forces

> How eaxPLC protects a VAR CONSTANT from every outside path (runtime API, NATS, OPC UA, UNS, Modbus TCP), what happens to forces and queued writes when a deploy or an online edit switches the program, and the order in which a project's FUNCTION_BLOCKs are linked - with the refusal codes and the audit records.

Product: eaxPLC · Type: Reference · Editions: Free, Pro
Source: https://eaxware.com/docs/eaxplc/reference/constants-writes-and-forces/
Verified against commit 8c56a08.

A `VAR CONSTANT` has storage in the process image like any other variable. The program never changes it, and eaxPLC makes sure nothing outside the program does either: every path that writes or forces a value refuses a constant's storage, by the constant's name **or by a direct address over its bytes** (`%MW40`, `%QX1.2`, a `%M` word that covers part of a STRING or of a FUNCTION_BLOCK's constant member). Each refusal is audited.

The same runtime also protects writes and forces against a program switch: a value checked against one program version is never applied while another version runs.

## Which constants are protected

| Constant | Example |
|---|---|
| A PROGRAM's constant, any type | `VAR CONSTANT Limit : INT := 100; Label : STRING[8] := 'abc'; END_VAR` |
| A located constant: the bit or the bytes it names | `VAR CONSTANT Lamp AT %QX1.2 : BOOL := TRUE; K AT %MW40 : INT := 7; END_VAR` |
| A FUNCTION_BLOCK's constant member, in every instance and every array element | `FUNCTION_BLOCK Valve VAR CONSTANT MaxOpen : INT := 50; END_VAR` (so `Line.Valve1.MaxOpen`, `Valves[2].MaxOpen`) |

The neighbours stay writable: another bit of the same byte, the next word.

## Refusals per path

| Path | A write onto a constant | Audit record |
|---|---|---|
| `POST /api/write` (HTTP and the NATS API) | `409`, `"code": "CONSTANT_READ_ONLY"`. All or none: a batch with one constant writes nothing. | `IO_WRITE` / `WRITE_DENIED`, one per entry of the request, cause `refused (409): CONSTANT_READ_ONLY ...` |
| `POST /api/force` (HTTP and the NATS API) | `409`, `"code": "CONSTANT_READ_ONLY"`. Unforce is never refused. | `IO_FORCE` / `FORCE_DENIED` |
| OPC UA | A constant is published read-only (no write bit in its access level); a write answers `BadNotWritable`. | `TAG_WRITE` / `WRITE_REFUSED` under the OPC UA user |
| UNS commands (`uns.command.<path>`) | A constant is published read-only; the reply is an error containing `CONSTANT_READ_ONLY`. | `TAG_WRITE` / `WRITE_REFUSED` under the user |
| Modbus TCP server (FC5 write single coil, FC6 write single register) | Exception `02` (illegal data address). | `TAG_WRITE` / `WRITE_REFUSED` under `modbus <client address>` |
| `GET /api/registry`, the cross-reference's UNS details | A constant is listed with `"writable": false` and `"constant": true`, whatever the writable patterns say. | - |

**Inputs a fieldbus drives (`IO_INPUT_DRIVEN`).** The same paths refuse an outside write whose storage overlaps a `%I` input that a configured bus drives (EtherCAT, CANopen, EtherNet/IP scanner, Modbus TCP master, MQTT inputs). For an MQTT entry this is every byte or bit it writes: its value, its quality bit, status, timestamp and age, and its source's status bit (see [Connect MQTT and Sparkplug B inputs](/docs/eaxplc/how-to/connect-mqtt-sparkplug-inputs/)). The overlap is checked to the bit: a word over one driven bit is refused, while another bit of a driven byte is not. The refusals are:

- `POST /api/write`: `409`, `"code": "IO_INPUT_DRIVEN"`, naming the bus, device and entry; the whole request is refused and audited `WRITE_DENIED`;
- OPC UA: `BadNotWritable`;
- Modbus TCP server: exception `02`;
- UNS and MQTT commands: refused, with the code in the reply.

While the PLC does not know which inputs its buses drive (`fieldbus.json` did not load, the devices were halted, or the I/O is suspended), every outside write into `%I` is refused this way; `%Q` and `%M` stay writable. **Forces are unchanged**: a force on a driven input is accepted (with the force right, audited) and wins over the device at every cycle, as the deliberate override.

**Inputs a simulator drives (eaxsim).** On a simulated PLC that an eaxsim host runs, the simulator writes its sensors into `%I` at the start of every scan, as a fieldbus does, and the same paths refuse an outside write onto them with `IO_INPUT_DRIVEN`: any byte of an analog sensor (`%IW`, `%ID`, `%IB`), or the same bit of a digital one (a byte or word over one simulated bit is refused; another bit of that byte is not). The message names the model port and address, for example `eaxSim model port T1.level_m -> %IW0`. A simulated PLC whose simulator input map is not registered, or cannot say what it drives, refuses every outside write into `%I` (`%Q` and `%M` stay writable). The simulator's own writes (sensors, a scenario's `init` values, a restored snapshot, sensor faults) are not checked, and a force on a simulated input is accepted and holds against the simulator. A real runtime is never marked simulated and is unchanged.

**Advertised read-only.** A variable whose storage a fieldbus, MQTT or simulator input drives is listed read-only: `"writable": false` in `GET /api/registry`, and no write bit in its OPC UA `AccessLevel` and `UserAccessLevel`. This is asked at each read, so it follows every fieldbus Apply, at bit level (a driven bit is read-only, another bit of the same byte stays writable). While the driven inputs are not known, every `%I` variable is listed read-only; `%Q` and `%M` are unchanged. The cross-reference's UNS `writable` flag does not take the driven inputs into account yet; the write itself is refused either way.

**Writes across a fieldbus Apply.** An outside write is checked against the driven inputs when it is queued. If a fieldbus Apply (or a halt) changes the driven inputs before the cycle boundary that would apply it, the write is checked again there: onto an input the new configuration drives (or into `%I` while the inputs are not known) it is dropped, never written, and audited `IO_INPUT_DRIVEN` under who asked for it (`/api/write`: `IO_WRITE` / `WRITE_DENIED`; UNS, MQTT, OPC UA and Modbus TCP: `TAG_WRITE` / `WRITE_REFUSED`), with a cause ending *... the fieldbus inputs changed after the write was checked (fieldbus Apply); dropped at the cycle boundary, not written*. The client had already been told the write was queued. Writes onto storage the new configuration does not drive land as before; forces are not concerned. The simulator's input map never changes while the PLC runs, so it needs no such re-check.

The runtime builds its Modbus TCP server with this check installed before the server listens: there is no moment after start-up when a Modbus write is taken unchecked. A Modbus server without its check refuses every write with exception `04`.

### What `GET /api/registry` tells per symbol

Each symbol (a variable with access none is not listed) carries its write metadata, so a client such as eaxScada can grey out an input and refuse a value before sending it:

| Field | Meaning |
|---|---|
| `writable` | The effective answer: access `read_write`, not a constant, allowed by the `--uns-writable` patterns, and no fieldbus, MQTT or simulator input drives its storage now. |
| `access` | The variable's own external access, `read` or `read_write`, before the writable patterns. |
| `constant` | `true` for a `VAR CONSTANT`. |
| `min`, `max` | The bounds of an outside write (the write range, narrowed by a subrange type). Only when bounded on that side, and only for numbers. |
| `enum` | The enumeration's values in declaration order, as `name` / `value` pairs. Only for an enumeration. |

The runtime still checks every write itself: this metadata only lets a client refuse early.

## Forces and constants

| Event | What happens to a force onto a constant's storage |
|---|---|
| Force requested | Refused, `409 CONSTANT_READ_ONLY`, audited `FORCE_DENIED`. |
| Deploy of a program that makes a forced location a constant (by name or by direct address) | The force is released by the deploy: audited `FORCE_RELEASED`, cause `automatic: deploy vN made <name> a CONSTANT: a constant is never forced`. A constant is never forced, not even by a force made before. |
| Online-edit **test** of a program that makes a forced location a constant (a PROGRAM's constant, located or not, or a FUNCTION_BLOCK member) | The test is refused, `409 forced_symbol_changes`, naming each force (`%MW40 (W constant)`, `Speed (constant)`); nothing changes. A test never releases a force: unforce first, then test. |

## Writes across a deploy or an online edit

An outside write is resolved (a name to an offset) and checked (not a constant, not forced) against the program that runs, then queued; the scan applies it at the next cycle boundary. A deploy, an online-edit test or an untest switches the program at a cycle boundary too. Each queued write therefore carries the version of the program it was checked against, and is applied only while that version still runs:

**Diagram: Outside writes across a program switch** (timeline)

Version 3 of the program runs, then a deploy or an online edit switches to version 4 at a cycle boundary. A write checked against version 3 and applied while version 3 still runs is written. A write checked against version 3 that is still waiting when version 4 starts is dropped at that boundary, never applied, and audited. A write checked against version 3 but queued after the switch is refused at once. A write checked against version 4 is written at the next boundary.

Time runs left to right: version 3 runs, then version 4 runs. Every tick is a cycle boundary; the switch happens at one too.

**Program**

- **Running program**: one version per cycle (v3; v4)

**Outside writes**

- 1. **Checked against v3**: applied while v3 runs (queued; written)
- 2. **Checked against v3**: still waiting at the switch (queued; dropped). Dropped at the cycle boundary, never applied: WRITE_STALE_PROGRAM, audited under the user or client who wrote. /api/write had already answered 200.
- 3. **Checked against v3**: queued after the switch (refused at once). WRITE_STALE_PROGRAM at once, nothing written: 409 on /api/write, BadInvalidState on OPC UA, an error on UNS, exception 04 on Modbus TCP; audited.
- 4. **Checked against v4**: applied at the next boundary (queued; written)

A write is applied only under the program version it was checked against; it never lands on a variable that moved.

| When the program changed | `/api/write` (HTTP, NATS) | UNS, OPC UA | Modbus TCP |
|---|---|---|---|
| Before the write was queued | `409`, `"code": "WRITE_STALE_PROGRAM"`, nothing written; audited `IO_WRITE` / `WRITE_DENIED` per entry | The write is refused: OPC UA answers `BadInvalidState`, UNS answers with an error; audited `TAG_WRITE` / `WRITE_REFUSED` | Exception `04`; audited `TAG_WRITE` / `WRITE_REFUSED` under the client |
| After it was queued, before the cycle that would apply it | Dropped at the cycle boundary, never applied (the request had already answered `200`); audited `IO_WRITE` / `WRITE_DENIED` under the user who wrote, with the request id, cause `refused (409): WRITE_STALE_PROGRAM ... dropped at the cycle boundary, not written` | Dropped, never applied; audited `TAG_WRITE` / `WRITE_REFUSED` (`external write (UNS or OPC UA) ... WRITE_STALE_PROGRAM`) | Dropped, never applied; audited `TAG_WRITE` / `WRITE_REFUSED` under the client |

An OPC UA write the runtime cannot queue because its write queue is full answers `BadResourceUnavailable`, nothing written; write again. A write to a constant answers `BadNotWritable` (see above).

So a write never lands on a variable that moved (its old bytes may now be another variable's), and never on storage the new program makes a constant. Write again once the new program runs. A write made while no program is loaded is not tied to a version.

The cause names both versions: `WRITE_STALE_PROGRAM: the write was checked against program version 3 and version 4 runs now (a deploy or an online edit switched the program); write again`.

Forces follow their own rule across a deploy: a force made by name follows its variable to its new place, or is released (`FORCE_RELEASED`) when the variable is gone, changed type or became a constant; a force made by direct address stays unless the new program makes it a constant.

## Link order of a project's FUNCTION_BLOCKs

A project deploy links the project's POUs into one source text, the same bytes on every run:

1. every FUNCTION_BLOCK, by name (case-insensitive), **except that a block comes after every block it holds an instance of** (directly or in an array);
2. every other PROGRAM, by name;
3. the main PROGRAM, last.

A project whose blocks hold no other block links in plain name order, as before. Blocks that hold instances of each other in a cycle (A holds B, B holds A) are a link error: `422`, `PROJECT_LINK_FAILED`, *the FUNCTION_BLOCKs hold instances of each other in a cycle (A -> B -> A): a block cannot contain itself*.

## See also

- [Runtime HTTP API routes](/docs/eaxplc/reference/generated/http-api-runtime/)
- [Make online edits](/docs/eaxplc/how-to/make-online-edits/)
- [Runtime architecture](/docs/eaxplc/explanation/runtime-architecture/)
- [Runtime metrics](/docs/eaxplc/reference/metrics/)
