Reading RS485 Modbus displays

Hi everybody.
I'm trying to read a Modbus RS485 display.
I'm using a MEGA 2560, a RS485 to TTL circuit based on MAX485.
I'm using Serial1 for the Max485. RE/DE both connected to pin 2
The final target is to read 6 displays (node 1 to 6) but I'm not there yet :rofl:.
I'm trying to read 1 display for the moment and I want to get
Voltage, frequency, Amperage.
I'm using a sample programme found on internet and it works, I get the reading but.
1- If I off the display (power lost) the last value is still displayed.
2- I tried to force to 0 the data before to send the request but this doesn't work, the result in this case is always 0.

This is the code

#include <SimpleModbusMaster.h>
#define baud 19200 
#define timeout 100 
#define polling 100 
#define retry_count 10 
#define TxEnablePin 2 
#define TOTAL_NO_OF_REGISTERS 13 
float V[7], A[7], F[7];

enum //Keep the number of registers in batches.
{
  PACKET1,
  PACKET2,
  PACKET3,
  PACKET4,
  TOTAL_NO_OF_PACKETS //leave this last entry.
};

Packet packets[TOTAL_NO_OF_PACKETS]; //Bringing data together
//unsigned int regs[TOTAL_NO_OF_REGISTERS];
int regs[TOTAL_NO_OF_REGISTERS];
float Result;
void setup() {
  Serial.begin(9600);
  modbus_configure(&Serial1, baud, SERIAL_8N1, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS, regs);   //กำหนดค่าตามเครื่องที่จะใช้อ่านค่า
  modbus_construct(&packets[PACKET1], 1, READ_INPUT_REGISTERS,0,1, 1);
}

void loop() {
	  delay(500);

Serial.print("Voltage 1: ");
Serial.println(readVolt(1));
Serial.print("Frequency: ");
Serial.print(readFreq(1));;
Serial.println(" Hz");


  }
float readVolt(int8_t Nd){
	//regs[0]=0; This doesn't work,
	modbus_construct(&packets[PACKET1], 1, READ_INPUT_REGISTERS,0,Nd, 0);
	modbus_update();
	Result=regs[0]*0.01;
	return Result;
}
float readFreq(int8_t Nd){
	//regs[12]=0;
	modbus_construct(&packets[PACKET2], 1, READ_INPUT_REGISTERS,12,Nd, 12);
	modbus_update();
	modbus_update();	
	Result=regs[12]*.1;
	return Result;
}	

This is the result

Voltage 1: 0.00
Frequency: 0.00 Hz
Voltage 1: 229.66
Frequency: 0.00 Hz
Voltage 1: 229.66
Frequency: 0.00 Hz
Voltage 1: 229.66
Frequency: 49.90 Hz
Voltage 1: 229.66
Frequency: 49.90 Hz
Voltage 1: 229.81
Frequency: 49.90 Hz

Voltage get one time 0 before to get the value
Frequency up to 3 times 0 before to get the value

I try to understand why I have those 0 at the beginning and how to solve this.

Thank you for your help

Thank you for your help.

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