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!