rPi_Tester
My Setup:

RPi 3 running as Modbus master with 3 devices connected as slaves:

Device 0: Carlo Gavazzi Energy Analyzer (reading 6 Input registers)
Device 1 and 2: Advantech ADAM-4150 I/O modules (reading 7 Discrete input registers and writing 8 coil registers)

The Problem:

When running the OpenPLC on the RPi 3 I got frequent but very random situations where the PLC program acted as if it
would have received bogus information from the I/O modules. I first suspected my own code (as I'm a novice programmer),
but even running a stripped down version of my program with the bare essentials didn't solve the problem.

The Debug:

The log showed that each time the PLC program started behaving strange there was a entry about Device 1 dropping out
(i.e. the "Device %s is disconnected. Attempting to reconnect..." message). I added a bit of verbosity to the
modbusmaster.cpp to se what had gone wrong. And sure enough it was due to a failed read of Discrete Input Registers on that
device.

For the longest time I couldn't understand why a failed read could impact the PLC program as nothing gets stored if a read (or write)
fails... Also observing the I/O modules revealed that no change of input took place.

It was first when I really started to look through the code that samples the slaves (the FOR loop) when I spotted a potential pitfall
in the code. As you know the I/O of the slaves is stored in arrays (one each for Boolean input/output and Input/Holding)
The arrays start at 0 and are incremented for each subsequent device until all the configured I/O have been read and/or written.

The vulnerable part in the code is the two IF statements that reads the inputs (both Discrete Input and Input) as in both
of them the incrementing of the array index is done only on a successful read. A failed read will result in an un-incremented
index which in turn will lead to any subsequent successful read of remaining devices being written to the wrong place in the array.

So in my scenario when the read failed on Device 1 the array got populated with the I/O status from Device 2, which lead to
the erratic behavior of the PLC program.

The "Quick-n-Dirty" fix:

I added a line of code to the IF statement that increments the array index regardless of whether the read was successful or not
(just before flagging the device offline):


//Read discrete inputs
        if (mb_devices[i].discrete_inputs.num_regs != 0)
        {
          uint8_t *tempBuff;
          tempBuff = (uint8_t *)malloc(mb_devices[i].discrete_inputs.num_regs);
          int return_val = modbus_read_input_bits(mb_devices[i].mb_ctx, mb_devices[i].discrete_inputs.start_address,
                              mb_devices[i].discrete_inputs.num_regs, tempBuff);
          if (return_val == -1)
          {
            printf("Modbus Read Discrete Input Registers failed on MB device %s: %s\n", mb_devices[i].dev_name, modbus_strerror(errno));
            modbus_close(mb_devices[i].mb_ctx);
            bool_input_index += (mb_devices[i].discrete_inputs.num_regs);
            mb_devices[i].isConnected = false;
          }
          else
          {
            pthread_mutex_lock(&ioLock);
            for (int j = 0; j < return_val; j++)
            {
              bool_input_buf[bool_input_index] = tempBuff[j];
              bool_input_index++;
            }
            pthread_mutex_unlock(&ioLock);
          }
          free(tempBuff);
        }


The array index gets incremented by the number of registers requested in the original Modbus function. The arrays
themselves does not get updated if the read fails. The printf function is only added for a bit more verbosity to the log output.
Also note that the fix is applied to both Read functions.

The Write functions are not affected by this as the array index is incremented before the evaluation of the Modbus function.

I'm hesitant to call this a bug because I haven't still found the reason for the random read errors that plagues the Device 1
(which ultimately lead to the failure in the first place). But after that addition the PLC program has worked flawlessly despite the
random read errors on Device 1.

And perhaps this addition (or a far better one supplied by someone more fluent in programming) could make the code a
little bit more immune to random errors from the slaves.

Regards,
Marcus
Reply 0 0
thiagoralves
Thank you for finding this! I'll take a look at this code and see if there is a better solution to it.
Reply 0 0
rPi_Tester
Thank you for a excellent piece of software [smile]

Oh, and just out of curiosity:

In the same FOR loop each of the I/O operations allocates memory for a temporary buffer (tempBuff) but only the Read functions
frees the memory after use.
Any particular reason why the Write functions don't free the memory allocated and won't this cause a memory leak at some point ?
Reply 0 0
Reply