Modbus TCP/IP Communication problem

Hello All,
First of all, since this is my first post, I want to thank everyone in this forum for the effort and time, sharing their projects as well as the support to people all around the world to build new devices and learn new skills.

In the specific issue that I am facing, I am using arduino mega together with the ethernet shield w5500 in order to control a stepper motor through an external plc. For the communication between the arduino and the plc I want to use Modbus TCP/IP.

The problem that I am facing is that when I connect the Arduino to my ethernet network, although in the beggining I can read and write variables after a while the communication is blocked and I can’t reach it anymore. Not even to ping its IP on the network. If I redownload the sketch then it works again for a while until it’s blocked again.

I attach here my sketch. Any ideas for the cause of the problem?

modbus_n2_gen_valve_V3_mega.ino (2.89 KB)

Any ideas for the cause of the problem?

The library you're using faces the same problem as the String library delivered with the Arduino IDE: it make intensive use of dynamic memory management (allocation and deallocation of heap memory). This fragments the memory of AVR Arduinos quite fast and sooner or later you end in an out of memory situation because the request for a certain block sizes fails. As the library don't even check the value returned by malloc() it won't detect a memory problem.

Use another library or change to an ARM based Arduino (e.g. MKR series).

Dear Pylon,
Thank you for your answer. I will try with a different library and post here the results as it can be useful for future reference.

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