# Siemens S7

Read and write Siemens S7 PLCs

- Category: Industrial
- Capabilities: trellis, Streams, Assets, Commands, Operations, Entities
- Runs on: Linux x64, Linux ARM64, macOS Apple silicon, macOS Intel, Windows x64

Read and write **Siemens S7-1500, S7-1200, S7-300 and S7-400** PLCs over
the network, with variables named the way your PLC project names them.

## What you get

- **Live variables** — data blocks, memory (M), inputs (I) and outputs (Q),
  each a channel named after the PLC variable: `DB_Process.Temperature`
  reads as `/s7/plc1/DB_Process/Temperature`.
- **Names from your project** — point the input at the files you export from
  TIA Portal or STEP 7, and every variable is listed with its type and
  comment. No addresses to type.
- **Typed values** — REAL, INT, DINT, BOOL bits, STRING, DTL and
  DATE_AND_TIME, and the rest of the elementary types, decoded as the PLC
  holds them. BOOLs show as on/off.
- **The CPU's state** — `cpu/run` (RUN or STOP) and `cpu/mode`, once a
  second. A CPU in STOP still answers: its values keep landing, and the run
  channel says it is stopped.
- **Setpoints** — only on variables you mark writable. Every write is read
  back, so you see what the PLC holds.
- **Honest gaps** — a variable the PLC refuses is never shown as zero. The
  refusal is stated once, with what to change.
- **PROFINET devices** — read through the PLC that controls them (see
  *PROFINET devices* below).

## Before you start

1. **The PLC's address.** S7 talks on TCP port **102**; the computer running
   Phloem must reach it.
2. **S7-1200 / S7-1500: allow access.** In TIA Portal, open the CPU's
   properties → **Protection & Security** → **Connection mechanisms** and tick
   **Permit access with PUT/GET communication from remote partner**. Download
   the hardware configuration.
3. **S7-1200 / S7-1500: data blocks with standard access.** A data block
   with **Optimized block access** has no fixed addresses, so it cannot be
   read this way. For each data block you want, open its properties →
   **Attributes**, clear **Optimized block access**, compile and download. (Or
   read optimized blocks through the CPU's OPC UA server — see below.)
4. **Export the variables** (recommended):
   - Data blocks and PLC data types: select them in the project tree →
     right-click → **Generate source from blocks** (`.db`, `.udt`), or export
     them as XML.
   - PLC tags: **PLC tags** → **Export** (`.xlsx`).
   - STEP 7 Classic: Symbol Editor → **Export** (`.sdf`, `.asc`, `.seq`), and
     **Generate source** for data blocks (`.awl`).
   Put the files in one folder.

## Set up

1. In **Inputs**, add **Siemens S7** and fill in:
   - **Device name** — a short label, e.g. `plc1`.
   - **PLC address** — `192.168.0.10`.
   - **PLC export** — the folder (or one file) you exported.
   - Under **Connection**: **Rack** and **Slot** — S7-1500 and S7-1200:
     rack 0, slot 1. S7-300: rack 0, slot 2. S7-400: the CPU's rack and slot.
2. Press **Test connection**. It names the CPU that answered, its mode, and
   reads every variable once, listing any the PLC refused and why.
3. Tick the variables you want in the **Variables** table. Every variable in
   the export is listed with its address and type.
4. Channels appear on **Live**. Writable variables are available in
   **Trellis**.

### Without an export

Type variables into **Variables**, one per line:

```
DB1.DBD4:Real
level=IW64:Int
M10.3
DB2.DBB10:String[20]
DB3.DBB0:DTL
```

An address alone takes the type its width implies (`X` bit → BOOL, `B` →
BYTE, `W` → WORD, `D` → DWORD); add `:type` for anything else. `name=` gives
it a channel name. German mnemonics (`E`, `A`) and the `%` prefix work.

### Data block numbers

A TIA source file names a data block but not its number. If the export is
`.db` sources, give each block its number under **Connection** → **Data block
numbers**: `DB_Process=10, DB_Line=4`. XML exports and STEP 7 sources carry
their numbers.

## Reference

### Writes

List writable variables in **Writable variables**:
`Valve_Open, DB_Process.SetPoint`. A value is checked against the variable's
type before it is sent — out of range, the wrong type, or a string too long is
refused, never cut short. Inputs (I) cannot be written: the CPU overwrites
them every cycle.

### Dead-man

If this computer stops, an output keeps its last value. A **dead-man** lets
the PLC take over: while a test setup is deployed, Phloem keeps changing a
**heartbeat** variable, and your PLC programme drives outputs safe when it
stops changing.

Your PLC needs a few lines of logic:

- The heartbeat is one INT, WORD or DINT in a data block or memory.
- **`0` means "not supervised"**: do nothing while it reads `0`.
- Any other value must **change** at least once per timeout. If it does not,
  drive the outputs to their safe states.

Then fill in the **Dead-man** section: the **Heartbeat variable**, the
**PLC timeout** you set in the programme, and **Outputs the PLC drives safe**
with their safe values, `Valve_Open=false, DB_Process.SetPoint=0`. When a
setup is undeployed, Phloem writes `0`.

### Timing

Each value is stamped with this computer's clock at the middle of the read
that fetched it. Variables are read in as few requests as the CPU allows (the
CPU states how large a request may be when the connection opens); many
variables in one data block are read together.

### Connections

Each input uses one of the CPU's connection resources for its live values,
and one more while you test or write. If the CPU has none free (HMIs
and engineering stations use them too), the connection is refused and the
input says so.

## PROFINET devices

PROFINET I/O devices — drives, remote I/O, valve terminals, sensors — are
read through the PLC that controls them. The PLC copies each device's data
into its process image every cycle, so:

- **Through this connector** — the device's inputs and outputs are the PLC's
  **I** and **Q** addresses assigned to it in TIA Portal (device view →
  **I address** / **Q address**). Export the PLC tags, or type the addresses
  (`IW256:Int`). For values the programme scales or a drive's status words,
  read the data block the programme writes them to.
- **Through OPC UA (S7-1500)** — enable the CPU's OPC UA server (CPU
  properties → **OPC UA** → **Server**), and use the **OPC UA** connector.
  Optimized data blocks, structures and arrays are readable there; use
  `nsu=http://www.siemens.com/simatic-s7-opcua;s="DB_Process"."Temperature"`
  so the node does not depend on the namespace's position on the server.

Phloem does not speak PROFINET to the devices directly: a device has exactly
one controller, and that is your PLC.

## What has not run on real hardware

| | |
|---|---|
| A real S7-1500, S7-1200, S7-300 or S7-400 CPU | tested against a protocol-accurate simulated CPU only |
| Export files from TIA Portal and STEP 7 | tested with files written to the published formats, not files exported by TIA Portal |
| The offsets of standard data blocks | computed by the published layout rules; not yet compared with a CPU |

## Troubleshooting

| You see | Meaning and fix |
|---|---|
| "PUT/GET access switched off" | Tick **Permit access with PUT/GET communication** (see *Before you start*) and download the hardware configuration. |
| "uses optimized block access" | Clear **Optimized block access** on that data block, or read it through OPC UA. |
| "address out of range" | The data block is shorter than the export says — the export is older than the PLC programme. Export again. |
| "no free connection resources" / "closed the connection during the handshake" | Every connection the CPU allows is in use. Close an engineering or HMI connection, or raise the CPU's connection limit. |
| "rack/slot … is not this CPU" | Check **Rack** and **Slot**. |
| "has no number in this export" | Add the block under **Data block numbers**, or export it as XML. |
| `cpu/run` is off | The CPU is in STOP. Values still read; they stop changing. |

## Supports

- Siemens S7-1500, S7-1200, S7-300 and S7-400 CPUs over ISO-on-TCP (port 102)
- Data blocks, memory, inputs and outputs, named from a TIA Portal or STEP 7 export
- PROFINET devices through the S7 PLC that controls them

## Connection fields

| Field | Type | Required | Notes |
|---|---|---|---|
| Device name | Text | Yes |  |
| PLC address | Text | Yes |  |
| PLC export | Path | No |  |
| Variables | Text | No |  |
| Writable variables | Text | No |  |
| Poll every (ms) | Number | No |  |
| Rack | Number | No |  |
| Slot | Number | No |  |
| Connection type | One of | No |  |
| Timeout (ms) | Number | No |  |
| Data block numbers | Text | No |  |
| Heartbeat variable | Text | No |  |
| PLC timeout (s) | Number | No |  |
| Outputs the PLC drives safe | Text | No |  |
