Well, here is what I was able to do dig up trying to make a mega 2560 work. I don't even remember which code worked so I'm going to throw a bunch of chunks at you and hopefully some of it helps. This is copy pasted from four different code revisions so sorry. I'm also attaching the library I believe I used for it.
ArduinoModbus-master.zip (76.6 KB)
#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoRS485.h> // ArduinoEIP depends on the ArduinoRS485 library
#include <ArduinoModbus.h>
byte mac[] = {0xA8, 0x61, 0x0A, 0xAE, 0x96, 0x4C};
IPAddress ip(192, 168, 2, 8);
// EIP client connection information
int EIP_Client_Port = 10001;
int EIP_Server_Port = 81; // Specify the target port to connect to
EthernetClient ethClient;
//ModbusTCPClient modbusTCPClient(ethClient);
IPAddress server(192, 168, 2, 1); // IP Address of your EIP server
// EIP Server information
EthernetServer ethServer = EthernetServer(EIP_Server_Port);
ethServer.begin();
At one point I tried using the arduino as the master, that may have been where this function was intended.
ethServer.accept();
EthernetClient ServerClient = ethServer.available();
if (ServerClient == true) {
Serial.println(ServerClient.read());
} // else Serial.println(ServerClient);
I think I had to add additional conditions to the .accept() function.
if (!ethClient.connect(server, EIP_Client_Port)) ethClient.connect(server, EIP_Client_Port);
byte test001 = 64;
ethClient.write(test001);
delay(100);
if ( ethClient.available() ) {
Serial.println("EIP data available");
char IncEIPdata = ethClient.read();
Serial.println(IncEIPdata);
}
Write a numeric value. Read out incoming data over serial to verify if anything is being received.
if (ethClient.connect(server, EIP_Port)) {
Serial.println("EIP connected");
Serial.print("Remote IP address: ");
Serial.println(ethClient.remoteIP());
} else {
Serial.println("connection failed");
}
Check that you're connected to the proper server over the correct port.
EthernetClient ServerClient = ethServer.available();
if (ServerClient == true) {
Serial.println(ServerClient.read());
} // else Serial.println(ServerClient);
if (ethClient.connected()) {
EIPcomms();
} else {
// Serial.println("Attempting to reconnect");
ethClient.connect(server, EIP_Port);
if (!modbusTCPClient.connected()) {
// client not connected, start the EIP TCP client
Serial.println("Attempting to connect to EIP TCP server");
if (!modbusTCPClient.begin(server, 502)) {
Serial.println("EIP TCP Client failed to connect!");
} else {
Serial.println("EIP TCP Client connected");
}
}
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Serial.println("Blink");
Serial.println(HeartBeat);
if (HeartBeat == 0) {
HeartBeat = 1;
if (!modbusTCPClient.coilWrite(0x00, 0x01)) {
Serial.print("Failed to write coil high! ");
Serial.println(modbusTCPClient.lastError());
LinkStatusError = 1;
} else {
LinkStatusError = 0;
}
} else {
HeartBeat = 0;
if (!modbusTCPClient.coilWrite(0x00, 0x00)) {
Serial.print("Failed to write coil low! ");
Serial.println(modbusTCPClient.lastError());
LinkStatusError = 1;
} else {
LinkStatusError = 0;
}
}
}
Pulse heartbeat using a coilWrite (setting a bit).