I am using DC energy meter as slave.When I am testing DC energy meter using Modbus tester tool,its showing value from 1 to 24 register.I am using my own atmega328P with on board RS485 as a master device.I have connected A,B from slave to mater board.Now,I want to read slave outputs using my master.For that I have written below mentioned code.when I am serially printing array,its showing 00000000000000000000000000000000769.I am not getting full data on my master.

#include <ModbusRtu.h>

// data array for modbus network sharing
uint16_t au16data[64];
uint8_t u8state;

int i;

 *  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 > 1 for RS-485
Modbus master(0,0,1); // this is master and RS-232 or USB-FTDI

 * This is an structe which contains a query to an slave device
modbus_t telegram;

unsigned long u32wait;

void setup() {
  master.begin( 9600 ); // baud-rate at 19200
  master.setTimeOut( 2000 ); // if there is no answer in 2000 ms, roll over
  u32wait = millis() + 1000;
  u8state = 0; 

void loop() {
  switch( u8state ) {
  case 0: 
    if (millis() > u32wait) u8state++; // wait state
  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 = 24; // 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)
  case 2:
    master.poll(); // check incoming messages
    if (master.getState() == COM_IDLE) {
      u8state = 0;
      u32wait = millis() + 100; 
 //au16data[4] = analogRead( 0 );

  • source of the library? shouldn't you print the data only if it is received in case 2?
    – Juraj
    May 23 '18 at 11:17
  • source of the library means #include <ModbusRtu.h> right?.So how to receive the polling data on master. May 23 '18 at 12:53
  • there is no ModbusRtu library in Library Manager. so from where do you have the library?
    – Juraj
    May 23 '18 at 13:06
  • I have downloaded this source code with library from Github .I have mentioned URL on below. github.com/smarmengol/Modbus-Master-Slave-for-Arduino/blob/… May 23 '18 at 13:09
  • Modbus (0,0,0) last argument I have changed as 1,because we are usin RS485 and My slave device of A,B is connected on my Master controller of MAX485 and Max is communicated with Atmega328P with the help of software serial.Pin is 8,9 May 23 '18 at 13:11

The response is received when ModbusRtu library object is again in the IDLE state after a request was sent. In your code based on the example from the library, the response is ready in state 2 which follows state 1 where the request was sent. If in state 2 when the master object state changes to IDLE, you can use the response in array au16data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.