Modbus TCP Master Client I/O

Categories: , ,

This is an example guide to setting up Modbus TCP communication between a client and server using CONTROLLINO and an Ethernet shield. This tutorial will help you configure the client to read and write inputs and outputs and the server to synchronize its state with the client.

Prerequisites

  • Installed Arduino IDE.
  • CONTROLLINO PLC board (e.g., MAXI(also Automation), or MEGA).
  • Basic understanding of Modbus TCP and Ethernet setup.

Example Code

You can download the example Modbus TCP Client and Server code from the following GitHub repository:

  • Modbus TCP Client code
  • Modbus TCP Server code
  • Detailed example explanation

Step-by-Step Guide

Modbus TCP Client

The Modbus TCP client communicates with the server to toggle coils and read and write registers.

Step 1: Setup the Arduino Environment

  • Connect your CONTROLLINO board to your computer.
  • Open the Arduino IDE.
  • Ensure that the CONTROLLINO is selected in the Tools menu.
  • Include the necessary libraries for communication:
    • ArduinoRS485
    • ArduinoModbus
    • Controllino
    • SPI
    • Ethernet

Step 2: Initialize the Ethernet and Modbus TCP Client

In the setup() function:

  1. Start the Ethernet connection by calling Ethernet.begin(mac, ip) using the MAC address of your Ethernet shield.
  2. Check the Ethernet hardware and cable connection.
  3. If the connection is successful, initialize the Modbus TCP client by using modbusTCPClient.begin(server, 502).

Step 3: Configure Digital and Analog Pins

In the loop() function:

  1. Digital Inputs and Coils:
    • Declare an array slaveInputs[] to store the state of the slave’s digital inputs.
    • Read these inputs using modbusTCPClient.discreteInputRead().
    • Synchronize the state of the slave inputs to the master outputs using digitalWrite().
    • Write the current state of the master digital input to the slave using modbusTCPClient.coilWrite().
  2. Analog Inputs and Holding Registers:
    • Declare an array slaveInRegisters[] to store the state of the slave’s holding registers.
    • Read the analog inputs using modbusTCPClient.inputRegisterRead().
    • Synchronize the master’s analog outputs using analogWrite() and update the slave’s holding registers using modbusTCPClient.holdingRegisterWrite().

Modbus TCP Server

The Modbus TCP server simulates a coil and synchronizes the state of its inputs and outputs with the client.

Step 1: Setup the Arduino Environment

  • Connect your CONTROLLINO board to your computer.
  • Open the Arduino IDE.
  • Ensure that the CONTROLLINO is selected in the Tools menu.
  • Include the same libraries as in the client setup:
    • ArduinoRS485
    • ArduinoModbus
    • Controllino
    • SPI
    • Ethernet

Step 2: Initialize the Ethernet and Modbus TCP Server

In the setup() function:

  1. Start the Ethernet connection using Ethernet.begin(mac, ip).
  2. Start the Modbus TCP server by calling modbusTCPServer.begin().
  3. Configure the Modbus coils, discrete inputs, holding registers, and input registers using:
    • modbusTCPServer.configureCoils()
    • modbusTCPServer.configureDiscreteInputs()
    • modbusTCPServer.configureHoldingRegisters()
    • modbusTCPServer.configureInputRegisters()

Step 3: Configure Digital and Analog Pins

  1. Set up digital input and output pins.
    • Use pinMode() to define the pins as either INPUT or OUTPUT.
    • Initialize output pins to a LOW state.
  2. Set up analog output pins for holding registers using analogWrite().

Step 4: Synchronizing Coils and Registers

In the loop() function:

  1. Client Connection:
    • Wait for incoming client connections using ethernetServer.available().
    • Accept the client connection with modbusTCPServer.accept(client).
  2. Synchronize Coils and Registers:
    • In the executeSynchronization() function:
      • Read the state of the server’s coils using modbusTCPServer.coilRead(), and update the corresponding output pins using digitalWrite().
      • Read the state of the input pins using digitalRead() and update the corresponding coil register with modbusTCPServer.coilWrite().
      • For analog outputs, write the value from the holding register to the PWM outputs using analogWrite().
      • Read the analog input pins and update the corresponding input registers using modbusTCPServer.inputRegisterWrite().

Conclusion

You have now successfully configured Modbus TCP communication between a CONTROLLINO client and server using an Ethernet shield. The client reads and writes digital and analog inputs/outputs, while the server synchronizes its state accordingly. Experiment with different pin configurations and data handling to expand your project!

Share This Post

More Tutorials to explore

Modbus TCP Master Client I/O

This is an example guide to setting up Modbus TCP communication between a client and server using CONTROLLINO and an Ethernet shield. This tutorial will

Modbus RTU Master Slave I/O

Introduction This tutorial will guide you through setting up Modbus RTU communication between a master and a slave device using two CONTROLLINO devices. The master device reads digital and analog inputs from the slave and sends