Arduino Modbus TCB/IP (Connection to Easergy P3)

Thread Starter

Tarek1266

Joined Oct 18, 2019
55
Hi everyone,
Hope you are doing well. I have a problem communicating with Easergy P3 Schneider device using Arduino to monitor the data on the serial of the Arduino. I am using Modbus TCP/IP communication protocol to have my job done. the setup is Arduino mega with W5100 Ethernet module connected to a router with the Easergy P3 device connected to the same router. Hope any one can help me.
Regards,


This is the code I am using.:
/*
  Modbus TCP Client Multiple for P1AM-100/P1AM-ETH
  This sketch increments a register of a Modbus TCP server.
  Multiple Clients can be created and maintained, but the Library
  code is blocking.  So all delays in communictions will delay the
  main loop() execution.
  This example uses Serial.print() to display status information.
  The Serial Monitor must be running for the sketch to start.
  Required Libraries which need to be installed.
  https://github.com/arduino-libraries/ArduinoModbus
  https://github.com/arduino-libraries/ArduinoRS485
*/

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

#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>

byte mac[] = { //Use the Reserved MAC printed on the right-side label of your P1AM-ETH.
  0x00, 0x1A, 0xD3, 0x40, 0x8A, 0x78
};
IPAddress ip(192, 168, 1, 1);//IP Address of the P1AM-ETH module.
int HR400001;
int HR400002;

EthernetClient clients[4];
ModbusTCPClient modbusTCPClient[4]={
  ModbusTCPClient(clients[0]),
  ModbusTCPClient(clients[1]),
  ModbusTCPClient(clients[2]),
  ModbusTCPClient(clients[3])
};

IPAddress servers[4]={//IP Addresses of the Servers
  IPAddress(192, 168, 1, 1),
  IPAddress(10, 11, 0,  23),
  IPAddress(10, 11, 0,  24),
  IPAddress(10, 11, 0,  25)
};

void setup() {
  Ethernet.begin(mac, ip);
  modbusTCPClient[0].setTimeout(500);//Adjust Response Timeout from 30 seconds to 500 ms.
//  modbusTCPClient[1].setTimeout(500);//Adjust Response Timeout from 30 seconds to 500 ms.
  Serial.begin(115200);// wait for serial port to connect.
  while (!Serial) {};// wait for serial port to connect.
  Serial.println("Modbus TCP Client Multiple Example");
  Serial.print("P1AM-ETH at IP:");
  Serial.println(ip);
}

void loop() {

  Serial.println(modbusTCPClient[0].connected());
  if (!modbusTCPClient[0].connected()) {// client not connected, start the Modbus TCP client
    Serial.print("Attempting to connect to Modbus TCP server at IP:");
    Serial.println(servers[0]);
    if (!modbusTCPClient[0].begin(servers[0])) {
      Serial.println("Modbus TCP Client failed to connect!");
    } else {
      Serial.println("Modbus TCP Client connected");
    }
  } else {// client connected
    if (!modbusTCPClient[0].holdingRegisterWrite(0x00, HR400001++)) {
      Serial.print("Failed to write! ");
      Serial.println(modbusTCPClient[0].lastError());
    }
  }

//  if (!modbusTCPClient[1].connected()) {// client not connected, start the Modbus TCP client
//    Serial.print("Attempting to connect to Modbus TCP server at IP:");
//    Serial.println(servers[1]);
//    if (!modbusTCPClient[1].begin(servers[1])) {
//      Serial.println("Modbus TCP Client failed to connect!");
//    } else {
//      Serial.println("Modbus TCP Client connected");
//    }
//  } else {// client connected
//    if (!modbusTCPClient[1].holdingRegisterWrite(0x01, HR400002--)) {
//      Serial.print("Failed to write! ");
//      Serial.println(modbusTCPClient[1].lastError());
//    }
//  }
}
image_2022-02-05_213215.png
 
Last edited:

djsfantasi

Joined Apr 11, 2010
8,323
There are different layers of communication over a network as compared to a directly connected serial link. These layers are defined in the “OSI Model”. A TCP/IP connection includes layers 1-5 (5 is actually omitted as it’s functions are included in layer 4 - TCP).

You’re left with the last two layers. Presentation includes translation of characters between disparate systems. An issue you’re unlikely to be concerned with.

That leaves the Applicstion layer, which concerns itself with the data stream contained in the communication. Parsing fields is a good application layer function.

So you’re left with establishing basic communications at the level 1-6 and getting the raw data which is communicated. You’re hardware and it’s libraries will be used to establish a connection and language. After establishing a connection, your app must read, parse and use the data.

So take a stepped approach in solving your problem. And be aware of what each step is.
 
Top