Modbus register stays HIGH

Hi,

I’m using Mega with W5100 shield to communicate with PLC via Modbus TCP/IP.
I wanted to put “status led” on the Arduino case, when communication is established, to light up.
On the side of the PLC, I’m constantly sending register with the HIGH state, and reading it with Arduino.
I’m facing the next problem - before communication is established, ledOn is turned off, and ledOff is turned On - which is fine. When communication is established, ledOff is turned OFF and ledOn starts blinking - which is also fine. But when I disconnect the cable (lost connection), ledOn still blinking and ledOff is turned off - which is not what I wanted (I wanted to ledOff turn on, and ledOn stop blinking).

Any help will be great…

Here is the code:

#include <SPI.h>
#include <Ethernet.h>
#include "Mudbus.h"
Mudbus Mb;


const int ledOn = 8; // led indikacija kada se uspostavi komunikacija sa weintek-om
const int ledOff = 9; // led indikacija dok se nije uspostavila komunikacija as wentek-om
int ledState = LOW;
int readBit;

unsigned long previousMillis = 0;
const long interval = 150;

void setup(void)
{

  pinMode (ledOn, OUTPUT);
  pinMode (ledOff, OUTPUT);

  uint8_t mac[]     = { 0x90, 0xA2, 0xDA, 0x00, 0x51, 0x06 };
  uint8_t ip[]      = { 192, 168, 0, 36 };
  uint8_t gateway[] = { 192, 168, 1, 1 };
  uint8_t subnet[]  = { 255, 255, 255, 0 };
  Ethernet.begin(mac, ip, gateway, subnet);
  Serial.begin(9600);

}

void loop(void)
{

  Mb.Run();
  unsigned long currentMillis = millis();

  if (Mb.C[6] == HIGH) {
    if (currentMillis - previousMillis >= interval) {
      // save the last time you blinked the LED
      previousMillis = currentMillis;

      digitalWrite (readBit, Mb.C[6]);
      digitalWrite (ledOff, LOW);
      // if the LED is off turn it on and vice-versa:
      if (ledState == LOW) {
        ledState = HIGH;
      } else {
        ledState = LOW;
      }

      // set the LED with the ledState of the variable:
      digitalWrite(ledOn, ledState);
    }
  }
  else if (Mb.C[6] == LOW) {
    digitalWrite (ledOff, HIGH);
    digitalWrite (ledOn, LOW);
  }
}

Hello,

You say you constantly send HIGH, I suppose in Mb.C[6]

When you disconnect, this variable does not automatically reset to LOW, is it ?

Also there is a "Active" flag (https://github.com/luizcantoni/mudbus/blob/master/Mudbus/Mudbus.h#L54), so maybe you can use that instead ?

I know nothing about modbus or mudbus so maybe I'm wrong

That's right, I meant Mb.C[6] stays HIGH all the time once upon it receives HIGH state from PLC, no matter what. So, when I disconnect it, the variable Mb.C[6] does not go LOW. I checked the Active flag from the link you provided, but I'm not sure how could I implement it in my code...

When Mb.C[6] is HIGH, set it to LOW, so when it's disconnected, the next time Mb.Run() is called this variable will not be set to HIGH again so you will know it's disconnected

About this Active variable, by looking in mudbus code, this variable is set to true when client is available, and after 60 seconds without reply from client, it is set to false.

I didn't find any way to set "manually" Mb.C[6] to LOW state, whatever I tried, it stays HIGH all the time.

I saw an Active variable in mudbus library, but I don't know how to call it in my code. When I just type "Active" it is not recognized, and it asks me to declare it...

Mb.Active