Dear all,
I am using Modbus library for first time, I have tested sample code Modbus RTu provided with Modbus library arduino. The code is working. Here what i am trying below.
The below function use to check device ID. I wanted to Select Device ID externally hardware config. FOr that i am using 8:1 multiplexer & DIP switch. I am reading status of pin & depend on status i am calculating the device ID. Using serial monitor i found below function is working fine.
ID_Check()
The problem i face when i use the below line in any part
slave.begin(9600);
```it will hang serial monitor ; its expected since it try to intialise slave here.
Please help me in
1) I would like to configure slave ID only once.Instead of hardware configure.
2)```
Modbus_setup()
```this function has to be called once, I put in setup. if use the slave.begin i cant able to debug. if i assume slave ID being changed ; & use Modbus poll with slave ID chosen I wont get any response.
Get Error Not receive data from SLave Or Timeout request error.
3) When i assign the ID directly between 1-255 . it works properly considering example code.
The library related information are below.
[http://arduino-experience.blogspot.in/2 ... ample.html](http://arduino-experience.blogspot.in/2014/02/modbus-slave-example.html)
#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;
void Modbus_setup()
{
curr_time= millis();
if(curr_time<4000)
{
Device_ID=1;
// Serial.print("before ID check:");Serial.println(Device_ID);
}
else
{
Device_ID=ID_Check();
// Serial.print(“after ID check:”);Serial.println(Device_ID);
}
// }
Modbus slave(Device_ID,0,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);
slave.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);
// Modbus slave(Device_ID,0,0);
// slave.begin(9600);
}
void loop() {
Modbus_setup();
delay(1000);
for(int i = 0; i<8; i++)
{
au16data[i];
}
}