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.
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```
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.
This is a function declaration. I think you meant to do a function call:
int holdingRegisterWrite(3900, increment);
Behavior for this statement is undefined:
increment = ++increment;
You don't need a ';' after the if code block
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!
/*
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.
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);
}
}
/*
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);
}
}