0

I'm a newbie to this forum so kindly excuse me for any errors.

I'm using an Arduino Uno and Max485 (RS-485 to TTL) to communicate with a Power meter, but I'm able to receive the data from the Power Meter only once and then it prints "Error" continuously. I need to reopen the Serial Monitor to get the new reading but again the issue persists thereafter. Reading parameter is voltage.

I am using ModbusMaster library. Please refer to the code below.

#include <ModbusMaster.h>

uint16_t vl1, v11;
uint8_t result1;
float v1;

ModbusMaster  node;
#define MAX485_DE      2
#define MAX485_RE_NEG  3


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

void postTransmission()
{
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);
}


void setup()
{
  // Open serial communications and wait for port to open:

  pinMode(MAX485_RE_NEG, OUTPUT);
  pinMode(MAX485_DE, OUTPUT);

  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);

  Serial.begin(9600);

  node.begin(1, Serial);

  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);

}

void loop()
{
  result1 = node.readInputRegisters(0x0001, 5);

  delay(1000);

  if (result1 == node.ku8MBSuccess)
  {
    //Retrieiving data from energy meter via modbus

    vl1 = node.getResponseBuffer(0x01);
    v11 = node.getResponseBuffer(0x00);
    uint16_t data[2] = {va11, val1};
    memcpy(&v1, data, 4);
    Serial.println("Voltage:);
    Serial.print(v1);
  }

  else
  {
    Serial.println("Error");
  }
}

This is the output I get from the Serial Monitor

Voltage:221.21
Error
Error
Error

FYI, Meter communication details as follows:

Baud Rate: 9600, Parity: None, Stop bits: 1, Slave ID: 1

I'm unable to figure out the issue therefore I'm looking forward in seeking your assistance. Thank you!

  • Please, confirm you're using this ModBus library. Let us also know if you cut&pasted code from somewhere or if you really wrote it as... I'm a bit skeptical about the "delay(1000)" you added right after the readInputRegisters call. Why you used it? As a final note, please consider that based on official documentation here the readInputRegisters function returns..."0 on success; exception number on failure". As it's failing, please print its value as well. – Damiano Verzulli Apr 9 at 19:32
  • I used the "half duplex" example from the Modbus Master library and modified the program according to my need. If I print instead of error value, it repeats the same voltage value. – Sanj_Ard Apr 11 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.