arduino-libraries / ArduinoRS485

GNU Lesser General Public License v2.1
88 stars 68 forks source link

Library doesn't work properly when used in Arduino Mega 2560 #2

Closed nemerdaud closed 1 year ago

nemerdaud commented 4 years ago

I was trying to use ArduinoModbus library that is very tied with ArduinoRS485. Using in Arduino Mega 2560, it was unable to work properly. The examples using MobBusRTUServerLed (Arduino Mega) and ModBusRTUClientToggle doesn't work correctly, presenting "Connection Timeout" in the client-side. If I change the Arduino Mega to another Arduino Uno, the exemple works like a charm... but doesn't with Mega.

Debugging a little bit the code, I could perceive that the problem was in the ArduinoRS485 library that doesn't setup correctly the board pins for the Arduino Mega board.

To fix the problem, in the end of the RS485.cpp file, I changed the instantiation of de RS485 object to:

#if defined(__AVR_ATmega2560__)
  RS485Class RS485(Serial1, 18, RS845_DEFAULT_DE_PIN, RS845_DEFAULT_RE_PIN);
#else
  RS485Class RS485(SERIAL_PORT_HARDWARE, RS485_DEFAULT_TX_PIN, RS845_DEFAULT_DE_PIN, RS845_DEFAULT_RE_PIN);
#endif

And all the communication started work great. I know I hard-coded some values, but I'm putting here just to help you guys to fix the code, if you intend to do in this way.

per1234 commented 1 year ago

The developers of the library have decided the AVR architecture of the Arduino Mega 2560 will not be supported: https://github.com/arduino-libraries/ArduinoRS485/pull/35