0

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 & DIP switch to select the port. I am reading status of digital pin & 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<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 <ModbusRtu.h>
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<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);

}
0
2

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().

4
  • can you show how can do it. How can declare slave as global pointer. As given in example ; if declare slave as globally & assign the device ID with No. code work. But my intension is to device id being update hw read – AMPS Feb 9 '15 at 10:16
  • If i cant configure using mux & DIP switch . I need to configure manually. or i need to remember each device ID being used so to avois conflict. – AMPS Feb 9 '15 at 10:17
  • I have tested their example code. When i declare slave globally. it will work. If i declare inside setup & assign slave ID . It wont work. How can assign my slave ID here. – AMPS Feb 9 '15 at 10:36
  • 1
    @AMPS Declare it globally as Modbus *slave = 0. In setup(), instantiate it using slave = new Modbus(...). (That's standard C++. I'd recommend looking up a tutorial on pointers if you're not familiar with them.) – Peter Bloomfield Feb 9 '15 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.