Communicating with a drive using modbusTCP

I have debounced the button properly now and moved the variable to the beginning of the program so that its loaded with a value before its sent out by the modbus function. I have also commented out server b. Its still does the same thing in that it stops transmitting and comes up with the following in the serial monitor:


Attempting to connect to Modbus TCP server A

Modbus TCP Server A now connected!

Reg 1 write sucessfull!

0

Modbus TCP Server A disconnected
0

"BUTTON PRESSED AT THIS POINT"

Attempting to connect to Modbus TCP server A
Modbus TCP Server A failed to connect!
Failed to write reg 1! Invalid argument
0
Modbus TCP Server A disconnected
0
Modbus TCP Server A already connected!
Failed to write reg 1! Invalid argument

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

#include <ArduinoRS485.h>
#include <ArduinoModbus.h>

#define LHTopLimit 4
#define LHObsLimit 5
#define LHBotLimit 6
#define RHTopLimit 7
#define RHObsLimit 8
#define RHBotLimit 9
#define UPButton 10
#define DWNButton 11
#define LEDPIN 13

int period = 50;
unsigned long time_now = 0;

int LH;
int RH;

int val = 0;

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
// The IP address will be dependent on your local network:

char dataChar;

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 1);

EthernetClient ethClient;
ModbusTCPClient modbusTCPClient(ethClient);

IPAddress servera(192, 168, 1, 11);  // update with the IP Address of your Modbus server
IPAddress serverb(192, 168, 1, 12);
int Speed = 0;

int ledState = HIGH;
int buttonState = 0;
int lastButtonState = 0;

unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

void setup() {

  time_now = millis();

  //Initialize serial and wait for port to open:
  Serial.begin(9600);

  pinMode(LHTopLimit, INPUT);
  pinMode(LHObsLimit, INPUT);
  pinMode(LHBotLimit, INPUT);
  pinMode(RHTopLimit, INPUT);
  pinMode(RHObsLimit, INPUT);
  pinMode(RHBotLimit, INPUT);
  pinMode(UPButton, INPUT);
  pinMode(DWNButton, INPUT);
  pinMode(LEDPIN, OUTPUT);

  LH = 0x00;
  RH = 0x00;

  while (!Serial) {
    ;
    // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);

  // 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);
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }
  if (!modbusTCPClient.connected()) {

    // client not connected, start the modbus TCP
    Serial.println("Attempting to connect to Modbus TCP serverB");

    if (!modbusTCPClient.begin(servera, 502)) {
      Serial.println("Modbus TCP Server A failed to connect!");
    } else {
      Serial.println("Modbus TCP Server A now connected");
    }
  }
  // client connected

  if ((!modbusTCPClient.holdingRegisterWrite(0, 505, 0x1388)) ||  // 05.006 - Motor rated frequency = 50Hz
      (!modbusTCPClient.holdingRegisterWrite(0, 506, 0x154)) ||   // 05.007 - Motor rated current = 3.4A
      (!modbusTCPClient.holdingRegisterWrite(0, 507, 0x3714)) ||  // 05.008 - Motor rated speed = 1410rpm
      (!modbusTCPClient.holdingRegisterWrite(0, 508, 0x190)) ||   // 05.009 - Motor rated voltage = 400V
      (!modbusTCPClient.holdingRegisterWrite(0, 509, 0x53)) ||    // 05.010 - Motor rated power factor = 0.83
      (!modbusTCPClient.holdingRegisterWrite(0, 510, 0x4)) ||     // 05.011 - Motor poles = 4
      (!modbusTCPClient.holdingRegisterWrite(0, 642, 0x1)))       // 06.043 - Control word enable = 1
  {
    Serial.print("Failed to write reg!");
    Serial.println(modbusTCPClient.lastError());
  }

  modbusTCPClient.end();
  Serial.println("Modbus TCP Server A disconnected");


  if (!modbusTCPClient.connected()) {

    // client not connected, start the modbus TCP
    Serial.println("Attempting to connect to Modbus TCP serverB");

    if (!modbusTCPClient.begin(serverb, 502)) {
      Serial.println("Modbus TCP Server B failed to connect!");
    } else {
      Serial.println("Modbus TCP Server B now connected");
    }
  }

  // client connected

  if ((!modbusTCPClient.holdingRegisterWrite(0, 505, 0x1388)) ||  // 05.006 - Motor rated frequency = 50Hz
      (!modbusTCPClient.holdingRegisterWrite(0, 506, 0x154)) ||   // 05.007 - Motor rated current = 3.4A
      (!modbusTCPClient.holdingRegisterWrite(0, 507, 0x3714)) ||  // 05.008 - Motor rated speed = 1410rpm
      (!modbusTCPClient.holdingRegisterWrite(0, 508, 0x190)) ||   // 05.009 - Motor rated voltage = 400V
      (!modbusTCPClient.holdingRegisterWrite(0, 509, 0x53)) ||    // 05.010 - Motor rated power factor = 0.83
      (!modbusTCPClient.holdingRegisterWrite(0, 510, 0x4)) ||     // 05.011 - Motor poles = 4
      (!modbusTCPClient.holdingRegisterWrite(0, 642, 0x1)))       // 06.043 - Control word enable = 1
  {
    Serial.print("Failed to write int reg!");
    Serial.println(modbusTCPClient.lastError());
  }

  modbusTCPClient.end();
  Serial.println("Modbus TCP Server B disconnected");
}

void loop() {

  int reading = digitalRead(UPButton);

  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;

      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }
  val = ledState;

  if (val == HIGH) {
    LH = 0x00183;
    digitalWrite(LEDPIN, HIGH);
  } else {
    LH = 0;
    digitalWrite(LEDPIN, LOW);
  }

  Serial.println(val);

  lastButtonState = reading;

  if (!modbusTCPClient.connected()) {
    // client not connected, start the modbus TCP
    Serial.println("Attempting to connect to Modbus TCP server A");

    if (!modbusTCPClient.begin(servera, 502)) {
      Serial.println("Modbus TCP Server A failed to connect!");
    } else {
      Serial.println("Modbus TCP Server A now connected!");
    }
  } else {
    Serial.println("Modbus TCP Server A already connected!");
  }

  // write the value of 0x183, to the register at address 0x281 to tell the drive to go up
  if (!modbusTCPClient.holdingRegisterWrite(0, 641, LH)) {
    Serial.print("Failed to write reg 1! ");
    Serial.println(modbusTCPClient.lastError());
  } else {
    Serial.println("Reg 1 write sucessfull!");
  }
  Serial.println(LH);
  modbusTCPClient.end();
  Serial.println("Modbus TCP Server A disconnected");

  while (millis() < time_now + period) {
    //wait approx. [period] ms
  }
  /*  if (!modbusTCPClient.connected()) {

    // client not connected, start the modbus TCP
    Serial.println("Attempting to connect to Modbus TCP serverB");

    if (!modbusTCPClient.begin(serverb, 502)) {
      Serial.println("Modbus TCP Server B failed to connect!");
    } else {
      Serial.println("Modbus TCP Server B now connected");
    }
  } else {
    Serial.println("Modbus TCP Server B already connected");
  }

  if (!modbusTCPClient.holdingRegisterWrite(0, 641, RH)) {
    Serial.print("Failed to write reg 2! ");
    Serial.println(modbusTCPClient.lastError());
  } else {
    Serial.println("Reg 2 write sucessfull!");
  }
  modbusTCPClient.end();
  Serial.println("Modbus TCP Server B disconnected");

  while (millis() < time_now + period) {
    //wait approx. [period] ms
  }
*/
}