— Understanding & Handling Errors

(Module 4 · Modbus Data Model & Function Codes)


Learning objectives

By the end of this chapter you will be able to …

  1. Describe the exact PDU structure of a Modbus exception frame (serial & TCP).
  2. Identify every public exception code (01 – 0B) and map it to its most likely field root-causes.
  3. Design client-side logic that backs off, retries, or escalates correctly—without flooding the bus.
  4. Instrument servers so they emit meaningful exceptions instead of silent time-outs.
  5. Diagnose hard-to-find issues (off-by-one, illegal value, device busy) with a repeatable, five-step workflow.

13.1 What is an exception response?

A Modbus server that cannot honour a request must answer anyway—it flips the MSB of the Function Code and appends a one-byte Exception Code.
An exception frame is therefore a deterministic, bandwidth-cheap error report—not a timeout.

Normal responseException response
FC = 0x03FC = 0x83 (0x80 + original FC)
Data bytes1 byte Exception Code

(Timing on wire identical; size only 5 B RTU / 9 B TCP.)


13.2 Byte-level anatomy

13.2.1 RTU (serial)

 Addr | FC+0x80 | ExCode | CRClo | CRChi

13.2.2 TCP (MBAP)

Transaction-ID | Protocol-ID | Length=0x0003 | Unit-ID | FC+0x80 | ExCode

Inspection tricktcp.len == 3 AND modbus.function_code >= 0x80 → Wireshark filter for exceptions only.


13.3 Exception code reference

CodeHexDescriptionMost common real-world triggersServer-side fixClient-side action
010x01Illegal FunctionMaster polls FC 06 but slave is RO sensorImplement FC or document unsupportedDrop from poll list; log once
020x02Illegal Data AddressOffset beyond register map; wrong 0-/1-baseExpand map; correct datasheetCorrect address; retry immediately
030x03Illegal Data ValueQty = 0 or > spec; write value out-of-rangeValidate arguments betterClamp or display clear error
040x04Slave Device FailureSensor open-circuit; arithmetic faultWatchdog/reboot; raise alarmSlow retry with exponential back-off
050x05AcknowledgeLong internal movement; write accepted but not doneReturn when done insteadPoll same address until clear
060x06Slave Device BusyFlashing firmware; HMI config modeQueue or reject new jobsRetry after 1–5 s, stop after N=3
080x08Memory Parity ErrorEEPROM CRC failSpare memory bank / resetStop polling; manual intervention
0A0x0AGateway Path UnavailableRTU loop offlineAuto-heal RS-485, raise SNMP trapReroute or wait for path recovery
0B0x0BGateway Target Device Failed to RespondSlave power-off or address clashIsolate glitching nodeSkip node; alert maintenance

(Figure 13-1 placeholder: colourful cheat-sheet wheel of exception codes.)


13.4 Timeouts vs exceptions vs silent discard

PhenomenonWire evidenceLikely layerResolution
Exception5 / 9 B replyApplicationFix request parameters
TimeoutNo reply within “Response Timeout”Physical / linkCheck wiring, baud, address
Silent discardReply CRC bad (scope shows), master dropsCRC calc bug or noiseInspect bias, termination, CRC routine

Rule Always aim for exception > timeout. A device that never times out is infinitely easier to debug.


13.5 Best-practice client logic

on_response(frame):
    if frame.is_exception():
        code = frame.ex_code
        if code in {ACKNOWLEDGE, DEVICE_BUSY}:
            sleep(short_retry)
            retry_same_request()
        elif code in {ILLEGAL_FUNC, ILLEGAL_ADDR}:
            disable_poll(entry)
            alert_config_team()
        else:  # 04, 08, 0B …
            retry_count += 1
            if retry_count >= Nmax:
                raise_alarm(slave_id, code)
    else:
        retry_count = 0
        process_data(frame)

Short_retry = 250 ms for busy/ack; Nmax = 3 configurable.


13.6 Server-side guidelines

TopicRecommendation
Granular checksReturn Illegal Data Value for out-of-range instead of generic Device Failure.
*WatchdogIf ISR > 100 ms, reply Acknowledge then complete.
LoggingMirror exception frames to Syslog (ex=02 addr=0x1234 qty=10).
TestingFuzz tool: sweep FC + offsets outside map; confirm correct ex-code each case.

(Listing 1: C pseudo showing exception builder in slave firmware.)


13.7 RTU-to-TCP gateway subtleties

13.7.1 Timeout conversion

If RTU slave times out → gateway should forward 0B Gateway Target Failed to Respond to TCP master instead of letting TCP session stall.

13.7.2 Path unavailable

If RS-485 bus offline → gateway returns 0A to all Unit-IDs on that bus.

(Figure 13-2 placeholder: flow diagram of gateway translating serial timeout → exception.)


13.8 Testing & diagnostics workflow

  1. Capture traffic (serial-tap or Wireshark).
  2. Filter exceptions (modbus.exception_code).
  3. Correlate with SCADA error log timestamp.
  4. Cross-check slave diagnostics (LED, HMI status).
  5. Iterate: adjust timing, address, range; re-capture.

Lab 13-1: Download the sample .pcapng file and identify why Slave 17 returns code 02 at 12:47:05.


13.9 Exception-aware polling strategies

StrategyBandwidthRobustnessUse-case
Fast retry, skip on 3×MedGoodSmall loop < 10 slaves
Exponential back-offLowExcellentUnreliable radio links
Buddy system (pair read + health coil)HighExcellentSafety-critical / pumps
Adaptive (increase interval on repeated 05/06)Med–lowGoodDrives in auto-tune

13.10 Checklist before leaving site

  • All slaves respond to an intentional Illegal Addr test with code 02.
  • SCADA alarm list maps every ex-code to plain English.
  • Gateway converts serial timeouts to code 0B.
  • Client software caps retry bursts (≤ 1 req / 100 ms).
  • Syslog/SNMP traps enabled for non-zero exception rate > 0.1 % of polls.

Chapter recap

  • An exception frame is a built-in error packet—use it.
  • MSB set + 1-byte code—easy to parse, easy to filter.
  • Code→root-cause tables speed field repair dramatically.
  • Smart clients distinguish “Illegal address” (fix config) from “Device busy” (just wait).
  • Good gateways translate timeouts to 0B; good servers return the most specific code.

Assets to create

IDVisual / asset
Fig-13-1Exception-code cheat wheel
Fig-13-2Gateway timeout→exception flow
Listing 1C exception builder
Lab pcapCapture with 01, 02, 03 errors

Next up: Module 5 – Development & Implementation begins with Chapter 14 – Setting Up Your Modbus Lab: hardware shopping list, simulator walkthroughs, and serial-sniffer setups so you can practice everything you’ve learned.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Chapter 24 – Modbus in Action

— Six Field-Proven Case Studies across Industry (Module 8 · Real-World Applications & the Future of Modbus) Chapter goals See the protocol outside textbooks. We examine six production deployments—each in…