IntegrationNode-RED Modbus Example
Handle Modbus Exceptions and Timeouts Cleanly
Modbus communication fails — devices go offline, CRC errors happen, exception responses arrive. This example adds a catch node and a state machine to distinguish between a Modbus exception (device replied with an error) and a timeout (device did not reply), and handles each gracefully.
What you need
- Node-RED v3+
- node-red-contrib-modbus installed
- Any Modbus device (intentionally power it off to test timeout handling)
Flow Overview
┌──────────────────────────────┐
│ Modbus Flex Getter │
Poll 5s ────────────▶│ port 1: data (success) │──▶ fn Success ──▶ Debug
│ port 2: error (fail/timeout) │──▶ fn Classify ──▶ Debug
└──────────────────────────────┘
Port 2 receives:
· Exception response → msg.error.message contains "Exception 0x02"
· Timeout → msg.error.message contains "Timed out"
· Connection error → msg.error.message contains "ECONNREFUSED"
Node-RED Flow JSON
To import: open Node-RED → Hamburger menu → Import → paste this JSON → Deploy.
Expected Output
// On success: msg.payload = 1234
// On exception: msg.payload = { error:'exception', msg:'Modbus exception 0x02' }
// On timeout: msg.payload = { error:'timeout', msg:'Timed out after 3000ms' }Common Gotchas
- modbus-flex-getter has TWO output ports — wire both: port 1 for data, port 2 for errors. Wiring only port 1 means errors are silently dropped.
- Timeouts and exceptions come through port 2; do not use a catch node as the primary error handler.
- Increase the timeout for slow RTU-to-TCP gateways or long RS-485 cables (3000–5000 ms is typical).
- An ECONNREFUSED error means the TCP connection was refused — check IP, port, and firewall rules.
- If a device consistently returns exception 02 on a specific address, the datasheet address is likely 1-based — subtract 1.
Same in ModPackQT — in 30 seconds
ModPackQT shows Modbus exception codes and timeout events in the console log with timestamps and decoded meanings — no custom error wiring required. Visit the Troubleshooting guide for the full list of exception codes and fixes.
Was this example helpful?
More Integration examples