Main Content

MODBUS RS485 Communication Between Client and Server Using Arduino Board

This example shows how to use the Simulink® Support Package for Arduino® Hardware to implement a MODBUS® RS485 asynchronous, serial communication between MODBUS client and server. It also shows the four modes of operation: Client Read, Client Write, Server Read, and Server Write.

Supported Arduino Boards:

  • Arduino MKR Zero

  • Arduino Mega 2560

  • Arduino Mega ADK

  • Arduino MKR 1000

  • Arduino MKR WIFI 1010

  • Arduino Leonardo

  • Arduino Micro

  • Arduino Nano 3.0

  • Arduino Nano 33 IoT

  • Arduino Uno

  • Arduino Robot Control board

  • Arduino Robot Motor board

Introduction

Simplicity, efficient communication, and streamlined and fast data communication make MODBUS RTU RS485 one of the most widely used serial communication protocols in electronics and instrumentation industries. Each device on the RS485 network is referenced by a unique 8-bit address or identifier.

This example shows the capabilities of the MODBUS RS485 protocol using two Arduino boards acting as client and server, with the arduino_modbus_client model deployed on the former and the arduino_modbus_server model deployed on the latter.

The example also deals with utilizing different modes of operation for the client and server device.

  • Client Read: Client reads data from server register(s) over RS485 network

  • Client Write: Client writes data to server register(s) over RS485 network

  • Server Read: Server reads data from the server registers over RS485 network

  • Server Write: Server writes data to the server registers over RS485 network

This table categories the different server registers referenced by the MODBUS client and server.

   Register Type   |   Register Size   |   Allowed Client Operation on Register
-----------------------------------------------------------------------------------
 Coil              |       1-bit       |            Read and Write
 Discrete Input    |       1-bit       |            Read
 Holding Register  |       16-bit      |            Read and Write
 Input Register    |       16-bit      |            Read

Prerequisites

We recommend that you complete the Get Started with Arduino Hardware and Communicating with Arduino Hardware examples.

Required Hardware

  • Two Arduino boards

This example uses one Arduino MKR 1000 board as a client and another Arduino MKR 1000 board as a server. You can use any of the Arduino boards from the list of suggested boards.

  • Two micro USB cables

  • Two RS485 shields

Dependencies

Implementing this example requires you to successfully deploy these Simulink® models on each of the supported Arduino boards:

   Simulink Model Name    |          Deployed Hardware         |            Purpose
------------------------------------------------------------------------------------------------
1. arduino_modbus_server    |   First of the two Arduino board   |   Behaves as a server device
2. arduino_modbus_client   |   Second of the two Arduino board  |   Behaves as a client device

Task 1: Connection Setup

The MODBUS client and server must be connected on the same RS485 network for successful communication.

Task 2: Configure Modbus Client Model and Calibrate Parameters

This support package provides a preconfigured model for the MODBUS client.

Open the arduino_modbus_client Simulink model.

The Simulink model for the client can be divided into two parts, Write to Server and Read from Server, based on the type of operation the client can perform on the server device registers. The default values for the Simulink client model are preconfigured in this example depending on the server register size and the allowed client operations.

MODBUS Client Configuration for Write to Server Mode of Operation

The client write operation is valid on coil and holding registers. This example uses two MODBUS RS485 Client Write blocks to implement the write operation on the coil registers and the holding registers, respectively.

To perform the 1-bit write operation on the coil registers, change the position of the slider switches. The On position depicts that data 1 is written on these registers, whereas the Off position depicts that data 0 is written on these registers. Two slider switches are used in this model, whose output is multiplexed and fed as an input to one of the MODBUS RS485 Client Write blocks.

You can configure the following parameters to write data from the slider switches to the coil registers:

  1. Enter the unique 8-bit server address of the identifier in the Server address parameter. In this model, it is configured as 1.

  2. Select the Write Multiple Coils option in the Function parameter.

  3. To notify the client to write data to two coil registers, specify the address of the first coil register in the Coil Address parameter. The default value is 0.

  4. Enter 2 in the Number of Coils parameter.

Similarly, to perform the 16-bit client write operation on the holding registers, this example uses a counter that counts values from 0 to 255. The output from the free-running counter is fed as an input to the second MODBUS RS485 Client Write block.

You can configure the following parameters to write data from the free-running counter to the holding registers:

  1. Enter the unique 8-bit server address of the identifier in the Server address parameter. In this model, it is configured as 1.

  2. Select the Write Multiple Holding registers option in the Function parameter.

  3. To notify the client to write data to the holding registers, specify the address of the first coil register in the Holding Register Address parameter. The default value is 0.

  4. Enter 1 in the Number of Holding registers parameter.

MODBUS Client Configuration for Read from Server Mode of Operation

The client read operation is valid on all the server device registers (coil, holding, discrete input, and input). Two MODBUS RS485 Client Read blocks are used to implement the read operation on input and discrete input registers, respectively.

A 1-bit pulse generator is used to write data to the discrete input registers. The data read from the discrete input registers is available on the data port of the MODBUS Client Read block for the discrete input register. This data is valid only if the status port value is 1, indicating a successful read operation and the presence of valid data on its data port. A lamp indicator is used to signify the 1-bit read from the discrete input register.

You can configure the following parameters to read 1-bit pulse generator data from the discrete input register:

  1. Enter the unique 8-bit server address of the identifier in the Server address parameter. In this model, it is configured as 1. Ensure to configure the same Server address as used in the MODBUS RS485 Server Write block for the discrete input register.

  2. Select the Read Discrete input option in the Function parameter.

  3. To notify the client to read data from one discrete input register, specify its address in the Discrete Input Address parameter. Ensure to configure the same discrete input register address as used in the MODBUS RS485 Server Write block for the discrete input register. The default value is 0.

  4. Set the Sample Time parameter.

Similarly, this example uses a free-running counter to write data to the input register. The data read from the input register is available on the data port of the MODBUS RS485 Client Read block for the input register. This data is valid only if the status port value is 1, indicating a successful read operation and the presence of valid data on its data port. A Display block is used to show the 16-bit data read from the input register.

You can configure the following parameters to read the counter data from the input register:

  1. Enter the unique 8-bit server address of the identifier in the Server address parameter. In this model, it is configured as 1. Ensure to configure the same Server address as used in the MODBUS RS485 Server Write block for the input register.

  2. Select the Read Input register option in the Function parameter.

  3. To notify the client to read data from one input register, specify its address in the Input Register Address parameter. Ensure to configure the same input register address as used in the MODBUS RS485 Server Write block for the input register. The default value is 0.

  4. Set the Sample Time parameter.

Task 3: Configure Modbus Server Model and Calibrate Parameters

This support package provides a preconfigured model for the MODBUS server.

Open the arduino_modbus_server Simulink model.

The Simulink model for the server can be divided into two parts, Write to Client and Read from Client, based on the type of operation the server device can perform on its registers. The default values for the Simulink server model are preconfigured in this example.

MODBUS Server Configuration for Write to Client Mode of Operation

This example uses two MODBUS RS485 Server Write blocks to implement the write operation on the input register and the discrete input register, respectively.

To perform the 16-bit server write operation on the input registers, this example uses a counter that counts values from 0 to 255. The output from the free-running counter is fed as an input to the MODBUS RS485 Server Write block for the input register.

You can configure the following parameters to write data from the free-running counter to the input registers:

  1. Select the Write Input register option in the Function parameter.

  2. To notify the client to read data from one input register, specify its address in the Input Register Address parameter. Ensure to configure the same input register address as used in the MDOBUS RS485 Client Read block for the input register. The default value is 0.

Similarly, this example uses a 1-bit pulse generator to write data to the discrete input register.

You can configure the following parameters to write 1-bit data from the pulse generator to the discrete input register:

  1. Select the Write Discrete Input option in the Function parameter.

  2. To notify the client to read data from one input register, specify its address in the Discrete Input Address parameter. Ensure to configure the same discrete input register address as used in the MDOBUS RS485 Client Read block for the input register. The default value is 0.

MODBUS Server Configuration for Read from Client Mode of Operation

This example uses two MODBUS RS485 Server Read blocks to implement the read operation on the coil and holding registers, respectively.

The server device reads the data written by the client device using two slider switches. This 1-bit data read from the coil registers is available on the data port of the MODBUS RS485 Server Read block for the coil registers. This data is valid only if the status port value is 1, indicating a successful read operation and the presence of valid data on its data port. A lamp indicator is used to signify the 1-bit data read from the coil registers.

You can configure the following parameters to read 1-bit slider switch data from the coil registers:

  1. Select the Read Coil option in the Function parameter.

  2. To notify the server to read data from the coil registers, specify its address in the Coil Address parameter. Ensure to configure the same coil register address as used in the MDOBUS RS485 Client Write block for the coil register. The default value is 0.

  3. Set the Sample Time parameter.

Similarly, the server device reads the free-running counter data fed as an input to the client. This 16-bit data read from the holding register is available on the data port of the MODBUS RS485 Server Read block for the holding register. This data is valid only if the status port value is 1, indicating a successful read operation and the presence of valid data on its data port. A Display block is used to show the 16-bit data read from the holding register.

You can configure the following parameters to read the 16-bit counter data from the holding register:

  1. Select the Read Holding register option in the Function parameter.

  2. To notify the server to read data from the holding registers, specify its address in the Holding Register Address parameter. Ensure to configure the same holding register address as used in the MDOBUS RS485 Client Write block for the holding register. The default value is 0.

  3. Set the Sample Time parameter.

Task 4: Run Simulink Model on Client and Server

1. Connect the Arduino boards acting as server and client to the host computer.

2. On the Hardware tab of the Arduino MODBUS Client Simulink model, in the Mode section, select Run on board and then click Monitor & Tune.

3. On the Hardware tab of the Arduino MODBUS Server Simulink model, in the Mode section, select Run on board and then click Monitor & Tune.

4. Coil register:

  • MODBUS RS485 Client Read block: Change the position of the slider switches to write data to the coil registers.

  • MODBUS RS485 Server Read block: Verify the status of the read operation on the coil registers. Observe the lamp displaying the corresponding data obtained from the coil registers. Verify if the lamp status exhibits the correct data written on the coil registers.

5. Holding register:

  • MODBUS RS485 Client Write block: The counter starts writing data ranging from 0 to 255 to the holding register.

  • MODBUS RS485 Server Read block: Check the status of the read operation on the holding register. Observe the counter value corresponding to the data obtained from the holding register and verify if the same value is displayed on the Display block.

6. Input register:

  • MODBUS RS485 Server Write block: The counter starts counting from the value that ranges from 0 to 255.

  • MODBUS RS485 Client Read block: Check the status of the read operation on the input register. Observe the counter value corresponding to the data obtained from the input register and verify if the same value is displayed on the Display block.

7. Discrete Input register:

  • MODBUS RS485 Server Write block: The 1-bit pulse generator starts toggling between 0 and 1.

  • MODBUS RS485 Client Read block: Check the status of the read operation on the discrete input register. Observe the lamp displaying the corresponding data obtained from the discrete input register. Verify if the lamp status exhibits the correct data written on the discrete input register.

Other Things to Try

  • Implement the MODBUS client and server models to read and write from and to multiple server device registers for all the register types.

  • Use MODBUS server devices such as temperature sensors, humidity sensors, and so on and communicate real-time data to the Arduino board acting as the client device.

  • Implement a monitoring system using PID controller as the server device and the Arduino board as the client device.