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