ModbusTCP Communication

Hi,

I need to establish a communication between Arduino and a power meter which uses Modbus TCP/IP for the communication.

I am sending a command with a function code 3 to get the reply from modbus. I am getting the reply same as the request which is not a proper output. Could you please help me how can I get the correct output.Or what is the output for the given request.Thank you.

Client : Arduino mega 2560
Server: Rail 350V , modbus TCP/IP

I/p : Request : 01 00 06 03 20 01
O/p: Reply: 01 00 06 03 20 01

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 0, 46);   //Client - Arduino uC - local IP

// Enter the IP address of the server you're connecting to:
IPAddress server(192, 168, 0, 127); // Server:Power meter Modbus

// Initialize the Ethernet client library
// with the IP address and port of the server
EthernetClient client;


void sendCommandToServer(byte fCode, int stAddress, int count)
{
    Serial.print("st address sent ");
    Serial.println(stAddress);
    byte mbMsg[12] ;
    int i;
    mbMsg[0] = 0;   //Transaction Id - high byte
    mbMsg[1] = 1;   //Transaction Id - low byte
    
    mbMsg[2] = 0;   //Protocol Id - high byte
    mbMsg[3] = 0;   //Protocol Id - low byte
    
    mbMsg[4] = 0;   //Length - high byte
    mbMsg[5] = 6;   //Length - low byte
    
    mbMsg[6] = 0;   //Unit Id  
    mbMsg[7] = fCode;   //Funcion Code
    
    mbMsg[8] = highByte(stAddress); // Starting Addres
    mbMsg[9] = lowByte(stAddress);
    
    mbMsg[10] = highByte(count);  //Quantity of registers to be read
    mbMsg[11] = lowByte(count);

    
    for(i = 0 ; i<sizeof(mbMsg); i++)
    {
      client.write(mbMsg[i]);
      Serial.print(mbMsg[i]);
    }
   
}
  

void setup() 
{
  // start the Ethernet connection:
  Ethernet.begin(mac, ip);
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) 
  {
    ;       // wait for serial port to connect. Needed for native USB port only
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");
  // if you get a connection, report back via serial:
  if (client.connect(server,502)) 
  {
      Serial.println("connected");
  } 
  else 
  {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop() 
{
    // if there are incoming bytes available from the server, read them and print them:
    if (client.available()) 
    {
        
        Serial.print("Server Response is: ");
        byte c = client.read();
        Serial.println(c);             
    }
  // as long as there are bytes in the serial queue,
  // read them and send them out the socket if it's open: (i.e. send data to server )  
    while (Serial.available() > 0) 
    {
      byte val = Serial.read();
      if (client.connected()) 
        {
              int address = 512;
              int count = 2;
              sendCommandToServer(3, address , count);
        }
    }
    // if the server's disconnected, stop the client:
    if (!client.connected()) 
    {
        Serial.println();
        Serial.println("disconnecting.");
        client.stop();
        // do nothing:
        while (true);
    }
}

This is the output I am getting at the serial COM:

Client Request: 010006032001

Server Response is: 0
Server Response is: 1
Server Response is: 0
Server Response is: 0
Server Response is: 0
Server Response is: 6
Server Response is: 0
Server Response is: 3
Server Response is: 2
Server Response is: 0
Server Response is: 0
Server Response is: 1