Help! My very first Arduino code, stuck on one error

I am writing code for a modbus communication. the error I keep getting is an expected initializer but I cannot figure out what I need to do or put where to fix it. Error code is posted below the code.

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

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


IPAddress ip(10, 10, 1, 5);
IPAddress gateway(10, 10, 1, 100);
IPAddress subnet(255, 255, 255, 0);
IPAddress myDns(8, 8, 8, 8);
IPAddress serverIP(10, 10, 1, 2);


EthernetClient (ethClient);
EthernetClient modbusTCPClient = ethClient;
int increment = 0;

void setup() {
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
}

void loop() {
  int ethClient.modbusTCPClient.begin(serverIP, 503);
  if (ethClient.connected()) {
    int holdingRegisterWrite(3900, increment);
    increment = ++increment;
  };
  delay(500);
}```

WARNING: library ArduinoRS485 claims to run on samd, mbed_portenta, mbed_opta architecture(s) and may be incompatible with your current board which runs on avr architecture(s).
WARNING: library ArduinoModbus claims to run on megaavr, samd, mbed_nano, mbed_portenta, mbed_opta architecture(s) and may be incompatible with your current board which runs on avr architecture(s).
C:\Users\Nolan\Documents\Arduino\sketch_aug14a\sketch_aug14a.ino: In function 'void loop()':
C:\Users\Nolan\Documents\Arduino\sketch_aug14a\sketch_aug14a.ino:26:22: error: expected initializer before '.' token
int modbusTCPClient.begin(serverIP, 503);
^
Multiple libraries were found for "Ethernet.h"
Used: C:\Users\Nolan\Documents\Arduino\libraries\Ethernet
Not used: C:\Users\Nolan\AppData\Local\Arduino15\libraries\Ethernet
exit status 1

Compilation error: expected initializer before '.' token```

Welcome to the forum

Which board are you compiling for ?

This is trying to be two things, a function declaration, and a function call. Drop the leading "int".

This result is undefined. Just use this:

++increment;

I'm not an expert on the ethernet library but I see many issues:

  1. Why are you doing this?
EthernetClient (ethClient);
EthernetClient modbusTCPClient = ethClient;

Just do this:

EthernetClient modbusTCPClient;

  1. This doesn't make sense:

int ethClient.modbusTCPClient.begin(serverIP, 503);

There is no begin() for EthernetClient! What you need to use is the connect() method and do it one time in the setup() function.

  1. This is a function declaration. I think you meant to do a function call:

int holdingRegisterWrite(3900, increment);

  1. Behavior for this statement is undefined:

    increment = ++increment;

  2. You don't need a ';' after the if code block

  3. You need to check the hardware status and the link status of the Ethernet interface to aid in debugging. There is no sense in proceeding thru setup() if there is a hardware issue, the ethernet cable is not plugged in or you can't connect to the server!

Thank you!
It is an Uno R3 with an ethernet sheild

You were correct the int was improper, and after changing it to the following it did not present any errors

void setup() {
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  modbusTCPClient.begin(serverIP, 503);
1 Like

Thank you for the simplification!

  1. That was the format I found in an example here
/*
  Ethernet Modbus TCP Client Toggle

  This sketch toggles the coil of a Modbus TCP server connected
  on and off every second.

  Circuit:
   - Any Arduino MKR Board
   - MKR ETH Shield

  created 16 July 2018
  by Sandeep Mistry
*/

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

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

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(10, 10, 1, 5);

EthernetClient ethClient;
ModbusTCPClient modbusTCPClient(ethClient);

IPAddress server(10, 10, 1, 2); // update with the IP Address of your Modbus server

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }
}

void loop() {
  if (!modbusTCPClient.connected()) {
    // client not connected, start the Modbus TCP client
    Serial.println("Attempting to connect to Modbus TCP server");
    
    if (!modbusTCPClient.begin(server, 502)) {
      Serial.println("Modbus TCP Client failed to connect!");
    } else {
      Serial.println("Modbus TCP Client connected");
    }
  } else {
    // client connected

    // write the value of 0x01, to the coil at address 0x00
    if (!modbusTCPClient.coilWrite(0x00, 0x01)) {
      Serial.print("Failed to write coil! ");
      Serial.println(modbusTCPClient.lastError());
    }

    // wait for 1 second
    delay(1000);

    // write the value of 0x00, to the coil at address 0x00
    if (!modbusTCPClient.coilWrite(0x00, 0x00)) {
      Serial.print("Failed to write coil! ");
      Serial.println(modbusTCPClient.lastError());
    }

    // wait for 1 second
    delay(1000);
  }
}

I'm going to back up a bit and start with their example and get that working.

  1. That syntax was used in an example in the Arduino IDE that I copied from. I'm not sure on why they have it this way.
  2. someone else recommended this same change and it did not cause any errors when changed.
  3. That is the syntax used in the example here ArduinoModbus - client.holdingRegisterWrite() - Arduino Reference
    I am tying to write to an address on a PLC using this function.
  4. another person also corrected this.
  5. I removed the ';'
  6. I am taking a step back and using the sample code from Arduino that includes this function. I tried to walk before I could crawl.

Thank you for your help!

Welcome to the forum @bnriggs

You have to put the solution so just tap someones solution bar (It should be right under there name) and it will show up as done

So I used the sample code for modbus from Arduino and It's not connecting to the Modbus. On the Serial Monitor it is just repeating 'Connecting to Modbus' and I cannot figure out what is wrong. Thank you in advance!

/*
  Ethernet Modbus TCP Client Toggle

  This sketch toggles the coil of a Modbus TCP server connected
  on and off every second.

  Circuit:
   - Any Arduino MKR Board
   - MKR ETH Shield

  created 16 July 2018
  by Sandeep Mistry
*/

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

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

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(10, 10, 1, 5);
IPAddress gateway(10, 10, 1, 100);
IPAddress subnet(255, 255, 255, 0);
IPAddress myDns(8, 8, 8, 8);
IPAddress serverIP(10, 10, 1, 2);
EthernetClient ethClient;
ModbusTCPClient modbusTCPClient(ethClient);


void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Shield not Found. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Eth not connected.");
  }
}

void loop() {
  if (!modbusTCPClient.connected()) {
    // client not connected, start the Modbus TCP client
    Serial.println("Connecting to Modbus");
    delay(1000);
    if (!modbusTCPClient.begin(serverIP, 502)) {
      Serial.println("Connection Failed!");
    } else {
      Serial.println("Connected");
    }
  } else {
    // client connected

    // write the value of 0x01, to the coil at address 0x00
    if (!modbusTCPClient.coilWrite(0x00, 0x01)) {
      Serial.print("Failed to write coil! ");
      Serial.println(modbusTCPClient.lastError());
    }

    // wait for 1 second
    delay(1000);


    // wait for 1 second
    delay(1000);
  }
}

12 posts were split to a new topic: Function brace style

Solution for anyone who gets here!

/*
  Ethernet Modbus TCP Client Toggle

  This sketch toggles the coil of a Modbus TCP server connected
  on and off every second.

  Circuit:
   - Any Arduino MKR Board
   - MKR ETH Shield

  created 16 July 2018
  by Sandeep Mistry
*/

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

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

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(10, 10, 1, 5);
IPAddress gateway(10, 10, 1, 100);
IPAddress subnet(255, 255, 255, 0);
IPAddress myDns(8, 8, 8, 8);
IPAddress serverIP(10, 10, 1, 2);
EthernetClient ethClient;
ModbusTCPClient modbusTCPClient(ethClient);
int counter = 0;

void setup() {
  //Initialize serial and wait for port to open:
 
  Serial.begin(9600);

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection and the server:  
  Ethernet.begin(mac, ip);
  delay(500);
  modbusTCPClient.begin(serverIP, 503);
  delay(500);


  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Shield not Found");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Eth not connected");
  }
}

void loop() {


  Serial.println("Start of Loop");


  if (!modbusTCPClient.connected()) {
    // client not connected, start the Modbus TCP client
    Serial.println("Connecting");
    modbusTCPClient.stop();
    modbusTCPClient.begin(serverIP, 503);
    delay(5000);
  // if (!modbusTCPClient.begin(serverIP, 503)) {
  //   Serial.println("Connection Failed");
  // } else {
  //   Serial.println("Connected");
  // }
  } else {
    // client connected
    Serial.println("Connected");
    // write the value of counter, to the coil at address 3900
    modbusTCPClient.holdingRegisterWrite(3900, counter);  
    ++counter;    
    
    // wait for 1 second
    delay(1000);

  }
  
}

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