Resolving Modbus TCP Redundancy Detection Delays on Siemens S7 PLCs
When a Siemens S7 acts as a Modbus TCP server and a redundant link to a Modbus TCP client fails, the CPU does not report the broken connection for at least one TCP keep-alive interval. This article explains the root cause of dual-link status errors observed on redundant Modbus TCP configurations (e.g. S7-300 with CP343-1 / CP342-5 plus Modbus PN library, or S7-1200/1500 with MB_RED_CLIENT/MB_RED_SERVER) and provides a field-proven procedure to detect link failure in seconds rather than the default 30+ seconds.
1. Problem Description
Symptom observed in the field:
- Two physically separate Ethernet lines connect an S7 to a Modbus TCP partner (HMI panel or third-party SCADA) for redundancy.
- When the first fiber-optic line is disconnected, the S7 still reports both lines connected.
- When the second line is disconnected, the S7 finally reports both lines disconnected.
- No IP address conflict exists; ARP and ping remain healthy until the cable is removed.
- When the S7 is replaced by the HMI as the Modbus client (cyclical polling), the broken connection is recognized within the next request cycle - typically under 1 second.
The two-line status word never enters the expected asymmetric state (one line OK, one line down). The S7 server reports both OK or both DOWN, with a long, deterministic latency between the events.
2. Root Cause Analysis
The anomaly is caused by TCP keep-alive handling on the S7 server side, not by the physical layer. A Modbus TCP server that uses the legacy MODBUSPN library block only inspects the TCP socket state when a request arrives or when the operating-system-level keep-alive probe sequence completes. With the Siemens CP firmware defaults, the first keep-alive probe is sent 30 seconds after the socket goes idle. If the panel never sends a request on the failed path during that interval, the server has no application-layer reason to mark the link as broken.
When the panel is a Modbus client that polls cyclically (for example, every 250 ms), the failed link produces either a connect error or a timeout on the next poll, so the status flips immediately. When the S7 is the server, only the TCP stack notices the failure - and only after the keep-alive timer elapses.
Three contributing factors:
- Server-side detection latency. The Modbus PN block does not implement an application-level heartbeat for redundant connections; it relies on the TCP keep-alive interval.
- Default keep-alive interval. The Siemens CP uses a default keep-alive time (TKA) of 30 s, with up to 3 retransmissions before the socket is declared dead.
- Asymmetric client/server roles. Detection speed depends on which node initiates traffic. A client with cyclic polling will surface errors quickly; an idle server will not.
3. Affected Products and Firmware
| Component | Catalog Number | Function | Notes |
|---|---|---|---|
| CP 342-5 | 6GK7 342-5DA02-0XE0 | PROFIBUS CP for S7-300 | Profibus master/slave; not a direct Modbus TCP device, but used in mixed configurations |
| CP 343-1 Lean | 6GK7 343-1CX10-0XE0 | Industrial Ethernet CP for S7-300 | Modbus TCP via MODBUSPN library, firmware >= V2.0 |
| CP 343-1 | 6GK7 343-1EX30-0XE0 | Industrial Ethernet CP for S7-300 | Default keep-alive ~30 s |
| S7-1200 CPU | 6ES7 2xx-1xxx | PROFINET on-board interface | Firmware V4.x supports MB_RED_CLIENT / MB_RED_SERVER blocks |
| S7-1500 CPU | 6ES7 5xx-1xxx | PROFINET on-board interface | Native Modbus TCP redundancy via TIA Portal V15.1+ |
| MODBUSPN library | Siemens MODBUS PN block library | Modbus TCP server/client on S7-1200/1500 with CP | Legacy; keep-alive behaviour dependent on CP configuration |
| MB_RED_CLIENT / MB_RED_SERVER | TIA Portal instruction set | Redundant Modbus TCP for S7-1500 | Redundant connection status with sub-second polling |
4. TCP Keep-Alive Mechanics in Siemens CPs
The TCP keep-alive mechanism used by Siemens Industrial Ethernet CPs is configurable in the STEP 7 / TIA Portal connection properties. Three timers control detection:
| Parameter | Default | Range | Description |
|---|---|---|---|
| Keep-alive time (TKA) | 30 s | 1 - 65535 s | Idle time after which the first keep-alive probe is sent |
| Keep-alive retries | 3 | 1 - 255 | Number of unanswered probes before socket reset |
| Keep-alive interval (TKP) | 5 s | 1 - 65535 s | Time between successive probes |
Total worst-case detection time = TKA + (retries x TKP). With defaults: 30 + (3 x 5) = 45 s. The same relationship applies to RFC 1122 keep-alive (the IETF RFC 1122 specification describes the keep-alive mechanism) and to the Modbus TCP frame handling described in the ProSoft Technology Modbus TCP/IP introduction (CRC handling, port 502, transaction ID pairings).
5. Solution: Three Independent Paths
Pick one or combine, depending on hardware generation.
5.1 Path A - Use MB_RED_CLIENT or MB_RED_SERVER (S7-1500)
The MB_RED_CLIENT instruction is designed for redundant Modbus TCP communication over PROFINET. It actively polls the redundant partner and reports link health based on the most recent job, not on TCP keep-alive alone. A Modbus TCP server is addressed using its IP address, so the MB_UNIT_ID parameter is not used in pure TCP addressing.
- Open the S7-1500 program in TIA Portal V15.1 or later.
- Insert
MB_RED_CLIENT(server role) orMB_RED_SERVERfrom the Communication > Modbus TCP folder. - Wire both connection IDs (primary and secondary) to the redundant partners.
- Read the
STATUSoutput - it returns the redundant link state directly, updated on every cyclic job. - Confirm the
MB_HOLD_REGbuffer is dimensioned for the maximum data length used by the slowest path.
5.2 Path B - Shorten TCP Keep-Alive on the CP
For S7-300 with CP 343-1 in STEP 7 V5.5 or TIA Portal:
- Open HW Config and select the CP 343-1.
- Open Properties > Options > Keep-Alive (or in TIA Portal: Properties > Ethernet interface > TCP connection properties > Extended).
- Set the keep-alive time to 5 seconds, keep-alive retries to 3, and keep-alive interval to 2 seconds.
- Compile and download the hardware configuration.
- Cycle power on the CP to ensure the new keep-alive parameters are loaded into the CP firmware (CP 343-1 Lean and CP 343-1 require power-cycle for non-volatile keep-alive changes).
Resulting worst-case detection: 5 + (3 x 2) = 11 seconds - acceptable for most redundant paths and a 4x improvement over the default.
5.3 Path C - Add an Application-Layer Heartbeat
When neither MB_RED_* nor shorter keep-alive is acceptable, implement a heartbeat register that the S7 writes cyclically and the partner reads each cycle:
// S7-1500 SCL - Heartbeat server (S7 acts as Modbus TCP server)
"Heartbeat" := "Heartbeat" + 1;
IF "Heartbeat" > 16#FFFF THEN "Heartbeat" := 0; END_IF;
// Cycle time: 100 ms in OB1 or a cyclic interrupt OB
The HMI/SCADA client reads register 0x0001 each poll. If the value does not change within 3 x poll interval, the link is flagged broken by the client. The two-line redundancy is then resolved on the client side using the heartbeat error of each path.
6. Hardware Configuration Considerations
The original configuration used one CP per CPU. With S7-300 and CP 342-5 (PROFIBUS), Modbus TCP must terminate on the on-board PROFINET interface of the CPU or on a separate CP 343-1. The CP 342-5 itself is a PROFIBUS DP/PA module and does not handle Modbus TCP. Verify:
- The Modbus TCP connection terminates on the PROFINET interface of the CPU or on a CP 343-1 - not on a CP 342-5.
- Both Ethernet lines are connected to independent physical interfaces (CPU PN + CP 343-1, or two CP 343-1 modules) so the redundancy is hardware-diverse, not just cable-diverse.
- Switch ports used by the redundant path are configured with no STP or RSTP fast to avoid spanning-tree blocking a port that is still active.
- The CP firmware version supports keep-alive parameter changes. CP 343-1 Lean (6GK7 343-1CX10-0XE0) firmware V2.0 and later support configurable keep-alive; older V1.x firmware uses fixed 30 s keep-alive.
7. Verification Procedure
After applying any of the three solutions, perform the following field test:
- Bring up the redundant Modbus TCP link and confirm cyclic data exchange on both lines.
- Note the current status of the redundant block (e.g.
STATUS= 16#0000 = OK, primary active). - Pull the fiber-optic connector on line 1 (primary).
- Start a stopwatch and read the redundant block
STATUScontinuously. - Record the elapsed time until the status reflects line 1 down and line 2 OK.
- Restore line 1, wait for the status to return to primary OK.
- Pull line 2 and repeat the measurement.
- Accept the system if both line-fail detections occur within the agreed detection time (typically < 5 s for substation and process automation, < 1 s for motion-critical loops).
8. Troubleshooting Matrix
| Observed Symptom | Likely Cause | Diagnostic | Fix |
|---|---|---|---|
| Status flips only after the second cable is pulled | Default 30 s TCP keep-alive on server side | Check CP keep-alive parameters; measure flip time | Reduce keep-alive to 5 s or use MB_RED_* |
| Both lines report OK after one line is down | Spanning-tree blocking the second port | Check switch port state via web interface | Enable RSTP fast or disable STP on the port |
| Status flaps every ~45 s | Keep-alive probes blocked by firewall or ACL | Wireshark capture on CP; look for SYN retransmissions | Open TCP port 502 in both directions; allow keep-alive probes |
| No status update at all on HMI | HMI configured as server, not client | Inspect HMI Modbus driver role setting | Switch HMI to Modbus client with cyclic polling |
| IP conflict warning in diagnostic buffer | Duplicate IP between primary and backup line | Issue arp -a from maintenance laptop |
Reassign unique IP per physical interface |
| Status OK with both lines physically down | Application-level heartbeat missing and idle socket | Confirm keep-alive is enabled in CP | Enable keep-alive with TKA < 10 s |
9. Field-Proven Best Practices
- Always enable TCP keep-alive on the S7 server side. Disabled keep-alive plus an idle socket equals a silent link failure that is only discovered at the next unsolicited reconnect.
- Set keep-alive to 5 s / 3 retries / 2 s interval on the CP. This gives 11 s worst-case detection and aligns with the typical 10 s watchdog timeout used in SCADA masters.
- When using MB_RED_CLIENT, dimension the cycle time to be at least 4x faster than the required detection time to absorb jitter from the slowest path.
- Use independent physical interfaces for redundancy. Two ports on the same switch is single-point-of-failure.
- Add a heartbeat register even when keep-alive is enabled. It is a cheap second line of defense and surfaces application-layer stalls that keep-alive cannot detect.
- Document the keep-alive and detection-time values in the system P&ID and the network architecture drawing. Detection time is a contractual SLA parameter in many process plants.
- Capture a Wireshark trace at commissioning to confirm that TCP keep-alive probes are visible. If they are not, the keep-alive is silently disabled at the OS or CP firmware level.
10. Related Siemens Documentation
- MB_RED_CLIENT - Redundant Communication over PROFINET as a Modbus TCP Client (Siemens TIA Portal Manual Collection)
- Introduction to Modbus TCP/IP (ProSoft Technology KB)
- RFC 1122 - Requirements for Internet Hosts - Communication Layers (IETF)
- SIMATIC S7-300 CP 343-1 Lean manual, entry ID 24458641 (Siemens Industry Online Support)
- SIMATIC S7-1200 Programmable Controller, System Manual, entry ID 91696622 (Siemens Industry Online Support)
Why does the S7 server not detect a broken Modbus TCP line for 30 seconds?
The S7 relies on the OS-level TCP keep-alive mechanism. Siemens CP firmware defaults to a 30 s keep-alive time and 3 retransmissions at 5 s intervals, giving a 45 s worst-case detection window. During that interval, the server has no application-layer reason to close the socket.
Why does the HMI as a client detect the failure in under 1 second?
The HMI acts as a Modbus client and polls cyclically. The next poll on the failed path returns a connect error or a timeout, and the HMI updates its link status immediately - typically within the poll cycle (100 ms to 1 s).
Can the CP 342-5 handle Modbus TCP directly?
No. The CP 342-5 is a PROFIBUS DP/PA communication processor. Modbus TCP must terminate on the on-board PROFINET interface of the S7-300 CPU or on a separate CP 343-1. Mixing CP 342-5 with Modbus TCP requires the Modbus PN library on a PROFINET-capable module.
What keep-alive values should I configure on a Siemens CP?
For redundant paths where sub-15 s detection is required, configure keep-alive time to 5 s, keep-alive retries to 3, and keep-alive interval to 2 s. This gives a worst-case detection of 11 s. The values must be written to non-volatile memory and the CP must be power-cycled for the change to take effect on CP 343-1 Lean and CP 343-1.
Is MB_RED_CLIENT supported on S7-1200 and S7-1500?
MB_RED_CLIENT and MB_RED_SERVER are available on S7-1500 with TIA Portal V15.1 and later. S7-1200 supports the Modbus TCP server/client blocks (MB_SERVER / MB_CLIENT) but the dedicated redundant variants are S7-1500-only. For S7-1200 redundancy, shorten keep-alive or implement an application-layer heartbeat.