🛠 Server Modbus TCP : exposer les relais sur Ethernet

Arduino Opta (micro PLC, STM32H747) · intermediaire · Schéma : IMG-opta-plc-intermediaire-1-opta-modbus-tcp.svg — Opta connectée au port Ethernet servant des coils Modbus TCP sur le port 502 à un superviseur SCADA sur le réseau industriel

Objectives

Turn the Opta into a Modbus TCP slave on its integrated 10/100BASE-T Ethernet port. All variants have this port (RMII, MDIX); the ArduinoModbus library provides ModbusTCPServer. You will expose the 4 relays as coils 0..3, controllable from any supervisor (SCADA, PLC, Python script).

Hardware

Complete code

// opta_modbus_tcp_server.ino — coils 0..3 = les 4 relais, port 502
#include <Ethernet.h>
#include <ArduinoModbus.h>

EthernetServer modbusServer(502);   // port standard Modbus TCP
ModbusTCPServer modbusTCPServer;

void setup {
  Serial.begin(9600);
  if (Ethernet.begin == 0) {      // DHCP ; sinon Ethernet.begin(ip)
    Serial.println("Echec DHCP Ethernet");
    while (1);
  }
  Serial.print("IP Opta : "); Serial.println(Ethernet.localIP);
  if (!modbusTCPServer.begin) { Serial.println("Defaillance Modbus TCP"); while (1); }
  modbusTCPServer.configureCoils(0x00, 4); // coils 0..3
  modbusServer.begin;
}

void loop {
  EthernetClient client = modbusServer.available;
  if (client) {
    modbusTCPServer.poll(client);
    while (modbusTCPServer.available) {
      uint16_t addr  = modbusTCPServer.readAddress;
      uint16_t value = modbusTCPServer.readValue;
      if (addr < 4) digitalWrite(RELAY1 + addr, value ? HIGH : LOW);
    }
    client.stop;
  }
}

Expected verification

The Opta's IP address is displayed on the serial port. From the Modbus client, connect to port 502, write to coil 0: relay 1 clicks. >

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials