Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shifting readed data from Modbus electricity Meter. #41

Open
Vasekdvor opened this issue Apr 18, 2018 · 1 comment
Open

Shifting readed data from Modbus electricity Meter. #41

Vasekdvor opened this issue Apr 18, 2018 · 1 comment

Comments

@Vasekdvor
Copy link

Vasekdvor commented Apr 18, 2018

Hi i have this code:

#include <SimpleModbusMaster.h>
#include <AltSoftSerial.h>

#define TxEnablePin 2 // RS485 modbus direction control pin:
#define baud 9600 // modbus port speed:
#define timeout 200 // modbus timeout in mSec:
#define polling 150 // modbus scan rate in mSec:
#define retry_count 10

#define TOTAL_NO_OF_REGISTERS 52 // number of registers to poll for:

AltSoftSerial altSerial;

enum
{
PACKET1,
PACKET2,
PACKET3,
PACKET4,
PACKET5,
PACKET6,
PACKET7,
PACKET8,
PACKET9,
PACKET10,
PACKET11,
PACKET12,
PACKET13,
PACKET14,
TOTAL_NO_OF_PACKETS // leave this last entry
};

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

unsigned int regs[TOTAL_NO_OF_REGISTERS]; // master register array
long previousMillis = 0;
long interval = 1000;
unsigned long currentMillis;
unsigned long prev_req;

void setup() {
Serial.begin(9600);
altSerial.begin(9600);
// modbus_construct(packet, id of slave, function, register adress, number of registers, local_start_address);
// Packets initializing:
modbus_construct(&packets[PACKET1], 5, READ_HOLDING_REGISTERS, 0x5000, 2, 0); // L1 Voltage
modbus_construct(&packets[PACKET2], 5, READ_HOLDING_REGISTERS, 0x5008, 2, 2); // Grid freqency
modbus_construct(&packets[PACKET3], 5, READ_HOLDING_REGISTERS, 0x500A, 2, 4); // L1 Current
modbus_construct(&packets[PACKET4], 5, READ_HOLDING_REGISTERS, 0x5012, 2, 6); // Total active power
modbus_construct(&packets[PACKET5], 5, READ_HOLDING_REGISTERS, 0x501A, 2, 8); // Total reactive power
modbus_construct(&packets[PACKET6], 5, READ_HOLDING_REGISTERS, 0x5022, 2, 10); // Total apparent power
modbus_construct(&packets[PACKET7], 5, READ_HOLDING_REGISTERS, 0x502A, 2, 12); // Power factor

modbus_construct(&packets[PACKET8], 5, READ_HOLDING_REGISTERS, 0x6000, 2, 14); // Total active energy
modbus_construct(&packets[PACKET9], 5, READ_HOLDING_REGISTERS, 0x600C, 2, 16); // Forward active energy
modbus_construct(&packets[PACKET10], 5, READ_HOLDING_REGISTERS, 0x6018, 2, 18); // Reverse active energy
modbus_construct(&packets[PACKET11], 5, READ_HOLDING_REGISTERS, 0x6024, 2, 20); // Total reactive energy
modbus_construct(&packets[PACKET12], 5, READ_HOLDING_REGISTERS, 0x6030, 2, 22); // Forward reactive energy
modbus_construct(&packets[PACKET13], 5, READ_HOLDING_REGISTERS, 0x603C, 2, 24); // Reverse reactive energy
modbus_construct(&packets[PACKET14], 5, READ_HOLDING_REGISTERS, 0x6049, 2, 26); // Ressetable day counter

modbus_configure(&Serial, baud, SERIAL_8E1, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS, regs);
}

void loop() {

modbus_update();

currentMillis = millis();

if (packets[PACKET1].successful_requests > prev_req)
{
prev_req = packets[PACKET1].successful_requests;

   float VoltageL1; 
   unsigned long temp_voltage = (unsigned long)regs[0] << 16 | regs[1];
   VoltageL1 = *(float*)&temp_voltage; 
  
   float Grid_freqency;
   unsigned long temp_freq = (unsigned long)regs[2] << 16 | regs[3];
   Grid_freqency = *(float*)&temp_freq;

   float CurrentL1;
   unsigned long temp_curr = (unsigned long)regs[4] << 16 | regs[5];
   CurrentL1 = *(float*)&temp_curr;

   float Tot_activ_power;
   unsigned long temp_act_power = (unsigned long)regs[6] << 16 | regs[7];
   Tot_activ_power = *(float*)&temp_act_power;

   float Tot_reactiv_power;
   unsigned long temp_react_power = (unsigned long)regs[8] << 16 | regs[9];
   Tot_reactiv_power = *(float*)&temp_react_power;

   float Tot_apparent_power;
   unsigned long temp_appa_power = (unsigned long)regs[10] << 16 | regs[11];
   Tot_apparent_power = *(float*)&temp_appa_power;
   
   float power_factor;
   unsigned long temp_power_fact = (unsigned long)regs[12] << 16 | regs[13];
   power_factor = *(float*)&temp_power_fact;
     
   /*altSerial.print("Exception errors: ");
   altSerial.println(packets[PACKET1].exception_errors);*/
  
   altSerial.print("Failed requests: ");
   altSerial.println(packets[PACKET1].failed_requests);
  
   altSerial.print("Successful requests: ");
   altSerial.println(packets[PACKET1].successful_requests);
        
   
   altSerial.print("L1 Voltage: ");
   altSerial.println(VoltageL1);
   
   altSerial.print("Grid Frequency: ");
   altSerial.println(Grid_freqency);

  
   altSerial.print("L1 Current: ");
   altSerial.println(CurrentL1);

  
   altSerial.print("Total active Power: ");
   altSerial.println(Tot_activ_power);

  
   altSerial.print("Total reactive Power: ");
   altSerial.println(Tot_reactiv_power);

   
   altSerial.print("Total apparent Power: ");
   altSerial.println(Tot_apparent_power);

   
   altSerial.print("Power factor: ");
   altSerial.println(power_factor);

   altSerial.println("----------");

previousMillis = currentMillis;
}

}

And this is an example of result:

Failed requests: 2
Successful requests:
L1 Voltage: 228.50
Grid Frequency: 49.97
L1 Current: 1.25
Total active Power: 0.29
Total reactive Power: 0.00
Total apparent Power: 0.29
Power factor: 1.00

Which is OK but sometimes happens that these values are shifted by one forward.
What i mean is in place where is Grid Frequency, it passes a value of L1 Voltage and so on.

Like this:

Failed requests: 2
Successful requests:
L1 Voltage: 0.12
Grid Frequency: 229.70
L1 Current: 49.97
Total active Power: 1.26
Total reactive Power: 0.29
Total apparent Power: 0.00
Power factor: 0.29

It is reading Data from electricity meter "Inepro Pro1-Mod".
I have tried to increase and decrease the timeout and polling but it doesn't help so much.

Can you help me why is these values sometimes shifted ?
I think that one register is not red and then its shifts all values.
Can you please help me with a solution? Or what am i doing wrong?

@rtu-dataframe
Copy link

Have you ever solved that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants