Hey guys, I'm needing to read the data from a power meter PM5110 Schneider using converter rs485 and Arduino Mega2560, but my code is not being able to read the data . I've followed several similar tutorials but it didn't work out.
Follow my code below
#include "REG_PM5110.h"
#include <ModbusMaster.h>
#define baud 9600
#define timeout 100
#define polling 100
#define retry_count 10
ModbusMaster node;
#define MAX485_DE 22
#define MAX485_RE_NEG 23
void preTransmission()
{
digitalWrite(MAX485_RE_NEG, 1);
digitalWrite(MAX485_DE, 1);
}
void postTransmission()
{
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
}
//------------------------------------------------
// Convent 32bit to float
//------------------------------------------------
float HexTofloat(uint32_t x)
{
return (*(float*)&x);
}
uint32_t FloatTohex(float x)
{
return (*(uint32_t*)&x);
}
//------------------------------------------------
float Read_Meter_float(char addr , uint16_t REG)
{
float i = 0;
uint8_t result,j;
uint16_t data[4];
uint32_t value = 0;
node.begin(ID_meter,Serial1);
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
result = node.readHoldingRegisters(REG,4); ///< Modbus function 0x03 Read Holding Registers
delay(500);
if (result == node.ku8MBSuccess)
{
for (j = 0; j < 4; j++)
{
data[j] = (node.getResponseBuffer(j));
}
value = data[0];
value = value << 16;
value = value + data[1];
i = HexTofloat(value);
Serial.println("Connect modbus Ok.");
return i;
} else
{
Serial.print("Connect modbus fail. REG >>> "); Serial.println(REG); // Debug
delay(1000);
return 0;
}
}
void GET_METER()
{ // Update read all data
delay(1000);
for (char i = 0; i < Total_of_Reg ; i++)
{
DATA_METER [i] = Read_Meter_float(ID_meter, Reg_addr[i]);
}
}
//**************************************************************************************************************
void setup()
{
Serial.begin(9600);
Serial1.begin(9600,SERIAL_8N2);
Serial.println(F("Test"));
pinMode(MAX485_RE_NEG, OUTPUT);
pinMode(MAX485_DE, OUTPUT);
// Init in receive mode
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
}
void loop()
{
//float x = Read_Meter_float(ID_meter,Reg_Volt);
GET_METER();
Serial.println();
Serial.print("Current = "); Serial.print(DATA_METER[0]);Serial.println(" A");
Serial.print("Voltage = "); Serial.print(DATA_METER[1]);Serial.println(" V");
Serial.print("Frequency = "); Serial.print(DATA_METER[2]);Serial.println(" Hz");
Serial.print("Power = "); Serial.print(DATA_METER[3]);Serial.println(" Kw");
delay(3000);
}
The list of equipment records is at this link
PM5110.ino (2.5 KB)
REG_PM5110.h (361 Bytes)