Modbus Protocol 3 – Function Codes (Part 1)

Function Codes

In Modbus communication, function codes are a core issue, with each function code representing the action to be executed by the message. The commonly used function codes are defined as follows:

Function Code Name Purpose
01 (0x01) Read Coils Read the status of one or more coils
02 (0x02) Read Discrete Inputs Read the status of one or more discrete inputs
03 (0x03) Read Holding Registers Read the values of one or more holding registers
04 (0x04) Read Input Registers Read the values of one or more input registers
05 (0x05) Write Single Coil Write to a single coil (force ON/OFF)
06 (0x06) Write Single Register Write to a single holding register
15 (0x0F) Write Multiple Coils Write to multiple coils
16 (0x10) Write Multiple Registers Write to multiple holding registers

Error Codes

The PDU data consists of the function code + data, divided into request messages and response messages. The response messages can be either normal responses or error responses. In the case of errors, error codes are defined to distinguish the cause of the error. Common error codes are as follows (the values of the error codes are represented in decimal):

Error Code Description
01 Illegal function. The slave does not support or recognize this function, for example, the slave does not support this function code.
02 Illegal data address. The data address does not exist.
03 Illegal data value. The data value is invalid.
04 Slave device failure. An unrecoverable error occurred while the server (or slave) was attempting to perform the requested operation.
05 Confirmation. Indicates that the slave has received the command, but processing requires more time. This command is intended to prevent the master from assuming that the message was not received and continuing to resend.
06 Slave device busy. The slave is currently unable to process the received message within the specified time.
08 Storage parity error. Used with function codes 20 and 21 and reference type 6, indicating that the extended file area cannot pass consistency checks. The server (or slave) attempted to read the record file but found a parity error in memory. The client (or master) can resend the request, but service may be required on the server (or slave) device.
10 Unavailable gateway path. Generally indicates a misconfiguration of the gateway.
11 Gateway target device response failure. The target device did not respond.

Function Code 01

Function code 01 indicates reading the status of one or more coils.

Request Message Format

Function Code (1 Byte) Starting Address (2 Bytes) Quantity (2 Bytes)
0x01 0x0000 – 0xFFFF 1 – 2000

Normal Response Message Format

Function Code (1 Byte) Byte Count (1 Byte) Coil Status (n Bytes)
0x01 n n bytes of data

The coil status is represented by each bit indicating the status of a coil. If N coils are read, the returned message byte count will be (N+7)/8..

1 indicates ON, 0 indicates OFF

Error Response Message Format

Function Code (1 Byte) Error Code (1 Byte)
0x01 + 0x80 See error codes

Example

  • Request Message: <span>01 00 04 00 15</span>

    • 01: Function Code
    • 00 04: Starting address of the coils to read 0x0004
    • 00 15: Number of coils to read 0x0015
  • Response Message:<span>01 03 AA 55 05</span>

    • 01: Function Code
    • 03: There are 3 bytes of coil status values
    • AA 55 05: The coil status values, where data AA’s bit0 indicates the status of coil 5 (since the starting address of the coils to read is 0x0004, the 5th coil), bit1 indicates the status of coil 6, and so on. Data AA indicates the status of coils 12-5, data 55 indicates the status of coils 20-13, and data 05’s low 5 bits indicate the status of coils 25-21.

More function code explanations will be provided in the next article.

Leave a Comment