emelianov / modbus-esp8266

Most complete Modbus library for Arduino. A library that allows your Arduino board to communicate via Modbus protocol, acting as a master, slave or both. Supports network transport (Modbus TCP) and Serial line/RS-485 (Modbus RTU). Supports Modbus TCP Security for ESP8266/ESP32.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ESP 32 - MODBUS TCP SLAVE - Error

drlukbgd opened this issue · comments

Hi,

I made code for ESP32 using example MODBUS TCP SLAVE. Actual function of ESP32 is to act like slave device with SPI interface to Wiz5500 Ethernet. As MODBUS MASTER TCP I used several different simulators. Master scan rate is 500ms.

  1. https://en.radzio.dxp.pl/modbus-master-simulator/
  2. https://sourceforge.net/projects/qmodmaster/
  3. https://www.winmodbus.com/

in all simulators behaver is same, after certain time appear error Modbus message time out :

Capture1
Capture2

Seams that ESP32 is losing communication for 5-7 sec and than start to communicate again.

Below is code running on ESP :

/*
ModbusTCP for W5x00 Ethernet library
Basic Server code example

(c)2020 Alexander Emelianov (a.m.emelianov@gmail.com)
https://github.com/emelianov/modbus-esp8266

This code is licensed under the BSD New License. See LICENSE.txt for more info.
*/

#include <SPI.h>
#include <Ethernet.h> // Ethernet library v2 is required

#include <ModbusEthernet.h>

// Enter a MAC address and IP address for your controller below.
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};

byte SubnetMask[] = { 255, 255, 0, 0} ;

IPAddress ip(192, 168, 1, 50); // The IP address test

byte server[] = { 192, 168, 1, 100 }; //

EthernetClient client;

ModbusEthernet mb; // Declare ModbusTCP instance

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* DEFINITION OF Digital INPUT REG MODBUS RTU 0x02 - Read Input Status (Read Discrete Inputs) */
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#define DI_ADDRS_0 0 // Start address of DInput 0
#define DI_ADDRS_1 1 // Start address of DInput 1
#define DI_ADDRS_2 2 // Start address of DInput 2

bool DI_0 = 1; // DInput 0 VALUE
bool DI_1 = 0; // DInput 2 VALUE
bool DI_2 = 1; // DInput 2 VALUE

///////////////////////////////////////////////////////////////////////////////////////////////
/* END OF DEFINITION */
///////////////////////////////////////////////////////////////////////////////////////////////

void modbus_RTU_init_val() {

///////////////////////////////////////////////////////////////////////////////////////
/* */
///////////////////////////////////////////////////////////////////////////////////////

mb.addIsts(DI_ADDRS_0);
mb.Ists(DI_ADDRS_0,DI_0);
mb.addIsts(DI_ADDRS_1);
mb.Ists(DI_ADDRS_1,DI_1);
mb.addIsts(DI_ADDRS_2);

}

void setup() {
Serial.begin(9600); // Open serial communications and wait for port to open

Ethernet.init(5); // SS pin
Ethernet.begin(mac, ip, SubnetMask); // start the Ethernet connection
delay(500); // give the Ethernet shield a second to initialize
modbus_RTU_init_val();
mb.server(); // Listen TCP server
delay(500);

if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found.");
}
else if (Ethernet.hardwareStatus() == EthernetW5100) {
Serial.println("W5100 Ethernet controller detected.");
}
else if (Ethernet.hardwareStatus() == EthernetW5200) {
Serial.println("W5200 Ethernet controller detected.");
}
else if (Ethernet.hardwareStatus() == EthernetW5500) {
Serial.println("W5500 Ethernet controller detected.");
}

if (Ethernet.linkStatus() == Unknown) {
Serial.println("Link status unknown. Link status detection is only available with W5200 and W5500.");
}
else if (Ethernet.linkStatus() == LinkON) {
Serial.println("Link status: On");
}
else if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Link status: Off");
}
Serial.println("IP adress: ");
Serial.print(Ethernet.localIP());

mb.autoConnect();

Serial.println("startus MB:");
Serial.println(mb.eventSource());

}

void loop() {

mb.task(); // Server Modbus TCP queries

delay(100);
}

Please if someone can send me guidelines how to solve this issue with Modbus time out error. it seams that slave stops to respond and after short time without resetting start to work again.

Thanks a lot.

Hi, 2 things that caught my attention:
1 - You do not have read and write tasks inside the modbus loop.
2 - Try not to use delay() inside the loop.
Within the loop you must associate reading and writing tasks of the type of data you use, here are examples of what I am talking about.

https://github.com/emelianov/modbus-esp8266/blob/master/examples/TCP-Ethernet/client/client.ino

Hi,

thanks for comments. After removing delay() in loop, slave behavior is perfect without error message and lost packages. Great library !!!