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.