Arduino Modbus Library

Hi I'm using an arduino Mega2560 and a standard RS485 module to connect to a Schneider PowerMeter PM2120 with the Official Arduino Library ArduinoModbus, I need to know where can I change the Modbus Serial Port, I was looking on the reference but I don't found a function to do that; After this I was looking on the libraries files directly but I dont found where to change that.

By default when I use "ModbusRTUClient.begin(9600)" It uses Serial RX0 and TX0 by default. I need to use another serial port like Serial3.

I wolud thank you if you can help me with this.

with the Official Arduino Library ArduinoModbus,

That library is designed for the MKR Arduino series. Don't use it on AVR type Arduinos (e.g. UNO, Mega2560, etc.).

This is the theory.

The ArduinoModBus library makes use of the ArduinoRS485 library. You can modify last line in RS485.cpp

RS485Class RS485(SERIAL_PORT_HARDWARE, RS485_DEFAULT_TX_PIN, RS845_DEFAULT_DE_PIN, RS845_DEFAULT_RE_PIN);

Changing SERIAL_PORT_HARDWARE to SERIAL_PORT_HARDWARE1 (or 2 or 3) should do the trick.

SERIAL_PORT_HARDWARE is defined in pins_arduino.h (for the mega on a windows install C:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\mega\pins_arduino.h)

#define SERIAL_PORT_MONITOR         Serial
#define SERIAL_PORT_HARDWARE        Serial
#define SERIAL_PORT_HARDWARE1       Serial1
#define SERIAL_PORT_HARDWARE2       Serial2
#define SERIAL_PORT_HARDWARE3       Serial3
#define SERIAL_PORT_HARDWARE_OPEN   Serial1
#define SERIAL_PORT_HARDWARE_OPEN1  Serial2
#define SERIAL_PORT_HARDWARE_OPEN2  Serial3

For you to put the theory into practice and check if it works :wink:

While looking for the solution, I stumbled over the below in the RS485.h file in the ArduinoRS485 library

#ifdef __AVR__
#define RS845_DEFAULT_DE_PIN 2
#define RS845_DEFAULT_RE_PIN -1
#else
#define RS845_DEFAULT_DE_PIN A6
#define RS845_DEFAULT_RE_PIN A5
#endif

It might be of interest of you want to change the DE pin on the Mega.

1 Like

pylon:
That library is designed for the MKR Arduino series. Don't use it on AVR type Arduinos (e.g. UNO, Mega2560, etc.).

pylon:
That library is designed for the MKR Arduino series. Don't use it on AVR type Arduinos (e.g. UNO, Mega2560, etc.).

Thanks pylon, which other library can I use to work with the Mega/UNO/Nano and Modbus?

sterretje:
This is the theory.

The ArduinoModBus library makes use of the ArduinoRS485 library. You can modify last line in RS485.cpp

RS485Class RS485(SERIAL_PORT_HARDWARE, RS485_DEFAULT_TX_PIN, RS845_DEFAULT_DE_PIN, RS845_DEFAULT_RE_PIN);

Changing SERIAL_PORT_HARDWARE to SERIAL_PORT_HARDWARE1 (or 2 or 3) should do the trick.

Hi sterretje, I did the change you suggest but it don't works. Which other library can I use to manage Modbus communication with an Arduino Mega/Uno/Nano?

Thanks in advance!

No idea which other libraries there are. ModBus has been used before on AVRs if I'm not mistaken, so I suggest you search the forum.

Thanks pylon, which other library can I use to work with the Mega/UNO/Nano and Modbus?

ModbusMaster should work, it's available in the IDE library manager.

pylon:
That library is designed for the MKR Arduino series. Don't use it on AVR type Arduinos (e.g. UNO, Mega2560, etc.).

I think this is incorrect. The library properties architectures field is set to *, which indicates compatibility with all architectures. The same goes with its dependency of the ArduinoRS485 library.

pert:
I think this is incorrect. The library properties architectures field is set to *, which indicates compatibility with all architectures. The same goes with its dependency of the ArduinoRS485 library.
ArduinoModbus/library.properties at master · arduino-libraries/ArduinoModbus · GitHub
ArduinoRS485/library.properties at master · arduino-libraries/ArduinoRS485 · GitHub

Juraj:
ArduinoModbus - Arduino Reference

Is this what you're trying to point out?

it is compatible with our MKR RS485 Shield and with our network enabled products like the Ethernet shield, the MKR family of boards and the Arduino UNO WiFi Rev 2 just to name a few.

Sometimes it's worth a little extra typing to make sure the message gets across. I'm guessing we all have a very high WPM (unless using some worthless mobile touchscreen stupidity) so it should only take a matter of seconds to do so.

Also, clickable links are the kind thing to do:

I think this is incorrect. The library properties architectures field is set to *, which indicates compatibility with all architectures. The same goes with its dependency of the ArduinoRS485 library.

That's correct, the library author doesn't mask the library for other platforms but a quick look at the code shows you that the library is definitely not intended to be used on the AVR platform. It uses RAM as if there is no limit for that resource and makes also use of the String class which definitely shouldn't be used on AVRs.

Read this topic yesterday while researching, dropped back to confirm that the first slave RTU library I have found to work for serial3 on a mega2560 is not in the repository.

is GitHub - yaacov/ArduinoModbusSlave: Modbus slave library for Arduino

Seems solid so far. I have modified the incoming addresses and have the "full" example working and no comms errors showing on the display.

Most of the libarys are aimed at the arduino being the master obviously and the only slave library (GitHub - lucasso/ModbusRTUSlaveArduino: ModbusRTU Slave) does not compile with an unmodified example for the mega. Probably an easy fix but did not inspire confidence.