Initial Modbus/TCP device configuration

As an example, the connection of the BNI to a CODESYS control is shown. The exact procedure depends on the project planning software used.

Add the Network Module as Modbus/TCP device

Before you begin

Make sure that:

  • A new, empty CODESYS project has been created.

  • The module is connected to the PLC via Ethernet and is powered up.

  • The module has been assigned a unique IP address.

  • The module’s bus type has been switched to Modbus/TCP via the WebUI.

Steps

To add the Modbus/TCP device in CODESYS:

  1. Add an Ethernet device.

    Right-click the device in the tree and select Add Device… from the context menu.

    In the General section of the Ethernet adapter, click Browse… and select the network adapter to use.

    ../../../../_images/add-ethernet-adapter.png

    Selecting the network adapter for the Ethernet device

  2. Add a Modbus client.

    Below the Ethernet adapter, right-click and select Add Device… from the context menu.

  3. Add a Modbus server.

    Below the Ethernet adapter, right-click and select Add Device… from the context menu. The Modbus server represents the network module.

  4. Optional: Rename the device.

    Right-click the device in the tree and select Refactoring > Rename from the context menu.

  5. Set the IP address of the network module.

    Open the Modbus server device, select General, and enter the IP address of the network module. Make sure that the Port is set to 502 (default).

    ../../../../_images/select-ip-modbus-marked.png

    Setting the IP address of the Modbus server/ BNI

  6. Download the program to the PLC and start it.

    ⇒ CODESYS reports an active connection.

Configure ports

Configure the port settings of the network module via the Port Configuration registers, see Registers 9100…9800 – Port Configuration for the register layout.

Note

The port settings can also be configured directly via the WebUI, see I/O ports. However, if the device is replaced, the port modes must then be applied manually again on the new device.

If the port settings are configured in CODESYS instead, they are written to the device automatically every time the PLC program starts.

Set port mode

Configure the port settings (e.g. Pin 2 / Pin 4 port mode) of the network module so that connected IO-Link devices and digital signals are set up as required.

Before you begin

Make sure that:

Steps

As an example, Port 4 Pin 4 is configured as IO-Link and Pin 2 as Digital Output. Adapt the register offset and value to the desired port and configuration.

To configure the ports in CODESYS:

  1. Select the Modbus server (network module) in the device tree.

  2. Navigate to Modbus Server Init.

  3. Click New….

  4. In the Initialization Value dialog, select Write Multiple Registers (Function Code 16) as the access type, and enter the register offset and the desired value.

    For example, to set port 4 to IO-Link and Pin 2 to Digital Output, use register offset 9401 (Port Configuration register for port 4, see Registers 9100…9800 – Port Configuration for the register layout 9100…9800), length 1, and initialization value 514 (hex 0x0202).

    Multiple ascending registers can also be set with an initial value each. For this, set the desired number of registers in the Length field and enter the corresponding initialization value.

  5. Optional: Enter a comment describing the configured value.

  6. Click OK.

    ../../../../_images/configure-port-modbus-marked.png

    Configuring a port via Modbus Server Init

  7. Download the program to the PLC and start it.

    ⇒ The port is set to the configured port mode.

Configure Validation and Backup

Configure the Validation and Backup behavior of an IO-Link port so that the network module checks the connected IO-Link device against the expected Vendor ID and Device ID, and backs up its parameters as required.

Before you begin

Make sure that:

Steps

As an example, Port 4 is configured with Validation and Backup mode Backup + Restore, together with the Vendor ID and Device ID of the expected IO-Link device. Adapt the register offset and values to the desired port and device.

To configure Validation and Backup in CODESYS:

  1. Select the Modbus server (network module) in the device tree.

  2. Navigate to Modbus Server Init.

  3. Click New….

  4. In the Initialization Value dialog, select Write Multiple Registers (Function Code 16) as the access type, and enter the register offset and the desired value.

    For example, to set the Vendor ID and Device ID of the expected IO-Link device for port 4, use register offset 9403 (Vendor ID) and 9404 (Device ID MSB, as the low byte of the register value), followed by register offset 9405 (Device ID LSB) (Port Configuration registers for port 4, see Registers 9100…9800 – Port Configuration for the register layout 9100...9800).

    Multiple ascending registers can also be set with an initial value each. For this, set the desired number of registers in the Length field and enter the corresponding initialization value.

  5. Repeat step 4 to set port 4 to Validation and Backup mode Backup + Restore, using register offset 9402, length 1, and initialization value 3.

  6. Optional: Enter a comment describing the configured value.

  7. Click OK.

  8. Download the program to the PLC and start it.

    ⇒ The Validation and Backup mode is applied to the configured port, and the connected IO-Link device is checked against the configured Vendor ID and Device ID.

Configure process data channels

Configure cyclic process data channels in CODESYS so that the registers described in Register Description are read and written cyclically and mapped to PLC variables.

Before you begin

Make sure that:

Steps

As an example, register 2000 (Digital Input State) is read cyclically and register 2001 (Digital Output State) is configured for writing. Adapt the register offset and access type to the desired registers.

To map the registers in CODESYS:

  1. Select the BNI in the device tree.

  2. Select Modbus Server Channel.

  3. Click Add Channel….

  4. In the Modbus Server Channel dialog, enter the values for the digital input channel.

    For example, to read register 2000 (Digital Input State, see Registers 2000, 2001 and 2047, 2048 – Digital Input / Output State for the register layout), enter a name (e.g. Digital Inputs), select Read Holding Registers (Function Code 3) as the access type, select Cyclic as trigger with a valid cycle time, and set the READ Register Offset to 2000 and Length to 1.

  5. Click OK.

    ../../../../_images/processdata-digital-inputs-marked.png

    Adding a channel for the digital input register

  6. Navigate to ModbusTCPServer I/O Mapping.

    ⇒ The added channel is shown here as process data and can be mapped to a PLC variable.

    ../../../../_images/digital-inputs-io-mapping-marked.png

    The digital input channel shown as process data in the I/O mapping

  7. Click Add Channel… again to add a channel for the digital output register.

  8. In the Modbus Server Channel dialog, enter the values for the digital output channel.

    For example, to write register 2001 (Digital Output State), enter a name (e.g. Digital Outputs), select Write Multiple Registers (Function Code 16) as the access type, select Cyclic as trigger with a valid cycle time, and set the WRITE Register Offset to 2001 and Length to 1.

  9. Click OK.

    ⇒ The digital output word is now available in ModbusTCPServer I/O Mapping with the mapped PLC addresses.

  10. Download the program to the PLC and start it.

    ⇒ The mapped PLC variables cyclically reflect the register values; writing to the output variable sets the corresponding digital output.