765  Modbus Function Codes and Exceptions

Complete reference for Modbus function codes, exception handling, and practical industrial applications

animation
modbus
industrial
protocols

765.1 Learning Objectives

By the end of this chapter, you will be able to:

  1. Identify and use all standard Modbus function codes
  2. Interpret exception responses and troubleshoot common errors
  3. Apply function codes to real-world industrial scenarios
  4. Select the appropriate function code for different operations

765.2 Introduction

Modbus function codes define the operations that can be performed on slave devices. Each function code has specific request and response formats, and understanding these is essential for effective industrial communication. This chapter provides an interactive reference for all standard function codes and exception handling.

Think of function codes like different commands you can give to a robot:

  • โ€œReadโ€ commands (01-04): Ask the robot to tell you something (temperature, status, etc.)
  • โ€œWriteโ€ commands (05-06, 15-16): Tell the robot to do something or change a setting

Each command has a number (the function code) so the robot knows exactly what you want!

765.3 Function Code Reference

765.4 Function Code Decision Tree

%% fig-alt: Decision tree for selecting appropriate Modbus function code based on operation type
%%{init: {'theme': 'base', 'themeVariables': {'primaryColor': '#2C3E50', 'primaryTextColor': '#fff', 'primaryBorderColor': '#16A085', 'lineColor': '#E67E22'}}}%%
flowchart TD
    START([Modbus Operation]) --> Q1{Read or<br/>Write?}

    Q1 -->|Read| Q2{Data Type?}
    Q1 -->|Write| Q3{Data Type?}

    Q2 -->|Bits| Q4{Coils or<br/>Inputs?}
    Q2 -->|Registers| Q5{Input or<br/>Holding?}

    Q4 -->|Coils 0x| FC01[/"FC 01<br/>Read Coils"/]
    Q4 -->|Discrete 1x| FC02[/"FC 02<br/>Read Discrete Inputs"/]

    Q5 -->|Input 3x| FC04[/"FC 04<br/>Read Input Registers"/]
    Q5 -->|Holding 4x| FC03[/"FC 03<br/>Read Holding Registers"/]

    Q3 -->|Bits Coils| Q6{How Many?}
    Q3 -->|Registers Holding| Q7{How Many?}

    Q6 -->|Single| FC05[/"FC 05<br/>Write Single Coil"/]
    Q6 -->|Multiple| FC15[/"FC 15 (0x0F)<br/>Write Multiple Coils"/]

    Q7 -->|Single| FC06[/"FC 06<br/>Write Single Register"/]
    Q7 -->|Multiple| FC16[/"FC 16 (0x10)<br/>Write Multiple Registers"/]

    FC01 --> RESULT([Execute + Response])
    FC02 --> RESULT
    FC03 --> RESULT
    FC04 --> RESULT
    FC05 --> RESULT
    FC06 --> RESULT
    FC15 --> RESULT
    FC16 --> RESULT

    style START fill:#2C3E50,color:#fff
    style RESULT fill:#16A085,color:#fff
    style FC01 fill:#3498DB,color:#fff
    style FC02 fill:#3498DB,color:#fff
    style FC03 fill:#3498DB,color:#fff
    style FC04 fill:#3498DB,color:#fff
    style FC05 fill:#E67E22,color:#fff
    style FC06 fill:#E67E22,color:#fff
    style FC15 fill:#E67E22,color:#fff
    style FC16 fill:#E67E22,color:#fff

765.5 Exception Codes

765.6 Practical Examples

765.6.1 Common Industrial Applications

TipTypical Modbus Use Cases

Temperature Monitoring (FC04 - Read Input Registers)

  • Read temperature from sensor at address 30001
  • Response: 0x0100 = 256 = 25.6 C (with scaling factor 0.1)

Motor Control (FC05 - Write Single Coil)

  • Start motor: Write 0xFF00 to coil 00001
  • Stop motor: Write 0x0000 to coil 00001

VFD Speed Setpoint (FC06 - Write Single Register)

  • Write speed to holding register 40001
  • Value 0x0BB8 = 3000 = 30.00 Hz (with scaling 0.01)

Batch Data Read (FC03 - Read Holding Registers)

  • Read 10 registers starting at 40100
  • Response includes 20 bytes of data (2 bytes per register)

765.6.2 Write Values for Coils

NoteSingle Coil Values (FC05)

When writing a single coil with function code 05, only two values are valid:

  • 0xFF00 = Turn coil ON
  • 0x0000 = Turn coil OFF

Any other value will result in an โ€œIllegal Data Valueโ€ exception (0x03).

765.6.3 Register Data Encoding

WarningData Scaling and Encoding

Most Modbus devices use scaling factors because registers only hold 16-bit integers:

Measurement Raw Value Scale Factor Actual Value
Temperature 0x0100 (256) 0.1 25.6 C
Pressure 0x03E8 (1000) 0.01 10.00 PSI
Voltage 0x15E0 (5600) 0.01 56.00 V

Always check the device documentation for scaling factors!

765.7 Knowledge Check

NoteQuick Quiz: Function Codes

Question 1: You need to read 10 temperature values from input registers. Which function code should you use?

  1. FC01 - Read Coils
  2. FC03 - Read Holding Registers
  3. FC04 - Read Input Registers
  4. FC06 - Write Single Register

c) FC04 - Read Input Registers - Input registers (3x) are used for read-only analog values like temperature readings. FC04 is the appropriate function code for reading these values.

NoteQuick Quiz: Exception Handling

Question 2: You receive a response with function code 0x83. What does this indicate?

  1. Successful read of holding registers
  2. An exception occurred during a FC03 operation
  3. A broadcast message to all slaves
  4. A write operation completed

b) An exception occurred during a FC03 operation - When bit 7 is set in the function code (0x83 = 0x03 | 0x80), it indicates an exception response. The original function code was 0x03 (Read Holding Registers), and the exception byte that follows will contain the specific error code.

765.8 Summary

  • 8 standard function codes: 4 for reading (01-04) and 4 for writing (05, 06, 0F, 10)
  • Read operations: Return byte count followed by data bytes
  • Write single: Response echoes the request (address + value)
  • Write multiple: Response contains address + quantity written
  • Exception format: Original FC + 0x80, followed by exception code
  • Common exceptions: Illegal function (01), Illegal address (02), Illegal value (03)

765.9 Whatโ€™s Next

Continue exploring Modbus with these related topics: