Dynamically enabling/disabling Modbus slave devices in runtime (performance issue with timeouts)

akrawczyk
2025-08-11
2025-08-13
  • akrawczyk - 2025-08-11

    Hi everyone,

    I'm working on a CODESYS-based project running on a Weintek cMT2078X HMI with a built-in CODESYS runtime. The system communicates with multiple Modbus RTU slave devices, arranged in a cascade. The number of connected units can vary between 2 and 8, depending on the installation.

    Each slave has 17 Modbus channels, which are polled cyclically every 5 seconds.

    The issue is that when fewer devices are physically connected (e.g. only 3 out of 8), the Modbus master still attempts to communicate with all configured slaves. This leads to massive delays due to timeouts (currently 200ms per channel). For one disconnected device, that’s:

    17 × 200ms = 3.4 seconds of delay per polling cycle,
    and this quickly multiplies with each additional missing slave.

    This behavior significantly degrades the system's performance and responsiveness.

    We attempted a workaround where we tried to trigger polling based on a variable in the PLC program, but this caused instability and even led to hardware hangs/freezes on the cMT2078X – likely due to clashes in how the Modbus polling engine and program logic interact at runtime.

    My main questions are:
    Is there a way in CODESYS to dynamically enable/disable Modbus slave devices at runtime, using a BOOL or INT variable (e.g. indicating number of active slaves)?

    Can devices or channels be conditionally polled or temporarily disabled without removing them from the device tree?

    Are there any best practices or examples for handling variable numbers of Modbus slaves in scalable systems like this?

    Would splitting communication across tasks or interfaces help reduce the impact of timeouts?

    Any advice, documentation references, or architectural suggestions would be extremely helpful. I'm looking for a robust way to dynamically scale slave polling without degrading system stability or performance.

    Thanks in advance!

     

    Last edit: akrawczyk 2025-08-11
  • ph0010421 - 2025-08-12

    Hello
    If you uncheck 'Auto restart Communication', it will try once and never again.
    You can manually reset the slave if needed : Slave.xReset (or something similar)

    Or I have a ModbusRTU library if you want to try it.

     

    Last edit: ph0010421 2025-08-12
  • TimvH

    TimvH - 2025-08-13

    As far as I remember, the Reconfigure functionality doesn't work for Modbus devices. But...

    Each Modbus device in your device tree has a name. You can set the property "Enable" of this device to FALSE to stop communicating with it.
    So if one of your Modbus Slave devices name is "MbSlave1" in the device tree, then in your code you can set the Enable to FALSE to let it stop communicating:
    MbSlave1.Enable := FALSE;

    Please let us know if this worked on your controller with your application and your CODESYS version.

    Alternatively you could use the ModbusFB library. See:
    https://forge.codesys.com/prj/codesys-example/modbus/home/Home/

     

Log in to post a comment.