# PLC-HMI Integration Patterns

Use this P3 reference for practical PLC/HMI linking: address allocation, command/status separation, screen switching, alarms, parameter writes, and commissioning tables.

Source anchors:

- TouchWin manual PDF page 38: PLC-register-controlled screen change and current screen report.
- TouchWin manual PDF pages 75-77: HMI button/indicator-button objects and warning about PLC direct `OUT` to the same bit operated by HMI.
- TouchWin manual PDF pages 82-88: data display/input, scaling, and HMI writes to lower-device registers.
- XDPPro software manual PDF pages 130-132: data monitoring can modify coils/registers; use cautiously during commissioning.
- `soft-elements-and-special-relays.md` and `ladder-basic-instructions.md` for PLC soft-element and ladder rules.

## Principle

Separate these four things:

| Type | Example | Owner |
|---|---|---|
| Command request | `M100` HMI start request | HMI writes, PLC consumes. |
| Internal state | `M0` motor run latch | PLC owns. |
| Physical output | `Y0` motor contactor | PLC owns. |
| Display status | `M200` motor running display, `D200` speed feedback | PLC writes, HMI reads. |

Do not let HMI directly own a physical output or a PLC coil that is also driven by normal scan logic.

## Address Allocation Template

| Range | Purpose | Example |
|---|---|---|
| `M100~M199` | HMI command requests | start, stop, reset, jog. |
| `M200~M299` | PLC status to HMI | running, ready, fault, auto/manual. |
| `M300~M399` | HMI page/permission/maintenance commands | page request, login state. |
| `M500~M599` | Alarm active bits | motor overload, sensor timeout. |
| `D100~D199` | HMI numeric setpoints | speed, time, count. |
| `D200~D299` | PLC feedback to HMI | actual speed, cycle count, position. |
| `D300~D349` | HMI screen/interaction registers | screen command/current screen. |
| `D500~D599` | Diagnostics/error codes | communication result, step number. |

Adjust ranges to the actual CPU model and project size.

## Command / Acknowledge Handshake

Use when HMI requests a one-shot PLC action.

| Address | Direction | Purpose |
|---|---|---|
| `M100` | HMI -> PLC | Start request. |
| `M101` | PLC -> HMI | Start accepted/ack. |
| `M102` | PLC -> HMI | Start rejected/fault reason available. |
| `D500` | PLC -> HMI | Reject/error code. |

Sequence:

1. HMI sets `M100`.
2. PLC detects rising edge of `M100`.
3. PLC checks permissives.
4. If accepted, PLC sets run latch and pulses/sets `M101`.
5. If rejected, PLC sets `M102` and writes `D500`.
6. HMI or PLC clears `M100` after acknowledgement.

For simple buttons, a momentary bit plus PLC edge detection is often enough.

## PLC-Controlled HMI Screen

Recommended registers:

| Address | Purpose |
|---|---|
| `D300` | Screen command register. |
| `D301` | Current screen report register. |

Rules:

- HMI `交互 -> 控制画面变换` reads `D300`.
- HMI clears `D300` to `0` after jump.
- HMI `报告当前画面号` writes `D301`.
- PLC writes `D300` only on event/edge.

Example:

| Event | PLC action |
|---|---|
| Alarm requires operator attention | `MOV K20 D300` to jump to alarm page 20. |
| Maintenance mode entered | `MOV K30 D300` to maintenance page 30. |
| Current page confirmation | Monitor `D301 = K20/K30`. |

## HMI Parameter Write

For operator setpoints:

1. HMI writes raw setpoint to `D100`.
2. PLC range-checks `D100`.
3. PLC copies valid value to internal working register `D110`.
4. PLC rejects invalid value by setting alarm bit and writing reason code.

Pattern:

| Address | Purpose |
|---|---|
| `D100` | HMI input setpoint. |
| `D101` | HMI input minimum or recipe value. |
| `D110` | PLC accepted setpoint. |
| `M510` | Setpoint invalid alarm. |
| `D510` | Invalid reason code. |

Do not use HMI-entered values directly for motion/heating/pressure until PLC validates the range.

## Alarm Integration

PLC owns alarm truth; HMI displays it.

| PLC bit | HMI message |
|---|---|
| `M500` | Emergency stop active. |
| `M501` | Motor overload. |
| `M502` | Sensor timeout. |
| `M503` | Communication fault. |

HMI reset button should write a request bit such as `M120`, not clear alarm-active bits directly. PLC clears alarms only after the condition is safe and reset is valid.

## Debug Page Template

Include a hidden/maintenance screen with:

- PLC RUN/system status: `SM000`, fault bits.
- Current screen report: `D301`.
- HMI communication counters: relevant `PSW` counters if used.
- Command bits `M100~M199`.
- Status bits `M200~M299`.
- Alarm bits `M500~M599`.
- Key setpoints and feedback registers.

Protect this page with password/permission where needed.

## Commissioning Checklist

- Confirm PLC and HMI use the same station/protocol/address notation.
- Test HMI buttons with PLC in safe manual mode.
- Confirm PLC does not `OUT` to HMI-operated command bits.
- Confirm HMI displays PLC-owned status bits, not only command bits.
- Test invalid setpoint rejection.
- Test alarm trigger, display, acknowledge, reset, and history.
- Test page switching command and current-screen report.
- Save the address table with the project.
