Need of Modbus TCP Server Library Example- Reg

Hi Sir,

I am using Arduino Mega + Arduino Ethernet Shield 2 Board. Using that, I am Planning to use the Arduino Boards as a Modbus TCP Server and My PC as a Master Client. On the PC Side, I am using the Modscan32 Application as a Master Client for testing.

I am planning to use the Arduino Modbus TCP Server library. Can you Please share the library Example which supports Arduino as Modbus TCP server to communicate with the Modsca32 Master Client.

Hi sir,

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

bool _1s;
unsigned long TimeAct, TimePrev, HodingResult, InputResult, HeartBeat, i, StartingAddr;
long Cmd;

EthernetServer EthServer(502);
ModbusTCPServer modbusTCPServer;

void setup() {
  // Ethernet Settings
  byte mac[] = { 0x4E, 0xA0, 0xBE, 0x3F, 0xFE, 0x0F };  // Define MAc address
  byte ip[] = { 192, 168, 1, 23 };                      // Define IP address
  byte subnet[] = { 255, 255, 255, 0 };                 // Define SubNEt mask

  // initialize the ethernet device
  Ethernet.begin(mac, ip, subnet);                      // Assign MAC, IP, and subnet mask
  Serial.begin(9600);
  EthServer.begin();          // start listening for clients
  modbusTCPServer.begin();    // start listening for clients

  // Define Holding register:
  HodingResult = modbusTCPServer.configureHoldingRegisters(0, 100);
  InputResult = modbusTCPServer.configureInputRegisters(0, 100);

  Serial.print("Holding Reg init result =");
  Serial.print(HoldingResult);
  Serial.print("\n");

  Serial.print("Input Reg init result =");
  Serial.print(InputResult);
  Serial.print("\n");

  Serial.print("Modbus server address=");
  Serial.println(Ethernet.localIP());
  Serial.print("\n");
}

void loop() {
  // Modbus server accept incoming connections
  EthernetClient client = EthServer.available();

  if (client.connected()) {
    modbusTCPServer.accept(client);
    // poll for Modbus TCP requests, while client connected
    modbusTCPServer.poll();
    // Serial.print("poll");
  }

  //---------------------------------------------------------------------------------
  // Modbus server  :

  // holding resgiter 40001: heartbeat (FC3)
  modbusTCPServer.holdingRegisterWrite(0x00, HeartBeat);

  // holding resgiter 40500: Command Word (FC6)
  Cmd = modbusTCPServer.holdingRegisterRead(90);
}

With the above Modbus TCP Server library Example, i am able to write and read the Holding register from my Modscan Master client application.

I have to identify whether the Frame received from the Master is Function code -->read(03) or Multiplewrite(10).

If the Frame received is read --> I have to read the holding register.
If the Frame received is MultipleWrite --> I have to write into that holding register and also to the EEPROM location.

So How to differentiate the Function code received is read or multiple write function. Kindly help on this.

@Juraj Sir. Can you Please guide on this.?

sorry. no. I never used the ArduinoModbus library

1 Like

Ok Sir. Thanks for the reply. Did anyone can help me on this.?

@pylon Sir @markd833 Sir. Can you Please guide on this.? I was stuck in this.

I'm sorry but I have no experience of using Modbus TCP.

1 Like

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