How does SimpleModbus work?

Hi all,

I have successfully got a simple sketch working which writes to a modbus address to a controller device I have. The sketch simply increments the value being written by one every second so I can see it working.

However I would like to know what is going on in the background and its does not work in the same way as other libraries I have used before.

I have the following code which is similar as suggested in the SimpleModbus documentation:

void setup() {
  // put your setup code here, to run once:
  // Initialize each packet
  modbus_construct(&packets[PACKET1], 0, PRESET_MULTIPLE_REGISTERS, 3960, 1, 0);
  // Initialize the Modbus Finite State Machine
  modbus_configure(&Serial, baud, SERIAL_8N2, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS, regs);

void loop() {
  static int i = 0;
  static long previousMillis = 0;
  long interval = 1000;
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
    previousMillis = currentMillis;
    // put your main code here, to run repeatedly:
    regs[0] = i++;


My question is as the modbus_update() function is called every cycle (as suggested) does that mean it sends a modbus message every cycle using the same value? or does it see that the value has not changed so does not actually send the message again unit the value changes?

I hope that it is the latter otherwise it is massively wasteful and will cause problems with the device it is talking to.

Any information would be great. Thanks.

The more I understand this library the more I believe that it is not suitable at all. I am having trouble getting the Read_holding_registers function to work, but even if it did i believe it will try to read that modbus parameter on every cycle.

I have also used the ModbusMaster library which was much easier and only makes a modbus call when you specifically wanted to do a read or write, however i moved away from that as it was not compatible with the latest compiler (1.5 +). unfortunately i need the latest version as that only works with my bluetooth sketch which I am trying to merge with modbus.

Can anyone suggest a modbus library which works with the latest version which doesnt make continual MB calls? Or is there a version of ModbusMaster which works with the latest compiler?


I have gone back to ModbusMaster as I have found versions which do work with the latest compiler, and much easier to use than (NotSo)SimpleModbus.

Please note it appears Modbusmaster 0.11.0 has a speed issue which means it takes around 2 seconds to get a response. where as all previous versions (0.10.3 for example) only takes 1/5 seconds (200ms). I have reported this to the developer.

I've been working with the SimpleModbusMaster library over the past few weeks now and have done a rewrite to adopt it to my needs.

At first I also was confused what is doing what and when, but now I guess there ist not much I can't tell You about it and the concepts behind it.

My modified version now allows for single calls and has a callback method that reports back whenever the client returns something or when a call finishes unsuccessful.

If You tell me what You actually need to do, I can probably tell You if and how You could do it with the one or the other version.

Edit: there is a longer thread on the SimpleModbusMaster: