Problem with connection between Arduino Uno and Energy Meter

Hi guys, I am new to Arduino. I have a project about getting data from DPM-C530 to Arduino Uno using rs485. I use module 485 to convert rs485 to UART. But the monitor always prints the constant value ( ~ 170 - 180 ) if the register has data.
Here is the result in the monitor ( The correct value are V1 : 222V and V : 77V)

Here is my code:

#include <ModbusMaster.h>

#define MAX485_DE     3
#define MAX485_RE_NEG 2 

ModbusMaster node;

void preTransmission()
{
  digitalWrite(MAX485_RE_NEG, 1);
  digitalWrite(MAX485_DE, 1);
  delay(200);
}

void postTransmission()
{
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);
  delay(200);
}
void setup() {
  // put your setup code here, to run once:
  pinMode(MAX485_RE_NEG, OUTPUT);
  pinMode(MAX485_DE, OUTPUT);
  //Init receive mode
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);

  //Modbus communication runs at 9600 baud
  Serial.begin(9600);

  //Modbus slave ID 3
  node.begin(2, Serial);
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);  
}

void loop() {
  // put your main code here, to run repeatedly:
  uint8_t resultMain;
  uint16_t data[8];


  resultMain = node.readHoldingRegisters(0x0100, 8);
  if (resultMain == node.ku8MBSuccess)
  {
    Serial.println("---------------------");
    Serial.print("PM3_V1N :");
    Serial.println(((node.getResponseBuffer(0x00)/10000.0f)+ (node.getResponseBuffer(0x01)/ 100.0f)));
    Serial.print("PM3_V2N :");
    Serial.println(node.getResponseBuffer(0x03)/ 100.0f);
    Serial.print("PM3_V3N :");
    Serial.println(node.getResponseBuffer(0x05)/ 100.0f);
    Serial.print("PM3_V :");
    Serial.println((((node.getResponseBuffer(0x06)/10000.0f)+ (node.getResponseBuffer(0x07))/100.0f)));
    Serial.print("Freq :");
    //Serial.println((((node.getResponseBuffer(0x43))/100.0f)));
    delay(5000);
  }
}

Please copy and paste serial monitor text like You post code. Screen shots are too often difficult to read or unreadable, blurring out.
Please use comments explaining the purpose of the code. digitalWrite of a zero to a pin tells nothing....

Maybe the data should be like that..... What actions have been taken to make it change more than that?

1 Like

Your conversion is probably wrong. I guess you should use something like this:

uint32_t v = node.getResponseBuffer(0);
v = v << 16 | node.getResponseBuffer(1);
float voltage = *((float *) &v);

Please note that the parameter of getResponseBuffer() is an index into the 16bit response array. You have to do above conversion for every float value you read-

1 Like

Yeah I see that. It hasn't been converted to float. Thank you

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.