ModbusTCP fails config but still works(possible error in offical documentation?)


I have run into a peculiar problem while programming a ModbusTCP Server sketch. It’s just a test program for now that acts as a server/slave and as such takes any modbus requests that come in over the ethernet connection and prints changes in register/coil values to the serial monitor.
It works really well when I test it with a modbus master simulator that’s installed on a second computer. The odd thing: It isn’t supposed to?

When I configure all of my modbus inputs and outputs in void setup my serial prints tell me that the configuration failed every time. The documentation isn’t very informative on what to do when things fail either.

I have tried to mess with the parameters of the configration (entered the address in dec and hex, minimised and maximised the amount of Coils/Registers to configure and even entered a negative value) without success.

When I look at the example code for ModbusTCPServer (the one which works with the Wifi shield) the command looks exactly the same as what I entered. I actually based my code on this example and just exchanged the wifi parts with ethernet parts.

The sketch works so it isn’t an immediate issue but I’d like to have it resolved because these things have the bad habit of breaking when you’ve forgotten about them and that’s where the fun begins (not).

Thank your for your help!

ModbusTest.ino (3.71 KB)

  if(modbusTCPServer.configureCoils(0x00, -2) == 0){

Do you think this makes sense? If yes, please describe what you expect it to do.

Hi pylon,

No that doesn't make any sense. It's a leftover from me trying out all kinds of parameter values to see if the output changes. Like, maybe I didn't read a crucial piece of information or something. Couldn't hurt to try.
I accidently left it in there, sorry.

Any idea why the proper calls like

if(modbusTCPServer.configureHoldingRegisters(0x00, 16) == 0){

return a failure?

Ok so I had a look at the source code for the modbus library (namely “ModbusServer.cpp”) and I have the impression that either the documentation is wrong or there’s a small bug in the code.

The definition for


looks like this:

int ModbusServer::configureCoils(int startAddress, int nb)
  if (startAddress < 0 || nb < 1) {
    errno = EINVAL;

    return -1;

  size_t s = sizeof(_mbMapping.tab_bits[0]) * nb;

  _mbMapping.tab_bits = (uint8_t*)realloc(_mbMapping.tab_bits, s);

  if (_mbMapping.tab_bits == NULL) {
    _mbMapping.start_bits = 0;
    _mbMapping.nb_bits = 0;

    return 0;

  memset(_mbMapping.tab_bits, 0x00, s);
  _mbMapping.start_bits = startAddress;
  _mbMapping.nb_bits = nb;

  return 1;

so when the realloc returns NULL the method returns 0 and when it returns the pointer to the reallocated memory the method actually finishes configuring the storage space for the coil data and returns 1.
Which means to me that 1 equals success and 0 equals failure.

The documentation says the exact opposite.
If I’m right about this where do I report this so it gets fixed?