Community
OpenEnergyMonitor

Community

Reading data from energy meter through modbus protocol

Hi All,

Im trying to read holding register values of Elmeasure LG1129 and BM5140 energy meter using Modbus protocol through Arduino. I used RS485 to TTL converter between Energy meter and Arduino Mega but I haven’t succeeded. Please help me

This is the board i used for RS485 communication:
https://robokits.co.in/control-boards/interface-boards/max485-ttl-to-rs485-converter-module?gclid=EAIaIQobChMIhMDH_d2J5gIVizUrCh2MKwVHEAQYAiABEgKyZvD_BwE

Typical circuit diagram connection:

Some reference Code I got from forum and burned the same in Arduino:

#include <SimpleModbusMaster.h>

#define baud 9600
#define timeout 1000
#define polling 2000 // the scan rate
#define retry_count 10


#define TxEnablePin 2

#define TOTAL_NO_OF_REGISTERS 31


enum
{
  PACKET1,
  PACKET2,
  

  TOTAL_NO_OF_PACKETS // leave this last entry
};

// Create an array of Packets to be configured
Packet packets[TOTAL_NO_OF_PACKETS];

// Masters register array
unsigned int regs[TOTAL_NO_OF_REGISTERS];

void setup()
{
  // Initialize each packet
  Serial.begin(9600);
Serial1.begin(9600);
  
  
  modbus_construct(&packets[PACKET1], 1, READ_HOLDING_REGISTERS, 40133, 2, 10); 
  modbus_construct(&packets[PACKET2], 1, READ_HOLDING_REGISTERS, 40135, 2, 12); 
  
  modbus_configure(&Serial1, baud, SERIAL_8E1, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS, regs);
}

void loop()
{
  modbus_update();

  int A, B, C, D, E, F, G, H, I, J, K, L, M, N, O;

  A = regs[10];
  B = regs[12];

  
  Serial.print("A = ");
  Serial.println(A);


  Serial.print("B = ");
  Serial.println(B);

  Serial.println("============================================");
  delay(1000);

  
  Serial.print("successful_requests: ");
  Serial.println(packets[PACKET1].successful_requests);
  Serial.println("                        || ");
  Serial.print("failed_requests: ");
  Serial.print(packets[PACKET1].failed_requests);
  Serial.println("     || ");
  Serial.print("exception_errors: ");
  Serial.print(packets[PACKET1].exception_errors);
  Serial.println("     || ");
  Serial.print("connection: ");
  Serial.print(packets[PACKET1].connection);
  Serial.println("           || ");
 delay(1000);
}

Also Please clarify me this line of code what does number 10 represents although we have register number starting from 0 to 30

modbus_construct(&packets[PACKET1], 1, READ_HOLDING_REGISTERS, 40133, 2, 10); 

I’ve tried to find documentation for that library, but I cannot find anything that explains in words what each API call does, and it appears that the library is no longer supported and maintained.

I can suggest that either you look inside the library and work out what each parameter does, or look at using a better supported library.

Have you searched here? There are many threads mentioning Modbus.


To prevent duplication, I’m locking this thread.

1 Like