# Run a simulated PLC, and install, check and run the PLC of an example project

> Start eaxPLC as a simulated PLC with --simulated - what it runs (virtual fieldbus buses only), what it refuses (every bus that opens a port, network discovery), the Simulated PLC chip in the Studio - and the plc-server example install, check and run commands that install and start an example project's PLC in a new folder.

Product: eaxPLC · Type: How-to guide · Editions: Free, Pro
Source: https://eaxware.com/docs/eaxplc/how-to/run-a-simulated-plc/
Verified against commit 425c67d.

## Goal

Run an eaxPLC that cannot drive real equipment: for training, a demonstration or a test bench. A simulated PLC runs its program and its virtual fieldbus buses. It refuses every bus that opens a port, and its Studio reaches no network.

The same mode runs the PLC of an [example project](/docs/platform/explanation/example-projects/). eaxScada adds the example as a new project from its Studio, and shows the two `plc-server example` commands that install and run its PLC. This page describes the eaxPLC side: the simulated mode and the `plc-server example` commands. For the eaxScada side, see [Add an example project in eaxScada](/docs/eaxscada/how-to/import-an-example-project/).

:::caution[A simulated PLC is not a safety barrier]
A simulated PLC opens no fieldbus port, and its values are not the plant's. Its other services still run as you start them: the Studio, the NATS connection, the Modbus TCP server (unless `--modbus 0`) and the OPC UA server (when `--opcua-port` is set). `plc-server example run` starts an example's PLC with the Modbus TCP and OPC UA servers off and the Studio on 127.0.0.1 only.
:::

## 1. Start a simulated PLC

Add `--simulated` to the start of `plc-server` or of the edge runtime `eaxplc-rt`:

```sh
plc-server --simulated
```

- The PLC marks itself simulated before its fieldbus starts and before it serves anything. `plc-server` prints `Simulated PLC:   yes (--simulated): virtual fieldbus buses only` at start-up.
- Its birth, heartbeat and death messages and `GET /api/stats` say `"simulated": true`.
- **Its overrun watchdog suits a desktop.** Without its own `data/watchdog.json`, a simulated PLC takes the examples' watchdog: 500 consecutive overruns, or one cycle over 2000 ms (twice the slowest task's period when that is longer); on for a new project, off for an existing install. The file written at the first start holds these values; a `data/watchdog.json` that exists is used as it is. A real PLC's default (5 consecutive overruns, or a cycle 20 periods long, at least 20 ms) is unchanged: one desktop OS stall trips it. The start-up log line `Overrun watchdog: ...` says `simulated=true`.
- **It is one way.** Nothing clears it while the PLC runs. To run the PLC as a normal PLC again, stop it and start it without `--simulated`.
- **An example's folder is always simulated.** A work folder made by `plc-server example install` holds `data/example.json`. A PLC started on such a folder is simulated with or without the flag, and also when that file cannot be read.

## 2. What a simulated PLC runs and refuses

| Bus | On a simulated PLC |
|---|---|
| CANopen on `sim:` | runs (virtual nodes in the runtime) |
| EtherCAT on `sim:...` | runs (virtual slaves) |
| MQTT inputs | accepted, never connected: their entries stay not connected (code 8) |
| CANopen on a CAN port or an SLCAN adapter | refused |
| EtherCAT on a network interface | refused |
| Modbus TCP | refused |
| EtherNet/IP | refused |

A bus that opens a port is refused whether it is enabled or not, at every step:

- **At start-up.** `data/fieldbus.json` is not loaded and nothing of it runs. `GET /api/fieldbus` reports why.
- **At every Apply and Validate.** This covers the configurator's Apply and Validate, a memory-layout change, and the re-apply of an inhibit or an enable. The configuration is refused with one problem per bus, for example `bus "line1" (modbus_tcp, interface ""): this PLC is simulated and runs virtual buses only (CANopen or EtherCAT on sim:); a bus that opens a port is refused`. The running configuration stays as it was.

A PLC that is not simulated is unchanged.

## 3. Network discovery is off

The configurator of a simulated PLC sends nothing to the network.

- In **Fieldbus**, the **Discover** button is disabled, with the tooltip **This PLC is simulated: network discovery is off**. On a virtual EtherCAT line (`sim:...`) the scan stays available, because it opens no port.
- `POST /api/fieldbus/discover` answers `409` with `"code": "SIMULATED_PLC"`, for an EtherNet/IP discovery (ListIdentity, broadcast or one target) and for an EtherCAT scan of a network interface. The error reads, for example, `this PLC is simulated: network discovery is off: an EtherNet/IP discovery (ListIdentity) is not sent`.
- Each refusal is in the audit trail as `SECURITY` / `FIELDBUS_DISCOVERY_DENIED`, with the resource `fieldbus/discover/ethernet_ip` or `fieldbus/discover/ethercat/<interface>`.
- **Test connection** and **Browse** of an MQTT input source were already refused on a simulated PLC (`409`).

## 4. The Simulated PLC chip

On a simulated PLC, the Studio's top bar shows a violet **Simulated PLC** chip before the scan state. Its tooltip reads **This PLC is simulated: its values are not the plant's, and it runs virtual fieldbus buses only (no port is opened)**. The chip is in the five Studio languages. Once shown, it stays.

## 5. A CANopen bus on sim: is not counted as a master

This applies to every PLC, simulated or not:

- A CANopen bus on `sim:` no longer counts as a fieldbus master. eaxPLC Free runs it, and eaxPLC Standard keeps its one master for a real bus.
- A CANopen bus on a port, and every EtherCAT line (`sim:` included), count as before.

## 6. Example projects: example install, example check and example run

```sh
plc-server example install <zip> [--dir <folder>]
plc-server example check <zip>
plc-server example run <id> [--dir <folder>] [settings]
```

An example's PLC is always a **new** PLC in a folder of its own, never an existing PLC.

**The install folder.** In the Windows package, `plc-server.exe` is in `bin\`, beside `data\` and `examples\`. The example commands take the folder that holds `bin\` as the install folder: the default example folder is `<install>\examples\<id>\`, and the default NKey file `<install>\data\security\nkeys\<plc id>.nk`. A `plc-server` that is not in a `bin` folder (a development build) uses its own folder. Before eaxPLC live123 these defaults pointed under `bin\`.

### Run the example eaxScada added

Once the example is added, eaxScada shows the two commands to run, with **Copy** buttons, for example:

```sh
plc-server example install "eaxware-example-bottling-line-1.0.0.zip"
plc-server example run bottling-line --nats nats://127.0.0.1:4222 --nats-nkey "<eaxScada data>\security\nkeys\ex-bottling.nk" --scada-url http://127.0.0.1:8080
```

The second command names eaxScada's broker, the key file eaxScada made for the example PLC, and eaxScada's address. Run them in the folder that holds `plc-server`. The example PLC keeps the sign-in key of the first eaxScada it works with in its own folder: to use it with another eaxScada, or after that eaxScada was reset, install it again into a new folder (`--dir <new folder>` on both commands) or remove its old folder first.

### Install an example project: `plc-server example install`

`plc-server example install <zip>` installs the example's PLC into a new folder: `--dir <folder>`, or by default `examples\<id>\` in the install folder.

- **It never writes over anything.** An existing folder or file is refused. A refused install removes the folder it made.
- **It checks the whole zip first.** It refuses:
  - a zip over 16 MB, over 64 MB unpacked, with over 1000 entries, an entry over 8 MB or a compression ratio over 100:1;
  - an entry with `..`, an absolute path, a drive, a backslash, a link, a directory entry, a duplicate, or an entry outside the example layout;
  - an entry whose SHA-256 differs from the one `example.json` lists, or that `example.json` does not list;
  - a newer format, or an example that needs a newer eaxPLC;
  - a POU that does not pass the project's unit rules or does not compile (named with its line);
  - a task that calls anything but the project's PROGRAMs;
  - a fieldbus bus that is not CANopen on `sim:`;
  - a writable list that is not one of the example's own global variable lists (so never `*`), a writable variable located on I/O or aliased, and a safety target among the writable variables.
- **The example layout** includes eaxScada's navigation pages and theme tokens, `scada/pages.json` and `scada/styles.json`. Before eaxPLC live123 the installer refused a zip that carried them.
- **Trend sets.** `plc/trendsets.json` is taken as the Studio writes it: each set may carry `rev`, `saved_by` and `saved_at`, and the file may leave out `format`. Any other key, a `format` other than 1 and a negative `rev` are refused. The installed sets start at rev 1, saved by `example-installer`. Before eaxPLC live123, a file with `rev` was refused (`unknown field "rev"`).
- **What it writes.** The project is created through the project store as the user `example-installer`, compiled by the PLC's own compiler on the example's CPU tier and recorded as deployed. The folder gets the program, its tasks, the watchdog (the example's own `plc/watchdog.json` as it is; without one, on, with 500 consecutive overruns or a cycle of 2000 ms, or twice the slowest task's period when that is longer), the CPU settings, the fieldbus configuration, the safety targets and `data/example.json`. The install is recorded in that PLC's own audit trail as `EXAMPLE` / `EXAMPLE_INSTALLED`, with the example's id and version, the zip's SHA-256, the program hash and the writable variables.
- **What it prints.** `INSTALLED example <id> <version> (sha256 ...)`, the PLC id, the UNS prefix, the Studio port, the counts of POUs, tasks and virtual buses, the variables writable from eaxScada, the folder, and the command that starts it: `Start it: plc-server example run <id>`.
- A refused example prints `REFUSED <zip>:` and one line per problem, and the command ends with exit code `1`.

### Example check: `plc-server example check`

The example check, `plc-server example check <zip>`, runs every install check in a temporary folder, which is then removed. Run this example check before you install a zip you received. Nothing is installed. It prints `CHECKED example <id> <version> ...`, or the problems.

### Run an example project: `plc-server example run`

`plc-server example run <id>` prints the full start (`Example PLC:     plc-server ...`) and starts the example's PLC. It refuses a folder that holds no installed example, or another example.

These settings are fixed:

- `--simulated`;
- the Studio on 127.0.0.1 only;
- no Modbus TCP server (`--modbus 0`) and no OPC UA server;
- writable from outside (UNS commands, OPC UA) only the variables the install recorded, each by its exact name;
- the UNS prefix `Examples/`.

By default the PLC connects to the broker `nats://127.0.0.1:4222` with the NKey file `data\security\nkeys\<plc id>.nk` in the install folder, and signs people in through eaxScada at `http://127.0.0.1:8080`.

**Its own inbox.** The PLC's broker connection takes the answers to its requests (those of its eaxTags edge scripts and any other) on its own inbox prefix, `_INBOX.<plc id>.>`, never on the shared `_INBOX.>`. This holds for every eaxPLC from live123. A broker can therefore narrow the PLC's subscribe permission to its own inbox: eaxScada does so for an example PLC, which may subscribe only to `plc.<plc id>.>`, the commands under its tag root and `_INBOX.<plc id>.>` (see [An example project's PLC](/docs/eaxscada/explanation/connections-and-stations/#an-example-projects-plc)).

These settings can be changed:

| Setting | |
|---|---|
| `--dir` | the example's folder |
| `--http` | the Studio port, on 127.0.0.1 (default: the example's) |
| `--nats` | the broker; empty (`--nats=`): the PLC alone, no NATS |
| `--nats-nkey`, `--nats-token`, `--nats-user`, `--nats-pass` | the broker credentials |
| `--scada-url` | eaxScada's address, for the Studio sign-in |
| `--plc-id`, `--uns-prefix` | the PLC id; the prefix, always `Examples/` |
| `--uns-writable` | a part of the recorded writable variables only |
| `--freq` | the scan frequency in Hz (default: the CPU tier's) |
| `--log-level`, `--log-format`, `--headless` | the log, and no Studio files |
| `--standalone`, `--users-file`, `--token-pubkey`, `--token-pin` | the sign-in; `--standalone` signs in with the PLC's own users and cannot be combined with `--scada-url` |

Any other setting is refused, for example `--simulated=false`, `--http-bind`, `--modbus` or `--opcua-port`. In the printed start, a broker password or token shows as `***`.

## 7. --modbus 0 starts no Modbus TCP server

`plc-server --modbus 0` now starts no Modbus TCP server, and the start-up line reads `Modbus TCP:      off`. Before, it listened on a random port. `eaxplc-rt` already read `--modbus 0` as no server.

## Related

- [Add an example project in eaxScada](/docs/eaxscada/how-to/import-an-example-project/)
- [Example projects](/docs/platform/explanation/example-projects/)
- [Apply fieldbus changes without stopping the other devices](/docs/eaxplc/how-to/apply-fieldbus-changes/)
- [Connect CANopen devices](/docs/eaxplc/how-to/connect-canopen-devices/)
- [Command-line flags](/docs/eaxplc/reference/generated/cli/)
- [What's new in eaxPLC](/docs/eaxplc/whats-new/)
