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);
}