Go Down

Topic: Arduino Modbus TCP Slave (server): IDE example (Read 334 times) previous topic - next topic

fleaplc

into IDE example, I find Arduino as Modbus Slave TCP for WIFI only.
Do someone have a sample of the code (declaration,...) to use the library with RJ45 (Ethernet shield W5100)?
thanks

pylon

You failed to provide a link to the library you're using, so we're not able to help you.

fleaplc

this

https://www.arduino.cc/en/ArduinoModbus/ModbusTCPServerModbusTCPServer

pylon

#3
May 14, 2019, 05:47 pm Last Edit: May 22, 2019, 07:53 pm by pylon
That's a simple combination of a Ethernet server sketch with the WiFiModbusServerLED sketch example of the linked library (not tested):

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>

// 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, 1, 177);
IPAddress myDns(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);


EthernetServer server(502);
ModbusTCPServer modbusTCPServer;

void setup() {
  // You can use Ethernet.init(pin) to configure the CS pin
  //Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet

  // 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();

  Serial.print("Modbus server address:");
  Serial.println(Ethernet.localIP());
  // start the Modbus TCP server
  if (!modbusTCPServer.begin()) {
    Serial.println("Failed to start Modbus TCP Server!");
    while (1);
  }

  // configure the LED
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);

  // configure a single coil at address 0x00
  modbusTCPServer.configureCoils(0x00, 1);
}

void loop() {
  // wait for a new client:
  EthernetClient client = server.available();

  // when the client sends the first byte, say hello:
  if (client) {
    // a new client connected
    Serial.println("new client");

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

    while (client.connected()) {
      // poll for Modbus TCP requests, while client connected
      modbusTCPServer.poll();

      // update the LED
      updateLED();
    }

    Serial.println("client disconnected");
  }
}

void updateLED() {
  // read the current value of the coil
  int coilValue = modbusTCPServer.coilRead(0x00);

  if (coilValue) {
    // coil value set, turn LED on
    digitalWrite(ledPin, HIGH);
  } else {
    // coild value clear, turn LED off
    digitalWrite(ledPin, LOW);
  }
}

fleaplc

Hi Pylon, by usig your code try compiling I get:

"'ModbusTCPServer' does not name a type "

Why?

pylon

I edited my post and inserted the forgotten includes.

fleaplc

#6
May 22, 2019, 01:26 pm Last Edit: May 22, 2019, 01:27 pm by fleaplc
Hi Pylon, I run your code but it's not working..
Using a Modbus Client (Modbus Poll) and analyzing with Wireshark,  Arduino answers to Modbus query only with a TCP message..not Modbus structure into the TCP frame.  Does it work on you? I'm using Arduino uno + W5100 Eth shield . I test also with mega 2560+Eth Shiled; same story

pylon

Quote
Does it work on you?
No, I wrote it's a quick hack, I don't have the equivalent hardware at hand.

I forgot the server configuration, I edited my code again to include that. Please notice the server will have only one single coil register available so you cannot request any information. If you want to send different requests you have to adapt the code accordingly.

fleaplc

I tried your code but no way..it doesn't work

pylon

Quote
I tried your code but no way..it doesn't work
Define "it doesn't work" in more detail. Did you adapt the code to your needs? Did you understand the last sentence of my previous post?

fleaplc

yes I did.
1-I copyand paste your code
2-try to adapt to my needs (holding register).

Both case not working (timeout answer in modbus call) (teh same of Wireshark print screen I sent)

pylon

Post the code you're currently working with! Don't forget the code tags!

fleaplc

es this


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

// IP Addressing
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);
IPAddress myDns(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

int Code;


EthernetServer server(502);
ModbusTCPServer modbusTCPServer;

void setup() {
  // Ethernet Settings
  byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xFF };  // Define MAc address
  byte ip[] = { 192, 168, 1, 177 };                     // Define IP address
  byte subnet[] = { 255, 255, 255, 0 };                 // Ddefine SubNEt mask

  // initialize the ethernet device
  Ethernet.begin(mac, ip, subnet);                      // Assign MAC, IP, and subnet mask
  Serial.begin(9600);
  server.begin();               // start listening for clients
  //modbusTCPServer.begin();    // start listening for clients

  // Define Holding register
  Code = modbusTCPServer.configureHoldingRegisters(0x00, 100);

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

void loop() {

  // wait for a new client:
  EthernetClient client = server.available();

  // when the client sends the first byte, say hello:
  if (client) {

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

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

    while (client.connected()) {
      // poll for Modbus TCP requests, while client connected
      modbusTCPServer.poll();
    }
    Serial.print("poll");
    Serial.print("client disconnected");
  }


}

fleaplc

#13
May 29, 2019, 08:09 am Last Edit: May 29, 2019, 08:09 am by fleaplc
In my code I defined 100 holding register (starting from addr. 0 (40000). Trying to poll it (ex Client Modbus) always get timeout (no answer).

pylon

Does that code really match your network?

Code: [Select]
IPAddress ip(192, 168, 1, 177);
IPAddress myDns(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);


Please edit your post and insert code tags!

Go Up