Settimino can no longer connect to PLC after 4 faliures

I am using Settimino library with Arduino Opta to connect to multiple siemens PLCs, but I have noticed if I get 4 errors when connecting to PLC it can then no longer connect to other plcs

In code below I first try to connect to PLC that doesn't exist (192.168.1.99) three times
and then in loop I can connect to PLC that does exist (192.168.1.90) in loop as many times as loop runs.
if I try to connect to nonexistant PLC 4 times, then in loop I can't connect to PLC that exist.
the same happens if I only am connecting to PLC that exists, but I get random connection errors, such as pluging the cable out and back in.
Is there a way to make sure that once you get 4 errors you can continue using S7Client?

Each client uses a socket (via the EthernetClient object) so you can instantiate up to four clients or less if you are instantiating other Ethernet object (or derived) in the same project.

I think error is somewhat connected to this line from Settimino documentation but I am not sure how to clear those clients

#include <Ethernet.h>
#include <SPI.h>
#include "Settimino.h"

byte mac[] = { 0xA8, 0x61, 0x0A, 0x50, 0x46, 0xA3 };
IPAddress ip(192, 168, 1, 233);
IPAddress myDns(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);

EthernetClient client;

void setup() {
  Serial.begin(9600);
  while (!Serial) {;}
  Serial.println("Starting setup");
  SetupEthernet();
  ReadS7(IPAddress(192,168,1,99));
  ReadS7(IPAddress(192,168,1,99));
  ReadS7(IPAddress(192,168,1,99));
  //ReadS7(IPAddress(192,168,1,99));
}

void loop() {
  delay(1000);
  ReadS7(IPAddress(192,168,1,90));
}
void SetupEthernet() {
  Serial.println("Initialize Ethernet:");
  if (Ethernet.begin(mac, ip, myDns, gateway) == 0) {
    Serial.println("Failed to configure Ethernet");
  } else {
    Serial.print("IP: ");
    Serial.println(Ethernet.localIP());
  }
  delay(1000);
}

void ReadS7(IPAddress ip)
{
  int area = S7AreaDB;
  S7Client *plcClient = new S7Client();
  if(ConnectPLC(plcClient, ip,0,1))
  {
  }
  plcClient->Disconnect();
  delete plcClient;
  plcClient = NULL;
  return;
}

bool ConnectPLC(S7Client *plcClient,IPAddress ip, int rack, int slot) {
  int Result = plcClient->ConnectTo(ip, rack, slot);
  Serial.print("Connecting to ");
  Serial.println(ip);
  if (Result == 0) {
    Serial.println("You are connected with Your PLC");
  } else {
    Serial.print("Connection error: ");
    Serial.println(Result);
    plcClient->Disconnect();
  }
  return Result == 0;
}

I have no idea what you're talking about (not your fault, I'm just not familiar with the stuff that you're using) and hence can't help you but I think that below is a serious flaw in your code

If you use dynamic memory allocation with malloc or new, you should always check the result before using it.

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