Look for in our Blog. You will find multiple applications, solutions, code examples. Navigate using the tag cloud or search using specific criteria

PLC Ethernet Reconnection

Introduction

This post shows how to reconnect your PLC to the server that it is usually connected. This solution works for different related kind of problems; in the one hand, if  the server starts later than the PLC and the PLC shows clear signs of connection errors and, in the other hand, if the PLC loss the connection with the server at anytime.


Solution

Code

/*
   Copyright (c) 2019 Boot&Work Corp., S.L. All rights reserved
   This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ #if defined(MDUINO_PLUS)
#include <Ethernet2.h>
#else
#include <Ethernet.h>
#endif // PLC MAC address
byte mac[] = {0xBE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress plcAddress(10, 10, 10, 2); // Server IP address and TCP port
IPAddress serverAddress(10, 10, 10, 1);
const uint16_t serverPort = 6543; // Ethernet client
EthernetClient client; ////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600L);
Serial.println("ethernet-reconnect started"); Ethernet.begin(mac, plcAddress); Serial.print("IP address: ");
Serial.println(Ethernet.localIP());
} ////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
// Update client connection status
updateConnectionStatus();
} ////////////////////////////////////////////////////////////////////////////////////////////////////
void updateConnectionStatus() {
static uint32_t lastReconnectionTime = 0UL; // Reconnect to server if not connected
if (!client.connected()) {
// Prevent fast reconnections
if (millis() - lastReconnectionTime > 1000) {
Serial.println("Client is not connected to server");
reconnect(); // Update last reconnection time to prevent fast tries
lastReconnectionTime = millis();
}
}
} ////////////////////////////////////////////////////////////////////////////////////////////////////
void reconnect() {
// Force client disconnection
client.stop(); // Connect to server
Serial.println("Trying to connect...");
client.connect(serverAddress, serverPort); // Check connection status
if (client.connected()) {
Serial.println("Client connected to server");
} else {
Serial.println("Impossible to connect to server");
}
}


Explanation

Having a look at the code solution we can see that, first of all, there is a function that updates the connection status every moment, reconnecting to the server if is not even connected. This function tries to reconnect to the server every 1 second, but this is an arbitrary time and you can change it as you want (it is good to set a time of at least 1 second to not overcharge the server connection). This one uses another function called "reconnect()" which forces client disconnection, connects to server  and checks connection status.

How to upload this code

If you want to upload this code on your PLC you have to follow these steps:
 1.-Select Tools > Board > M-Duino Family (or your working shield but considering that it have to have Ethernet or the Wifi module).


  

2.-Select Tools > Industrial Shields > M-Duino 21+ (or your working shield model but considering that it have to have Ethernet or the Wifi module).

 


3.-Select Tools > Port > COM65 (or the port where you have your device connected).



4.-Upload the program. Click on the arrow located on the left top of the window.



5.- If everything is working right, your problems should be solved as we saw in the previous sections and, if you want to check out all the process you can see the confirmation messages (as "ethernet-reconnect started", "IP address: ", "Client connected to server", etc.) in the serial monitor. The Serial Monitor icon is located in the right top corner of the window (make sure to configure the baud rate to 9600 to sync up with the code configuration).