W5500 disconnecting

hi, everyone,
I am working on a project where I have connected an Arduino Mega to a W5500 Ethernet module to communicate with an Allen-Bradley Micro850 PLC over Ethernet. Initially, the communication between the Arduino and the PLC works smoothly, and I am able to send data without any issues.

In my setup, I have 16 current coils connected to the Arduino Mega, and it continuously reads data from these coils. However, I encounter a problem when current is applied to the coils. The Arduino reads the data correctly, but the communication with the PLC disconnects intermittently.

#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>
#include "ACS712.h" // Library for ACS712
int x;
// Ethernet configuration
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 1, 111);

EthernetServer ethServer(502);
ModbusTCPServer modbusTCPServer;

// ACS712 configuration
const int numSensors = 16; // Total number of sensors
const int sensorPins[numSensors] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15};
ACS712 sensors[numSensors] = { // Create an array of ACS712 objects
ACS712(A0, 5.0, 1023, 185), ACS712(A1, 5.0, 1023, 185), ACS712(A2, 5.0, 1023, 185),
ACS712(A3, 5.0, 1023, 185), ACS712(A4, 5.0, 1023, 185), ACS712(A5, 5.0, 1023, 185),
ACS712(A6, 5.0, 1023, 185), ACS712(A7, 5.0, 1023, 185), ACS712(A8, 5.0, 1023, 185),
ACS712(A9, 5.0, 1023, 185), ACS712(A10, 5.0, 1023, 185), ACS712(A11, 5.0, 1023, 185),
ACS712(A12, 5.0, 1023, 185), ACS712(A13, 5.0, 1023, 185), ACS712(A14, 5.0, 1023, 185),
ACS712(A15, 5.0, 1023, 185)
};

void setup() {
// Serial communication
Serial.begin(9600);
while (!Serial);
Serial.println("Ethernet Modbus TCP Server with ACS712 Sensors");

// Ethernet initialization
Ethernet.begin(mac, ip);
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield not found. Stopping.");
while (true) delay(100);
}
ethServer.begin();

// Modbus server initialization
if (!modbusTCPServer.begin()) {
Serial.println("Failed to start Modbus TCP Server!");
while (true);
}

// Configure 16 holding registers
modbusTCPServer.configureHoldingRegisters(0x00, numSensors);

// Initialize ACS712 sensors
for (int i = 0; i < numSensors; i++) {
sensors[i].autoMidPoint(); // Automatically calculate midpoint
}

Serial.println("Modbus TCP Server is running with ACS712 sensors.");
}

void loop() {
// Listen for Modbus clients
EthernetClient client = ethServer.available();
if (client) {
Serial.println("New client connected.");
modbusTCPServer.accept(client);

while (client.connected()) {
  modbusTCPServer.poll(); // Handle Modbus requests
  updateHoldingRegisters(); // Update sensor values in holding registers
}

Serial.println("Client disconnected.");

}
}

void updateHoldingRegisters() {
for (int i = 0; i < numSensors; i++) {
int mA = sensors[i].mA_AC(); // Read current in mA for each sensor
if(mA>=600){
x=4;
}
else{
x=6;
}
modbusTCPServer.holdingRegisterWrite(i, x); // Update the Modbus holding register

// Debug output
Serial.print("Sensor ");
Serial.print(i);
Serial.print(": Current = ");
Serial.print(mA);
Serial.print(" mA        ");
  Serial.println(x);

}
}

here is the full code

Your topic does not indicate a problem with IDE 1.x and therefore has been moved to a more suitable location on the forum.

Please edit your post with the code, select all code and click the <CODE/> button; next save your post. This will apply code tags which make the code easier to read, easier to copy and the forum software will display it correctly.

Please provide a schematic / wiring diagram containing all connections (including power and GND); a photo of a hand drawn one is OK.