Skip to content

General

The CODESYS works as a Modbus TCP/IP slave. The steps below show how to add the Modbus TCP/IP slave device in the CODESYS and how to configure it.

Important

  • Programs names should contains alphabet characters, using numbers only is not allowed.
  • The following steps show the PLC program which accepting a program name with maximum of 20 characters. If you need to use longer programs names, please adapt the PLC program to holds and deals with more registers.
  • To write variable values onto the modbus holding register it is necessary to add a Modbus TCP/IP Master > Slave module in CoDeSys

Requirements

Below codesys packages are required for the Modbus TCP/IP communication.

  • CODESYS Modbus TCP Device SL.
  • CODESYS Modbus TCP Master SL.

Setup

Set Global Variables

  1. Right click on the Application and select Add Object then Global Variable List... codesys_021

  2. Keep the GVL name, and click Add. codesys_022

  3. Add global variables as shown below. codesys_023

Add Ethernet Interface

  1. Right click on your PLC and select Add Device. codesys_000

  2. Under the Ethernet Adapter add the Ethernet interface. (if not added in your PLC). codesys_001

  3. Double click on the Ethernet interface, then under the Interface select the adaptor interface to be used. codesys_002

Add Modbus Slave/Server

  1. Right click on the Ethernet interface, and click on Add Device. codesys_003

  2. Select the Modbus then ModbusTCP Slave Device and add the ModbusTCP Slave Device. codesys_004

  3. Double click on the ModbusTCP Slave Device, then select the required communication port and the number of the Holding & Input Registers (Min. 15 Holding Registers are required, these 15 registers is reserved for the drag&bot control header and should not be used by the user for application purposes). codesys_005

  4. From the Modbus TCP Slave Device I/O Mapping select the bus cycle task that the Modbus should with and link register number 2 to your global variables. Register number 2 (Inputs[2]) contains the drag&bot program status. codesys_006

Add Modbus Master/Client

Codesys does not allow to write to the slave holding registers directly. We cannot just map a CoDeSys variable in this case. We need to add a Modbus Master and use it to write these registers.

  1. Right click on the Ethernet interface, and click on Add Device. codesys_012

  2. Select the Modbus then ModbusTCP Master and add the ModbusTCP Master. codesys_013

  3. Right click on the Modbus TCP Master interface, and click on Add Device. codesys_014

  4. Select the Modbus then ModbusTCP Slave and add the ModbusTCP Slave. codesys_015

  5. Double click on the Modbus TCP Master and under the General tab, activate the Auto-reconnect. codesys_016

  6. Double click on the Modbus TCP Slave and under the General tab, enter your PLC IP number and modbus slave port number. codesys_017

  7. Under the Modbus Slave Channel tab, select Add Channel. codesys_018

  8. Under access type select Write Multiple Registers (Function Code 16). Under Trigger select Rising edge, and under Length put 15, then click OK. codesys_019

  9. Under the ModbusTCPSlave I/O Mapping , link the registers to your global variables as shown below. codesys_020

Change the Program Name to words array

In order to convert the program name from a string type into a word type (used by the modbus registers), you need to add and use the below function block. This function block is deigned to process a program name with maximum of 20 charterers. You can modify it to process longer program name if required.

  1. Right click on the Application and select Add Object then POU... codesys_024

  2. Enter the function block name , select type as a Function Block and choose Structured Text ST as an implementation language. codesys_025

  3. Add the function block variables as shown below. Code is DOWNLOAD 💾 codesys_026

  4. Add function block body as shown below. Code is DOWNLOAD 💾 codesys_027

PLC Task Program

  1. In the PLC Task program, add below code and variables. Code is DOWNLOAD 💾 codesys_028

  2. You can link the enabling bit (sendCMD) of the modbus master channel writing functionality to an HMI push button to activate the modbus writing (program triggering).

Build

  1. From the Build menu select Build. codesys_007

  2. From the Online menu select Login to download the project to the PLC. codesys_008

  3. Click on Yes. codesys_009

  4. From the Debug menu click Start, The PLC status in the bottom should be Running and green. codesys_010

  5. The symbol beside the Modbus slave should be Orange (ready for communication), now start the Dragandbot and add the Modbus Module (Enter the PLC IP number and port number. Activate Use Header option in the module configuration). For debugging, you can activate the Always update variables to see the real time registers values. codesys_011