Ah en Modbus TCP 8294 en 03 PDF
Ah en Modbus TCP 8294 en 03 PDF
Application note
8294_en_03 © PHOENIX CONTACT 2015-10-08
1 Description
This application note describes how Phoenix Contact con-
trollers receive and process I/O signals from I/O devices
using Modbus/TCP.
Startup of the Modbus/TCP client function and the Modbus/
TCP server function is described using a project creation
process with PC Worx as an example, see Section 5 and
Section 7.
This application note is valid for all products listed under “Ordering data” on page 4.
MODBUS/TCP
2 Table of contents
1 Description.................................................................................................................................. 1
2 Table of contents ........................................................................................................................ 2
3 Ordering data.............................................................................................................................. 4
4 Modbus/TCP .............................................................................................................................. 5
5 Configuring a controller as a Modbus/TCP client – example project ..................................................... 6
5.1 Cyclic communication:
sequence for creating the Modbus project ..................................................................................................... 6
5.1.1 Creating a new project ........................................................................................................................ 7
5.1.2 Specifying project information ............................................................................................................. 7
5.1.3 Checking/modifying IP settings for the controller ................................................................................ 7
5.1.4 Inserting a “Generic Modbus Device”.................................................................................................. 7
5.1.5 Modifying the settings for the “Generic Modbus Device”..................................................................... 8
5.1.6 Compiling after completing the bus topology ...................................................................................... 9
5.1.7 Creating the program .......................................................................................................................... 9
5.1.8 Compiling after creating the program .................................................................................................. 9
5.1.9 Creating process data and assigning Modbus function codes ............................................................ 9
5.1.10 Generating variables and assigning process data............................................................................... 9
5.1.11 Generating diagnostic and control variables and assigning process data ......................................... 11
5.1.12 Compiling a project ........................................................................................................................... 12
5.1.13 Configuring the Modbus/TCP server ................................................................................................. 12
5.1.14 Sending a project and performing a cold restart................................................................................ 12
5.2 Acyclic communication using the MB_ASYNC_RW function block .............................................................. 13
5.2.1 Old version of the function block ....................................................................................................... 13
5.2.2 New version of the function block...................................................................................................... 14
5.3 Error codes of the “Status” output (Error = TRUE) ........................................................................................ 15
5.4 Using the MBT_STATION_DIAG diagnostic structure ................................................................................. 16
5.5 Specifying the size of the ReadBuffer/WriteBuffer........................................................................................ 17
3 Ordering data
Products
Description Type Order No. Pcs./Pkt.
Inline controller with Ethernet interface for coupling to other controllers and ILC 131 ETH 2700973 1
systems, with programming options according to IEC 61131-3, complete with
connector plug and marking field.
Inline controller with Ethernet interface for coupling to other controllers and ILC 151 ETH 2700974 1
systems, with programming options according to IEC 61131-3, complete with
connector plug and marking field.
Inline controller with Ethernet interface for coupling to other controllers and ILC 171 ETH 2TX 2700975 1
systems, with programming options according to IEC 61131-3, complete with
connector plug and marking field.
Inline controller with Ethernet interface for coupling to other controllers and ILC 191 ETH 2TX 2700976 1
systems, with programming options according to IEC 61131-3, complete with
connector plug and marking field.
Inline controller with Ethernet interface for coupling to other controllers and ILC 131 ETH/XC 2701034 1
systems, with programming options according to IEC 61131-3, complete with
connector plug and marking field.
Inline controller with Ethernet interface for coupling to other controllers and ILC 151 ETH/XC 2701141 1
systems, with programming options according to IEC 61131-3, complete with
connector plug and marking field.
Inline controller with integrated pulse/direction interface, RS-485/RS-422, an- ILC 191 ME/AN 2700074 1
alog inputs (0 ... 10 V), and analog outputs (0 ... 10 V), with programming op-
tions according to IEC 61131-3
Inline controller with integrated pulse/direction interface, RS-485/RS-422, fast ILC 191 ME/INC 2700075 1
counters, and incremental encoder inputs, with programming options accord-
ing to IEC 61131-3
Inline controller with Ethernet interface and GSM modem for coupling to other ILC 151 GSM/GPRS 2700977 1
controllers and systems, with programming options according to IEC 61131-
3, complete with connector plug and marking field
Axiocontrol for the direct control of Axioline I/Os. With 2 Ethernet interfaces AXC 1050 2700988 1
and programming options according to IEC 61131-3. Complete with connec-
tor plug and marking field.
Axiocontrol for the direct control of Axioline I/Os. With 2 Ethernet interfaces, AXC 1050/XC 2701295 1
extended temperature range, and programming options according to IEC
61131-3. Complete with connector plug and marking field.
Axiocontrol for the direct control of Axioline I/Os. With 3 Ethernet interfaces AXC 3050 2700989 1
and programming options according to IEC 61131-3. Complete with connec-
tor plug and marking field.
Software PLC PC WORX RT BASIC 2700291 1
Software PLC PC WORX SRT 2701680 1
Documentation
Description Type Order No. Pcs./Pkt.
“Installing and operating the ILC 131 ETH, ILC 151 ETH, ILC 171 ETH 2TX, UM EN ILC 1X1 – –
ILC 191 ETH 2TX, ILC 131 ETH/XC, and ILC 151 ETH/XC Inline controllers”
user manual
“Installing and operating the ILC 191 ME/AN and ILC 191 ME/INC Inline con- UM EN ILC 191 ME/X – –
trollers” user manual
“Installing, starting up, and operating the ILC 151 GSM/GPRS Inline control- UM EN ILC 151 GSM/GPRS – –
ler” user manual
“Installing and operating the AXC 1050 and AXC 1050 XC controllers” user UM EN AXC 1050 (XC) – –
manual
“Installing and operating the AXC 3050 controller” user manual UM EN AXC 3050 (/XC) – –
“Installing and operating the PC WORX RT BASIC software PLC with Value- UM EN PC WORX RT BASIC – –
line industrial PCs and PROFINET” user manual
“Installing and operating the PC WORX SRT software PLC” user manual UM EN PC WORX SRT – –
“PC Worx” quick start guide UM QS EN PC WORX – –
4 Modbus/TCP
Modbus/TCP is a communication protocol used to ex- – Acyclic communication
change process data between a client and a server in an Process data between the Modbus/TCP client and the
Ethernet network. Modbus/TCP server is exchanged as required. The
A Modbus/TCP client function is therefore integrated in “MB_ASYNC_RW” function block, which is used on the
Phoenix Contact controllers (see “Ordering data” on client side, is available in PC Worx for this (see
page 4). The I/O devices must include a Modbus/TCP Section 5.2).
server function. The TCP/IP connection is established, it is disconnect-
As of firmware Version 4.40, ILC 1X1 controllers can also be ed once the data has been transmitted, and is then re-
configured as Modbus/TCP servers (see Section 7). established in the event of a new communication
request. Connection establishment and connection
The TCP protocol (Transmission Control Protocol) or the abort are initiated automatically by the Modbus/TCP cli-
UDP protocol (User Datagram Protocol) can be used for ent.
data transmission. The Modbus protocol data to be trans-
mitted is embedded in the relevant protocol. The controller By default, port 502, which is reserved for Modbus/TCP, is
supports two methods of communication: used for communication.
– Cyclic communication The Modbus/TCP client initiates communication between
the Modbus/TCP client and server. The client sends a re-
Process data between the Modbus/TCP client and the
quest in the form of a Modbus/TCP function code (and data,
Modbus/TCP server is exchanged cyclically. This is
if necessary) to the Modbus/TCP server. After successful
triggered by the corresponding task in which the pro-
receipt of the request, the server sends a corresponding re-
cess data is defined.
sponse to the client which includes the requested data and
The Modbus/TCP client can maintain process data
status information or an error message. The data may con-
watchdogs of Modbus/TCP servers in a time period that
tain bit or word information.
can be defined. To do so, the Modbus/TCP client re-
peats all the write access operations to the relevant The device-internal organization of the data (Modbus mem-
Modbus/TCP server using the current data. ory addresses, etc.) and of the supported Modbus function
During cyclic communication, the established TCP/IP codes varies depending on the device and manufacturer.
connection between the client and server remains per- For more information, please refer to the documentation for
manently active. the relevant device.
For communication between the client and server, Modbus/
TCP provides various services for read and write access to
digital inputs and outputs and to registers.
The following table shows the supported Modbus function
codes:
The following example project consists of the ILC 151 ETH OFFLINE
(controller) and the ILB ETH 24 DI16 DIO16-2TX (Inline Creating a new project
Block IO module for Ethernet with 16 digital inputs and 16
digital inputs or outputs).
A Modbus/TCP network is used exclusively in the example Specifying
project information
project.
For information on using a gateway to connect a Modbus/
TCP network to Modbus/RTU devices, please refer to Checking/modifying
Section 6. IP settings for the controller
Generating variables
and assigning process data
ONLINE
Configuring
the Modbus/TCP server
Operation (end)
8294C001
Station Name
“Hide Device” or “Deactivate Bus” can be select- This name is the unique identification for the Modbus/TCP
ed via the context menu for the device. server (here: ILB ETH 24 DI16 DIO16-2TX) in the network.
To show the device again or to activate the bus,
select the aforementioned settings from the con- MAC Address
text menu again. Enter the MAC address of the Modbus/TCP server (here:
ILB ETH 24 DI16 DIO16-2TX). It is printed on the respective
5.1.5 Modifying the settings for the “Generic Modbus device. It starts with “00.a0.45.” on Phoenix Contact de-
Device” vices.
Following insertion in the bus configuration, default values Entry of the MAC address is optional and is not absolutely
are set for each “Generic Modbus Device”. The settings can necessary.
be modified via the “Modbus-Settings” tab.
IP Address
The settings for the “Generic Modbus Device” are The IP address allows the Modbus/TCP server (here:
required for communication between the Mod- ILB ETH 24 DI16 DIO16-2TX) to be accessed during opera-
bus/TCP client and server. tion. PC Worx selects the address from the area that is set
• Make sure you are in the bus configuration workspace. on the project node.
• Select the “Generic Modbus Device” in the “Bus Struc-
Subnetmask
ture” window.
• Select the “Modbus-Settings” tab in the “Device Details” The subnet mask that was specified on the project node is
window. assigned to each Modbus/TCP server (here:
ILB ETH 24 DI16 DIO16-2TX). It can be modified specifi-
• Modify the Modbus settings according to your require-
cally for each individual device.
ments.
Protocol
Select the protocol (TCP or UDP) to be used for communi-
cation here.
Port
Select the port for communication here. By default, port 502,
which is reserved for Modbus/TCP, is used.
Swap Bytes
If required, the byte order for the entire communication with
the Modbus/TCP server can be swapped here, provided
that the Modbus/TCP server and the Modbus/TCP client
(here: ILC 151 ETH) operate using a different byte order.
Select “Yes” to reverse the byte order used on the client
side.
For information on the byte order, refer to Section 9.
• Select the “Generic Modbus Device” in the top right win- • Variables are created when the program is created. Us-
dow. The standard configuration is then displayed in the ing drag and drop, link the selected variable to one of
top left window (“Symbols/Variables”). the variables displayed on the left-hand side.
• In the top left window (“Symbols/Variables”), select the If you would like to link further process data but no cor-
standard resource. responding variables have been created yet, select
• In the top right window, select the device for which you “Create Variable” in the context menu.
would like to link the process data to variables (in The created variable is displayed in the bottom left window.
Figure 6: “Generic Modbus Device”; the • Repeat this procedure for all inputs to be evaluated and
ILB ETH 24 DI16 DIO16-2TX module is used in the ex- for all outputs to be controlled.
ample). The result of process data assignment is shown in Figure 6.
• Select the process data item to be linked.
• Switch to the process data assignment workspace to The result of process data assignment is shown in Figure 8.
assign the process data to the control and diagnostic
variables, as described in Section 5.1.10 on page 9.
5.2 Acyclic communication using the • In the “MBT_STATION_DIAG” structure, replace the
MB_ASYNC_RW function block “IP:DWORD;” data type with the “Handle:DWORD;”
data type.
The MB_ASYNC_RW function block is used for acyclic
communication between the controller (Modbus/TCP client) • In the “MBT_STATION_DIAG” structure, replace the
and the Modbus/TCP server. “UnitID:BYTE;” data type with the “StationNum-
ber:BYTE;” data type.
The old function block can now be used.
The MB_ASYNC_RW function block is available
in two different versions. The two versions differ
with respect to the “ServerID” input and the “IP” For further information on the function block,
and “UnitID” inputs. The function block either has please refer to the online help for PC Worx.
the “ServerID” input (old block) or the “IP” and
“UnitID” inputs (new block). The new function
block (see Figure 9) is supported by the following
devices:
– ILC 1x1 ≥ firmware 4.20
– AXC 1050 (XC) ≥ firmware 2.10
– AXC 3050 ≥ firmware 5.50
All other controllers (see “Ordering data” on
page 4) support the old block (“ServerID” input).
Recommended:
Use the IP address from the previously created diagnostic struc-
ture (see Section 5.4). This ensures that the current settings
from the bus configuration are always used.
Alternatively, the IP address can be entered directly.
Recommended:
Use the UnitID from the previously created diagnostic structure
(see Section 5.4). This ensures that the current settings from the
bus configuration are always used.
Alternatively, the UnitID can be entered directly.
FC BYTE Modbus function code (see Table “Modbus function codes” on page 5)
ReadStartAddr WORD Start address of the memory from which data should be read.
WriteStartAddr WORD Start address from which data should be written to the memory.
MaxReadCount INT Number of bits or registers to be read.
WriteCount INT Number of bits or registers to be written.
Status DWORD In the event of an error (Error = TRUE), the “Status” output contains an error
code. The possible error codes are shown in Table “Error codes of the “Sta-
tus” output” on page 15.
ReadByteCount INT Number or read bits/registers
WrittenByteCount INT Number of written bits/registers
ReadBuffer BYTE Buffer (250 bytes, maximum) in which the read bits/registers are stored (de-
WORD pending on the function code used).
DWORD
ARRAY OF BYTE, To define the size of the buffer, see Section 5.5 “Specifying the
ARRAY OF WORD size of the ReadBuffer/WriteBuffer” .
ARRAY OF DWORD
WriteBuffer BYTE Buffer (250 bytes, maximum) for the written bits/registers (depending on the
WORD function code used).
DWORD
ARRAY OF BYTE, To define the size of the buffer, see Section 5.5 “Specifying the
ARRAY OF WORD size of the ReadBuffer/WriteBuffer” .
ARRAY OF DWORD
To define the size of the buffer, see Section 5.5 “Specifying the
size of the ReadBuffer/WriteBuffer” .
To define the size of the buffer, see Section 5.5 “Specifying the
size of the ReadBuffer/WriteBuffer” .
0x0201 0000 Unknown server ID (not configured)
0x0202 xxxx Modbus protocol error code
For detailed information on the error codes of the Modbus/TCP protocol, please
refer to the “MODBUS APPLICATION PROTOCOL SPECIFICATION” docu-
ment.
Current document versions can be found at modbus org
0x0203 0016 Timeout when receiving the response from the Modbus/TCP server.
0x0203 0019 The connection was terminated by the Modbus/TCP server.
0x0203 001F The request was not sent. The Modbus/TCP server is not accessible.
0x0301 0000 Timeout when receiving the response from the Modbus stack. The Modbus/TCP server is
not accessible.
• Put a period after the selected name and select the “IP”
entry from the list that appears.
• Once you have defined the data types, select the “Build,
Make” command.
Once compiled, the data types can be used in the variable
worksheet. In Figure 15, the previously created
“MODBUS_BUFFER_BSP1” data type is used for the
“ReadBuffer” input/output parameter.
• Double-click on the “ReadBuffer” input parameter in the
MB_ASYNC_RW_1 function block.
• In the “Variable Properties” window, enter a name for
the variable (in the example: “Read_Buffer_1”).
Figure 13 MB_DIAG.IP variable as “IP” in the function • In the “Data Type” list, select the previously created
block data type you wish to use (in the example:
“MODBUS_BUFFER_BSP1”).
• Create the MB_DIAG.UnitID variable for the “UnitID” in-
put parameter. Proceed as described above for the
MB_DIAG.IP variable.
7 Configuring controllers as Mod- • Open the “Phoenix Contact, Generic, Server” device
catalog.
bus/TCP servers
• Insert the controller as the generic Modbus/TCP server
As of firmware Version 4.40, ILC 1X1 controllers by selecting “Modbus Server”.
can also be configured as Modbus/TCP servers.
This function is available as of
AUTOMATIONWORX Software Suite Version
1.82 AddOn V1.
7.1.3 Checking/modifying IP settings for the control- 7.1.5 Modifying the settings for the Modbus/TCP
ler server
See Section 5.1.3 Following insertion in the bus configuration, default values
are set for the Modbus/TCP server. The settings can be
7.1.4 Inserting the Modbus/TCP server modified via the “Modbus-Settings” tab.
• Make sure you are in the bus configuration workspace. • Make sure you are in the bus configuration workspace.
• If the device catalog is hidden, show it by selecting the • Select the “Modbus Server” in the “Bus Structure” win-
“View, Device Catalog” menu. dow.
• Select the “Modbus-Settings” tab in the “Device Details” – “Inputs keep the last value”
window. Inputs retain their last value.
• Modify the Modbus settings according to your require- – “Inputs perform write accesses”
ments. Inputs are updated. The state of the process data
watchdog is displayed in the “NODE_STATUS_255” di-
agnostic structure under “Flags”. If the watchdog has
been triggered, the corresponding flag is set. In this
case, you need to decide what should be done with the
values of the inputs and a corresponding algorithm
must be programmed.
Port
TCP/UDP port via which communication with the Modbus
client takes place.
Port 502 is used by default.
Controller (Server)
WORD Read
OUT
Input Registers FC04
0000hex ... ffffhex
WORD Read/Write
PLC IN
Holding Registers FC03/FC16/FC06/FC23
Client(s)
0000hex ... ffffhex
runtime
(eCLR) BIT
Discrete Inputs Read
OUT FC02
0000hex ... ffffhex
BIT Read/Write
IN Coils FC01/FC05/FC15
0000hex ... ffffhex
Parameter Description
AcceptCnt Number of existing and cur-
rently established connec-
tions
AcceptErrorCnt Number of errors during con-
nection establishment
Figure 28 Register definitions that are coordinated ReceiveErrorCnt Number of errors when re-
ceiving requests
ModbusRequesCnt Number of requests received
ModbusExceptionCnt Number of exceptions sent
SocketErrorCode Error code of the socket error
Parameter Description
ModbusRequestCnt Number of requests received
for this server instance (node)
ModbusExceptionCnt Number of exceptions sent for
this server instance (node)
Flags If the process data watchdog
has been triggered, the flag
for this server instance (node)
is set (bit 0 = 1).
reserved Reserved
Parameter Description
Flags All parameters of the diagnos-
tic structures are reset with a
rising edge at bit 0.
reserved1 Reserved
reserved2 Reserved
reserved3 Reserved
• Switch to the process data assignment workspace to The result of process data assignment is shown in
assign the process data to the control and diagnostic Figure 31.
variables, as described in Section 5.1.10 on page 9.
7.1.12 Defining the cyclic task for the update time of • Create a cyclic task and specify an appropriate cycle
the Modbus registers time (“Interval” in the “Task Settings” window; recom-
mended setting: > 10 ms).
The Modbus registers are updated with the standard task
For information on creating a task, please refer to the
(STD_TASK) by default. The standard task runs with the
UM QS EN PC WORX quick start guide or the PC Worx
fastest possible cycle, where the cycle time is often 4 ms.
online help.
This results in a high CPU load. In order to reduce the CPU
load, a cyclic task can be defined with a cycle time that is • In the “Bus Structure” window, select the standard re-
greater than the standard task cycle time. Since the Modbus source and in the “Device Details” window select the cy-
registers are updated within a longer cycle time, the load on clic task created previously for “I/O Update by Task”
the CPU is lower and more resources are available for the (see Figure 32).
PC Worx project.
8.9 Modifying the settings for the Modbus/TCP 8.14 Compiling after creating the program
server
• Select the “Build, Make” command.
See Section 7.1.5
8.15 Creating process data and assigning Modbus
8.10 Inserting a “Generic Modbus Device” function codes
In the example, the ILC 191 ME/AN controller • Create the necessary process data for the specific
should simultaneously be operated as a Modbus/ Modbus device of the ILC 191 ME/AN controller and for
TCP server (in relation to the ILC 131 ETH con- the “Generic Modbus Device” and assign it to the corre-
troller) and sponding Modbus function codes.
as a Modbus/TCP client (in relation to the Proceed as described in Section 5.1.9.
EEM-MA400 energy meter) (see Figure 33).
8.16 Generating variables and assigning process
Specific settings are required for communication data
between the ILC 191 ME/AN controller and the
EEM-MA400 energy meter. • Generate variables for the specific Modbus device of
These settings are made in PC Worx via the “Ge- the ILC 191 ME/AN controller and for the “Generic
neric Modbus Device”. Modbus Device” and assign the corresponding process
data to the variables.
• To insert the “Generic Modbus Device”, proceed as de- Proceed as described in Section 5.1.10.
scribed in Section 5.1.4.
Figure 38 shows the bus configuration with the inserted 8.17 Generating diagnostic and control variables
“Generic Modbus Device”. and assigning process data
• Generate diagnostic and control variables for the spe-
cific Modbus device of the ILC 191 ME/AN controller
and for the “Generic Modbus Device” and assign the
corresponding process data to the variables.
Proceed as described in Section 5.1.11.
8.24 Sending a project and performing a cold restart 9.1 General examples
See Section 5.1.14 The examples below show how the transmitted data of the
Modbus/TCP frame is mapped in the process variables of
the PC Worx project.
Example 1
Process variable of WORD data type, “Swap Bytes” setting
equals “no”.
Example 2
Process variable of Array [0 ... 1] of WORD data type,
“Swap Bytes” setting equals “no”.
Example 3
Process variable of WORD data type, “Swap Bytes” setting
equals “yes”.
③
Figure 52 Definition of the “MB_2Word” and
“MB_2DWord” data types
Key:
1. Modbus/TCP telegram in the Wireshark software (soft- Figure 53 Linking of the process variables to the Modbus
ware for analyzing data traffic in the network) function codes
2. Linked process variables in the Watch window in
PC Worx when the “Swap Bytes” setting equals “no”
3. Linked process variables in the Watch window in
PC Worx when the “Swap Bytes” setting equals “yes”
Online values during operation Figure 55 shows the online values during operation when
the “Swap Bytes” setting equals “yes”.
Figure 54 shows the online values during operation when
the “Swap Bytes” setting equals “no”.
①
①
②
②
8294_en_03 PHOENIX CONTACT GmbH & Co. KG • Flachsmarktstraße 8 • 32825 Blomberg • Germany 32 / 32
phoenixcontact.com