title: Arduino to Arduino Modbus RTU in RS-485
body: <p>I'm trying Arduino to Arduino (master-slave) communication using Modbus RTU protocol in RS-485. I am using an Arduino Mega 2560 for the project and using this <a href="https://github.com/smarmengol/Modbus-Master-Slave-for-Arduino" rel="nofollow noreferrer">library Modbus RTU</a>. As of now, the master simply reads data slave and prints in the Serial monitor.</p>
  
  <p>I tried the following code:</p>
  
  <p><strong>Slave</strong></p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;ModbusRtu.h&gt;
  #define ID   3
  
  Modbus slave(ID, 0, 0);
  boolean led;
  int8_t state = 0;
  unsigned long tempus;
  // data array for modbus network sharing
  uint16_t au16data[9];
  
  void setup() {
    // define i/o
    pinMode(2, INPUT);
    pinMode(3, INPUT);
    pinMode(4, INPUT);
    pinMode(5, INPUT);
  
    // start communication
    slave.begin(19200);
    tempus = millis() + 100;
    digitalWrite(13, HIGH);
  }
  
  void loop() {
    // poll messages
    // blink led pin on each valid message
    state = slave.poll( au16data, 9 );
    if (state &gt; 4) {
      tempus = millis() + 50;
      digitalWrite(13, HIGH);
    }
    if (millis() &gt; tempus) digitalWrite(13, LOW );
    // get digital inputs -&gt; au16data[0]
    bitWrite( au16data[0], 0, digitalRead( 2 ));
    bitWrite( au16data[0], 1, digitalRead( 3 ));
    bitWrite( au16data[0], 2, digitalRead( 4 ));
    bitWrite( au16data[0], 3, digitalRead( 5 ));
  
    // diagnose communication
    au16data[6] = slave.getInCnt();
    au16data[7] = slave.getOutCnt();
    au16data[8] = slave.getErrCnt();
  }
  </code></pre>
  
  <p><strong>Master</strong></p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;ModbusRtu.h&gt;
  
  // data array for modbus network sharing
  uint16_t au16data[16];
  uint8_t u8state;
  
  /**
   *  Modbus object declaration
   *  u8id : node id = 0 for master, = 1..247 for slave
   *  u8serno : serial port (use 0 for Serial)
   *  u8txenpin : 0 for RS-232 and USB-FTDI 
   *               or any pin number &gt; 1 for RS-485
   */
  Modbus master(0,1,1); // this is master and RS-485
  
  /**
   * This is an structe which contains a query to an slave device
   */
  modbus_t telegram;
  
  unsigned long u32wait;
  
  void setup() {
    master.begin(19200); // baud-rate at 19200
    master.setTimeOut(2000); // if there is no answer in 2000 ms, roll over
    u32wait = millis() + 1000;
    u8state = 0; 
    Serial.begin(9600);
  }
  
  void loop() {
    Serial.print(u8state);
    switch(u8state) {
    case 0:
      if (millis() &gt; u32wait) u8state++; // wait state
      break;
    case 1: 
      telegram.u8id = 1; // slave address
      telegram.u8fct = 3; // function code (this one is registers read)
      telegram.u16RegAdd = 1; // start address in slave
      telegram.u16CoilsNo = 4; // number of elements (coils or registers) to read
      telegram.au16reg = au16data; // pointer to a memory array in the Arduino
      master.query(telegram); // send query (only once)
      u8state++;
      break;
    case 2:
      master.poll(); // check incoming messages
      if (master.getState() == COM_IDLE) {
        u8state = 0;
        u32wait = millis() + 100; 
      }
      break;
    }
    Serial.println(au16data[0])
  }
  </code></pre>
  
  <p>The output in the serial monitor is not changed when the input at Pin 2 is high in slave.</p>
  
  <pre><code>20
  00
  </code></pre>
  
  <p>Could someone please tell me the possible cause and solution? If I've missed out anything, over- or under-emphasized a specific point, let me know in the comments.</p>