Arduino modbus not working on Platformio

Hi Guys and Girls,

I have been struggling a bit moving from the arduino IDE to platformio. The interface is awesome and everything is soo much better than arduino. Everything is working when I move it over except for the modbus interface.

I am assuming arduino is doing something on the background that I am missing on this side.

Please see the basic example that is working on arduino but not on platformio below:

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

const int numCoils = 100;                   //Relay1, Relay2, Digital Out1, Digital Out2
const int numHoldingRegisters = 100;        //ADC Measure, Timestamp?, 
const int numDevices = 2;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);  
  Serial2.begin(9600);                      //need to open the serial port to allow RS485 communication
  // start the Modbus RTU server, with (slave) id 1
  if (!ModbusRTUServer.begin(1, 9600)) {
    Serial.println("Failed to start Modbus RTU Server!");
    while (1);
  }

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

  //configure coil registers at address 0x00
  ModbusRTUServer.configureCoils(0x0, numCoils);
  //configure discrete inputs at address 0x00
  ModbusRTUServer.configureDiscreteInputs(0x00, numCoils);
  //configure holding registers at address 0x00
  ModbusRTUServer.configureHoldingRegisters(0x00, numHoldingRegisters);
  // configure input registers at address 0x00
  ModbusRTUServer.configureInputRegisters(0x00, numHoldingRegisters);
  Serial.println("Started Modbus RTU Server!");
}

void loop() {
      // poll for Modbus RTU requests
    ModbusRTUServer.poll();
    //Serial.println(UpdateRegisters());
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Please let me know if you have any ideas? I changed the rs485 library to point to serial 2. Could it be that I’m not pointing it correctly?

Regards,

Bart

Please see the basic example that is working on arduino but not on platformio below:

You did not explain how exactly you see that it’s not working. What is the exact difference in behavior? Any error messages?

Also there’s a lot of info missing.

  • what version of the ArduinoModbus library is being used in the Arduino IDE (look in the library manager)
  • what version of ArduinoRS485 is used in the Arduino IDE?
  • what version is the ESP32 core in the Arduino IDE?
  • are you running the exact same sketch in PlatformIO and Arduino IDE?
  • you say you modified the RS485 library. What ere the exact code changes? Are you using the same modified library in the Arduino IDE as well as in PlatformIO? How did you include the modified library in PlatformIO?
  • What is the platformio.ini?
  • What are the board settings (“Tools” menu) in the Arduino IDE?

Hi, thanks for coming back to me :slight_smile:

Please see answers to your questions below:

You did not explain how exactly you see that it’s not working. What is the exact difference in behaviour? Any error messages?
It seems to have similar warnings regarding the ‘config’ variable being truncated in the RS485 class. A uint32 is being used but a uint16 is being expected. So everything looks similar.
But on the arduino ide I am able to use modpoll to send and receive data to the device. Unfortunately I only get timeouts on the platformio side.

what version of the ArduinoModbus library is being used in the Arduino IDE
V1.0.3
ArduinoRS485: V1.0.0

On platformio I am using the latest versions:
ArduinoModbus 1.0.6
ArduinoRS485 1.0.1

Are you running the exact same sketch in PlatformIO and Arduino IDE?
Yes I am.

You say you modified the RS485 library. What where the exact code changes?
Changes were made in the RS485.cpp file, line 181 from
//RS485Class RS485(SERIAL_PORT_HARDWARE, RS485_DEFAULT_TX_PIN, RS485_DEFAULT_DE_PIN, RS485_DEFAULT_RE_PIN);
to
RS485Class RS485(Serial2, 17, 21, 5);
I just made the changes in the file that is located in the libdeps folder. I assumed this should be ok?

What is the platformio.ini?
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps =
arduino-libraries/ArduinoRS485@^1.0.1
arduino-libraries/ArduinoModbus@^1.0.6

What are the board settings (“Tools” menu) in the Arduino IDE?

I hope this helps.

What about this one? (See Tools → Boards → Board Manager → ESP32)

Hi Gerhardt,

I’m not able to see the core package via the arduino ide because I pulled it down manually via git. But looking in the folder I am able to find the following in the platform text file:

name=ESP32 Arduino
version=2.0.0

I hope that helps. Please send some suggestions if you have any. :slight_smile:

Regards,

Bart

With the latest Espressif32 platform version in PlatformIO (see releases) you’re getting Arduino core 1.0.6. So in total, you’re using a different Arduino core version and the two libraries are also of different versions.

You can do a quick check whether it also not works when you select 1.0.6 as the ESP32 core version in thte Arduino IDE.

Otherwise I suggest equalizing all options between the Arduino IDE and PlatformIO, comparison is not otherwise possible.

You should try this platformio.ini, which will give you the latest master-branch version of Arduino-ESP32 and the library versions equal to what you use in the Arduino IDE.

First, delete the .pio folder of the project, then paste in that platformio.ini

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps =
   arduino-libraries/ArduinoRS485@1.0.0
   arduino-libraries/ArduinoModbus@1.0.3
platform_packages =
   toolchain-xtensa32 @ ~2.80400.0
   framework-arduinoespressif32@https://github.com/espressif/arduino-esp32.git#master
   platformio/tool-esptoolpy @ ~1.30100

and build once. Cloning of the newer version will take some time. Then, you will have to re-do your library modifications in .pio\libdeps\esp32dev\ArduinoRS485. (to make the changes permanent, its better to pull the ArduinoRS485 folder into lib/, .pio is a temporary folder).

1 Like

Hi Gerhardt,

Thanks so much for the help. You’re a genius! Works like a charm!!

I’ll keep track of the library versions in the future. I wasn’t sure if there is a one to one correlation between the two systems. So thanks for teaching me the basics.

Again, thanks so much for making the platform awesome and keeping on supporting and building it.

Regards,

Bart