Library's class not recognized in code

Hello,

I am currently facing a problem while using the library ArduinoModbus 1.0.6. What I want to do is to read and write coils using Ethernet and Modbus TCP with my Arduino Mega2560 as the master.

I read that you need to use ModbusTCPClient to setup your Arduino as the master.
https://www.arduino.cc/reference/en/libraries/arduinomodbus/

Problem is, in my code, I can set up as a slave using ModbusTCPServer without any problem, but the IDE doesn't seem to recognize the ModbusTCPClient class at all, wich is weird because it comes from the same library. I tried writing "modbusTCPClient", "ModbusTCPclient" and "modbusTCPclient", I tried with an instance but same result: the class in not recognized.

Here is my code:

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


byte mac[]={0xA8, 0x61, 0x0A, 0xAE, 0x7A, 0x08}; // Adresse MAC de l'Arduino
IPAddress ipUno; // Adresse IP de l'Arduino
IPAddress ipAuto; // Adresse IP de l'automate

// Initialisation
void setup(){  
  Serial.begin(115200);
  
  // Démarer la connection Ethernet
  while(!Ethernet.begin(mac)){ // Demander une adresse en DHCP toutes les secondes si échec
    delay(1000);
  }
}

// Programme principale
void loop(){
  Ethernet.maintain(); // Maintenir la connection en DHCP

  ipAuto=lireIp(); // Obtenir l'IP de l'automate avec le dip switch

  if(!ModbusTCPClient.connected()){ // Se connecter à l'automate
    ModbusTCPClient.begin(ipAuto);
  }
}

Here is my errors:

Arduino : 1.8.15 (Windows 10), Carte : "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"


D:\2021-2022\Stage\Programme\Programme.ino: In function 'void loop()':

Programme:57:22: error: expected primary-expression before '.' token

   if(!ModbusTCPClient.connected()){ // Se connecter à l'automate

                      ^

Programme:58:20: error: expected unqualified-id before '.' token

     ModbusTCPClient.begin(ipAuto);

                    ^

exit status 1

expected primary-expression before '.' token

I put a screenshot so you can see the ModbusTCPClient not recognized (in black) and the ModBusTCPServer being recognized (orange).
Screen

Have someone an idea of where it is coming from, or how to solve this?

I thank you in advance and don't hesitate to ask for more details.

I don't see where you've instantiated an object of the ModbusTCPClient class.

I haven't instantiated an object of the ModbusTCPServer class but it is still recognized by the IDE as you can see in my previous screenshot.

I tried writing "modbusTCPClient", "ModbusTCPclient" and "modbusTCPclient", I tried with an instance but same result. Here's code and a screenshot (I'll add those details in my original post).

You can see the example I used, instantiation is at lines 29 and 30.
https://github.com/arduino-libraries/ArduinoModbus/blob/master/examples/TCP/EthernetModbusClientToggle/EthernetModbusClientToggle.ino

Here's the screen of me trying to instantiate:
Instance
You can see again the difference in colours that shows if it's recognized or not.

Then it won't work.

Post THAT code and the resultant errors. Use Code Tags, not a screen shot. Color is irrelevant, all identifiers may not be in the keywords file.

Ooooh, I didn't know that. All the libraries I used until now didn't have that problem so I was thinking: "Well, if it's black then there's a problem".

By instantiating an object, just as I did on my screenshot really, it works. I think the first time I tested it I did something bad because now it works.

I thank the both of you!

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