PLC reading information from Arduino via ModbusTCP

Hardware
Arduino UNO R3
Arduino Ethernet Shield 2 W5500
Libraries
ArduinoModbus.h version 1.0.6

I am trying to communicate a PLC (client) with Arduino (server) via ModbusTCP to read some calculations performed in the Arduino. Here's the code I am using:

#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoRS485.h>
#include <ArduinoModbus.h>
/*
Set the Ethernet settings of the Arduino shield
*/
byte mac[]={0xDE,0xAD,0xBE,0xEF,0xFE,0xED};
byte ip[]={192,168,1,168};
//Create an Ethernet server that listens on port 502 for ModbusTCP
EthernetServer server=EthernetServer(502);
//Create a ModbusTCP server
ModbusTCPServer modbusserver;
int CoilRegCnfg;
int HoldigRegCnfg;
int InputRegCnfg;

void setup() {
  Serial.begin(9600);//Initiate the serial monitoring interface
  Ethernet.begin(mac, ip);//Initiate the Ethernet settings
  server.begin();//Initiate listening on port 502
  modbusserver.begin();//Initiate the ModbusTCP server
  CoilRegCnfg = modbusserver.configureCoils(1, 100);//Coil Registers 00001 to 10101
  HoldigRegCnfg = modbusserver.configureHoldingRegisters(1,100);//Holding Registers 40001 to 40101
  InputRegCnfg = modbusserver.configureInputRegisters(1,100);//Input Registers 30001 to 30101
  if(CoilRegCnfg==1){
    Serial.println("Coil Registers Failed.");
  }
  if(HoldigRegCnfg ==1){
    Serial.println("Holding Registers Failed.");
  }
  if(InputRegCnfg==1){
    Serial.println("Input Registers Failed.");
  }
}

It appears that I can't create the registers and I cannot understand why.
Is there something I am missing?

Regards.

Hi, @tunedlooplabs , welcome to the forum.

The Modbus library you are using may be too big for the Uno. Have you had a chance to see how much flash and ram is being used by your program?
You may also want to start trouble shooting with checking the status of the Ethernet. You should check to see what ethernet.begin returns

As a matter of fact, I do get a "warning" message regarding the size of the memory available after compile/download.

Oddly, after multiple tries, the HoldingRegCnfg and InputRegCnfg returned 0 which according to the library documentation, it succeeded creating the registers in the memory. Now only the coils are returning 1, which is fail.

Do you recommend any other library? I have seen some YouTube videos already but I would like to use something that other people have actually used in this forum just to increase my chances of discussion and feedback.

Thanks for the reply.

One option is to try the code with a bigger version of an arduino.. Let's say a arduino Mega 2560... If the shield you are using has not Mega 2560 compatibility then just use jumper wires to connect it to the right pins..

Or borrow a arduino Mega.

That way you will check if the problem is in low available memory in nano or in your code

I will second the suggestion by @caslor. I have only seen Mega versions of it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.