Arduinomodbus wont reconnect

I am using the arduinomodbus library
with a ethernet feather and feather express
I am initializing the server and using the Modbus poll tool on my pc and am able to connect to the Arduino.
if I disconnect from Modbus poll I am not able to reconnect it times out at that point.
the Arduino needs reset at that time to continue.
has any one else run into this type of problem using the Modbus library?
is the library not closing the client port correctly?

code:
/*
Chat Server

A simple server that distributes any incoming messages to all
connected clients. To use, telnet to your device's IP address and type.
You can see the client's input in the serial monitor as well.
Using an Arduino Wiznet Ethernet shield.

Circuit:

  • Ethernet shield attached to pins 10, 11, 12, 13

created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe

*/

#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoModbus.h>
extern void updateREG() ;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress ip(192, 168, 4, 75);
IPAddress subnet(255, 255, 248,0);
IPAddress Gateway (192,168,4,1);
IPAddress myDns(192, 168, 4, 2);
EthernetClient client;
EthernetServer server(502);
bool connected = 0;
// use the numeric IP instead of the name for the server:
byte Eserver[] = { 192,168,2,17 }; // PC
ModbusTCPServer modbusTCPServer;
boolean alreadyConnected = false; // whether or not the client was connected previously
int Code;
int test = 123;
int adc0_result;
void setup() {
// You can use Ethernet.init(pin) to configure the CS pin
Ethernet.init(10); // Most Arduino shields

// initialize the ethernet device
Ethernet.begin(mac, ip, myDns, Gateway, subnet);

// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}

// start listening for clients
//server.begin(); // for telnet connection
Code = modbusTCPServer.configureHoldingRegisters(0x00, 20);
Serial.print(" server address:");
Serial.println(Ethernet.localIP());

server.begin();

if (! modbusTCPServer.begin()){
Serial.println("modbus server failed to start");
while(1);
}
else{
Serial.println("modbus server started");
}

// if (client.connect(Eserver, 502))
// {
// Serial.println("connected");
// }
// else
// {
// Serial.println("connection failed");
// };

Code = modbusTCPServer.configureHoldingRegisters(0x00, 20);

Serial.print("Holding register initializatinn Result= ");
Serial.print(Code);
Serial.print("\n");
Serial.print("Modbus server address:");
Serial.println(Ethernet.localIP());
test = 110;
}

void loop() {
connected =0;
// wait for a new client:
client = server.available();
//Serial.print("client = ");
// Serial.println(client);

if(client){
modbusTCPServer.accept(client);

// a new client connected
Serial.println("new client");
//modbusTCPServer.begin();    // start listening for clients

while (client.connected()) {
  // poll for Modbus TCP requests, while client connected
  modbusTCPServer.poll();  
  updateREG(); 
}

// client.stop();
Serial.print("poll ");
Serial.println(" client disconnected");
connected = 0;
// modbusTCPServer.accept(NULL);
// modbusTCPServer.end();
if (! modbusTCPServer.begin()){
Serial.println("modbus server failed to start");
while(1);
}
else{
Serial.println("modbus server started");
Code = modbusTCPServer.configureHoldingRegisters(0x00, 20);
}
}
}

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