Arduino Mega 2560, and Modbus Slave RTU Library

Hello,

I am looking for the simplest way to interface the Arduino Mega as Slave and a PLC as a Master over RS232 or RS485.
I already looked for the available libraries at arduino.cc but I got multiple errors at the compiling.

Any help is appreciated.

Regards.

Have you got a hardware converter between the Mega and the device ? The Mega operates at TTL (5V) levels, not RS232 or RS485 levels

Please post the sketch you have problems with and the full error messages associated with it following the advice given in How to get the best out of this forum

Hello,

Thanks for the follow up, here s the requested information:

The library used is:

ModbusRTU_Slave_RS485

Here s the Schematic:

Here s the code:

#include <ModbusRTUSlave.h>

// size of data which will be read and written
#define DATA_SIZE 100
// data array which will be read and written
u16 _D[DATA_SIZE];

// address (kind of name) of above data, may be anything
#define VIRTUAL_ADDRESS 0x7000

#define OUR_ID_AS_A_SLAVE 101
#define PIN_CONNECTED_TO_BOTH_DE_AND_RE 3

ModbusRTUSlave rtu(OUR_ID_AS_A_SLAVE, &Serial1, PIN_CONNECTED_TO_BOTH_DE_AND_RE);

void setup()
{
rtu.addWordArea(VIRTUAL_ADDRESS, _D, DATA_SIZE);
rtu.begin(9600);

//Serial.begin(9600); // not needed, for logging purpose only

// set some value in data array to test if master can read and modify it
_D[0] = 160;
}

void loop()
{
// waiting for requests from master
// reading and writing _D according to requests from master happens here
rtu.process();
}

Here s the error message:

Arduino: 1.8.15 (Windows 10), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

In file included from C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485/ModbusRTUSlave.h:5:0,

             from C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\examples\SimpleModbus\SimpleModbus.ino:1:

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485/utility/LinkedList.h:311:3: warning: redeclaration of 'T LinkedList::get(int, bool)' may not have default arguments [-fpermissive]

T LinkedList::get(int index, bool useCached = false){

^~~~~~~~~~~~~

In file included from C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\ModbusRTUSlave.h:5:0,

             from C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\ModbusRTUSlave.cpp:1:

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\utility/LinkedList.h:311:3: warning: redeclaration of 'T LinkedList::get(int, bool)' may not have default arguments [-fpermissive]

T LinkedList::get(int index, bool useCached = false){

^~~~~~~~~~~~~

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\utility/LinkedList.h: In instantiation of 'LinkedList::LinkedList() [with T = ModbusRTUSlaveWordAddress*]':

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\ModbusRTUSlave.cpp:10:52: required from here

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\utility/LinkedList.h:101:6: error: cannot convert 'bool' to 'ListNode<ModbusRTUSlaveWordAddress*>*' in assignment

root=false;


C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\utility/LinkedList.h:102:6: error: cannot convert 'bool' to 'ListNode<ModbusRTUSlaveWordAddress*>*' in assignment

last=false;

~~~~^~~~~~

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\utility/LinkedList.h: In instantiation of 'LinkedList<T>::LinkedList() [with T = ModbusRTUSlaveBitAddress*]':

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\ModbusRTUSlave.cpp:11:50:   required from here

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\utility/LinkedList.h:101:6: error: cannot convert 'bool' to 'ListNode<ModbusRTUSlaveBitAddress*>*' in assignment

root=false;

~~~~^~~~~~

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\utility/LinkedList.h:102:6: error: cannot convert 'bool' to 'ListNode<ModbusRTUSlaveBitAddress*>*' in assignment

last=false;

~~~~^~~~~~

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\utility/LinkedList.h: In instantiation of 'bool LinkedList<T>::add(T) [with T = ModbusRTUSlaveWordAddress*]':

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\ModbusRTUSlave.cpp:27:65:   required from here

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\utility/LinkedList.h:192:12: error: cannot convert 'bool' to 'ListNode<ModbusRTUSlaveWordAddress*>*' in assignment

tmp->next = false;

~~~~~~~~~~^~~~~~~

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\utility/LinkedList.h: In instantiation of 'bool LinkedList<T>::add(T) [with T = ModbusRTUSlaveBitAddress*]':

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\ModbusRTUSlave.cpp:37:63:   required from here

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\utility/LinkedList.h:192:12: error: cannot convert 'bool' to 'ListNode<ModbusRTUSlaveBitAddress*>*' in assignment

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\utility/LinkedList.h: In instantiation of 'ListNode<T>* LinkedList<T>::getNode(int) [with T = ModbusRTUSlaveWordAddress*]':

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\utility/LinkedList.h:312:28:   required from 'T LinkedList<T>::get(int, bool) [with T = ModbusRTUSlaveWordAddress*]'

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\ModbusRTUSlave.cpp:48:46:   required from here

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\utility/LinkedList.h:158:9: error: cannot convert 'bool' to 'ListNode<ModbusRTUSlaveWordAddress*>*' in return

return false;

       ^~~~~

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\utility/LinkedList.h: In instantiation of 'ListNode<T>* LinkedList<T>::getNode(int) [with T = ModbusRTUSlaveBitAddress*]':

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\utility/LinkedList.h:312:28:   required from 'T LinkedList<T>::get(int, bool) [with T = ModbusRTUSlaveBitAddress*]'

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\ModbusRTUSlave.cpp:58:44:   required from here

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\utility/LinkedList.h:158:9: error: cannot convert 'bool' to 'ListNode<ModbusRTUSlaveBitAddress*>*' in return

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\utility/LinkedList.h: In instantiation of 'T LinkedList<T>::pop() [with T = ModbusRTUSlaveBitAddress*]':

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\ModbusRTUSlave.cpp:426:1:   required from here

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\utility/LinkedList.h:248:13: error: cannot convert 'bool' to 'ListNode<ModbusRTUSlaveBitAddress*>*' in assignment

 tmp->next = false;

 ~~~~~~~~~~^~~~~~~

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\utility/LinkedList.h:256:8: error: cannot convert 'bool' to 'ListNode<ModbusRTUSlaveBitAddress*>*' in assignment

 root = false;

 ~~~~~^~~~~~~

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\utility/LinkedList.h:257:8: error: cannot convert 'bool' to 'ListNode<ModbusRTUSlaveBitAddress*>*' in assignment

 last = false;

 ~~~~~^~~~~~~

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\utility/LinkedList.h: In instantiation of 'T LinkedList<T>::pop() [with T = ModbusRTUSlaveWordAddress*]':

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\ModbusRTUSlave.cpp:426:1:   required from here

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\utility/LinkedList.h:248:13: error: cannot convert 'bool' to 'ListNode<ModbusRTUSlaveWordAddress*>*' in assignment

 tmp->next = false;

 ~~~~~~~~~~^~~~~~~

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\utility/LinkedList.h:256:8: error: cannot convert 'bool' to 'ListNode<ModbusRTUSlaveWordAddress*>*' in assignment

 root = false;

 ~~~~~^~~~~~~

C:\Users\Julio\Documents\Arduino\libraries\ModbusRTU_Slave_RS485\utility/LinkedList.h:257:8: error: cannot convert 'bool' to 'ListNode<ModbusRTUSlaveWordAddress*>*' in assignment

 last = false;

 ~~~~~^~~~~~~

exit status 1

Error compiling for board Arduino Mega or Mega 2560.



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Please follow the advice given in the link below when posting code . Use code tags when posting code here to make it easier to read and copy for examination

Sorry, I just Auto Format it, copy and paste, here is the Code:

#include <ModbusRTUSlave.h>

// size of data which will be read and written
#define DATA_SIZE 100
// data array which will be read and written
u16 _D[DATA_SIZE];

// address (kind of name) of above data, may be anything
#define VIRTUAL_ADDRESS 0x7000

#define OUR_ID_AS_A_SLAVE 101
#define PIN_CONNECTED_TO_BOTH_DE_AND_RE 3

ModbusRTUSlave rtu(OUR_ID_AS_A_SLAVE, &Serial1, PIN_CONNECTED_TO_BOTH_DE_AND_RE);

void setup()
{
rtu.addWordArea(VIRTUAL_ADDRESS, _D, DATA_SIZE);
rtu.begin(9600);

//Serial.begin(9600); // not needed, for logging purpose only

// set some value in data array to test if master can read and modify it
_D[0] = 160;
}

void loop()
{
// waiting for requests from master
// reading and writing _D according to requests from master happens here
rtu.process();
}

Still no code tags

Copy the code from the IDE using the "Copy for forum" option and paste it here. The code tags will be added automatically during teh copy

Thanks for the advice, autoformat, copy to forum, and paste, here is the code:

#include <ModbusRTUSlave.h>

// size of data which will be read and written
#define DATA_SIZE 100
// data array which will be read and written
u16 _D[DATA_SIZE];

// address (kind of name) of above data, may be anything
#define VIRTUAL_ADDRESS 0x7000

#define OUR_ID_AS_A_SLAVE 101
#define PIN_CONNECTED_TO_BOTH_DE_AND_RE 3

ModbusRTUSlave rtu(OUR_ID_AS_A_SLAVE, &Serial1, PIN_CONNECTED_TO_BOTH_DE_AND_RE);

void setup()
{
  rtu.addWordArea(VIRTUAL_ADDRESS, _D, DATA_SIZE);
  rtu.begin(9600);

  //Serial.begin(9600); // not needed, for logging purpose only

  // set some value in data array to test if master can read and modify it
  _D[0] = 160;
}

void loop()
{
  // waiting for requests from master
  // reading and writing _D according to requests from master happens here
  rtu.process();
}

Thank you

I hope that you can see how much easier it is to read and copy

Thanks, yes, now I can see it.

Is there a specific reason for the mega as a slave
seems like a bit of over kill for just a slave.
What ModbusRTUSlave.h file are you using.