# Arduino Modbus Library

**URL:** <https://forum.arduino.cc/t/arduino-modbus-library/602283>\
**Category:** General Guidance\
**Created:** [July 18, 2019, 3:13pm UTC](https://forum.arduino.cc/t/arduino-modbus-library/602283 "2019-07-18T15:13:59Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![macsim16](https://avatars.discourse-cdn.com/v4/letter/m/839c29/32.png) [@macsim16](https://forum.arduino.cc/u/macsim16)\
**Post date:** [July 18, 2019, 3:13pm UTC](https://forum.arduino.cc/t/arduino-modbus-library/602283/1 "2019-07-18T15:13:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pylon](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@pylon](https://forum.arduino.cc/u/pylon)\
**Post date:** [July 18, 2019, 4:38pm UTC](https://forum.arduino.cc/t/arduino-modbus-library/602283/2 "2019-07-18T16:38:29Z")

</div>

> 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.).

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [July 18, 2019, 4:41pm UTC](https://forum.arduino.cc/t/arduino-modbus-library/602283/3 "2019-07-18T16:41:31Z")

</div>

This is the theory.

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

```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)

```cpp
#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 😉

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

```cpp
#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.

---

<div class="post-metadata">

**Author:** ![Juraj](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/juraj/32/238785_2.png) [@Juraj](https://forum.arduino.cc/u/Juraj)\
**Post date:** [July 18, 2019, 5:13pm UTC](https://forum.arduino.cc/t/arduino-modbus-library/602283/4 "2019-07-18T17:13:06Z")

</div>

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

> **[ArduinoModbus - Arduino Reference](https://www.arduino.cc/reference/en/libraries/arduinomodbus/)**
>
> The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.

---

<div class="post-metadata">

**Author:** ![macsim16](https://avatars.discourse-cdn.com/v4/letter/m/839c29/32.png) [@macsim16](https://forum.arduino.cc/u/macsim16)\
**Post date:** [July 18, 2019, 9:33pm UTC](https://forum.arduino.cc/t/arduino-modbus-library/602283/5 "2019-07-18T21:33:43Z")

</div>

> 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?

---

<div class="post-metadata">

**Author:** ![macsim16](https://avatars.discourse-cdn.com/v4/letter/m/839c29/32.png) [@macsim16](https://forum.arduino.cc/u/macsim16)\
**Post date:** [July 18, 2019, 9:41pm UTC](https://forum.arduino.cc/t/arduino-modbus-library/602283/6 "2019-07-18T21:41:15Z")

</div>

> sterretje:  
> This is the theory.
> 
> The ArduinoModBus library makes use of the ArduinoRS485 library. You can modify last line in RS485.cpp
> 
> ```arduino
> 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!

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [July 19, 2019, 5:03am UTC](https://forum.arduino.cc/t/arduino-modbus-library/602283/7 "2019-07-19T05:03:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pylon](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@pylon](https://forum.arduino.cc/u/pylon)\
**Post date:** [July 19, 2019, 5:49pm UTC](https://forum.arduino.cc/t/arduino-modbus-library/602283/8 "2019-07-19T17:49:48Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![pert](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/pert/32/217111_2.png) [@pert](https://forum.arduino.cc/u/pert)\
**Post date:** [July 19, 2019, 9:45pm UTC](https://forum.arduino.cc/t/arduino-modbus-library/602283/9 "2019-07-19T21:45:32Z")

</div>

> 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.

> <https://github.com/arduino-libraries/ArduinoModbus/blob/master/library.properties#L9>

> <https://github.com/arduino-libraries/ArduinoRS485/blob/master/library.properties#L9>

---

<div class="post-metadata">

**Author:** ![Juraj](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/juraj/32/238785_2.png) [@Juraj](https://forum.arduino.cc/u/Juraj)\
**Post date:** [July 20, 2019, 4:29am UTC](https://forum.arduino.cc/t/arduino-modbus-library/602283/10 "2019-07-20T04:29:34Z")

</div>

> 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](https://github.com/arduino-libraries/ArduinoModbus/blob/master/library.properties#L9)  
> [ArduinoRS485/library.properties at master · arduino-libraries/ArduinoRS485 · GitHub](https://github.com/arduino-libraries/ArduinoRS485/blob/master/library.properties#L9)

> **[ArduinoModbus - Arduino Reference](https://www.arduino.cc/reference/en/libraries/arduinomodbus/)**
>
> The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.

---

<div class="post-metadata">

**Author:** ![pert](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/pert/32/217111_2.png) [@pert](https://forum.arduino.cc/u/pert)\
**Post date:** [July 20, 2019, 5:33am UTC](https://forum.arduino.cc/t/arduino-modbus-library/602283/11 "2019-07-20T05:33:00Z")

</div>

> Juraj:  
> [ArduinoModbus - Arduino Reference](https://www.arduino.cc/en/ArduinoModbus/ArduinoModbus)

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:

> **[ArduinoModbus - Arduino Reference](https://www.arduino.cc/reference/en/libraries/arduinomodbus/)**
>
> The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.

---

<div class="post-metadata">

**Author:** ![pylon](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@pylon](https://forum.arduino.cc/u/pylon)\
**Post date:** [July 22, 2019, 5:02pm UTC](https://forum.arduino.cc/t/arduino-modbus-library/602283/12 "2019-07-22T17:02:40Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Stoney](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stoney/32/1137285_2.png) [@Stoney](https://forum.arduino.cc/u/Stoney)\
**Post date:** [June 26, 2020, 4:00pm UTC](https://forum.arduino.cc/t/arduino-modbus-library/602283/13 "2020-06-26T16:00:58Z")

</div>

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](https://github.com/yaacov/ArduinoModbusSlave)

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](https://github.com/lucasso/ModbusRTUSlaveArduino)) does not compile with an unmodified example for the mega. Probably an easy fix but did not inspire confidence.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 6:58pm UTC](https://forum.arduino.cc/t/arduino-modbus-library/602283/14 "2021-05-05T18:58:39Z")

</div>

Closed on May 5, 2021, 6:58pm UTC


