Modbus Server Protocol

MODBUS Overview

Modbus is a communication protocol used to transmit data across serial lines between various electronic devices. The protocol is open-source and can be used for major in industrial applications such as PLCs, variable speed drives, HMIs, Data acquisition systems etc. This is probably utilized for the connection of a system controlling using an RTU in SCADA in the power domain.

Embien offers Modbus Stack for both use in server and client configurations. Our implementation supports all the function codes defined by the protocol and supports extension via APIs too. Physical layers supported include RTU, ASCII and TCP/IP.

Some of the salient features of RAPIDSEA Modbus protocol stack are
  • Single/Multiple Read/Write functionalities.

  • Coils/Holding Registers and Input Registers.

  • Supports major baud rates from 9600 to 115200 bauds and beyond

  • RTU/ASCII modes can be operated with and without parity and configurable Stop bits.

  • Server and Client Mode of operation.

  • Royalty Free Licensing Model.

  • Highly Optimized for MCU’s.

  • Uses less amount of RAM and ROM.

  • Runs on Linux, Windows and with/without RTOS.

The implementation is fully configurable and is offered for both server and client mode of operation.

Modbus Server

RAPIDSEA implements Modbus server where multiple clients can connect and communicate with it to obtain information.

Supported Functionalities

Following services are supported in the Modbus Stack

Supported Functionalities

Function Code

Register Type

Value Type

Access Type

01 (0x01)

Read Coil Status

Discrete

Read

02 (0x02)

Read Input Status

Discrete

Read

03 (0x03)

Read Holding Registers

16 Bit

Read

04 (0x04)

Read Input Registers

16 Bit

Read

05 (0x05)

Write Single Coil

16 Bit

Read

06 (0x06)

Write Single Holding Register

16 Bit

Write

15 (0x0F)

Write Multiple Coils

Discrete

Write

16 (0x10)

Write Multiple Holding Registers

16 Bit

Write

Application Interface

While the RAPIDSEA Modbus server stack can handle most of the functionality such as client request validation, if it has error request, proper intimation to the client. The RAPIDSEA Modbus server clearly defines API and callback functions that are essential for the user to use/implement.

The below table captures the function that are to be called from the application logic.

API Functions

Function

Description

rs_modbus_server_open

To Initialize the serial/socket configuration.

rs_modbus_server_run

To set the state to be open

rs_modbus_server_process

To be called periodically to prepare the client response.

rs_modbus_server_reconfig

To restart the server application by reconfiguring modbus server

The below table contains rcb calls to perform the server stack.

callback Functions

Function

Description

rcb_mb_set_holding_register

Called to set the holding register value

rcb_mb_set_coil_status_value

Called to set the coil status value

rcb_mb_get_coil_status_value

Called to get the coil status value

rcb_mb_get_holding_reg_val

Called to get the holding register value

rcb_mb_get_input_register_value

Called to get the input register value

rcb_mb_get_discrete_input_value

Called to get the discrete input value

rcb_modbus_serial

Called to handle serial timeouts

Only a millisecond timer is sufficient for the stack to run.

Implementation Guide

This section explains how to implement the Modbus client stack using the RAPIDSEA stack, the steps to be followed are

  • Initially start the Modbus server task from the application layer.

  • Then the server task state is in idle condition, until Modbus server starts.

  • Modbus server starts change the state from idle to state open.

  • In Modbus server process, process the client request and prepare the response for the client.

  • In case of a request failing, error code will be sent from the server response.

The below diagram captures the high level sequence of operations associated with the Modbus server RTU.

Modbus Sequence Diagram

High level flow chart is depicted below for Modbus server:

Modbus Client Flow Diagram

Modbus Supported Modes

Modbus RTU (Remote Terminal Unit)

To establish protocol communication, this represents data in a binary format and is mostly utilized in serial communication. The messages in this version are divided by idle periods. The format that is followed in the RTU version is a cyclic redundancy check to verify mechanism and this makes sure of data reliability.

Serial communication header details refer to the serial interface page for more details Serial Interface

Modbus TCP/IP (Transmission Control Protocol)

This version of Modbus is implemented for communications through TCP/IP networks linking over port 502. This variant does not require any kind of checksum calculation as lower levels itself provides this. Modbus TCP protocol uses a 10 Mbps Ethernet standard to convey the entire structure of Modbus messages.

Socket communication header details refer to the socket interface page for more details Socket Interface

Modbus Server Stack Memory Usage

The Modbus server stack has a size of 22588 bytes.

Modbus Server Stack RAM Consumption

The RAM consumption is different for each of the interfaces.

RAM Consumption

Interface

Size

RTU

944 bytes

ASCII

1716 bytes

TCP

3232 bytes

RTU Interface

The memory (944 bytes) needed can be separated as follows:
  • 256 bytes - buffer space for RX/TX in Modbus

  • 256 bytes - buffer space for handling the RX FIFO buffer by the interface layer

  • 256 bytes - buffer space for handling the TX FIFO buffer by the interface layer

  • 176 bytes - other configuration related memory

ASCII Interface

The memory (1716 bytes) needed can be separated as follows:
  • 513 bytes - buffer space for RX/TX in Modbus

  • 513 bytes - buffer space for handling the RX FIFO buffer by the interface layer

  • 513 bytes - buffer space for handling the TX FIFO buffer by the interface layer

  • 197 bytes - other configuration related memory

TCP Interface

The memory (3232 bytes) needed can be separated as follows:
  • 2600 bytes - buffer space for RX/TX in Modbus for 10 client connections

  • 632 bytes - other configuration related memory

Modbus Server Header Details

Documentation from the relevant server header as follows:

Warning

doxygenfile: Cannot find file “rs_modbus_server.h