Dear all,
Here simple code for modbus slave for arduino. The code is working fine. with below configuration. Now I have alterned below code bit. I would to assign Slave_ID by hardware config.SO i am using 8:1 mux & 8 pin DIP switch . depend on 8 pin dip status we come to know which device ID being slected.
Problem i am facing assigning here, I could not able to assign slave iD globally. If i assign witin setup code wont work. can you suggest how can i assign.
Here simple code for modbus slave for arduino. The code is working fine. with below configuration. Now I have alterned below code bit. I would to assign Slave_ID by hardware config.SO i am using 8:1 mux & 8 pin DIP switch . depend on 8 pin dip status we come to know which device ID being slected.
Problem i am facing assigning here, I could not able to assign slave iD globally. If i assign witin setup code wont work. can you suggest how can i assign.
- #include <ModbusRtu.h>
- #define Slave_ID 1
- uint16_t au16data[16] = {
- 3, 1415, 9265, 4, 2, 7182, 28182, 8, 0, 0, 0, 0, 0, 0, 1, -1 };
- Modbus slave(Slave_ID,0,0); // this is slave @1 and RS-232 or USB-FTDI
- void setup() {
- slave.begin( 9600 ); // baud-rate at 19200
- }
- void loop() {
- slave.poll( au16data, 16 );
- }
- int ID_Check() {
- 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);
- }
- void setup()
- {
- int Device_id=ID_Check();
- Modbus slave(Device_id,0,0);
- slave.setID(Device_id);
- slave.begin(9600);
- }
Attachments
-
195.3 KB Views: 70