selomarunto / simple-modbus

Automatically exported from code.google.com/p/simple-modbus
0 stars 0 forks source link

Assign Modbus slave ID by hardware #37

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Dear all,

Here i am attaching working code & modified code for simple modbus.The 
difference between working code & alter code is assign method of slave ID. If i 
wanted to config the slave ID i need to do individually .i checked the code 
work with change in ID no in working code. but it difficult to keep track if 
there are 255 devices .

For that i used 8:1 multiplexer & 8pin dip . Depend on status output 
multiplexer i calculated the device ID. The function ID_Check() determine 
device ID>.when i debug initially via serial monitor the code seems device ID 
updating as expected. but when i enable slave , i get error msg didnt receive 
any response from slave. if i assign device id directly code start working. 

Let me know how to assign device ID here

Working code:

#include <ModbusRtu.h>
#define Device_ID 1
uint16_t au16data[16] = {
  3, 1415, 9265, 4, 2, 7182, 28182, 8, 0, 0, 0, 0, 0, 0, 1, -1 };

Modbus slave(Device_ID,10,0); // this is slave @1 and RS-232 or USB-FTDI

void setup() {
  slave.begin( 19200 ); // baud-rate at 19200
}

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

Now alter code as below

#include <ModbusRtu.h>
int SO_enable=5;
int S1_enable=4;
int S2_enable=3;
int Status_Out[8];
int Output_Read=2;
static int Device_ID;
unsigned long int curr_time;
Modbus *slave=0;

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() {
  //  Serial.begin(9600);
  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);
  unsigned int Device_ID=ID_Check();
  slave=new Modbus(Device_ID,0,0);
  slave->begin(9600);

}

void loop() {
// Modbus_setup();
  delay(1000);
  for(int i = 0; i<8; i++)
  {
    au16data[i];
  }
}

What steps will reproduce the problem?
1.
2.
3.

What is the expected output? What do you see instead?

What version of the product are you using? On what operating system?

Please provide any additional information below.

Original issue reported on code.google.com by ajit.nay...@gmail.com on 10 Feb 2015 at 8:35

GoogleCodeExporter commented 9 years ago
If you altered the library I cant help you with out the source code ;-)

Original comment by bester.j...@gmail.com on 12 Feb 2015 at 4:52

GoogleCodeExporter commented 9 years ago
I didn't change any thing.I have all defined in Modbus RTU function.

Original comment by ajit.nay...@gmail.com on 12 Feb 2015 at 5:35