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
- Connect your MICRO to your computer.
- Open the Arduino IDE.
- Ensure that the appropriate board and port are selected in the Tools menu.
- 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!