Problems with Arduino Modbus library with Arduino Mega

Hi guys ! this is my first post in the forum.
I am working with a modbus over IP implementation on an Arduino Mega 2560. I am triying to build a simple Modbus client in the Arduino board using an official library called ArduinoModbus. I follow the example provided in the GitHub repo and I get a compiler error:

C:…\libraries\ArduinoModbus\src\libmodbus\modbus-tcp.cpp: In function ‘_modbus_tcp_recv’:

C:…\libraries\ArduinoModbus\src\libmodbus\modbus-tcp.cpp:223:1: internal compiler error: Segmentation fault

Does anybody had a similar issue with arduino Mega ?

Here is a sample of the code I used: the command that throws the error is
modbusTCPClient.holdingRegisterRead(0x00);

#include <SPI.h>
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>
#include <Ethernet.h>


EthernetClient client;
ModbusTCPClient modbusTCPClient(client);

IPAddress server_IP(192, 168, 6, 10); // IP de Twiddo

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 6, 51); //IP cliente

int reads;

void setup() {
  
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Ethernet.begin(mac, ip);
  Serial.println("Modbus TCP Client Test");
}

void loop() {

  modbusTCPClient.begin(server_IP, 502);
  modbusTCPClient.holdingRegisterRead(0x00);
  delay(10000);
}

This bug is specific to the 5.4.0-atmel3.6.1-arduino2 version of avr-gcc used by Arduino AVR Boards 1.6.22 and newer. It has been reported here:

Here’s the traditional workaround:

  • Tools > Board > Boards Manager
  • Wait for downloads to finish.
  • When you move the mouse pointer over “Arduino AVR Boards”, you will see a “Select version” dropdown menu appear. Select “1.6.21”.
  • Click “Install”.
  • Wait for installation to finish.
  • Click “Close”.

Due to a bug, this workaround doesn’t work with Arduino IDE 1.8.6, but it will work with any other version of the Arduino IDE.

If you have File > Preferences > Check for updates on startup checked, the Arduino IDE may occasionally notify you that a new version of Arduino AVR Boards is available, you’ll need to refrain from updating back to the new Arduino AVR Boards version, otherwise you’ll be back to seeing the segmentation fault error again.

Thank you very much for your help pert, It worked just fine!
Kind regards from Argentina!

You’re welcome. I’m glad to hear it’s working now. Enjoy!
Per