Could you tell me the mistake here?

I want to connect modbus TCP/IP from mega2560 using W5100 to Laptop and read the holding register, using modbus simulation, here is the code but it not work, it shows " no matching function for call to ' modbustTCP(int)', Could you show me how to fix that problem ? thanks you. This code is example modbus TCP Master W5100.!

[code] :


/
 This is Modbus test code to demonstrate all the Modbus functions with
 with Ethernet IC WIZNET W5100

 ModbusTCP is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 ModbusTCP is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with ModbusTCP.  If not, see <http://www.gnu.org/licenses/>.

 Adopted from ModbusMaster for RTU over RS-485 by Doc Walker
 Modified by Narendra Dehury for TCP.
 copyright @ phoenixrobotix.com

/
#define WIZNET_W5100 1

unsigned int param_value_int[7];
#include <Ethernet.h>

IPAddress ModbusDeviceIP(192, 168, 1, 8);  // Put IP Address of PLC here
IPAddress moduleIPAddress(192, 168, 1, 7);  // Assign Anything other than the PLC IP Address

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

#include <ModbusTCP.h>

ModbusTCP node(1);                            // Unit Identifier.

void setup()
{

 pinMode(4, OUTPUT);
 digitalWrite(4, HIGH);                      // To disable slave select for SD card; depricated.

 Serial.begin(9600);
 delay(1000);
 Ethernet.begin(mac, moduleIPAddress);
 node.setServerIPAddress(ModbusDeviceIP);
 delay(500);                                // To provide sufficient time to initialize.

}

void loop()
{
 uint8_t result;

 node.setTransactionID(random(100));           // Not necessary; but good to distinguish each frame individually.
 result = node.readHoldingRegisters(0, 1);    // Read Holding Registers

 Serial.println(result, HEX);
 if (result != 0)
 {
   Serial.println("TimeOut");

   delay(6000);
 }

 int len = node.getResponseBufferLength();
 Serial.println("Response Length: " + String(len));// See the length of data packet received.
 for (byte j = 0; j < len; j++)
 {
   Serial.print(node.getResponseBuffer(j));       // Inspect the data.
   Serial.print(" ");
 }
 Serial.println();
 node.clearResponseBuffer();
 delay(100);

 /

   node.writeSingleRegister(5, 3);                  // Write single register
   Serial.println(result, HEX);
   delay(500);



   for (byte i = 0; i < 5; i++)
   {
     node.setTransmitBuffer(i, (i+100));
   }
   node.writeMultipleRegisters(2, 5);               // Write multiple register
   Serial.println(result, HEX);
   delay(500);



   node.writeSingleCoil(20, 1);                     // Write Single coil
   delay(500);



   node.setTransmitBuffer(0, 0b1010101001010101);

   node.writeMultipleCoils(20, 16);                 // Write multiple coils
   delay(500);
   result = node.readCoils(20, 18);
   len = node.getResponseBufferLength();
   Serial.println("Response Length: " + String(len));
   Serial.println(node.getResponseBuffer(0), BIN);


   node.clearResponseBuffer();
/
 delay(500);
}`

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation for your ask).

1 Like

thank you so much, my first time do in here, could you tell me is it fine now ?

Hi,
This is your code formatted and a small comments error fixed.

/*

This is Modbus test code to demonstrate all the Modbus functions with

with Ethernet IC WIZNET W5100



ModbusTCP is free software:
you can redistribute it and / or modify

it under the terms of the GNU General Public License as published by

the Free Software Foundation, either version 3 of the License, or

(at your option) any later version.



ModbusTCP is distributed in the hope that it will be useful,

          but WITHOUT ANY WARRANTY;
without even the implied warranty of

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

GNU General Public License for more details.



You should have received a copy of the GNU General Public License

along with ModbusTCP.  If not, see < Licenses - GNU Project - Free Software Foundation > .



Adopted from ModbusMaster for RTU over RS - 485 by Doc Walker

  Modified by Narendra Dehury for TCP.

    copyright @ phoenixrobotix.com



   */

#define WIZNET_W5100 1

unsigned int param_value_int[7];

#include <Ethernet.h>


IPAddress ModbusDeviceIP(192, 168, 1, 8);  // Put IP Address of PLC here

IPAddress moduleIPAddress(192, 168, 1, 7);  // Assign Anything other than the PLC IP Address


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };


#include <ModbusTCP.h>


ModbusTCP node(1);                            // Unit Identifier.


void setup()

{
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);                      // To disable slave select for SD card; depricated.
  Serial.begin(9600);
  delay(1000);
  Ethernet.begin(mac, moduleIPAddress);
  node.setServerIPAddress(ModbusDeviceIP);
  delay(500);                                // To provide sufficient time to initialize.
}


void loop()

{
  uint8_t result;
  node.setTransactionID(random(100));           // Not necessary; but good to distinguish each frame individually.
  result = node.readHoldingRegisters(0, 1);    // Read Holding Registers
  Serial.println(result, HEX);
  if (result != 0)
  {
    Serial.println("TimeOut");
    delay(6000);
  }
  int len = node.getResponseBufferLength();
  Serial.println("Response Length: " + String(len));// See the length of data packet received.
  for (byte j = 0; j < len; j++)
  {
    Serial.print(node.getResponseBuffer(j));       // Inspect the data.
    Serial.print(" ");
  }
  Serial.println();
  node.clearResponseBuffer();
  delay(100);
  /
  node.writeSingleRegister(5, 3);                  // Write single register
  Serial.println(result, HEX);
  delay(500);
  for (byte i = 0; i < 5; i++)
  {
    node.setTransmitBuffer(i, (i + 100));
  }
  node.writeMultipleRegisters(2, 5);               // Write multiple register
  Serial.println(result, HEX);
  delay(500);
  node.writeSingleCoil(20, 1);                     // Write Single coil
  delay(500);
  node.setTransmitBuffer(0, 0b1010101001010101);
  node.writeMultipleCoils(20, 16);                 // Write multiple coils
  delay(500);
  result = node.readCoils(20, 18);
  len = node.getResponseBufferLength();
  Serial.println("Response Length: " + String(len));
  Serial.println(node.getResponseBuffer(0), BIN);
  node.clearResponseBuffer();
  /
  delay(500);
}

It does not compile for me, as I believe I don't have that current library.

Tom.. :smiley: :+1: :coffee: :australia:

1 Like

Can you describe your setup (electrical schematics, parts you use etc ?) this is often as important as the code

1 Like

My controller arduino is Mega2560 , i use module ethernet W5100 and i want to solar inverter but i don't have it now, so i want to connect to my laptop instead to read the holding register in modbus simulation . but unfortunately the example not work, when i upload the error : no matching function for call to 'modbusTCP :: ModbusTCP(int)' .. Could you tell me what the problem is?

I guess its library is ModbusTCP.h ! but it still doesn't work @@

assuming this is this library ➜ GitHub - goddland16/Modbus-TCP: Modbus TCP client library to interact with Modbus servers such as PLCs.
(which seems crap as it's left with a .h that does not even compile properly)

1/ download the Zip of the current source code.

2/ Unzip and rename Modbus-TCP-master into Modbus-TCP

3/ Open and Edit the ModbusTCP.h file in that directory

Change the crappy start of the code which reads

#ifndef Modbus_TCPIP_h
#define Modbus_TCPIP_h

#ifndef WIZNET_W5100
#define WIZNET_W5100  0       /**< define 1 if  WIZNET W5100 IC is used, otherwise 0 */
#ifndef ENC28J60
#define ENC28J60      0       /**< define 1 if  ENC28J60 IC is used, otherwise 0     */
#ifndef ESP8266
#define ESP8266       1

into this

#ifndef Modbus_TCPIP_h
#define Modbus_TCPIP_h

#ifndef WIZNET_W5100
#define WIZNET_W5100  1       /**< define 1 if  WIZNET W5100 IC is used, otherwise 0 */
#endif
#ifndef ENC28J60
#define ENC28J60      0       /**< define 1 if  ENC28J60 IC is used, otherwise 0     */
#endif
#ifndef ESP8266
#define ESP8266       0
#endif

4/ Move Modbus-TCP into your personal Arduino library folder

5/ try compiling this code, which is a modified version of the code posted by @TomGeorge where I removed weird '/' characters in the middle of the code and rearranged a bit as I saw fit. (removed some delays, move the Serial terminal to 115200 bauds, removed printing stuff that was useless like printing result when you where not updating its value etc)

#include <ModbusTCP.h>
ModbusTCP node(1);                            // Unit Identifier.

unsigned int param_value_int[7];
IPAddress ModbusDeviceIP(192, 168, 1, 8);  // Put IP Address of PLC here
IPAddress moduleIPAddress(192, 168, 1, 7);  // Assign Anything other than the PLC IP Address

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

void setup() {
  Serial.begin(115200);
  Ethernet.begin(mac, moduleIPAddress);
  node.setServerIPAddress(ModbusDeviceIP);
  delay(500);                                // To provide sufficient time to initialize.
}

void loop() {
  node.setTransactionID(random(100));           // Not necessary; but good to distinguish each frame individually.
  uint8_t result = node.readHoldingRegisters(0, 1);    // Read Holding Registers
  Serial.println(result, HEX);
  if (result != 0) {
    Serial.println("TimeOut");
    delay(6000);
  }

  size_t len = node.getResponseBufferLength();
  Serial.print("Response Length: "); Serial.println(len);// See the length of data packet received.
  for (byte j = 0; j < len; j++) {
    Serial.print(node.getResponseBuffer(j));       // Inspect the data.
    Serial.write(' ');
  }
  Serial.println();
  node.clearResponseBuffer();

  node.writeSingleRegister(5, 3);                  // Write single register
  for (byte i = 0; i < 5; i++) node.setTransmitBuffer(i, (i + 100));

  node.writeMultipleRegisters(2, 5);               // Write multiple register
  node.writeSingleCoil(20, 1);                     // Write Single coil
  node.setTransmitBuffer(0, 0b1010101001010101);
  node.writeMultipleCoils(20, 16);                 // Write multiple coils

  result = node.readCoils(20, 18);
  len = node.getResponseBufferLength();
  Serial.print("Response Length: "); Serial.println(len);// See the length of data packet received.
  Serial.println(node.getResponseBuffer(0), BIN);
  node.clearResponseBuffer();

  delay(500);
}

NOTE : I've no clue about what it does, so actual result may be far from what you expect, but may be it will compile....

Also given the quality of the .h in the library, I'm unsure it's a reliable Modbus library to use. Last update is 4 years ago, there is an issue posted 2 years ago with the same compile error that has not even been answered and so this library is probably abandoned and did not follow progress of the Ethernet Library for example. I'd suggest you search for something more current. A quick search showed ➜ ArduinoModbus - Arduino Reference

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