Hi guys,
I have problem in reading data from SELEC MFM383A Multifunction Meter
My hardware includes: ESP32 <-> TTL to RS485 <-> SELEC MFM383A Multifunction Meter.
I use ModbusRtu.h library to read data from SELEC MFM383A (GitHub - smarmengol/Modbus-Master-Slave-for-Arduino: Modbus Master-Slave library for Arduino).
I can read V1, V2, V3 but I can not read Power Factor1, Power Factor 2, Power Factor 3, Frequency, Power (all above values = 0). I don't understand what wrong in my code.
Thank you for your attention.
My code
#include <ModbusRtu.h>
union Fu {
float f;
uint32_t u;
};
// data array for modbus network sharing
uint16_t au16data[64];
uint8_t u8state;
uint8_t u8query = 0;
Modbus master(0, Serial2, 0); // this is master and RS-232 or USB-FTDI / Serial2
//UART2 RX2: GPIO16 TX2: GPIO17
modbus_t telegram[2];
unsigned long u32wait;
void setup() {
Serial.begin(9600);
Serial2.begin(9600);
master.start();
master.setTimeOut(2000); // if there is no answer in 2000 ms, roll over
u32wait = millis() + 1000;
u8state = 0;
telegram[0].u8id = 1; // slave address
telegram[0].u8fct = 4; // function code 04 (this one is registers read)
telegram[0].u16RegAdd = 0; // start address in slave
telegram[0].u16CoilsNo = 64; // number of elements (coils or registers) to read
telegram[0].au16reg = au16data; // pointer to a memory array in the Arduino
}
void loop() {
switch (u8state) {
case 0:
if (millis() > u32wait) u8state++; // wait state
break;
case 1:
master.query(telegram[u8query]); // send query (only once)
u8state++;
break;
case 2:
master.poll(); // check incoming messages
if (master.getState() == COM_IDLE) {
u8state = 0;
u8query++;
if (u8query > 0) {
u8query = 0;
}
u32wait = millis() + 100;
}
break;
}
readDataFromSelec();
}
void readDataFromSelec(){
//Read V1
float v1n = decodeFloat(au16data[0], au16data[1]);
Serial.print("V1: ");
Serial.print(v1n);
Serial.print(" | ");
//Read V2
float v2n = decodeFloat(au16data[2], au16data[3]);
Serial.print("V2: ");
Serial.print(v2n);
Serial.print(" | ");
//Read V2
float v3n = decodeFloat(au16data[4], au16data[5]);
Serial.print("V3: ");
Serial.print(v3n);
Serial.print(" | ");
//Read I1
float i1 = decodeFloat(au16data[16], au16data[17]);
Serial.print("I1: ");
Serial.print(i1);
Serial.print(" | ");
//Read I2
float i2 = decodeFloat(au16data[18], au16data[19]);
Serial.print("I2: ");
Serial.print(i2);
Serial.print(" | ");
//Read I3
float i3 = decodeFloat(au16data[20], au16data[21]);
Serial.print("I3: ");
Serial.print(i3);
Serial.print(" | ");
//Read Power Factor 1
float pf1 = decodeFloat(au16data[48], au16data[49]);
Serial.print("PF1: ");
Serial.print(pf1);
Serial.print(" | ");
//Power Factor 2
float pf2 = decodeFloat(au16data[50], au16data[51]);
Serial.print("PF2: ");
Serial.print(pf2);
Serial.print(" | ");
//Power Factor 3
float pf3 = decodeFloat(au16data[52], au16data[53]);
Serial.print("PF3: ");
Serial.print(pf3);
Serial.print(" | ");
//Read Frequency
float f = decodeFloat(au16data[56], au16data[57]);
Serial.print("f: ");
Serial.print(f);
Serial.print(" | ");
//Read P (kWh)
float P = decodeFloat(au16data[58], au16data[59]);
Serial.print("P: ");
Serial.print(P);
Serial.println(" | ");
}
float decodeFloat(uint16_t regs_1, uint16_t regs_2) {
union Fu fu;
fu.u = ((uint32_t)regs_2 << 16) | regs_1;
return fu.f;
}