Problem in Serial Communication with RS-485

Hello there,

Hope everyone here are fine and god bless you all.

I've a Modbus enabled Energy meter where I can take some readings from the meter using RS-485 medium. I've used Simple Modbus Master & Adam View software for data acquisition. Currently I'm trying to take readings using Arduino.

For this process, I'm using Max485 built in Arduino RS-485 module with Arduino Mega.

Pin configurations are:

DE & RE of Max485 module into arduino Mega's PIN 3 RO of Max485 module into Serial1 Rx DI of Max485 module into Serial1 Tx RS-485 twisted pair cables were connected accordingly and +5V power and GND were given to the module and Arduino.

I tried many libraries; but they didn't worked for me. So I choose the manual way by sending the Modbus command in correct order.

Yes, they worked very well only for a single parameter..!

Here is my code:

Setup function & Loop function:

#define controlPin 3
int i = 0;
bool print_en;


void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  pinMode(controlPin, OUTPUT);
}

void loop() {
  //kVAh
  Serial.print("kVAh: ");
  read_modbus(0x01, 0x04, 0x00, 0x3D, 0x00, 0x01,0xA0, 0x06);
//
//
//  //kWh
//  Serial.print("kWh: ");
//  read_modbus(0x01, 0x04, 0x00, 0x3B, 0x00, 0x01,0x40, 0x07);
//  
//
//  //Power factor
//  Serial.print("Power factor: ");
//  read_modbus(0x01, 0x04, 0x00, 0x31, 0x00, 0x01,0x60, 0x05);

//  //kVArh
//  Serial.print("kVArh: ");
//  read_modbus(0x01, 0x04, 0x00, 0x3F, 0x00, 0x01,0x01, 0xC6);

  Serial.println("******************************************************************************************");
}

Read Modbus Function:

void read_modbus(int slave_ID, int function_code, int reg_adr_byte_1, int reg_adr_byte_2, int num_of_reg_byte_1, int num_of_reg_byte_2, int CRC_1, int CRC_2){
  float x;
  int input[32];

  
  digitalWrite(controlPin, HIGH);
  /***************************************************************/
  Serial1.write(slave_ID);    //slave id
  //delay(1);
  Serial1.write(function_code);    //Function code
  //delay(1);
  Serial1.write(reg_adr_byte_1);    //First byte of the Register address to be read
  //delay(1);
  Serial1.write(reg_adr_byte_2);    //Second byte of the Register address to be read
  //delay(1);
  Serial1.write(num_of_reg_byte_1);    //Number of registers to be read byte 1
  //delay(1);
  Serial1.write(num_of_reg_byte_2);    //Number of registers to be read byte 2
  //delay(1);
  Serial1.write(CRC_1);    //CRC byte 1
  //delay(1);
  Serial1.write(CRC_2);    //CRC byte 2
  Serial1.flush();
  /***************************************************************/
  digitalWrite(controlPin, LOW);
  delay(100);

  if(Serial1.available()>0){
    //Serial.println("Serial available: " + String(Serial1.available()));
    //Serial.flush();
    while(Serial1.available()>0){
      input[i] = Serial1.read();
      i++;
    }
  }
  digitalWrite(controlPin, HIGH);
  
    //Serial.println(String(input[0])+","+String(input[1])+","+String(input[2])+","+String(input[3])+","+String(input[4])+","+String(input[5])+","+String(input[6])+","+String(input[7])+","+String(input[8])+","+String(input[9]));
    ((byte*)&x)[3]= input[3];
    ((byte*)&x)[2]= input[4]; 
    ((byte*)&x)[1]= input[5];
    ((byte*)&x)[0]= input[6];

    Serial.println(x,7);
    Serial.flush();
}

when I tried to take multiple readings, by uncommenting the declared functions in the loop, serial monitor keep printing the first reading for all the places wherever I declared Serial.print()

Kindly help me to solve this. May be what I have tried here is wrong. Please correct me If I'm so.

Thank you very Much.

I really don't understand the problem, And I think you should include "MODBUS" in our title as for me it seems to be a modbus problem more than RS-485, or you can even try in modbus forum threads there you can get better attention and support. Yatin

Thank you Yatin, I'll try.