Modbus TCP Server

Categories: ,

Introduction

This tutorial will guide you through setting up a Modbus TCP server on a CONTROLLINO MICRO. We’ll be using a modified version of an example code that demonstrates how to control an LED via a Modbus TCP server.

Prerequisites

  • Installed Arduino IDE.
  • CONTROLLINO MICRO board.

Example Code

Please grab the following example .ino file from Github:

Step-by-Step Guide

Step 1: Setup the Arduino Environment

  1. Connect your MICRO to your computer.
  2. Open the Arduino IDE.
  3. Ensure that the appropriate board and port are selected in the Tools menu.
  4. In the Arduino IDE, include the SPI, Ethernet, ArduinoRS485, and ArduinoModbus libraries.

Step 2: Configure Network Settings

Define a MAC address and IP address for your device. Replace the mac and ip variables with your desired values.

Step 3: Initialize Ethernet and Modbus TCP Server

In the setup() function, start the Ethernet connection using Ethernet.begin(mac, ip). Verify Ethernet hardware and connection status and initialize the Modbus TCP server with modbusTCPServer.begin().

Step 4: Configure Digital Outputs & Modbus Coils

Set the LED pin as an output and initialize it to LOW (off). Configure a single coil at address 0x00 using modbusTCPServer.configureCoils(0x00, 1).

Step 5: Handling connections and updating the LED value

Handling Client Connections

In the loop() function, use ethServer.available() to listen for incoming clients. Once a client is connected, use modbusTCPServer.accept(client) to accept the connection.

Polling and Controlling the LED

Continuously call modbusTCPServer.poll() to listen for Modbus requests. Use updateLED() to read the coil value and update the LED state.

The updateLED Function

This function reads the coil value using modbusTCPServer.coilRead(0x00). It then sets the LED state based on the coil value.

Troubleshooting

  • If the LED does not respond, check your network connection and ensure your Modbus client is configured correctly.
  • Verify the MAC and IP address are correctly set for your network environment.
  • Make sure the correct board and port are selected in the Arduino IDE.

Conclusion

You have now successfully created a Modbus TCP server on your CONTROLLINO MICRO. This server can interact with Modbus clients, allowing for various automation and control applications. Experiment with different Modbus registers and functionalities to expand your project!

Share This Post

More Tutorials to explore

HTTP Web Client

Introduction Welcome to the “HTTP Web Client” tutorial using the CONTROLLINO MICRO. In this tutorial, we will learn how to create a web client that

Scan for I2C Devices

Introduction The I2C (Inter-Integrated Circuit) protocol is a popular means of communication between various sensors and devices. In this tutorial, we will learn how to