modbus slave ID setting I have small problem in setting device ID for Mosbus slave arduino. Here is simple example

In Modus library; device ID being configure hardcode while program. This code works for Me.

Modbus slave(1,0,0); 


Now I wanted to recognise 0-255 device ID , I am using 8:1 digital Multiplexer &amp; DIP switch to select the port. I am reading status of digital pin &amp; depend on status of 8 DIP pin i come to know whats device ID. Below function return device ID. I have checked Serial it returns value properly. If i assign the value to Device ID it wont work . Please let me know how can i solve this.

unsigned int ID_Check() {
      unsigned   int ID_value;
      for(int row=0;row&lt;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[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);
}


Below is complete code.I tried to call function loop But it wont work

#include &lt;ModbusRtu.h&gt;
int SO_enable=5;
int S1_enable=4;
int S2_enable=3;
int Status_Out[8];
int Output_Read=2;
Modbus slave(ID_Check(),0,0); // this is slave @1 and RS-232 or USB-FTDI
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    }


};

unsigned int ID_Check() {
  unsigned   int ID_value;
  for(int row=0;row&lt;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[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);
}


uint16_t au16data[16] = 
{
  3, 1415, 9265, 4, 2, 7182, 28182, 8, 0, 0, 0, 0, 0, 0, 1, -1
};



void setup() {
  slave.begin( 9600 ); // baud-rate at 19200
  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(Output_Read,INPUT);
}

void loop() {
  slave.poll( au16data, 16 );
}


The other way i tried

void Modbus_Setup()
{
  Device_ID=ID_Check();
     delay(100);
    Modbus slave(Device_ID, 0, 0); 
     slave.begin(9600);
  //slave.setID(Device_ID);

}
It looks like the problem may be initialisation order. In C/C++, global variables/objects are usually initialised in the order they are declared, and before any global functions run.

In your case, this means ID_Check() gets called before array is populated, and before setup() runs.

It looks like you need to make sure everything else is setup before your attempting to initialise the Modbus object. I'm not familiar with the library, but perhaps there's a way to set the device ID after it's been constructed (i.e. not in the constructor)?

If not, you could declare slave as a global pointer, and then instantiate it using new in setup().

As a side note, I'm guessing your Modbus_Setup() function didn't work because the slave object in there is completely separate from the slave object that's declared globally and used in loop().