RAK4631 RUI ArduinoModbus fails to compile

The example code of ArduinoModbus Fails to compile on the RAK4631, even though its listed here: [WisBlock/examples/RAK4630/IO/RAK5802_RS485 at master · RAKWireless/WisBlock · GitHub as](WisBlock/examples/RAK4630/IO/RAK5802_RS485 at master · RAKWireless/WisBlock · GitHub as) required library.
The output in Arduino IDE is as follows (anonymized some paths):

WARNING: Library ArduinoRS485 claims to run on samd, mbed_portenta, mbed_opta architecture(s) and may be incompatible with your current board, which runs on nrf52 architecture(s).
WARNING: Library ArduinoModbus claims to run on megaavr, samd, mbed_nano, mbed_portenta, mbed_opta architecture(s) and may be incompatible with your current board, which runs on nrf52 architecture(s).
In file included from C:\Users[UserPath]\packages[Hardware1]\hardware\nrf52[Version]/cores/nRF5/component/[API_Name]/Arduino.h:1,
from C:\Users[UserPath]\Documents\Arduino\libraries\ArduinoModbus\src\libmodbus\modbus.c:23:
C:\Users[UserPath]\packages[Hardware1]\hardware\nrf52[Version]/cores/nRF5/component/[API_Name]/[HeaderFile].h:67:11: error: expected '=', ',', ';', 'asm' or 'attribute' before '<' token
67 | template<class T, class L>
| ^
C:\Users[UserPath]\packages[Hardware1]\hardware\nrf52[Version]/cores/nRF5/component/[API_Name]/[HeaderFile].h:73:11: error: expected '=', ',', ';', 'asm' or 'attribute' before '<' token
73 | template<class T, class L>
| ^
C:\Users[UserPath]\packages[Hardware1]\hardware\nrf52[Version]/cores/nRF5/component/[API_Name]/[HeaderFile].h:319:6: error: conflicting types for 'random'
319 | long random(long max);
| ^~~~~~
In file included from C:\Users[UserPath]\Documents\Arduino\libraries\ArduinoModbus\src\libmodbus\modbus.c:13:
c:\users[UserPath]\packages[Hardware1]\tools[Compiler][Version]\arm-none-eabi\include\stdlib.h:255:6: note: previous declaration of 'random' was here
255 | long random (void);
| ^~~~~~
In file included from C:\Users[UserPath]\packages[Hardware1]\hardware\nrf52[Version]/cores/nRF5/component/[API_Name]/Arduino.h:1,
from C:\Users[UserPath]\Documents\Arduino\libraries\ArduinoModbus\src\libmodbus\modbus.c:23:
C:\Users[UserPath]\packages[Hardware1]\hardware\nrf52[Version]/cores/nRF5/component/[API_Name]/[HeaderFile].h:350:6: error: conflicting types for 'random'
350 | long random(long min, long max);
| ^~~~~~
In file included from C:\Users[UserPath]\Documents\Arduino\libraries\ArduinoModbus\src\libmodbus\modbus.c:13:
c:\users[UserPath]\packages[Hardware1]\tools[Compiler][Version]\arm-none-eabi\include\stdlib.h:255:6: note: previous declaration of 'random' was here
255 | long random (void);
| ^~~~~~
In file included from C:\Users[UserPath]\packages[Hardware1]\hardware\nrf52[Version]/cores/nRF5/component/[API_Name]/Arduino.h:1,
from C:\Users[UserPath]\Documents\Arduino\libraries\ArduinoModbus\src\libmodbus\modbus.c:23:
C:\Users[UserPath]\packages[Hardware1]\hardware\nrf52[Version]/cores/nRF5/component/[API_Name]/[HeaderFile].h:653:62: error: expected ';', ',' or ')' before '=' token
653 | void tone(uint8_t pin, uint32_t frequency, uint64_t duration = 0);
| ^
C:\Users[UserPath]\packages[Hardware1]\hardware\nrf52[Version]/cores/nRF5/component/[API_Name]/[HeaderFile].h:767:73: error: expected ';', ',' or ')' before '=' token
767 | unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
| ^
C:\Users[UserPath]\packages[Hardware1]\hardware\nrf52[Version]/cores/nRF5/component/[API_Name]/[HeaderFile].h:783:77: error: expected ';', ',' or ')' before '=' token
783 | unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
| ^
In file included from C:\Users[UserPath]\packages[Hardware1]\hardware\nrf52[Version]/cores/nRF5/component/[API_Name]/[HeaderFile].h:917,
from C:\Users[UserPath]\packages[Hardware1]\hardware\nrf52[Version]/cores/nRF5/component/[API_Name]/Arduino.h:1,
from C:\Users[UserPath]\Documents\Arduino\libraries\ArduinoModbus\src\libmodbus\modbus.c:23:
C:\Users[UserPath]\packages[Hardware1]\hardware\nrf52[Version]/cores/nRF5/component/[API_Name]/WString.h:4:10: fatal error: cstdlib: No such file or directory
4 | #include <cstdlib>
| ^~~~~~~~~
compilation terminated.
exit status 1
Error compiling for the board [BoardName].

Welcome back to the forum @rs485toLWAN

What is the BSP version you are using?

What version of the RS485 and Modbus library are you using?

With above versions, it compiles for me without problems.

I used:

BSP: 4.0.5
RS485: 1.0.5
ArduinoModbus: 1.0.8

I downgraded my packages to your version but still got the error.

I am trying to compile the following code (examples/ArduinoModbus/TCP/EthernetModbusClientTogggle.ino)

/*
  Ethernet Modbus TCP Client Toggle

  This sketch toggles the coil of a Modbus TCP server connected
  on and off every second.

  Circuit:
   - Any Arduino MKR Board
   - MKR ETH Shield

  created 16 July 2018
  by Sandeep Mistry
*/

#include <SPI.h>
#include <Ethernet.h>

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

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);

EthernetClient ethClient;
ModbusTCPClient modbusTCPClient(ethClient);

IPAddress server(192, 168, 1, 10); // update with the IP Address of your Modbus server

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
  }

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);

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

void loop() {
  if (!modbusTCPClient.connected()) {
    // client not connected, start the Modbus TCP client
    Serial.println("Attempting to connect to Modbus TCP server");
    
    if (!modbusTCPClient.begin(server, 502)) {
      Serial.println("Modbus TCP Client failed to connect!");
    } else {
      Serial.println("Modbus TCP Client connected");
    }
  } else {
    // client connected

    // write the value of 0x01, to the coil at address 0x00
    if (!modbusTCPClient.coilWrite(0x00, 0x01)) {
      Serial.print("Failed to write coil! ");
      Serial.println(modbusTCPClient.lastError());
    }

    // wait for 1 second
    delay(1000);

    // write the value of 0x00, to the coil at address 0x00
    if (!modbusTCPClient.coilWrite(0x00, 0x00)) {
      Serial.print("Failed to write coil! ");
      Serial.println(modbusTCPClient.lastError());
    }

    // wait for 1 second
    delay(1000);
  }
}

I also downgraded the ArduinoModbus library to 1.0.5 and got the following compilation error:

ld.exe: <REDACTED>/arduino15/packages/rak_rui/tools/arm-none-eabi-gcc/9-2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: <REDACTED>/arduino/sketches/C9B415494FB9013D024586740A904CA0/libraries/ArduinoModbus/libmodbus/modbus.c.o: in function _sleep_response_timeout': <REDACTED>/ArduinoModbus/src/libmodbus/modbus.c:161: undefined reference to delay'
ld.exe: <REDACTED>/ArduinoModbus/src/libmodbus/modbus.c:162: undefined reference to `delayMicroseconds'
collect2.exe: error: ld returned 1 exit status

You are right, the ModBus library is not compatible with RUI3 (or the other way around, not sure).

The examples in the WisBlock repo are for the open source Arduino BSP and not for RUI3.

Not really helping, but the Arduino Modbus library throws error as well with the Espressif ESP32 BSP.

You might want to have a look into the modbus-esp8266 library, which at least compiles with RUI3. I cannot test the functionality, I do not have any RS485/Modbus devices.

@beegee thank you for your support.
The modbus-esp8266 compiles and works.

@rs485toLWAN

Glad it worked.

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