Modbus library issue

Dear all.

Here is code written for Modbus. This code working with indivual slave.This code developed on simple modbus library. I have attached complete code in zip file

The problem I am facing when I do loopback test.

  1. when I use 2 salves .I am getting data properly.

  2. when I connect 3 device . I get an error Modbus timeout & received invalid response to Modbus query.

  3. How to implement error handling expectation here,

If device ID request not matched with set device ID , don’t read data

if match Then read the data from device. How to give error if request is inavalid.

My main program

#include <avr/wdt.h>
#include"glob.h"
Modbus *slave = 0;
int8_t state = 0;
uint16_t au16data[30];
float MINV_RANGE = 0.5;
//float MIDV_RANGE1=1.875;
float MAXV_RANGE = 4.5;


int SPD;
int DISCONNECTOR;


int array[8][3] = {
  {
    0, 0, 0
  }
  , {
    0, 0, 1
  }
  , {
    0, 1, 0
  }
  , {
    0, 1, 1
  }
  , {
    1, 0, 0
  }
  , {
    1, 0, 1
  }
  , {
    1, 1, 0
  }
  , {
    1, 1, 1
  }
};
void Take_Reading() {

  for (int row = 0; row < 8; row++)
  {
    //   // get rid of me:
    digitalWrite(SO_enable, array[row][0]);
    digitalWrite(S1_enable, array[row][1]);
    digitalWrite(S2_enable, array[row][2]);
    delay(20);
    analog_1_pv[row] = ANALOG_SCALING * analogRead(A0);
    analog_2_pv[row] = ANALOG_SCALING * analogRead(A1);
    analog_3_pv[row] = ANALOG_SCALING * analogRead(A2);
    if ( (analog_1_pv[row] <= MINV_RANGE || (analog_1_pv[row] >= MAXV_RANGE) ))
    {
      Current_Value1[row] =  0.0;
    }
    else
    {
      Current_Value1[row] =  (analog_1_pv[row] * 12.5) - 31.25;

    }

    if (  (analog_2_pv[row] <= MINV_RANGE || (analog_2_pv[row] >= MAXV_RANGE) ))
    {
      Current_Value2[row] =  0.0;
    }
    else
    {
      Current_Value2[row] =  (analog_2_pv[row] * 12.5) - 31.25;

    }

    if (  (analog_3_pv[row] <= MINV_RANGE || (analog_3_pv[row] >= MAXV_RANGE) ))
    {
      Current_Value3[row] =  0.0;
    }
    else
    {
      Current_Value3[row] =  (analog_3_pv[row] * 12.5) - 31.25;

    }


    Mod_current[row] = (uint16_t)(Mul_Factor * Current_Value1[row]);
    Mod_current[row + 8] = (uint16_t)(Mul_Factor * Current_Value2[row]);
    Mod_current[row + 16] = (uint16_t)(Mul_Factor * Current_Value3[row]);


  }



}


void modbus_call() {

  for (int i = 0; i < 8; i++)
  {
    au16data[i] = Mod_current[i];
    au16data[i + 8] = Mod_current[i + 8];
    au16data[i + 16] = Mod_current[i + 16];
  }

  au16data[24] =(uint16_t)temp_int;
  au16data[25] = ID_Check();
  au16data[26] = SPD;
  au16data[27] = DISCONNECTOR;
  au16data[28] = HV_Reading;
   //  au16data[28] =(100*ID_Check())+( 10*SPD)+(1*DISCONNECTOR);
  // au16data[29]=slave->getInCnt();
  // au16data[30]=slave->getOutCnt();
  //  au16data[31]=slave.getErrCnt();
}



int ID_Check() {

  int ID_value;
  for (int row = 0; row < 8; row++)
  {
    digitalWrite(SO_enable, array[row][0]);
    digitalWrite(S1_enable, array[row][1]);
    digitalWrite(S2_enable, array[row][2]);
    Status_Out[row] = digitalRead(Output_Read);

  }

  ID_value = 1 * Status_Out[7] + 2 * Status_Out[6] + 4 * Status_Out[5] + 8 * Status_Out[4] + 16 * Status_Out[3] + 32 * Status_Out[2]
    + 64 * Status_Out[1] + 128 * Status_Out[0];
  //  ID_value = 1 * Status_Out[0] + 2 * Status_Out[1] + 4 * Status_Out[2] + 8 * Status_Out[3] + 16 * Status_Out[4] + 32 * Status_Out[5
  + 64 * Status_Out[6] + 128 * Status_Out[7];

  return (ID_value);

}

void setup()
{
  wdt_enable(WDTO_8S);

  //analogReference(INTERNAL);

  pinMode(3, OUTPUT);
  pinMode(SO_enable, OUTPUT) ;// pin can enable/disable using digital IO 7 of arduino
  pinMode(S1_enable, OUTPUT) ;// pin can enable/disable using digital IO 6 of arduino
  pinMode(S2_enable, OUTPUT) ;// pin can enable/disable using digital IO 5 of arduino
  //  pinMode(Enablepin, OUTPUT) ;// pin can enable/disable using digital IO 4 of arduino
  pinMode(A0, INPUT) ;
  pinMode(A5, INPUT) ;
  //spd & DC STATUS
  pinMode(SPD_STATUS_PIN, INPUT);
  pinMode(DC_STATUS_PIN, INPUT);

  if (millis() < 5000)
  {
    Device_ID = ID_Check();
  }
   slave = new Modbus(Device_ID, 0, 0);
   slave->begin(9600);
 // Serial.begin(9600);
}



void loop() {
  wdt_reset();
  Take_Reading();
  temp_int=Take_Temp_Reading();
 SPD= SPD_Check();
DISCONNECTOR=  DC_Status();
  Take_HV_Reading();
  modbus_call();
   slave->poll( au16data, 30 );
 // Print_Result();

}

Modbus_code.zip (8.52 KB)

error.pdf (322 KB)