Dear all ,
I am working with Modbus protocol library for arduino uno.the code written in c++. I have attached the library detail in PDF file.
Example code: The code works properly. The problem here if we want to add more slave 0-255 each time i need to configure manually set device id & very difficult to track which ID being used.
Code:
#include <ModbusRtu.h>
uint16_t au16data[16] = {
3, 1415, 9265, 4, 2, 7182, 28182, 8, 0, 0, 0, 0, 0, 0, 1, -1 };
Modbus slave(1,0,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 );
}
SO i decide to build 8:1 digital multiplexer & 16 pin with 8 out dip package. The output given to digital multiplexer.Depend on status of pin . The device ID being calculated. Even viewer can come to know which ID being set.
Code:
#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];
}
}
I have already checked in serial monitor Device id changes as pin status changes.But when disable serial monitor & assign to device ID. slave fails to communicate. Please let me know how can resolve this.if ID_check() not used it work properly else it wont work.
I also tried with slave.setID(); & slave.getID(); is there way to make it work.
Modbus.pdf