ENC28J60 Arduino help needed

Hi

I am attempting to use an ENC28j60 to pass serial through to an Arduino Uno from Ethernet.

When I connect using putty, I can connect to the ENC28J60 and send commands. If the computer is left connected , all is well.
Should the Ethernet plug be disconnected , then the ENC28J60 stops passing serial.
I can still ping the ENC, but until the unit is repowered, then it will not respond.

Below is the code I am using

If anyone can see why the unit stops receiving , any assistance would be great

And I am using the ENC28J60 as I want to be able to make my own PCB for the project

[code]
#include <UIPEthernet.h>
#include <SPI.h>


// ethernet
byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0x90, 0x86 };
byte ip[] = { 192, 168, 1, 151 };
byte gateway[] = { 192,168,1,129 };
byte subnet[] = { 255, 255 ,255 , 0 };
 
// serial connection
int serialBaud = 19200;      // Baud speed
int serialCfg = SERIAL_8N1; // unit has 8 data,no parity, 1 stop bit
 
// socket parameters
int serverPort = 8888;
 
// start TCP servers
EthernetServer server(serverPort);
 
void setup() {
  Serial.begin(serialBaud, serialCfg); // open serial communications
  Ethernet.begin(mac, ip, gateway, subnet); // start the Ethernet connection
  server.begin(); // begin listening for TCP connections
}
 
void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    String clientMsg ="";
    while (client.connected()) {
      // transmit
      while (client.available()) {
        char c = client.read();
        clientMsg+=c; // store received chars up to newline
        if (c == '\n') {
          Serial.print(clientMsg); // then send the message through serial
          clientMsg = "";
          Serial.flush();
        }
      }
 
      // receive
      int incomingByte = 0; // for incoming serial data
      while (Serial.available() > 0) { // if data has been received from the serial connection
        incomingByte = Serial.read();
        client.print(char(incomingByte)); // print the char data back to the client
        if (char(incomingByte) == '\n')
           client.flush();
      }
    }
  }
}

[/code]

I guess that when you pull out the ethernet plug, then put it in again, the ENC28j60 is capable of rebuilding the connection to the router so you can ping it (from another computer on the same network). However, the Arduino has not repeated all that configuration in setup() so your application will no longer work.

Maybe periodically test on the arduino if the network connection still exists and, if not, force a system restart or try repeating the setup() code.

Would putting the below into the loop be sufficient ?

Ethernet.begin(mac, ip, gateway, subnet); // start the Ethernet connection
  server.begin(); // begin listening for TCP connections
}

I don't know the Ethernet shield and library well enough to say. In general, repeating an initialisation on every loop iteration which is intended to be done once at program setup is not a good idea.

Can you find a library method which can be used to test if the connection is still there ?

I'm just starting to play with the ENC28J60 and the library's so am still finding my feet with them :confused: