modbus tcp connection lost

Hello, I have a code that I am using to communicate between an arduino UNO and LOGO V8 Siemens.
everything works 5*, but if the network cable connection is interrupted a few times I leave data in the LOGO the communication is lost, I have to disconnect and reconnect the arduino to recover the connection. what can i do to solve it?

#include <SPI.h>
#include <Ethernet.h>
#include <MgsModbus.h>
#include <Wire.h>
#define GPS_RX 4
#define GPS_TX 3
#define GPS_Serial_Baud 9600
#include <SoftwareSerial.h>
#include <TinyGPS.h>
TinyGPS gps;
SoftwareSerial gpsSerial(GPS_RX, GPS_TX);

MgsModbus Mb; //Port 502
int cont = 0;
float GPS_HMI1, GPS_HMI2, GPS_HMI3, GPS_HMI4, GPS_HMI5, GPS_HMI6, flat, flon;
float GPS_LAT;
float GPS_LONG;
float angle;
float GPS_HMI_LAT = 38.123456;
float GPS_HMI_LONG = -7.123456;


byte mac[] = {0xE0, 0xDC, 0xDA0, 0x7A, 0xE5, 0x00 };
IPAddress ip(192, 168, 1, 6);

float toRadians(float angle) {
  return (PI / 180) * angle;
}

float toDegrees(float rad) {
  return (rad * 180) / PI;
}

void setup() {
Wire.begin();
Ethernet.begin(mac, ip);

delay(5000);
Serial.begin(9600); //9600
gpsSerial.begin(GPS_Serial_Baud);
}

void loop() 
{
  Mb.MbsRun();
// gps
bool newData = false;
cont = 5;
  unsigned long chars;
  // For one second we parse GPS data and report some key values
  for (unsigned long start = millis(); millis() - start < 1000;)
  {
    while (gpsSerial.available()) 
    {
      char c = gpsSerial.read();
      if (gps.encode(c)) // Atribui true para newData caso novos dados sejam recebidos
        newData = true;
    }
  }
  if (newData){
    float flat, flon;
    unsigned long age;
    gps.f_get_position(&flat, &flon, &age);
    float GPS_LAT = flat;
    float GPS_LONG = flon;
   // int cont = gps.satellites();

  float lat1 = toRadians(GPS_HMI_LAT);
  float lat2 = toRadians(GPS_LAT);
  float lng1 = toRadians(GPS_HMI_LONG);
  float lng2 = toRadians(GPS_LONG);
  float Y = sin(lng2 - lng1) * cos(lat2);
  float X = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(lng2 - lng1);  
  float deg = toDegrees(atan2(Y, X));  
  // note that this implementation doesn't use the module, but angles lower than 0 get augmented by 360 only
  if (deg < 0) {
    deg = 360 + deg;
  }
  int angle = deg;
  
  //Mb.MbsRun();
 // Mb.MbData[0] = angle * 10;
  
 //  Serial.print("GPS_LAT: ");     
 //  Serial.println(GPS_LAT, 6);
 //  Serial.print("GPS_LONG: ");
 //  Serial.println(GPS_LONG, 6);
 //  Serial.print("Angle: ");
 //  Serial.println(angle);
 //  Serial.print("GPS: ");
 //  Serial.println(cont);
   Mb.MbsRun();
delay(10);

 Mb.MbData[0] = angle * 10;
 cont = 10;

//delay(10);
 } 
  Mb.MbData[1] = cont;
}

Solve what? Recover from a faulty network condition?

The MsgModbus library isn't designed to be operated in a bad network. Either fix your network or make the library more robust to recognize a failing network and reset if that happened.

Please correctly indent your code or let the IDE do it for you (Ctrl-T)!

Remove these two lines:

They are not necessary and just slow down your sketch.

Hello,

yes, recover from a faulty network, the network is 400 meters wireless. I have a wireless bridge at 1 km of range, but sometimes it is possible for the signal to lose, if this happens sometimes the communication is not recovered.

i removed the two lines :slight_smile:

As I already wrote, the library you use isn't able to handle a bad network. Either fix the network (might be tricky in your case), make the library more tolerant for network outages (check the connection state before every message, reconnect if connection is lost) or use another library which handles such states better (I don't know if that exists).