0

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
 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 = 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)
 u8state++;
 break;
 case 2:
 master.poll(); // check incoming messages
 if (master.getState() == COM_IDLE) {
 u8state = 0;
 u32wait = millis() + 100; 
 }
 break;
 }
 //au16data[4] = analogRead( 0 );
 for(i=0;i<=64;i++)
 {
 Serial.print(au16data[i]);
 }
 Serial.println("\n");
 Serial.println("\n");
 delay(1000);
 }
Juraj
18.3k4 gold badges31 silver badges49 bronze badges
asked May 23, 2018 at 7:10
10
  • source of the library? shouldn't you print the data only if it is received in case 2? Commented May 23, 2018 at 11:17
  • source of the library means #include <ModbusRtu.h> right?.So how to receive the polling data on master. Commented May 23, 2018 at 12:53
  • there is no ModbusRtu library in Library Manager. so from where do you have the library? Commented May 23, 2018 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/… Commented May 23, 2018 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 Commented May 23, 2018 at 13:11

1 Answer 1

0

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.

answered May 24, 2018 at 8:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.