Thread: Modbus arduino library

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    13

    Modbus arduino library

    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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
      for(int i = 0; i<8; i++)
      {
        au16data[i];
      }
    So what were you planning to do here?
    This just reads each element of the array, then throws the result of that read away.

    Assuming the optimiser doesn't remove the whole thing as being pointless (that is, free of any side effects).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2015
    Posts
    13

    Modbus slave ID assign

    Yes you are right When i change it to this code start working .
    Code:
    
    void loop() {
     slave->poll( au16data, 16 );
    }
    My application is string monitoring . I have used 3 analog mux Which shared by port Ao,A1,A2. A5 temprature,A3 High voltage. The code seems working fine when hard code the slave ID. when i assign using function i get errors..,



    Now I based on this i implemented code , For below i didn't receive data, Here so,s1,s2 also used to read analog mux value




    Code:
    #include <avr/wdt.h>
    #include"glob.h"
    Modbus *slave=0;
    int8_t state = 0;
    unsigned long tempus;
    // data array for modbus network sharing
    uint16_t au16data[30];
    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()
    {
      //  wdt_enable(WDTO_8S);
    
    
      Device_ID= ID_Check();
      slave=new Modbus(Device_ID,0,0);
    
    
    
    
      tempus = millis() + 1000;
      pinMode(3,OUTPUT);
      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(Enablepin, OUTPUT) ;// pin can enable/disable using digital IO 4 of arduino
      pinMode(A0, INPUT) ;
      pinMode(A5, INPUT) ;
      //spd & DC STATUS 
      pinMode(SPD_STATUS_PIN,INPUT);
      pinMode(DC_STATUS_PIN,INPUT);
      //Serial.begin(9600);
      slave->begin(9600);
    }
    void modbus_call() {
    
    
      for(int i = 0; i<8; i++)
      {
        au16data[i] = Mod_current1[i];
        au16data[i+8] = Mod_current2[i+8];
        au16data[i+16] = Mod_current3[i+16];
      }
    
    
      au16data[25]=(int)(temp*1000);
      au16data[26]=ID_Check();
      au16data[27]=SPD_Check();
      au16data[28]= DC_Status();
      au16data[29]=slave->getInCnt(); 
      au16data[30]=slave->getOutCnt(); 
      //  au16data[31]=slave.getErrCnt();
    }
    
    
    
    
    void loop() {
      // wdt_reset();
      Take_Reading();
      Take_Temp_Reading();
      modbus_call();
      // slave->poll( au16data, 30 );
    }
    Please let me know how to resolve this

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modbus Implimentation code
    By AJITnayak in forum C Programming
    Replies: 1
    Last Post: 04-09-2014, 11:56 AM
  2. modbus rtu c programming
    By barramundi9 in forum Networking/Device Communication
    Replies: 5
    Last Post: 08-02-2012, 09:32 PM
  3. Replies: 4
    Last Post: 04-10-2012, 10:51 AM
  4. TCP IP Modbus
    By boschow in forum Networking/Device Communication
    Replies: 0
    Last Post: 03-27-2008, 12:20 PM
  5. Termios Flags Rs232 Serial Ttys0 Modbus Rtu
    By Mighty-D in forum Linux Programming
    Replies: 2
    Last Post: 10-31-2006, 11:53 AM

Tags for this Thread