Modbus server with ethernet feather

I am setting up a modbus master using the adafruit feather express m4 and the ethernet feather. Compiling with arduino 1.8.13
I am using the libraries Ethernet.h and ArduinoModbus.h
I am able to open a ethernet connection to my pc and can ping my module from the pc.

I have tried to initiate a modbus session but have had no luck
using modbus poll application on windows 10 pc I have not been able to establish a connection .

if any one has used the arduino modus library and can offer advice thanks in advance

my code is
/

#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoModbus.h>
extern void updateREG() ;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress ip(192, 168, 4, 75);
IPAddress subnet(255, 255, 248,0);
IPAddress Gateway (192,168,4,1);
IPAddress myDns(192, 168, 4, 2);
EthernetClient client;
EthernetServer server(502);

// use the numeric IP instead of the name for the server:
byte Eserver[] = { 192,168,2,17 }; // PC
ModbusTCPServer modbusTCPServer;
boolean alreadyConnected = false; // whether or not the client was connected previously
int Code;
int test = 123;
int adc0_result;
void setup() {
// You can use Ethernet.init(pin) to configure the CS pin
Ethernet.init(10); // Most Arduino shields

// initialize the ethernet device
Ethernet.begin(mac, ip, myDns, Gateway, subnet);

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

// 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.");
}

// start listening for clients
//server.begin(); // for telnet connection
Code = modbusTCPServer.configureHoldingRegisters(0x00, 20);
Serial.print(" server address:");
Serial.println(Ethernet.localIP());
if (! modbusTCPServer.begin()){
Serial.println("modbus server failed to start");
while(1);
}
else{
Serial.println("modbus server started");
}

Serial.print("Holding register initialization Result= ");
Serial.print(Code);
Serial.print("\n");
Serial.print("Modbus server address:");
Serial.println(Ethernet.localIP());
test = 110;
}

void loop() {
// wait for a new client:
modbusTCPServer.accept(client);

// when the client sends the first byte, say hello:

// modbus addition
 // a new client connected

// Serial.println("new client");
//modbusTCPServer.begin(); // start listening for clients

// let the Modbus TCP accept the connection
//modbusTCPServer.accept(client);

if(client.connected()){
Serial.println("we have a new client");

}
else {
Serial.println(" no new client ");
}
while (client.connected()) {
// poll for Modbus TCP requests, while client connected
modbusTCPServer.poll();
updateREG();
}
Serial.print("poll ");
Serial.print(" client disconnected");
}
void updateREG() {
//adc0 = ads.readADC_SingleEnded(1);
modbusTCPServer.holdingRegisterWrite(0x00, test);
adc0_result = modbusTCPServer.holdingRegisterRead(0x00);
Serial.print("AIN0: "); Serial.println(adc0_result);
// Serial.println(" ");
}

was missing
server.begin(); prior to starting the modbus server
this allows me to make the connection.
However now Modbuss poll is throwing an
error
Error #2 invalid address

I am able to connect to the server from Modbus tool ap. II can read and write input registers. However when I disconnect from Modbus poll
the code recognizes it. then if I try try to reconnect from modbus poll tcpserver. connect does not work. in order to reconnect I need to rest the express
I added a modbusservertcp.end and then .begin to try to resolve but this did not work
any thoughts?
code listed below

#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoModbus.h>
extern void updateREG() ;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress ip(192, 168, 4, 75);
IPAddress subnet(255, 255, 248,0);
IPAddress Gateway (192,168,4,1);
IPAddress myDns(192, 168, 4, 2);
EthernetClient client;
EthernetServer server(502);
bool connected = 0;
// use the numeric IP instead of the name for the server:
byte Eserver[] = { 192,168,2,17 }; // PC
ModbusTCPServer modbusTCPServer;
boolean alreadyConnected = false; // whether or not the client was connected previously
int Code;
int test = 123;
int adc0_result;
void setup() {
// You can use Ethernet.init(pin) to configure the CS pin
Ethernet.init(10); // Most Arduino shields

// initialize the ethernet device
Ethernet.begin(mac, ip, myDns, Gateway, subnet);

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

// 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.");
}

// start listening for clients
//server.begin(); // for telnet connection
Code = modbusTCPServer.configureHoldingRegisters(0x00, 20);
Serial.print(" server address:");
Serial.println(Ethernet.localIP());

server.begin();

if (! modbusTCPServer.begin()){
Serial.println("modbus server failed to start");
while(1);
}
else{
Serial.println("modbus server started");
}

// if (client.connect(Eserver, 502))
// {
// Serial.println("connected");
// }
// else
// {
// Serial.println("connection failed");
// };

Code = modbusTCPServer.configureHoldingRegisters(0x00, 20);

Serial.print("Holding register initializatinn Result= ");
Serial.print(Code);
Serial.print("\n");
Serial.print("Modbus server address:");
Serial.println(Ethernet.localIP());
test = 110;
}
void loop() {
connected =0;
// wait for a new client:
client = server.available();
if(client){
modbusTCPServer.accept(client);

// when the client sends the first byte, say hello:

// modbus addition
// a new client connected
// Serial.println("new client");
//modbusTCPServer.begin(); // start listening for clients

// let the Modbus TCP accept the connection
//modbusTCPServer.accept(client);

if(client.connected()){

//if( connected == 0 ) 
Serial.println("we have a new client");
//connected = 1;

}
else {
//if ( connected == 1)
Serial.println(" no new client ");
// connected = 0;
}
while (client.connected()) {
// poll for Modbus TCP requests, while client connected
modbusTCPServer.poll();
updateREG();
}

Serial.print("poll ");
Serial.println(" client disconnected");
connected   = 0;  

modbusTCPServer.end();

if (! modbusTCPServer.begin()){
  Serial.println("modbus server failed to start");
  while(1);
}
else{
  Serial.println("modbus server started");
  Code = modbusTCPServer.configureHoldingRegisters(0x00, 20);
  }
 }

}

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