Problem with ModbusMaster library

I want to read a modbus device.

My program crashes or, sometimes, reboot with this piece of code:

#include <ModbusMaster.h>

void setup()

void newReadingMB(uint8_t id, uint16_t address,char *format, char *inversion, char *method, char *uuid)
  uint8_t j = 0;
  uint16_t data[6];
  uint8_t result;
  ModbusMaster node(2,id);  
  result = node.readInputRegisters(address,2);
  for (j=0;j<2;j++)
   data[j] = node.getResponseBuffer(j);

When I declare node as a global variable I receive the value but, of course, I am not able to choose other device to read.

Is there any way to destruct the ModbusMaster object so I can declare another? Or, how can I declare multiple ModbusMaster instances? If I have 3 Modbus devices do I need to have 3 different ModbusMaster instances?

As an update, I declared a global ModbusMaster variable, MBnode, and changed ModbusMaster library. I declared _u8SerialPort and _u8MBSlave as public instead of private variables. Now, when I want to change the slave ID e repeat this steps:

MBnode._u8SerialPort = 2;
MBnode._u8MBSlave = atoi(id);

Maybe it’s not the prettiest thing to do but now it works :wink: .

I'm surprised you would run out of memory in this example because it looks like you instantiate a total of (2) ModbusMaster objects each time your Arduino boots up. What happens in loop()? From your description, it sounds like you may be running out of memory by instantiating the object each loop.

Someone has proposed a patch that allows multiple slave objects. I have not reviewed or tested the proposed code, but you can see if this solution helps you:

Please let me know if this works because it will help expedite pulling this into the ModbusMaster library's main source branch.


RX, many thanks for your reply.

I edited the post yesterday (the edited part starts with "As an update"). I am sorry for not posting in a clearer way.
With that trick (declaring _u8SerialPort and _u8MBSlave as public variables) it works fine for me.

As soon as I have the opportunity I surely want to check that patch and then I'll inform you :wink: .

Almost one year after my last topic reply I recently got new issues…

As I explained before, I had to edit the library in order to declare _u8SerialPort and _u8MBSlave as public variables (to avoid sketch reboots and/or crashes). I also added a delay after transmitting the request “delayMicroseconds(1100);” and other delay before receive the response “delay(3);”. These delay where chosen by trial and error when trying to communicate with Circutor CVM-Mini.

Everything was working well and I wrote a sketch to read some variables every 15 minutes. My sketch would look something like this:

#include <ModbusMaster.h>

ModbusMaster MBnode;

void setup()

void loop()
        MBnode._u8SerialPort = 2;
	MBnode._u8MBSlave = 1;



I deployed this configuration in two different locations and with the same hardware and software. In one location everything is working fine and I correctly read all the values. In the other location I constantly read wrong values like this example (I tried to read Active Energy, power factor, phase 1 voltage and other registers. This examples shows what I received when asking Active Energy):

| 2874684 | 2013-06-03 12:00:00 |//Active energy - correct
| 2884527 | 2013-06-03 12:15:00 |//Active energy - correct
| 2894717 | 2013-06-03 12:30:00 |//Active energy - correct
| 2905251 | 2013-06-03 12:45:00 |//Active energy - correct
| 2913680 | 2013-06-03 13:00:00 |//Active energy - correct
| 2921294 | 2013-06-03 13:15:00 |//Active energy - correct
| 2929305 | 2013-06-03 13:30:00 |//Active energy - correct
| 2937062 | 2013-06-03 13:45:00 |//Active energy - correct
| 2944986 | 2013-06-03 14:00:00 |//Active energy - correct
| 2952193 | 2013-06-03 14:15:00 |//Active energy - correct
| 2952193 | 2013-06-03 14:15:00 |//Active energy - correct
|      94 | 2013-06-03 14:30:00 | //power factor - wrong
|  2326 | 2013-06-03 14:45:00 | //Phase 1 voltage - wrong

As you can see, only the first values are correct. The other values are wrong and seem like a response for my other requests. I also noticed that after my daily arduino reboot the values are read correctly but only for some hours…

Does anyone have a clue on whats happening? At first I thought it was a hardware problem because I cannot ensure all communication cables have the same length but, after noticing that the problem is solved for some hours when rebooting my arduino, I am lost… Is there any problem with the way I declare my ModbusMaster object? Can it be related with my library editing?

Thanks in advance :wink: .

Post as much of the code as you can.