How to programmatically determine if a Modbus device is disconnected

Introduction:

In certain applications it may be preferable to validate communication between Codesys and a connected Modbus TCP device, like an iR-ETN, programmatically. Within this post we demonstrate how to easily monitor the state of a connected Modbus TCP device.

Software Version:

Codesys IDE 3.5.15.50
Codesys package

Instructions:

These instructions assume that you have developed a working application using the recommended Modbus TCP device drivers.

IEC Objects:

A Modbus TCP device has properties that you may reference to determine communication status and error codes. These properties are displayed during runtime within the “ModbusTCPSlave IEC Objects” tab of the Modbus TCP device.

POU:

To reference these objects within a POU, please use the device name followed by the ‘.’ dot operator.
Note: xError will not turn “TRUE” if “Auto-reconnect” is enabled and a disconnect occurs. However, we highly recommended and in some cases require “Auto-reconnect” to be enabled. Therefore, the method described below does not utilize xError to determine device disconnection.

  1. Within the desired POU define an “NE” function block:

  2. Copy the Modbus TCP device name:
    image

  3. Paste the device name in the first input:

  4. Add the ‘.’ operator and select the “byModbusError” parameters as the first input:

  5. A list of potential values for “byModbusError” is shown below called “MB_ErrorCodes”. Under normal operation, the byModbusError property should be equivalent to the ENUM “RESPONSE_SUCCESS” which has a decimal value of 0:

  6. In most cases, when a Modbus TCP device is disconnected “byModbusError” will be “UNDEFINED”. However, since we do not know with certainty which error code will occur, it is recommended that we compare “byModbusError” to “RESPONSE_SUCCESS” to determine if device behavior is abnormal:
    Note: The variable “err” in this example will turn “TRUE” if a connection issue has occurred between Codesys and the connected device.

Keywords

Check if iR-ETN is disconnected, Test if iR-ETN is disconnected, Check if iR-ETN is not connected, Test if iR-ETN is not connected, Check if iR-ETN40R is disconnected, Test if iR-ETN40R is disconnected, Check if iR-ETN40R is not connected, Test if iR-ETN40R is not connected