Skip to main content

About Modbus

Modbus is a ubiquitous communication protocol used to communicate with various industrial devices, most notably PLCs. Its simplicity and versatility make it popular in numerous applications. It supports both TCP/IP and serial communication, and more recently, has been defined for secure operation over TLS. Modbus is sometimes referred to as "Modicon Modbus" or just "Modicon" for historical reasons.

Modbus is a simple request-response protocol, and the server component is completely stateless. A client issues a request to the server, then the server performs the action and returns the matching response or an exception code. Clients and servers are referred to as "masters" and "slaves" in the specification and older documents, however, we avoid this terminology in our documentation.

Data model​

The Modbus data model consists of four point types described in the following table:

Point type nameTypeR/W
CoilSingle bitread/write
Discrete inputSingle bitread-only
Holding register16-bit wordread/write
Input register16-bit wordread-only

Each object type is addressed with a 16-bit index. The indices are not shared between the types. In other words, a Modbus device may provide up to 65,536 points of each type.

tip

On the wire, all types are addressed with indices from 0 to 65535. However, you will frequently encounter a convention in product documentation where addresses are prefixed with an entity type identifier and begin addressing from 1 instead of zero:

  • Coils might be identified as running from 00001 to 09999
  • Discrete inputs might be identified as running from 10001 to 19999
  • Input registers might be identified as running from 30001 to 39999
  • Holding registers might be identified as running from 40001 to 49999

This convention is often confusing and purely artificial. This library only uses the raw 16-bit addressing. To convert the entity convention above to raw indices, remove the leading entity type and subtract 1.

Point interpretation​

Because Modbus has limited point types, applications often extend the data model to represent more complex values. You will need to read device documentation to interpret the data appropriately. For example, applications often use multiple registers to represent a single physical value.

Here are some examples of common point interpretations:

  • Interpreting the first bit of a register as a sign bit making the range -32,768 to 32,767 instead of 0 to 65,535
  • Using different bits within one or more registers to represent distinct states (aka bitfields)
  • Using two registers to represent a 32-bit value
  • Using four registers to represent a 64-bit value
  • Using two registers to represent single precision IEEE-754 floating point value
  • Using four registers to represent a double precision IEEE-754 floating point value
  • Using a coil or holding register as a heartbeat that the client must periodically write to maintain a certain state

The endianness of how compound values are laid out in registers may also vary from device to device.

Function codes​

The following table lists the functions currently supported by our library:

CodeDescription
0x01Read coils
0x02Read discrete inputs
0x03Read holding registers
0x04Read input registers
0x05Write single coil
0x06Write single register
0x0FWrite multiple coils
0x10Write multiple registers

These functions are all that is required for the vast majority of applications using Modbus. The library doesn't support other function codes, including the user-defined ones in the ranges 65 to 72 and 100 to 110, but support can be added upon request.

Conformance​

The library follows the MODBUS Application Protocol Specification V1.1b3, the Modbus over Serial Line Specification V1.02 and the Modbus Messaging on TCP/IP Implementation Guide V1.0b for the functions above. Security describes how it validates what it receives, and how that is tested.

Exceptions​

Modbus servers return an exception code when the request cannot be fulfilled. Here's the complete list of possible exceptions:

CodeNameDescription
0x01ILLEGAL FUNCTIONFunction code is not allowed or supported
0x02ILLEGAL DATA ADDRESSA data address is not allowed
0x03ILLEGAL DATA VALUEA value contained in the request is not allowed
0x04SERVER DEVICE FAILUREAn unrecoverable error occurred when trying to perform the action
0x05ACKNOWLEDGESpecialized use in conjunction with programming commands (unsupported)
0x06SERVER DEVICE BUSYSpecialized use in conjunction with programming commands (unsupported)
0x08MEMORY PARITY ERRORUsed with function code 20 and 21 (unsupported)
0x0AGATEWAY PATH UNAVAILABLEGateway was unable to establish a path to the target device (only for gateways)
0x0BGATEWAY TARGET DEVICE FAILED TO RESPONDGateway did not receive a response from the target device (only for gateways)