Modbus server TCP/IP

Hi All,
Trying to get a Modbus server running via TCP/IP but getting a few issues. I’m using an Arduino uno SMD R3 and Ethernet-shield 2.

I got it work now to a certain point. By changing the ethernet library to the Ethernet2 library, I got it working.

I setup the Holding registers and the result is 1, denoting that it was successfully setup. I then fill the holding registers with a simple number and again the result is 1 for each one.

I then managed to connect using Modbus poll software, but I get the message illegal address. on the Arduino side, it connects fine and I see the message poll, but now when I print the content of the holding registers I get -1.

I’m at a bit of a loss on what is happening…

this is my code:

#include <SPI.h>
#include <Ethernet2.h>
#include <EthernetUdp2.h>
#include <ArduinoModbus.h>

int Code;
int numberOfSensors = 40;
int res;

EthernetServer server(502);
ModbusTCPServer modbusTCPServer;

void setup() {
  // Ethernet Settings for unit 1 needs the same config for each of the units
  byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x85, 0x80 };  // Define MAc address
  byte ip[] = { 10, 0, 41, 91 };                     // Define IP address
  byte subnet[] = { 255, 255, 254, 0 };                 // Ddefine SubNEt mask

  // initialize the ethernet device
  //EthernetServer server = EthernetServer(502);

  Ethernet.begin(mac, ip); //, subnet); // Assign MAC, IP, and subnet mask
  server.begin();               // start listening for clients
  //modbusTCPServer.begin();    // start listening for clients
  // Define Holding register
  Code = modbusTCPServer.configureHoldingRegisters(0x01, numberOfSensors);

  Serial.print("Holding register initialization Result= ");
  Serial.print("Modbus server address:");

  // Filling holding registers
  for(uint16_t i = 0; i < numberOfSensors; i++)
    res = modbusTCPServer.holdingRegisterWrite((0x01 + i),(i + 10));
    Serial.println(res, DEC);  
  Serial.println("End of Setup");
  //server.begin();               // start listening for clients

void loop() {

  //for(uint16_t i = 0; i < numberOfSensors; i++)
    //Serial.println(modbusTCPServer.holdingRegisterRead((0x00 + i)), DEC);  
  // wait for a new client:
  EthernetClient client = server.available();

  // when the client sends the first byte, say hello:
  if (client) {

    // a new client connected
    Serial.println("new client");
    modbusTCPServer.begin();    // start listening for clients

    // let the Modbus TCP accept the connection

    while (client.connected()) {
      // poll for Modbus TCP requests, while client connected
      for(uint16_t i = 0; i < numberOfSensors; i++)
        Serial.println(modbusTCPServer.holdingRegisterRead((0x01 + i)), DEC);
    Serial.print("client disconnected");


Thank you.
Best Regards

I got it work now to a certain point. By changing the ethernet library to the Ethernet2 library, I got it working.

Current version of the Ethernet library should include all changes (and more) done for the Ethernet2 library. Are your libraries up-to-date actually?

#include <ArduinoModbus.h>

You shouldn’t use the ArduinoModbus library on AVR Arduinos as it uses too much RAM and fragments the memory in no time. This library was developed with the MKR series of Arduinos in mind.

Calling modbusTCPServer.begin() after modbusTCPServer.configureHoldingRegisters() isn’t the expected way to use that library. Most probably that’s the reason why your presets get deleted.