Modbus TCP 7
Modbus TCP 7
Modbus_TCP_7
for PC Worx
Documentation for
PHOENIX CONTACT function blocks
PHOENIX CONTACT GmbH Co. KG
Flachsmarktstrasse 8
D-32825 Blomberg, Germany
This documentation is available in English only.
Modbus_TCP_7 2/50
Table of Contents
1 Installation hint
2 General information
3 Change notes
4 Supported PLCs
5 Function blocks
6 MB_TCP_Server
6.1 Function block call
6.2 Input parameters
6.3 Output parameters
6.4 Inout parameters
6.5 Diagnosis
7 MB_TCP_Client
7.1 Function block call
7.2 Input parameters
7.3 Output parameters
7.4 Inout parameters
7.5 Diagnosis
8 MB_TCP_FC1
8.1 Function block call
8.2 Input parameters
8.3 Output parameters
8.4 Inout parameters
8.5 Diagnosis
9 MB_TCP_FC2
9.1 Function block call
9.2 Input parameters
9.3 Output parameters
9.4 Inout parameters
9.5 Diagnosis
10 MB_TCP_FC3
10.1 Function block call
10.2 Input parameters
10.3 Output parameters
10.4 Inout parameters
10.5 Diagnosis
11 MB_TCP_FC4
11.1 Function block call
11.2 Input parameters
11.3 Output parameters
11.4 Inout parameters
11.5 Diagnosis
12 MB_TCP_FC5
12.1 Function block call
Modbus_TCP_7 3/50
1 Installation hint
Please copy the library data to your PC Worx 6 working library directory.
If you did not specify a different directory during PC Worx 6 installation the default PC Worx 6 working library
directory is
C:\Users\Public\Documents\PC WORX\Libraries
2 General information
The Modbus_TCP library offers function blocks for communication with the controller via Modbus TCP protocol.
Modbus_TCP_7 6/50
3 Change notes
Library Library PC Worx Change notes
version build version
7 20230126 6.30.2972 Improved documentation with:
Modbus_TCP runtime (see Appendix)
7 20230120 6.30.2972 MB_TCP_Client:
MB_TCP_Client:
MB_TCP_Client:
Internal improvement.
MB_TCP_Client:
MB_TCP_Client_2:
MB_TCP_FC15_2:
MB_TCP_FC16_2:
MB_TCP_FC23_2:
1 20180316 6.10.200 Extracted from Modbus_3 library. Modbus_TCP_1 is NOT compatible with
the Modbus TCP function blocks of Modbus_3.
MB_TCP_Server_1:
MB_TCP_Client_1:
Input tReconnectDelay: new In former versions the value has been set
to 100ms inside the function block. Now it can be set by the
application.
Output udtDiag: new
MB_TCP_FC*_1:
4 Supported PLCs
AXC 1050 (2700988)
AXC 1050 XC (2701295)
AXC 3050 (2700989)
ILC 130 ETH (2988803)
ILC 131 ETH (2700973)
ILC 131 ETH/XC (2701034)
ILC 150 ETH (2985330)
ILC 151 ETH (2700974)
ILC 151 GSM/GPRS (2700977)
ILC 170 ETH 2TX (2916532)
ILC 171 ETH 2TX (2700975)
ILC 190 ETH 2TX (2700527)
ILC 191 ETH 2TX (2700976)
ILC 191 ME/AN (2700074)
ILC 191 ME/INC (2700075)
ILC 330 ETH (2737193)
ILC 330 PN (2988191)
ILC 350 ETH (2737203)
ILC 350 ETH/M (2985819)
ILC 350 PN (2876928)
ILC 370 ETH 2TX-IB (2876999)
ILC 370 PN 2TX-IB (2876915)
ILC 370 PN 2TX-IB/M (2985576)
ILC 390 PN 2TX-IB (2985314)
RFC 430 ETH-IB (2730190)
RFC 450 ETH-IB (2730200)
RFC 470S PN 3TX (2916794)
RFC 470 PN 3TX (2916600)
RFC 480S PN 4TX (2404577)
Modbus_TCP_7 10/50
5 Function blocks
Function block Description Version Supported License
articles
MB_TCP_Client The function block enables communication as 6 - none
client with Modbus TCP devices (server).
MB_TCP_Server The function block enables communication as a 6 - none
server with a TCP client.
MB_TCP_DiagInfo_EN This optional function block displays diagnostic 1 - none
messages of the Modbus client as plain text in
English.
MB_TCP_FC1 This function code is used to read from 1 to 2000 1 - none
contiguous status of coils in a remote device.
MB_TCP_FC2 This function code is used to read from 1 to 2000 1 - none
contiguous status of discrete inputs in a remote
device.
MB_TCP_FC3 This function code is used to read from 1 to 125 1 - none
contiguous holding registers in a remote device.
MB_TCP_FC4 This function code is used to read from 1 to 125 1 - none
contiguous input registers in a remote device.
MB_TCP_FC5 This function code is used to write a single 1 - none
output to either ON or OFF in a remote device.
MB_TCP_FC6 This function code is used to write a single 1 - none
holding register in a remote device.
MB_TCP_FC15 This function code is used to force each coil in a 2 - none
sequence of coils to either ON or OFF in a
remote device.
MB_TCP_FC16 This function code is used to write a block of 2 - none
contiguous registers (1 to 123 registers) in a
remote device.
MB_TCP_FC23 This function code performs a combination of one 2 - none
read operation and one write operation in a single
MODBUS transaction.
Modbus_TCP_7 11/50
6 MB_TCP_Server
This function block organizes the read and writes accesses of a remote control system to internal data via Modbus
TCP. The data is stored in the arrModbusData block parameter, which is a WORD array with 7167 elements. Since
this array has been defined as IN/OUT parameter, it can be read and written to in the local user program as
required.
Attention: Depending on the control system used, the number of functions that the Modbus server can process in a
given time is limited. In the event of overloading it may happen that the server does not respond to a telegram. In
this case the accessing control system must send the telegram again.
Only one Modbus client can access the Modbus server at a time.
Access to Registers:
The Read Holding Registers, Write Multiple Registers and Read/Write Multiple Registers Modbus functions access
the WORD elements in the udt_TCP_ComData array. The array index and the register address to be specified
when calling the Modbus functions are identical.
Access to internal bits:
When accessing internal bits all bits existing in the udt_TCP_ComData array are numbered consecutively. These
numbers can be used to read this data with the Read Coils service. The numbering scheme is shown in below. The
Modbus bit number must be specified when the data is accessed via the Modbus function.
Modbus_TCP_7 12/50
tTimeout Time Timeout monitoring when there is a write access. After a write function
was executed, a write telegram must be received again within the time
specified here. A timeout error is reported if this is not the case. Specify
the timeout value T#0s to disable this function.
uiOffsetInputRegister UINT Virtual address of the first input register (0 – 7167). Example: The value
1000 means that the first Input-Register has the address 1000. Required
for FC4.
uiOffsetHoldingRegister UINT Virtual address of the first holding register (0 – 7167). Example: The value
1000 means that the first Holding-Register has the address 1000.
Required for FC3, FC6, FC16, FC23.
uiOffsetInputs UINT Register-address of the first Input (0 – 7167). Required for FC2.
uiOffsetCoils UINT Register-address of the first Coil (0 – 7167). Required for FC1, FC5, FC15.
6.5 Diagnosis
wDiagCode wAddDiagCode Description
16#0000 16#0000 The block is not active
16#8000 16#0000 The block is active
16#8100 16#0000 Socket is open, no Client connected
16#C010 Wrong Parameter
16#0010 Invalid parameter at uiOffsetInputs ( > 7167)
16#0020 Invalid parameter at uiOffsetCoils ( > 7167)
16#0030 Invalid parameter at uiOffsetHoldingRegister ( > 7167)
16#0040 Invalid parameter at uiOffsetInputRegister. ( > 7167)
16#C020 16#0000 Timeout error. A write services was executed and another write telegram
was not received within the time specified in the tTimeout parameter. Enter
the value T#0s in the tTimeout parameter to disable this function.
16#C030 16#xxxx IP_CONNECT error 16#xxxx. Refer to appendix.
16#C040 16#xxxx IP_USEND error 16#xxxx. Refer to appendix.
16#C050 16#xxxx IP_URCV error 16#xxxx. Refer to appendix.
6.5.1 udtDiag
The udtDiag structure contains all inputs and outputs of the used firmware function blocks:
Modbus_TCP_7 15/50
7 MB_TCP_Client
7.1 Function block call
xError BOOL TRUE: An error has occurred. For more details refer to
wDiagCode and wAddDiagCode.
wDiagCode WORD Diagnosis code. Refer to diagnostic table.
wAddDiagCode WORD Additional diagnosis code. Refer to diagnostic table.
udtDiag MB_TCP_UDT_CLI_DIAG Additional diagostic data.
7.5 Diagnosis
wDiagCode wAddDiagCode Description
16#0000 16#0000 The block is not active
16#8000 16#0000 The block is active
16#8100 16#0000 The block is connecting to server
16#C010 A parameterization error has occurred
16#0010 The IP address of the Modbus server was not specified.
16#C020 16#0000 Timeout error
16#C030 16#xxxx IP_CONNECT error 16#xxxx. Refer to appendix.
16#C040 16#xxxx IP_USEND error 16#xxxx. Refer to appendix.
16#C050 16#xxxx IP_URCV error 16#xxxx. Refer to appendix.
Modbus_TCP_7 17/50
7.5.1 udtDiag
The udtDiag structure contains all inputs and outputs of the used firmware function blocks:
Modbus_TCP_7 18/50
8 MB_TCP_FC1
This function block reads coils from a Modbus slave.
Before this function block is Activated, xReady of the MB_TCP_Client must be TRUE.
The data is read in the form of a byte array (CoilStatus parameter), the first bit in the first byte of this array
corresponds to the first output to be read. The following diagram shows the assignment of the bits in the byte array
to the outputs and the internal bits, in this example output 1 to 16 are read:
Byte 1 (Coil Status) Byte 2 (Coil Status)
Bit number 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
Bit number 8 7 6 5 4 3 2 1 16 15 14 13 12 11 10 9
8.5 Diagnosis
wDiagCode wAddDiagCode Description
16#0000 16#0000 The block is not active.
16#8000 16#0000 The block is active.
16#C010 16#0000 The MB_TCP_Client block is not ready.
16#C030 16#0000 The iMT_ID used is invalid. The iMT_ID must be in the range from 1 to 10.
16#C040 16#0000 The iMT_ID used is assigned twice. Several Modbus function blocks use the
same iMT_ID, in this case only the first called block operates, all other
blocks indicate this error.
16#C060 Modbus error
16#0001 Illegal function
16#0002 Illegal data address
16#0003 Illegal data value
16#0004 Slave device failure
16#0005 Acknowledge
16#0006 Slave device busy
16#0008 Memory parity error
16#000A Gateway path unavailable
16#000B Gateway target device failed to respond
Modbus_TCP_7 20/50
9 MB_TCP_FC2
This function block reads discrete inputs from a Modbus slave.
Before this function block is Activated, xReady of the MB_TCP_Client must be TRUE.
The data is read in the form of a byte array (InputStatus parameter), the first bit in the first byte of this array
corresponds to the first input to be read. The following diagram shows the assignment of the bits in the byte array
to the inputs in this example inputs 1 to 16 are read:
Byte 1 (Input Status) Byte 2 (Input Status)
Bit number 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
Bit number 8 7 6 5 4 3 2 1 16 15 14 13 12 11 10 9
9.5 Diagnosis
wDiagCode wAddDiagCode Description
16#0000 16#0000 The block is not active.
16#8000 16#0000 The block is active.
16#C010 16#0000 The MB_TCP_Client block is not ready.
16#C030 16#0000 The iMT_ID used is invalid. The iMT_ID must be in the range from 1 to 10.
16#C040 16#0000 The iMT_ID used is assigned twice. Several Modbus function blocks use the
same iMT_ID, in this case only the first called block operates, all other
blocks indicate this error.
16#C060 Modbus error
16#0001 Illegal function
16#0002 Illegal data address
16#0003 Illegal data value
16#0004 Slave device failure
16#0005 Acknowledge
16#0006 Slave device busy
16#0008 Memory parity error
16#000A Gateway path unavailable
16#000B Gateway target device failed to respond
Modbus_TCP_7 22/50
10 MB_TCP_FC3
This function block reads holding registers from a Modbus slave.
Before this function block is Activated, xReady of the MB_TCP_Client must be TRUE.
The data is read in the form of a byte array (InputStatus parameter), the first bit in the first byte of this array
corresponds to the first input to be read. The following diagram shows the assignment of the bits in the byte array
to the inputs. In this example inputs 1 to 16 are read:
Byte 1 (InputStatus) Byte 2 (InputStatus)
Bit number 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
Bit number 8 7 6 5 4 3 2 1 16 15 14 13 12 11 10 9
10.5 Diagnosis
wDiagCode wAddDiagCode Description
16#0000 16#0000 The block is not active.
16#8000 16#0000 The block is active.
16#C010 16#0000 The MB_TCP_Client block is not ready.
16#C030 16#0000 The iMT_ID used is invalid. The iMT_ID must be in the range from 1 to 10.
16#C040 16#0000 The iMT_ID used is assigned twice. Several Modbus function blocks use the
same iMT_ID, in this case only the first called block operates, all other
blocks indicate this error.
16#C060 Modbus error
16#0001 Illegal function
16#0002 Illegal data address
16#0003 Illegal data value
16#0004 Slave device failure
16#0005 Acknowledge
16#0006 Slave device busy
16#0008 Memory parity error
16#000A Gateway path unavailable
16#000B Gateway target device failed to respond
Modbus_TCP_7 24/50
11 MB_TCP_FC4
This function block reads input registers from a Modbus slave.
Before this function block is Activated, xReady of the MB_TCP_Client must be TRUE.
11.5 Diagnosis
wDiagCode wAddDiagCode Description
16#0000 16#0000 The block is not active.
16#8000 16#0000 The block is active.
16#C010 16#0000 The MB_TCP_Client block is not ready.
16#C030 16#0000 The iMT_ID used is invalid. The iMT_ID must be in the range from 1 to 10.
16#C040 16#0000 The iMT_ID used is assigned twice. Several Modbus function blocks use the
same iMT_ID, in this case only the first called block operates, all other
blocks indicate this error.
16#C060 Modbus error
16#0001 Illegal function
16#0002 Illegal data address
16#0003 Illegal data value
16#0004 Slave device failure
16#0005 Acknowledge
16#0006 Slave device busy
16#0008 Memory parity error
16#000A Gateway path unavailable
16#000B Gateway target device failed to respond
Modbus_TCP_7 26/50
12 MB_TCP_FC5
This function block writes a bit in the memory of a Modbus slave.
Before this function block is Activated, xReady of the MB_TCP_Client must be TRUE.
12.5 Diagnosis
wDiagCode wAddDiagCode Description
16#0000 16#0000 The block is not active.
16#8000 16#0000 The block is active.
16#C010 16#0000 The MB_TCP_Client block is not ready.
16#C030 16#0000 The iMT_ID used is invalid. The iMT_ID must be in the range from 1 to 10.
16#C040 16#0000 The iMT_ID used is assigned twice. Several Modbus function blocks use the
same iMT_ID, in this case only the first called block operates, all other
blocks indicate this error.
16#C060 Modbus error
16#0001 Illegal function
13 MB_TCP_FC6
This function block writes a single holding register into the memory of a Modbus slave.
Before this function block is Activated, xReady of the MB_TCP_Client must be TRUE.
13.5 Diagnosis
wDiagCode wAddDiagCode Description
16#0000 16#0000 The block is not active.
16#8000 16#0000 The block is active.
16#C010 16#0000 The MB_TCP_Client block is not ready.
16#C030 16#0000 The iMT_ID used is invalid. The iMT_ID must be in the range from 1 to 10.
16#C040 16#0000 The iMT_ID used is assigned twice. Several Modbus function blocks use the
same iMT_ID, in this case only the first called block operates, all other
blocks indicate this error.
16#C060 Modbus error
16#0001 Illegal function
14 MB_TCP_FC15
This function block writes multiple coils in the memory of the Modbus slave. The data is read in the form of a byte
array, the first bit in the first byte of this array corresponds to the first output to be written.
Before this function block is Activated, xReady of the MB_TCP_Client must be TRUE.
14.5 Diagnosis
wDiagCode wAddDiagCode Description
16#0000 16#0000 The block is not active.
16#8000 16#0000 The block is active.
16#C010 16#0000 The MB_TCP_Client block is not ready.
16#C030 16#0000 The iMT_ID used is invalid. The iMT_ID must be in the range from 1 to 10.
16#C040 16#0000 The iMT_ID used is assigned twice. Several Modbus function blocks use the
same iMT_ID, in this case only the first called block operates, all other
blocks indicate this error.
16#C060 Modbus error
16#0001 Illegal function
15 MB_TCP_FC16
This function block writes multiple registers into the memory of a Modbus slave.
Before this function block is Activated, xReady of the MB_TCP_Client must be TRUE.
15.5 Diagnosis
wDiagCode wAddDiagCode Description
16#0000 16#0000 The block is not active.
16#8000 16#0000 The block is active.
16#C010 16#0000 The MB_TCP_Client block is not ready.
16#C030 16#0000 The iMT_ID used is invalid. The iMT_ID must be in the range from 1 to 10.
16#C040 16#0000 The iMT_ID used is assigned twice. Several Modbus function blocks use the
same iMT_ID, in this case only the first called block operates, all other
blocks indicate this error.
16#C060 Modbus error
16#0001 Illegal function
16 MB_TCP_FC23
This function block performs a combination of one read operation (holding registers) and one write operation
(holding registers) in a single MODBUS transaction. The write operation is performed before the read.
Before this function block is Activated, xReady of the MB_TCP_Client must be TRUE.
16.5 Diagnosis
wDiagCode wAddDiagCode Description
16#0000 16#0000 The block is not active.
16#8000 16#0000 The block is active.
16#C010 16#0000 The MB_TCP_Client block is not ready.
16#C030 16#0000 The iMT_ID used is invalid. The iMT_ID must be in the range from 1 to 10.
16#C040 16#0000 The iMT_ID used is assigned twice. Several Modbus function blocks use the
same iMT_ID, in this case only the first called block operates, all other
blocks indicate this error.
16#C060 Modbus error
16#0001 Illegal function
16#0002 Illegal data address
16#0003 Illegal data value
16#0004 Slave device failure
16#0005 Acknowledge
16#0006 Slave device busy
16#0008 Memory parity error
16#000A Gateway path unavailable
Modbus_TCP_7 36/50
17 MB_TCP_DiagInfo_EN
If there is an error, this block shows the diagnostics of the master block as a text in English. The source code of
the block can be read and modified. To show the diagnostic messages in other languages, copy the block and
translate the diagnostic text into the desired language. The text outputs (strDiagCode and strAddDiagCode) are
limited to 80 characters.
17.5 Diagnosis
None
Modbus_TCP_7 38/50
18 Startup examples
For the startup instruction of the Modbus_TCP function block library please find the following example:
MB_TCP_*_EXA_Server.zwt
MB_TCP_*_EXA_Client.zwt
18.1 System
To start the example set the xStart input of the Example function block to TRUE.
Modbus_TCP_7 39/50
To start the example set the xStart input of the Example function block to TRUE.
Modbus_TCP_7 41/50
19 Appendix
19.1 Modbus_TCP runtime
To simplify the correct selection of task cycle time and Modbus mode, the Modbus packet runtime at different
settings is shown below.
Reference: WORD;
Word_Count: WORD;
DATA: MB_TCP_ARR_B_1_250;
END_STRUCT;
MB_TCP_UDT_MBS_RESPONSE : STRUCT
DATA_CNT: WORD; (* Not part of Modbus protocol *)
Transaction_ID: WORD;
Protocol_ID: WORD;
Length: WORD;
Unit_ID: BYTE;
FC: BYTE;
DATA: MB_TCP_ARR_B_1_250;
END_STRUCT;
(* Buffers for requests *)
MB_TCP_ARR_MBS_BUFFER: ARRAY [0..10] OF MB_TCP_UDT_MBS_REQUEST;
(* I/O parameter of firmware function blocks as struct *)
MB_TCP_UDT_IP_CONNECT : STRUCT
(* Inputs *)
xEN_C : BOOL;
strPartner : STRING;
(* Outputs *)
xValid : BOOL;
xError : BOOL;
iStatus : INT;
iID : INT;
END_STRUCT;
MB_TCP_UDT_IP_USEND : STRUCT
(* Inputs *)
xReq : BOOL;
iID : INT;
strR_ID : STRING;
(* Outputs *)
xDone : BOOL;
xError : BOOL;
iStatus : INT;
END_STRUCT;
MB_TCP_UDT_IP_URCV : STRUCT
(* Inputs *)
xEN_R : BOOL;
iID : INT;
strR_ID : STRING;
(* Outputs *)
xNDR : BOOL;
xError : BOOL;
iStatus : INT;
END_STRUCT;
(* Diag structure for MB_TCP_Server *)
MB_TCP_UDT_SER_DIAG : STRUCT
udtIP_CONNECT : MB_TCP_UDT_IP_CONNECT;
udtIP_USEND : MB_TCP_UDT_IP_USEND;
udtIP_URCV : MB_TCP_UDT_IP_URCV;
END_STRUCT;
(* Diag structure for MB_TCP_Client *)
MB_TCP_UDT_CLI_DIAG : STRUCT
Modbus_TCP_7 48/50
udtIP_CONNECT : MB_TCP_UDT_IP_CONNECT;
udtIP_USEND : MB_TCP_UDT_IP_USEND;
udtIP_URCV : MB_TCP_UDT_IP_URCV;
END_STRUCT;
END_TYPE
Modbus_TCP_7 49/50
20 In case of problems
In case of problems you should check in general:
1. Is the latest version of the library used? You can download the latest version from the Phoenix Contact
website.
2. Is the library released for the PC Worx 6 version you are using? Please refer to the chapter “Change notes” of
this documentation.
3. Is the library released for the PLC / PLC firmware version you are using? Please refer to the chapter “Change
notes” of this documentation.
4. Does every module in your system has the required firmware version? In some cases functionalities are
supported for higher firmware versions only.
5. Is your system running? Check all LEDs of all modules. Check the status in the web based management of
your PLC.
Modbus_TCP_7 50/50
21 Support
For technical support please contact your local PHOENIX CONTACT agency
at https://www.phoenixcontact.com
Owner:
PHOENIX CONTACT Electronics GmbH
Business Unit Automation Systems
System Services
Library Services
In case of a support request we need: