Reading Energy Meter data through Modbus using Arduino

Submitted by Priya on Wed, 11/27/2019 - 10:49

Hello All,

Im trying to read holding register data values of energy meter through arduino mega using RS485 to TTL converter from past 1 month but I havent suceeded. Please help me out in this regard

Im using Elmeasure energy meter of model LG1129 & BM5140

RS485 to TTL converter module link

https://www.amazon.in/xcluma-MAX485-Module-Converter-Arduino/dp/B0728BTF5H

Im attaching the code i used:

#include <SimpleModbusMaster.h>

#define baud 19200
#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,
  //  PACKET3,
  //  PACKET4,
  //  PACKET5,
  //  PACKET6,
  //  PACKET7,
  //  PACKET8,
  //  PACKET9,
  //  PACKET10,
  //  PACKET11,
  //  PACKET12,
  //  PACKET13,
  //  PACKET14,
  //  PACKET15,
  //  PACKETn,

  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(19200);
Serial1.begin(19200);
  
  //    Serial1.begin(9600);READ_HOLDING_REGISTERS 43118
  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  //  modbus_construct(&packets[PACKET1], 1(adres PM710 sampai ke n), READ_INPUT_REGISTERS, 4010(adress registry), 1(jenis data), 10(pengurutan data sampai ke n));
  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  modbus_construct(&packets[PACKET1], 1, READ_HOLDING_REGISTERS, 0x40133, 2, 10); //L-N                  //A
  modbus_construct(&packets[PACKET2], 1, READ_HOLDING_REGISTERS, 0x40135, 2, 12); //Frekuency            //B
  //    modbus_construct(&packets[PACKET3], 1, READ_INPUT_REGISTERS, 4032, 1, 12); //A-N                  //C
  //    modbus_construct(&packets[PACKET4], 1, READ_INPUT_REGISTERS, 4033, 1, 13); //B-N                  //D
  //    modbus_construct(&packets[PACKET5], 1, READ_INPUT_REGISTERS, 4034, 1, 14); //C-N                  //E
  //
  //    modbus_construct(&packets[PACKET6], 2, READ_INPUT_REGISTERS, 3998, 2, 15); //REAL ENERGY          //F
  //    modbus_construct(&packets[PACKET7], 1, READ_INPUT_REGISTERS, 4005, 1, 16); //REAL POWER           //G
  //    modbus_construct(&packets[PACKET8], 1, READ_INPUT_REGISTERS, 4006, 1, 17); //AMP POWER            //H
  //    modbus_construct(&packets[PACKET9], 1, READ_INPUT_REGISTERS, 4007, 1, 18); //KVAR DAYA REAKTIF    //I
  //
  //    modbus_construct(&packets[PACKET10], 1, READ_INPUT_REGISTERS, 4029, 1, 19); //A-B                  //J
  //    modbus_construct(&packets[PACKET11], 1, READ_INPUT_REGISTERS, 4030, 1, 20); //B-C                  //K
  //    modbus_construct(&packets[PACKET12], 1, READ_INPUT_REGISTERS, 4031, 1, 21); //C-A                  //L
  //
  //    modbus_construct(&packets[PACKET13], 1, READ_INPUT_REGISTERS, 4019, 1, 22); //ARUS A               //M
  //    modbus_construct(&packets[PACKET14], 1, READ_INPUT_REGISTERS, 4020, 1, 23); //ARUS B               //N
  //    modbus_construct(&packets[PACKET15], 1, READ_INPUT_REGISTERS, 4021, 1, 24); //ARUS C               //O
  //
  //modbus_construct(&packets[PACKET2], 3, PRESET_MULTIPLE_REGISTERS, 0, 1, 20);
  /*
  Valid modbus byte formats are:
  SERIAL_8N2: 1 start bit, 8 data bits, 2 stop bits
  SERIAL_8E1: 1 start bit, 8 data bits, 1 Even parity bit, 1 stop bit
  SERIAL_8O1: 1 start bit, 8 data bits, 1 Odd parity bit, 1 stop bit
  */
  // Initialize the Modbus Finite State Machine
  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];
  //    C = regs[12];
  //    D = regs[13];
  //    E = regs[14];
  //    F = regs[15];
  //    G = regs[16];
  //    H = regs[17];
  //    I = regs[18];
  //    J = regs[19];
  //    K = regs[20];
  //    L = regs[21];
  //    M = regs[22];
  //    N = regs[23];
  //    O = regs[24];
  //    n = regs[n];

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

  Serial.print("B = ");
    Serial.println(B);
//  Serial.println(B / 333.333333);
  //
  //    Serial.print("C = ");
  //    Serial.println(C/10);
  //
  //    Serial.print("D = ");
  //    Serial.println(D/10);
  //
  //    Serial.print("E = ");
  //    Serial.println(E/10);
  //
  //    Serial.print("F = ");
  //    Serial.println(F);
  //
  //    Serial.print("G = ");
  //    Serial.println(G);
  //
  //    Serial.print("H = ");
  //    Serial.println(H);
  //
  //    Serial.print("I = ");
  //    Serial.println(I);
  //
  //    Serial.print("J = ");
  //    Serial.println(J);
  //
  //    Serial.print("K = ");
  //    Serial.println(K);
  //
  //    Serial.print("L = ");
  //    Serial.println(L);
  //
  //    Serial.print("M = ");
  //    Serial.println(M);
  //
  //    Serial.print("N = ");
  //    Serial.println(N);
  //
  //    Serial.print("O = ");
  //    Serial.println(O);
  Serial.println("============================================");
  delay(1000);

  //Voltage2 = regs[12];
  //Serial.print("Voltage L-N : ");
  //Serial.print(Voltage);
  //Serial.println(" V");
  //Serial.println(Voltage1);
  //Serial.println(Voltage2);
  //Serial.println(regs[10]);
  //Serial.println(regs[11]);

  //  Serial.println("DEBUG                     || OUTPUT");
  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  //  Serial.print("requests: ");
  //  Serial.print(packets[PACKET1].requests);
  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  //    Serial.print(" || Voltage L-N :");
  //    Serial.println(Voltage);
  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  //    Serial.print(" || Voltage L-N :");
  //    Serial.print(Voltage/10);
  //    Serial.println("   Volt");
  //    Serial.println("=======================================");
  //    Serial.print(" || Frekuency   :");
  //    Serial.print(F/100);
  //    Serial.println(" Hz");
  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  //              Serial.println(" V");
  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("           || ");
 
  //              Serial.println(" ");

  delay(1000);
}

 

The program looks good and I dont think the problem is with the program. What energy meter are using and how sure are you that the meter speaks RS485. Here in australia its not leagal to play with energy meters and its also not easy to tap out the communication with RS485 to TTL converter. But it can very well be done if your energy meter supports. So first make sure whats inside your energy meter.

Also if you are still doubtfull about your program you can set up anothe Arduino to speak RS485 and and try rading it with your RS485 to TTL converter. This way you can make sure your converter and code is working as expected. This link might be helpful 

https://circuitdigest.com/microcontroller-projects/rs485-serial-communication-between-arduino-uno-and-arduino-nano

  Joined August 11, 2018      35
Saturday at 01:52 PM