Assign slave ID to modbus library.

Discussion in 'Programmer's Corner' started by ajitnayak, Feb 9, 2015.

  1. ajitnayak

    Thread Starter Member

    Feb 22, 2013
    36
    0
    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.

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

    5. Modbus slave(Slave_ID,0,0); // this is slave @1 and RS-232 or USB-FTDI

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

    9. void loop() {
    10. slave.poll( au16data, 16 );
    11. }
    Slave ID function:
    1. int ID_Check() {
    2. int ID_value;
    3. for(int row=0;row<8;row++)
    4. {
    5. digitalWrite(SO_enable,array[row][0]);
    6. digitalWrite(S1_enable,array[row][1]);
    7. digitalWrite(S2_enable,array[row][2]);
    8. Status_Out[row]=digitalRead(Output_Read);

    9. }
    10. 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];
    11. return(ID_value);
    12. }
    Modified Setup function
    1. void setup()
    2. {
    3. int Device_id=ID_Check();
    4. Modbus slave(Device_id,0,0);
    5. slave.setID(Device_id);
    6. slave.begin(9600);
    7. }
    I have attached library for reference. Please let me know how can use the ID_check();i tested with serial monitor intially i found device ID changes when dip pin changes. problem faced when i declare like above.
     
    • lib.pdf
      File size:
      195.3 KB
      Views:
      69
Loading...