# Beckhoff TwinCAT (ADS)

Read and write TwinCAT PLC variables by name

- 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 the variables of a running TwinCAT PLC by name —
`MAIN.fbPump.rSpeed`, `GVL.rPressure` — over ADS, Beckhoff's own protocol.
The PLC sends each value when it changes, stamped by the PLC's clock.

## What you get

- **Every variable the PLC publishes**, listed by name with its type. Pick
  single variables, or a structure, function block, array or whole program
  (`MAIN`, `GVL`) to take all its members.
- **Values when they change** (or every cycle), sent by the PLC itself as
  ADS device notifications and stamped with the PLC's time.
- **Members kept together.** A member of a structure or function block says
  which variable it belongs to, so its siblings can be shown as one.
- **Writes with read-back.** Variables you name as writable can be set from
  Phloem; each write is read back from the PLC and refused while the PLC is
  not running.
- **The PLC's run state** (`run`, `stop`, `config`, …) as a channel.
- **Online changes followed.** A download or online change is noticed, the
  variables are found again and the stream carries on — no restart.

## Before you start

- The PLC runs TwinCAT 3 (any build) or TwinCAT 2, with its PLC project
  started.
- This computer can reach the PLC on **TCP 48898** (and UDP 48899 to look up
  its AMS Net ID or add a route).
- **A route on the PLC for this computer.** TwinCAT only answers computers it
  has a route for. Either:
  - add one in TwinCAT: *System › Routes › Add Route*, with this computer's
    IP address and AMS Net ID (its IP address followed by `.1.1`, unless
    you set another under *This computer's AMS Net ID*); or
  - fill in **PLC user** and **PLC password** (the PLC's Windows or
    TwinCAT/BSD account). The connector then adds the route itself the first
    time the PLC refuses it.

## Set up

1. **Device name** — a short name for this PLC; it begins every channel name.
2. **PLC address** — its IP address or host name.
3. **PLC AMS Net ID** — leave blank to ask the PLC; fill it in if the PLC is
   behind a firewall that blocks UDP 48899.
4. **Runtime** — which PLC runtime: 851 is the first TwinCAT 3 PLC, 801 the
   first TwinCAT 2 PLC.
5. Tick the **variables** to record. Ticking a structure's members, or typing
   a structure's name, records all its members.

Channels are named `/ads/<device>/<variable>`, for example
`/ads/plc1/MAIN.fbPump.stMotor.rCurrent` and `/ads/plc1/GVL.aTemp[3]`.

## Reference

### Reading

| Read | What happens |
|---|---|
| When a value changes (default) | The PLC checks every cycle and sends what changed. A value that stands still is sent once. |
| Every cycle | The PLC sends every value every cycle. |
| Polled | This computer asks for every value every cycle. Stamped by this computer's clock. |

**Cycle** is how often the PLC checks (or this computer asks), 1 ms to 60 s;
10 ms by default. The PLC cannot check faster than its own task cycle.

If the PLC refuses device notifications for some variables, those are polled
instead, and the connector says so.

### Types

| PLC type | Channel |
|---|---|
| `BOOL` | an on/off state |
| `SINT` … `ULINT`, `BYTE` … `LWORD` | a whole number |
| `REAL`, `LREAL` | a number (a non-number such as NaN is recorded as missing) |
| `TIME`, `TOD` | a whole number in ms |
| `LTIME` | a whole number in ns |
| `DATE`, `DT` | a whole number in s since 1970 |
| `STRING`, `WSTRING` | text |
| enumerations, aliases | their base type |
| structures, function blocks, arrays | their members, one channel each |

Not recorded, and listed as not readable: pointers and references,
properties, interfaces, and `BIT` members.

### Timing

Notified values carry the PLC's own timestamp. Polled values carry this
computer's time at the middle of the request. Keep the PLC's clock set (for
example by NTP) for notified values to line up with other sources.

### Writing

List the variables that may be written under **Writing › Writable variables**.
Each write:

- is refused unless the variable is listed, and refused if the PLC marks it
  read-only;
- is checked against the variable's type and range (a `USINT` takes 0–255, a
  `STRING(20)` up to 20 characters);
- is refused while the PLC is not in RUN;
- is read back from the PLC, and the value read is what Phloem shows.

### Safety

**Safe values** (`MAIN.bValve=false, MAIN.rSetpoint=0`) are what the outputs
should go to when a test is stopped. The *Write safe values* action writes
them all, in order, even while the PLC is stopped.

**Dead-man.** If your PLC programme watches a heartbeat variable — and drives
its outputs to the safe values itself when the heartbeat stops changing —
name it under **Dead-man**, with the timeout your PLC programme uses. Phloem
then changes the heartbeat while a supervised test runs. The heartbeat must be
a 16- to 64-bit integer, must not be among the writable variables, and `0`
means nobody is supervising. The timeout and the safe behaviour live in your
PLC programme; this setting only says what they are.

## What has not been tested on a real PLC

| Feature | Status |
|---|---|
| Every feature on this page | Tested against a simulated TwinCAT PLC that follows Beckhoff's ADS specification; not yet run against a real TwinCAT runtime. |
| Adding a route | Follows Beckhoff's own route request; not yet run against a real TwinCAT router. |
| TwinCAT 2 | Untested. |

## Troubleshooting

| Message | What to do |
|---|---|
| *closed the connection on its first request — it has no route for this computer* | Add a route on the PLC (see *Before you start*), or fill in the PLC user and password. |
| *refused to add one* | The PLC user or password is wrong, or the account may not add routes. |
| *no PLC runtime answers on AMS port 851* | Start the PLC project, or pick the runtime it runs on. |
| *did not say its AMS Net ID* | Fill in the PLC AMS Net ID (TwinCAT shows it under *System › Routes*). |
| *the PLC is in STOP — writes wait until it runs* | Start the PLC. |
| *not among this input's writable variables* | Add it under *Writing › Writable variables*. |
| A variable is listed as not found | The PLC project no longer has it, or it is spelled differently. Names are not case-sensitive. |
| Another program's ADS connection drops when this input starts | TwinCAT keeps one connection per computer: give this computer a second AMS Net ID (and a route for it) under *This computer's AMS Net ID*. |

## Supports

- Beckhoff TwinCAT 3 and TwinCAT 2 PLC runtimes over ADS (TCP 48898)
- Named variables from the PLC's symbol table — structures, function blocks and arrays read as their members
- Device notifications on change or every cycle, stamped by the PLC; polled sum reads where notifications are refused
- Writes to declared variables, read back, refused while the PLC is not running

## Connection fields

| Field | Type | Required | Notes |
|---|---|---|---|
| Device name | Text | Yes |  |
| PLC address | Text | Yes |  |
| PLC AMS Net ID | Text | No |  |
| Runtime | One of | No |  |
| Variables | Text | No |  |
| Read | One of | No |  |
| Cycle (ms) | Number | No |  |
| Writable variables | Text | No |  |
| Safe values | Text | No |  |
| Heartbeat variable | Text | No |  |
| PLC timeout (s) | Number | No |  |
| This computer's AMS Net ID | Text | No |  |
| Route service port (UDP) | Number | No |  |
| PLC user (to add a route) | Text | No |  |
| PLC password (to add a route) | Secret | No |  |
