ModbusTCP Codesys Siemens
ModbusTCP Codesys Siemens
This application note helps the user to establish Modbus com- TCP/IP
munication between Siemens PLC and Codesys PLC.
It explains the configurations needed in both the controllers in
order to achieve Modbus communication between them.
It also explains the Function Blocks needed in Siemens PLC side
to establish Modbus communication.
100253
Title .................................................... Modbus TCP/IP Communication between Siemens and Codesys Controller
Version ............................................................................................................................................................. 1.10
Document no. .............................................................................................................................................. 100253
Original .................................................................................................................................................................en
Author ............................................................................................................................................................. Festo
Copyright Notice
This documentation is the intellectual property of Festo AG & Co. KG, which also has the exclusive copyright. Any
modification of the content, duplication or reprinting of this documentation as well as distribution to third par-
ties can only be made with the express consent of Festo AG & Co. KG.
Festo AG & Co KG reserves the right to make modifications to this document in whole or in part. All brand and
product names are trademarks or registered trademarks of their respective owners.
Legal Notice
Hardware, software, operating systems and drivers may only be used for the applications described and only in
conjunction with components recommended by Festo AG & Co. KG.
Festo AG & Co. KG does not accept any liability for damages arising from the use of any incorrect or incomplete
information contained in this documentation or any information missing therefrom.
Defects resulting from the improper handling of devices and modules are excluded from the warranty.
The data and information specified in this document should not be used for the implementation of safety func-
tions relating to the protection of personnel and machinery.
No liability is accepted for claims for damages arising from a failure or functional defect. In other respects, the
regulations with regard to liability from the terms and conditions of delivery, payment and use of software of
Festo AG & Co. KG, which can be found at www.festo.com and can be supplied on request, shall apply.
All data contained in this document do not represent guaranteed specifications, particularly with regard to func-
tionality, condition or quality, in the legal sense.
The information in this document serves only as basic information for the implementation of a specific, hypo-
thetical application and is in no way intended as a substitute for the operating instructions of the respective
manufacturers and the design and testing of the respective application by the user.
The operating instructions for Festo products can be found at http://www.festo.com/sp.
Users of this document (application note) must verify that all functions described here also work correctly in the
application. By reading this document and adhering to the specifications contained therein, users are also solely
responsible for their own application.
1 Components/Software used
Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10 Page 5 of 38
Application description
2 Application description
This document explain how to communicate between Siemens PLC and Codesys controller using Modbus TCP/IP
protocol.
2.1 Topology
When we want to communicate between Siemens PLC and Festo PLC the following topology must be followed.
NOTE
• The IP address of the Siemens PLC, Festo PLC and the PC used for programming the PLC must
be in the same range.
Page 6 of 38 Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10
Configuration of Festo PLC in CODESYS
NOTE
Any Festo controller which supports Modbus TCP protocol can be used for this integration with
Siemens controller via Modbus TCP Protocol. In our case we will be using CPX-E-CEC-C1-PN con-
troller.
Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10 Page 7 of 38
Configuration of Festo PLC in CODESYS
4. Select CPX-E-CEC project. Give a name to the project, select the folder where the project has to be saved.
Click OK to create the project.
NOTE
• Depending on the controller type being used, select other package from the available pack-
ages.
• All the Festo Controllers available support Modbus TCP/IP communication.
NOTE
• Modbus communication is helpful in those cases where the controller chosen for the project
doesnt have a profinet module.
• In our application note we have considered CPX-E-CEC-C1-PN . The Procedere remains same for
all other Festo PLC‘s
5. Since we are using CPX-E-CEC-C1-PN as our controller, select it as the device for our project.
Page 8 of 38 Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10
Configuration of Festo PLC in CODESYS
7. Once the device has been added the project view looks as shown below.
In order to establish any TCP communication in Codesys controller first we have to create and setup a gate-
way(network path ) for the controller.
Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10 Page 9 of 38
Configuration of Festo PLC in CODESYS
6. Double click on the Gateway to scan the network to see if any controller is connected .
Page 10 of 38 Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10
Configuration of Festo PLC in CODESYS
8. The network path is made active by double clicking on the controller found in the network.
The controller found in network gets bolded once the network path is set active.
The actual hardware configuration of the controller can be found automatically by searching the controller con-
nected in the network.
Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10 Page 11 of 38
Configuration of Festo PLC in CODESYS
3. Click on Actual Configuration to detect the actual hardware configuration of the controller connected in the
network.
4. Once the scan is started and if the controller is connected in the network then the detected hardware con-
figurations will be as shown below.
Page 12 of 38 Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10
Configuration of Festo PLC in CODESYS
Right click on the Device. In this case CPX-E-CEC-C1-PN and then select Add Device .
Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10 Page 13 of 38
Configuration of Festo PLC in CODESYS
NOTE
• The IP address of the Festo controller and Siemens PLC must be in the same range for success-
ful communication to be established.
1. Double click on the ethernet device >>> General to open the network parameters properties.
2. Choosing the ethernet interface for the device. The added network parameters must be chosen as the net-
work interface for the device.
3. Select the configured network adapter from the available options.
Page 14 of 38 Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10
Configuration of Modbus TCP between Festo and Siemens Controller
Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10 Page 15 of 38
Configuration of Modbus TCP between Festo and Siemens Controller
Page 16 of 38 Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10
Configuration of Modbus TCP between Festo and Siemens Controller
Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10 Page 17 of 38
Configuration of Modbus TCP between Festo and Siemens Controller
2. In the properties of the Modbus TCP Slave , under General configure the Network parameters of the slave(
Siemens PLC).
NOTE
• Port number of the slave(Siemens controller) is in general 502.
The slave channels have to be configured with appropriate Function Codes , Modbus addresses and the data
length to be communicated with the master.
Modbus TCP channels can be configured to perform following functions on the slave:
• Reading Coils
• Writing coils
• Reading the holding registers
• Writing holding registers of the slave
• Reading input registers of the slave
• Reading discrete inputs of the slave.
Page 18 of 38 Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10
Configuration of Modbus TCP between Festo and Siemens Controller
3. Click Add channel to create a new Modbus Slave Channel as shown in above image. Once you start to add
new channel the following popup will be displayed.
Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10 Page 19 of 38
Configuration of Modbus TCP between Festo and Siemens Controller
The following options are available when we want to configure a Modbus slave channel .
Access Type : This is the function code which is used for accessing the memory locations of the slave.
The following function codes are available.
Read Register – Offset : This is the starting address of the memory location of the slave from where the data
has to be read.
Read Register – Length : This is the number of words which has to be read from the offset mentioned.
Write Register – Offset : This is the starting address of the memory location of the slave to where the data has
to be written.
Write Register – Length : This is the number of words which has to be written from the offset mentioned.
4. Once the needed Function code is chosen the modbus slave channel will look as shown below.
The above example is used for reading 10 words starting at the address 0000 from the holding registers .
NOTE
• If one more function is needed then create one more channel.
In the above example 20 words starting from the address 0000 will be written to the siemens controller.
5. Select the option Modbus TCP Slave I/O Mapping to view the configured IO mapping .
Page 20 of 38 Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10
Configuration of Modbus TCP between Festo and Siemens Controller
In order to configure the Siemens PLC as Modbus TCP Slave we have to use MB_SERVER function block.
The MB_SERVER will have the following schematic look.
INPUT DATA
NAME DATA TYPE DESCRIPTION
OUTPUT DATA
NAME DATA TYPE DESCRIPTION
Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10 Page 21 of 38
Configuration of Modbus TCP between Festo and Siemens Controller
TCON_IP_v4 Structure
Page 22 of 38 Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10
Configuration of Modbus TCP between Festo and Siemens Controller
• ID : This value must be different if the slave is communicating with different number of masters at a
time. If its communicating with only one master then its value is 1.
• Remote address : This is the IP address of the master with which communication has to be established.
3. Call the MB_SERVER function block and assign the appropriate parameters.
In the above example 40 words starting at the memory address M1000 is used for Modbus communication.
Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10 Page 23 of 38
Configuration of Modbus TCP between Festo and Siemens Controller
1. Create a watch table mapping the modbus holding registers considered in the program.
Example : Write 20 holding registers of siemens controller starting at the address 0X0000.
• At the codesys side force values to the configured Modbus TCP slave channels .
Page 24 of 38 Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10
Configuration of Modbus TCP between Festo and Siemens Controller
• Now open the watch table in TIA portal and observe that the 20 holding registers of siemens PLC have
been written with the above values from the Modbus master ( Festo PLC).
Example : Reading 20 holding registers of siemens controller starting at the address 0X0000.
• When the function code configured in codesys for reading holding registers of siemens controller is exe-
cuted the words are read as shown below.
• The data written to the Siemens PLC holding registers are properly read back as shown in above exam-
ple.
Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10 Page 25 of 38
Configuration of Modbus TCP between Festo and Siemens Controller
3. Click Add Device to finish adding the Modbus TCP slave device.
Page 26 of 38 Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10
Configuration of Modbus TCP between Festo and Siemens Controller
4. After the slave device is added the device tree will look as shown below.
1. Double click the added Modbus TCP Slave to open its settings.
• Slave Port : This the network port of Slave ( in this case Codesys controller).
• Unit ID : The slave ID . The same Unit ID value must be given when configuring Modbus communication
at Siemens side in TIA Portal.
• Holding Registers: The number of holding registers at the slave device configured and available for the
Master for reading and writing.
• Input Registers: The number of input registers at the slave device configured and available for the
Master for reading .
• Start Addresses : This is the address from where the Coils, discrete inputs, holding registers and input
registers will start.
Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10 Page 27 of 38
Configuration of Modbus TCP between Festo and Siemens Controller
Once a new projected is created and the hardware configurations have been done in TIA Portal, the next job is to
do the network configurations of the PLC.
Page 28 of 38 Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10
Configuration of Modbus TCP between Festo and Siemens Controller
NOTE
• The IP address of Siemens and Festo PLC must be in same range.
INPUT DATA
Name Data Type Description
Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10 Page 29 of 38
Configuration of Modbus TCP between Festo and Siemens Controller
These 3 parameters are co-related with each other. Proper values must be given in order to read or write to spe-
cific memory areas of the slave device.
The below table shows the values to be given for these parameters.
Page 30 of 38 Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10
Configuration of Modbus TCP between Festo and Siemens Controller
OUTPUT DATA
NAME DATA TYPE DESCRIPTION
TCON_IPV4 Structure
The connect parameter is of the type TCON_IPV4 structure. Th below table describes this structure in detail.
Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10 Page 31 of 38
Configuration of Modbus TCP between Festo and Siemens Controller
ERROR STATUS
Page 32 of 38 Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10
Configuration of Modbus TCP between Festo and Siemens Controller
Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10 Page 33 of 38
Configuration of Modbus TCP between Festo and Siemens Controller
• Double click on Add New Block . Select Data Block to create a global data block.
• Create a variable of the structure TCON_IPV4.
• Create variables for monitoring the status of the instruction execution.
The Global Data Block will have the following elements:
3. Assign Proper values to the input parameters of the FB which are needed to configure siemens PLC as
Modbus TCP Master.
In our example the FB was given the following values to implement Siemens Controller as Modbus TCP Master:
• Toggle the value of the variable DataBlock.ReqWrite to start sending Modbus requests to the slave de-
vice.
Page 34 of 38 Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10
Configuration of Modbus TCP between Festo and Siemens Controller
MB_MODE has the value 103 as the function code for reading the holding registers is 103.
• Toggle the value of the variable DataBlock.ReqRead to start sending Modbus requests to the slave de-
vice.
Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10 Page 35 of 38
Configuration of Modbus TCP between Festo and Siemens Controller
NOTE
• Always seperate the addresses considered for reading and writing data from the modbus slave.
• This ensures that data read and written do not overlap with each other.
• This is the reason in Example 1 we have considered 20 words from the address M1000 TO 1038
for writing data to modbus slave.
• In example 2 for reading 20 words from modbus slave we have considered from the address
M1040 to 1078.
4. Create a watch table as shown below. Map the addresses considered for writing data to the Modbus slave
during the configuration of MB_CLIENT function block.
5. Create a watch table as shown below. Map the addresses considered for reading data from the Modbus
slave during the configuration of MB_CLIENT function block.
Page 36 of 38 Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10
Configuration of Modbus TCP between Festo and Siemens Controller
Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10 Page 37 of 38
Configuration of Modbus TCP between Festo and Siemens Controller
Example2 : The data which was written to the holding registers are read back using the function code 103.
The data is read back to the address starting from M1040 to 1078
Page 38 of 38 Application Note – Modbus TCP/IP Communication between Siemens and Codesys Controller – 1.10