0% found this document useful (0 votes)
3 views

Modbus TCP 7

The document provides a comprehensive guide for the Modbus_TCP_7 function block library for PC Worx, detailing installation, general information, supported PLCs, and various function blocks for Modbus TCP communication. It includes specific sections on function block calls, input/output parameters, and diagnostic information. Additionally, it outlines changes made in recent versions and provides examples for startup configurations.

Uploaded by

jose.moura
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Modbus TCP 7

The document provides a comprehensive guide for the Modbus_TCP_7 function block library for PC Worx, detailing installation, general information, supported PLCs, and various function blocks for Modbus TCP communication. It includes specific sections on function block calls, input/output parameters, and diagnostic information. Additionally, it outlines changes made in recent versions and provides examples for startup configurations.

Uploaded by

jose.moura
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Modbus_TCP_7 1/50

Function block library

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

12.2 Input parameters


12.3 Output parameters
12.4 Inout parameters
12.5 Diagnosis
13 MB_TCP_FC6
13.1 Function block call
13.2 Input parameters
13.3 Output parameters
13.4 Inout parameters
13.5 Diagnosis
14 MB_TCP_FC15
14.1 Function block call
14.2 Input parameters
14.3 Output parameters
14.4 Inout parameters
14.5 Diagnosis
15 MB_TCP_FC16
15.1 Function block call
15.2 Input parameters
15.3 Output parameters
15.4 Inout parameters
15.5 Diagnosis
16 MB_TCP_FC23
16.1 Function block call
16.2 Input parameters
16.3 Output parameters
16.4 Inout parameters
16.5 Diagnosis
17 MB_TCP_DiagInfo_EN
17.1 Function block call
17.2 Input parameters
17.3 Output parameters
17.4 Inout parameters
17.5 Diagnosis
18 Startup examples
18.1 System
18.2 Modbus server example
18.3 Modbus client example
19 Appendix
19.1 Modbus_TCP runtime
19.2 Call Modbus data with associated function codes
19.3 IP_CONNECT, IP_URCV, IP_USEND
19.4 Data types
20 In case of problems
21 Support
Modbus_TCP_7 4/50
Modbus_TCP_7 5/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:

Bugfix: Error just for one cycle.

6 20221205 6.30.2972 MB_TCP_Server & MB_TCP_Client:

xAcknowledge & xAutoAck: Also IP-errors are acknowledged.

MB_TCP_Client:

Bugfix: Error without Diag codes after loosing connection.

5 20210527 6.30.2972 MB_TCP_Server:

tTimeout: Specify the timeout value TIME#0s to disable this function.


Internal improvement.

MB_TCP_Client:

Changed xConnected to xReady.

4 20201204 6.30.2907 MB_TCP_Server:

Internal improvement.

MB_TCP_Client:

Added xConnected output


Internal improvement.
Modbus_TCP_7 7/50

3 20201028 6.30.2907 MB_TCP_Server_3:

To avoid error messages when deactivating and reactivating the block:


Increasing the default value for tReconnectDelay from 100ms to
500ms

MB_TCP_Client_2:

To avoid error messages when deactivating and reactivating the block:


Increasing the default value for tReconnectDelay from 100ms to
500ms

MB_TCP_FC15_2:

Limitation of the allowed written coils to 1968

MB_TCP_FC16_2:

Limitation of the allowed written coils to 123

MB_TCP_FC23_2:

New function block for the support of FC23.

2 20200709 6.30.2907 MB_TCP_Server_2:

Adapted uiOffsetInputRegister and uiOffsetHoldingRegister

1 20200304 6.10.200 Update of documentation


Modbus_TCP_7 8/50

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:

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_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:

wDiagCode in case of Modbus error is 16#C060, not 16#C050 any


more because 16#C050 is used for IP_URCV error. This was a
conflict in former versions.

New version number: Functional changes of at least one function block


New build number: No functional changes, but changes in the ZIP file (e.g. documentation update, additional
examples)
Modbus_TCP_7 9/50

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

6.1 Function block call

6.2 Input parameters


Name Type Description
xActivate BOOL Rising edge: Activates the function block.
FALSE: Deactivates the function block.
xAcknowledge BOOL Rising edge: Modbus timeouts and IP-errors are acknowledged. The
function block is not reinitialized.
xAutoAck BOOL Automatic acknowledgment and IP-errors of modbus timeouts.
TRUE: The error is acknowledged automatically. Error data is present at
the xError, wDiagCode and wAddDiagCode outputs for only one cycle.
xUDP_Mode BOOL Selection of communication protocol.
FALSE: Modbus-TCP is used
TRUE: Modbus-UDP is used
iTCP_Port INT Port to be used (UDP and TCP). If no number is specified here, the default
port 502 is used.
strPartner_IP STRING IP-Address of the client who is allowed to connect to the server. This
function is neccessary if multiple clients and servers are using the same
port. If no IP is specified here, all clients are allowed to connect to the
server.
tReconnectDelay TIME Delay between two IP connect executions. Needed by PLC. Default value
= 500ms. In case of increased occurrence of wDiagCode = 16#C030 and
wAddDiagCode = 16#0010 during a FB restart increase this time.
Modbus_TCP_7 13/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.3 Output parameters


Name Type Description
xActive BOOL FALSE: Function block is not active.
TRUE: Function block is active. Do not start any further action
unless xActive is TRUE after activation!
xConnected BOOL TCP:
TRUE: The Server is connected to a client.
UDP:
TRUE: The Socket of the MB_TCP_Server is open.
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_SER_DIAG Additional diagostic data.

6.4 Inout parameters


Name Type Description
arrModbusData MB_TCP_ARR_W_0_7167 WORD array
First index: 0
Last index: 7167
This array contains the Modbus registers.
Modbus_TCP_7 14/50

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

7.2 Input parameters


Name Type Description
xActivate BOOL Rising edge: Activates the function block.
FALSE: Deactivates the function block.
xAcknowledge BOOL Rising edge: Modbus timeouts and IP-errors are acknowledged. The function
block is not reinitialized.
xAutoAck BOOL Automatic acknowledgment and IP-errors of modbus timeouts.
TRUE: The error is acknowledged automatically. Error data is present at the
xError, wDiagCode and wAddDiagCode outputs for only one cycle.
strServer_IP STRING IP address of the Modbus server. The address should be specified as follows:
xxx.xxx.xxx.xxx
iPort INT Port of the Modbus server. If no address is specified here, the default address
502 is used.
xUDP_Mode BOOL FALSE: Modbus-TCP is used.
TRUE: Modbus-UDP is used.
tReconnectDelay TIME Delay between two IP connect executions. Needed by PLC. Default value =
500ms. In case of increased occurrence of wDiagCode = 16#C030 and
wAddDiagCode = 16#0010 during a FB restart increase this time.
tTimeout TIME Timeout for communication monitoring. The Modbus server must respond to a
request within the time specified here, otherwise an error is triggered. If no value
is specified here, the block operates with a timeout of 2 seconds.
Modbus_TCP_7 16/50

7.3 Output parameters


Name Type Description
xActive BOOL FALSE: Function block is not active.
TRUE: Function block is active. Do not start any further action
unless xActive is TRUE after activation!
xReady BOOL TCP:
TRUE: The Client is ready.
UDP:
TRUE: The Socket of the MB_TCP_Client is open.

TCP: The MB_TCP_Client is connected to a Server.


TCP: The MB_TCP_Client is connected to a Server.

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.4 Inout parameters


Name Type Description
udtTCP_ComData MB_TCP_UDT_COMMUNICATION Communication structure of the block family. The
blocks are connected together using this structure.

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.1 Function block call

8.2 Input parameters


Name Type Description
xActivate BOOL Rising edge: Activates the function block.
FALSE: Deactivates the function block.
xAcknowledge BOOL Rising edge: Modbus timeouts and IP-errors are acknowledged. The function
block is not reinitialized.
iMT_ID INT Value in the range from 1 to 10. Represents the connection between the blocks
and may only be assigned once per MB_TCP_Client. This parameter should be
static.

tUpdateTime TIME Waiting time in ms between two read tasks.


uiUnitIdentifier UINT Parameter for addressing of a Modbus RTU device, which is connected via a
gateway
wStartRegister WORD Address of the first bit / output / register to be read.
uiQuantityOfCoils UINT Number of bits to be read. Valid value range: 1 - 2000.
Modbus_TCP_7 19/50

8.3 Output parameters


Name Type Description
xActive BOOL FALSE: Function block is not active.
TRUE: Function block is active. Do not start any further action
unless xActive is TRUE after activation!
xNDR BOOL TRUE for one cycle: New data has been received.
uiByteCount UINT Number of bytes received.
arrCoilStatus MB_TCP_ARR_B_1_260 Status of the outputs/internal bits. BYTE array with the limits 1 to
250. The first output corresponds to the first bit of the first byte.
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.

8.4 Inout parameters


Name Type Description
udtTCP_ComData MB_TCP_UDT_COMMUNICATION Communication structure of the block family. The
blocks are connected together using this structure.

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.1 Function block call

9.2 Input parameters


Name Type Description
xActivate BOOL Rising edge: Activates the function block.
FALSE: Deactivates the function block.
xAcknowledge BOOL Rising edge: Modbus timeouts and IP-errors are acknowledged. The function
block is not reinitialized.
iMT_ID INT Value in the range from 1 to 10. Represents the connection between the blocks
and may only be assigned once per MB_TCP_Client. This parameter should be
static.
tUpdateTime TIME Waiting time in ms between two read tasks.
uiUnitIdentifier UINT Parameter for addressing of a Modbus RTU device, which is connected via a
gateway
wStartRegister WORD Address of the first bit / output / register to be read.
uiQuantityOfInputs UINT Quantity of inputs to read. Valid value range: 1 - 2000.
Modbus_TCP_7 21/50

9.3 Output parameters


Name Type Description
xActive BOOL FALSE: Function block is not active.
TRUE: Function block is active. Do not start any further action
unless xActive is TRUE after activation!
xNDR BOOL TRUE for one cycle: New data has been received.
uiByteCount UINT Number of bytes received.
arrInputStatus MB_TCP_ARR_B_1_250 Status of the inputs. BYTE array with the limits 1 to 250. Each
input is represented by a bit, the first input corresponds to the
first bit of the first byte.
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.

9.4 Inout parameters


Name Type Description
udtTCP_ComData MB_TCP_UDT_COMMUNICATION Communication structure of the block family. The
blocks are connected together using this structure.

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.1 Function block call

10.2 Input parameters


Name Type Description
xActivate BOOL Rising edge: Activates the function block.
FALSE: Deactivates the function block.
xAcknowledge BOOL Rising edge: Modbus timeouts and IP-errors are acknowledged. The function
block is not reinitialized.
iMT_ID INT Value in the range from 1 to 10. Represents the connection between the
blocks and may only be assigned once per MB_TCP_Client. This parameter
should be static.
tUpdateTime TIME Waiting time in ms between two read tasks.
uiUnitIdentifier UINT Parameter for addressing of a Modbus RTU device, which is connected via a
gateway
wStartRegister WORD Address of the first bit / output / register to be read.
uiQuantityOfRegisters UINT Number of registers to be read. Valid value range: 1 - 125.
Modbus_TCP_7 23/50

10.3 Output parameters


Name Type Description
xActive BOOL FALSE: Function block is not active.
TRUE: Function block is active. Do not start any further action
unless xActive is TRUE after activation!
xNDR BOOL TRUE for one cycle: New data has been received.
uiByteCount UINT Number of bytes received.
arrRegisterValue MB_TCP_ARR_W_1_125 Values read from the registers. WORD array with the limits 1 to
125.
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.

10.4 Inout parameters


Name Type Description
udtTCP_ComData MB_TCP_UDT_COMMUNICATION Communication structure of the block family. The
blocks are connected together using this structure.

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.1 Function block call

11.2 Input parameters


Name Type Description
xActivate BOOL Rising edge: Activates the function block.
FALSE: Deactivates the function block.
xAcknowledge BOOL Rising edge: Modbus timeouts and IP-errors are acknowledged. The function
block is not reinitialized.
iMT_ID INT Value in the range from 1 to 10. Represents the connection between the
blocks and may only be assigned once per MB_TCP_Client. This parameter
should be static.
tUpdateTime TIME Waiting time in ms between two read tasks.
uiUnitIdentifier UINT Parameter for addressing of a Modbus RTU device, which is connected via a
gateway
wStartRegister WORD Address of the first bit / output / register to be read.
uiQuantityOfRegisters UINT Number of registers to be read. Valid value range: 1 - 125.
Modbus_TCP_7 25/50

11.3 Output parameters


Name Type Description
xActive BOOL FALSE: Function block is not active.
TRUE: Function block is active. Do not start any further action
unless xActive is TRUE after activation!
xNDR BOOL TRUE for one cycle: New data has been received.
uiByteCount UINT Number of bytes received.
arrInputRegisters MB_TCP_ARR_W_1_125 Values read from the registers. WORD array with the limits 1
to 125.
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.

11.4 Inout parameters


Name Type Description
udtTCP_ComData MB_TCP_UDT_COMMUNICATION Communication structure of the block family. The
blocks are connected together using this structure.

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.1 Function block call

12.2 Input parameters


Name Type Description
xActivate BOOL Rising edge: Activates the function block.
FALSE: Deactivates the function block.
xAcknowledge BOOL Rising edge: Modbus timeouts and IP-errors are acknowledged. The function
block is not reinitialized.
iMT_ID INT Value in the range from 1 to 10. Represents the connection between the blocks
and may only be assigned once per MB_TCP_Client. This parameter should be
static.
tUpdateTime TIME Waiting time in ms between two read tasks.
uiUnitIdentifier UINT Parameter for addressing of a Modbus RTU device, which is connected via a
gateway
wOutputAddress WORD Address of the bit/output to be written. During addressing, the address of the first
digital output is zero.
wOutputValue WORD Value to be written. Only the following two values are permitted:
TRUE: 16#FF00
FALSE: 16#0000
Modbus_TCP_7 27/50

12.3 Output parameters


Name Type Description
xActive BOOL FALSE: Function block is not active.
TRUE: Function block is active. Do not start any further action unless xActive is
TRUE after activation!
xDone BOOL TRUE: The request was sent and the response was successfully received from the
communication partner. The parameter is TRUE for only one cycle.
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.

12.4 Inout parameters


Name Type Description
udtTCP_ComData MB_TCP_UDT_COMMUNICATION Communication structure of the block family. The
blocks are connected together using this structure.

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

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 28/50

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.1 Function block call

13.2 Input parameters


Name Type Description
xActivate BOOL Rising edge: Activates the function block.
FALSE: Deactivates the function block.
xAcknowledge BOOL Rising edge: Modbus timeouts and IP-errors are acknowledged. The function
block is not reinitialized.
iMT_ID INT Value in the range from 1 to 10. Represents the connection between the blocks
and may only be assigned once per MB_TCP_Client. This parameter should be
static.
tUpdateTime TIME Waiting time in ms between two read tasks.
uiUnitIdentifier UINT Parameter for addressing of a Modbus RTU device, which is connected via a
gateway
wRegisterAddress WORD Address of the register to be written.
wRegisterValue WORD Value to be written to the register.
Modbus_TCP_7 29/50

13.3 Output parameters


Name Type Description
xActive BOOL FALSE: Function block is not active.
TRUE: Function block is active. Do not start any further action unless xActive is
TRUE after activation!
xDone BOOL TRUE: The request was sent and the response was successfully received from the
communication partner. The parameter is TRUE for only one cycle.
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.

13.4 Inout parameters


Name Type Description
udtTCP_ComData MB_TCP_UDT_COMMUNICATION Communication structure of the block family. The
blocks are connected together using this structure.

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

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 30/50

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.1 Function block call

14.2 Input parameters


Name Type Description
xActivate BOOL Rising edge: Activates the function block.
FALSE: Deactivates the function block.
xAcknowledge BOOL Rising edge: Modbus timeouts and IP-errors are
acknowledged. The function block is not reinitialized.
iMT_ID INT Value in the range from 1 to 10. Represents the connection
between the blocks and may only be assigned once per
MB_TCP_Client. This parameter should be static.
tUpdateTime TIME Waiting time in ms between two read tasks.
uiUnitIdentifier UINT Parameter for addressing of a Modbus RTU device, which is
connected via a gateway
wStartRegister WORD Address of the first bit / output / register to be read.
uiQuantityOfOutputs UINT Number of outputs to be written. Valid value range: 1 - 1968.
arrOutputsValue MB_TCP_ARR_B_1_250 Values to be written to the registers. BYTE array with the
limits 1 to 250.
Modbus_TCP_7 31/50

14.3 Output parameters


Name Type Description
xActive BOOL FALSE: Function block is not active.
TRUE: Function block is active. Do not start any further action unless xActive is
TRUE after activation!
xDone BOOL TRUE: The request was sent and the response was successfully received from the
communication partner. The parameter is TRUE for only one cycle.
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.

14.4 Inout parameters


Name Type Description
udtTCP_ComData MB_TCP_UDT_COMMUNICATION Communication structure of the block family. The
blocks are connected together using this structure.

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

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 32/50

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.1 Function block call

15.2 Input parameters


Name Type Description
xActivate BOOL Rising edge: Activates the function block.
FALSE: Deactivates the function block.
xAcknowledge BOOL Rising edge: Modbus timeouts and IP-errors are
acknowledged. The function block is not reinitialized.
iMT_ID INT Value in the range from 1 to 10. Represents the
connection between the blocks and may only be assigned
once per MB_TCP_Client. This parameter should be
static.
tUpdateTime TIME Waiting time in ms between two read tasks.
uiUnitIdentifier UINT Parameter for addressing of a Modbus RTU device, which
is connected via a gateway
wStartingAddress WORD Address of the first register to be written.
uiQuantityOfRegisters UINT Number of registers to be written. Valid value range: 1 -
123.
arrRegisterValue MB_TCP_ARR_W_1_125 Values to be written to the registers. WORD array with
the limits 1 to 125.
Modbus_TCP_7 33/50

15.3 Output parameters


Name Type Description
xActive BOOL FALSE: Function block is not active.
TRUE: Function block is active. Do not start any further action unless xActive is
TRUE after activation!
xDone BOOL TRUE: The request was sent and the response was successfully received from the
communication partner. The parameter is TRUE for only one cycle.
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.

15.4 Inout parameters


Name Type Description
udtTCP_ComData MB_TCP_UDT_COMMUNICATION Communication structure of the block family. The
blocks are connected together using this structure.

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#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 34/50

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.1 Function block call

16.2 Input parameters


Name Type Description
xActivate BOOL Rising edge: Activates the function block.
FALSE: Deactivates the function block.
xAcknowledge BOOL Rising edge: Modbus timeouts and IP-errors are
acknowledged. The function block is not reinitialized.
iMT_ID INT Value in the range from 1 to 10. Represents the
connection between the blocks and may only be
assigned once per MB_TCP_Client. This parameter
should be static.
tUpdateTime TIME Waiting time in ms between two read tasks.
uiUnitIdentifier UINT Parameter for addressing of a Modbus RTU device,
which is connected via a gateway
wReadStartingAddress WORD Address of the first register to be read.
uiQuantityToRead UINT Number of registers to be read. Valid value range: 1 -
125.
wWriteStartingAddress WORD Address of the first register to be written.
Modbus_TCP_7 35/50

uiQuantityToWrite UINT Number of registers to be written. Valid value range: 1 -


121.
arrWriteRegistersValue MB_TCP_ARR_W_1_125 Values to be written to the register. WORD array with
the limits 1 to 125.

16.3 Output parameters


Name Type Description
xActive BOOL FALSE: Function block is not active.
TRUE: Function block is active. Do not start any further
action unless xActive is TRUE after activation!
xDone BOOL TRUE: The request was sent and the response was
successfully received from the communication partner.
The parameter is TRUE for only one cycle.
uiByteCount UINT Number of bytes received.
arrReadRegistersValue MB_TCP_ARR_W_1_125 Values read from the registers. WORD array with the
limits 1 to 125.
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.

16.4 Inout parameters


Name Type Description
udtTCP_ComData MB_TCP_UDT_COMMUNICATION Communication structure of the block family. The
blocks are connected together using this structure.

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

16#000B Gateway target device failed to respond


Modbus_TCP_7 37/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.1 Function block call

17.2 Input parameters


None

17.3 Output parameters


Name Type Description
strDiagCode STRING If there is an error, the variable shows the description for the current wDiagCode
in English.
strAddDiagCode STRING If there is an error, the variable shows the description for the current
wAddDiagCode in English.

17.4 Inout parameters


Name Type Description
udtTCP_ComData MB_TCP_UDT_COMMUNICATION Communication structure of the block family. The
blocks are connected together using this structure.

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

This example is packed in the zipped Examples folder of the library.


It describes the communication between a Modbus server and a Modbus client.

18.1 System

18.2 Modbus server example


18.2.1 Bus structure
For this example, the following hardware is used:

ILC 191 ETH 2TX (2700976)

18.2.1.1 Example machine

To start the example set the xStart input of the Example function block to TRUE.
Modbus_TCP_7 39/50

18.2.1.2 State machine


CASE iState OF
0: (* Init TCP_Server for example and wait for xStart *)
udtExample.udtMB_TCP_Server.xActivate := FALSE;
udtExample.udtMB_TCP_Server.xAcknowledge := FALSE;
udtExample.udtMB_TCP_Server.xAutoAck := FALSE;
udtExample.udtMB_TCP_Server.xUDP_Mode := FALSE;
udtExample.udtMB_TCP_Server.iTCP_Port := 502;
udtExample.udtMB_TCP_Server.strPartner_IP := '';
udtExample.udtMB_TCP_Server.tTimeout := TIME#10s;
udtExample.udtMB_TCP_Server.uiOffsetInputRegister := UINT#3000;
udtExample.udtMB_TCP_Server.uiOffsetHoldingRegister := UINT#2000;
udtExample.udtMB_TCP_Server.uiOffsetInputs := UINT#1000;
udtExample.udtMB_TCP_Server.uiOffsetCoils := UINT#0;
FOR iIndex := 0 TO 7167 DO
udtExample.udtMB_TCP_Server.arrModbusData[iIndex] := INT_TO_WORD(iIndex);
END_FOR;
udtExample.udtMB_TCP_Server.arrModbusData[0] := WORD#16#FEDC;
IF xStart = TRUE THEN
udtExample.iState := 100;
END_IF;
100: (* Activate MB_TCP_Client *)
udtExample.udtMB_TCP_Server.xActivate := TRUE;
IF udtExample.udtMB_TCP_Server.xError = TRUE THEN
udtExample.iState := 9000;
ELSIF udtExample.udtMB_TCP_Server.xActive = TRUE THEN
udtExample.iState := 200;
END_IF;
200: (* Wait for deactivation *)
IF udtExample.udtMB_TCP_Server.xError = TRUE THEN
udtExample.iState := 9000;
ELSIF xStart = FALSE THEN
udtExample.udtMB_TCP_Server.xActivate := FALSE;
udtExample.iState := 32000;
END_IF;
9000: (* Error handling *)
(* Implement your error handling here *)
(* In this example: wait for xStart = FALSE and reset *)
IF xStart = FALSE THEN
udtExample.udtMB_TCP_Server.xActivate := FALSE;
udtExample.iState := 32000;
END_IF;
32000: (* Deactivate TCP_Server *)
IF xStart = FALSE THEN
udtExample.udtMB_TCP_Server.xActivate := FALSE;
END_IF;
IF udtExample.udtMB_TCP_Server.xActive = FALSE THEN
iState := 0;
END_IF;
END_CASE;
iState := udtExample.iState;
Modbus_TCP_7 40/50

18.3 Modbus client example


18.3.1 Bus structure
For this example, the following hardware is used:

AXC 1050 (2700988)

18.3.2 Example description


The Modbus client as well as the FC blocks are connected with each other via a structure.

18.3.2.1 Example machine

To start the example set the xStart input of the Example function block to TRUE.
Modbus_TCP_7 41/50

18.3.2.2 State machine


CASE udtExample.iState OF
0: (* Wait for start of example program *)
IF xStart = TRUE THEN
udtExample.iState := 10;
END_IF;
10: (* Initialization of client and FC3 function block *)
(* Client *)
udtExample.udtMB_TCP_Client.xActivate := FALSE;
udtExample.udtMB_TCP_Client.xAutoAck := FALSE;
udtExample.udtMB_TCP_Client.strServer_IP := '192.168.1.19';
udtExample.udtMB_TCP_Client.iPort := 502;
udtExample.udtMB_TCP_Client.xUDP_Mode := FALSE;
udtExample.udtMB_TCP_Client.tTimeout := TIME#10s;
(* FC3 function block *)
udtExample.udtMB_TCP_FC3.xActivate := FALSE;
udtExample.udtMB_TCP_FC3.iMT_ID := 1;
udtExample.iState := 100;
100: (* Start client function block first and wait for xActive *)
udtExample.udtMB_TCP_Client.xActivate := TRUE;
IF udtExample.udtMB_TCP_Client.xError = TRUE THEN
udtExample.iState := 9000;
ELSIF udtExample.udtMB_TCP_Client.xReady = TRUE THEN
udtExample.iState := 1000;
END_IF;
1000: (* Read min. number of registers *)
(* Now activate FC3 function block and set inputs *)
udtExample.udtMB_TCP_FC3.xActivate := TRUE;
udtExample.udtMB_TCP_FC3.uiQuantityOfRegisters := UINT#1;
udtExample.udtMB_TCP_FC3.wStartRegister := WORD#0;
(* Wait for xActive of FC3 function block *)
IF udtExample.udtMB_TCP_FC3.xError = TRUE THEN
udtExample.iState := 9000;
ELSIF udtExample.udtMB_TCP_FC3.xActive = TRUE THEN
udtExample.iState := 1010;
END_IF;
1010: (* Check values *)
(* Your Modbus server has to deliver these values! *)
IF udtExample.udtMB_TCP_FC3.xError = TRUE THEN
udtExample.iState := 9000;
ELSIF
udtExample.udtMB_TCP_FC3.xNDR = TRUE AND
udtExample.udtMB_TCP_FC3.arrRegisterValue[1] = WORD#16#07D0
THEN
udtExample.iState := 2000;
END_IF;
2000: (* Read max. number of registers *)
(* Deactivation of FC3 function block before read again *)
udtExample.udtMB_TCP_FC3.xActivate := FALSE;
(* Wait for xActive = FALSE after deactivation *)
IF udtExample.udtMB_TCP_FC3.xError = TRUE THEN
udtExample.iState := 9000;
ELSIF udtExample.udtMB_TCP_FC3.xActive = FALSE THEN
udtExample.iState := 2010;
END_IF;
Modbus_TCP_7 42/50

2010: (* Activation of FC3 function block and set inputs *)


udtExample.udtMB_TCP_FC3.xActivate := TRUE;
udtExample.udtMB_TCP_FC3.uiQuantityOfRegisters := UINT#125;
udtExample.udtMB_TCP_FC3.wStartRegister := WORD#200;
(* Wait for xActive = TRUE *)
IF udtExample.udtMB_TCP_FC3.xError = TRUE THEN
udtExample.iState := 9000;
ELSIF udtExample.udtMB_TCP_FC3.xActive = TRUE THEN
udtExample.iState := 2020;
END_IF;
2020: (* Check values *)
(* Modbus server has to deliver these values! *)
IF udtExample.udtMB_TCP_FC3.xError = TRUE THEN
udtExample.iState := 9000;
ELSIF
udtExample.udtMB_TCP_FC3.xNDR = FALSE AND
udtExample.udtMB_TCP_FC3.uiByteCount = UINT#250
THEN
udtExample.iState := 32000;
END_IF;
9000: (* Error handling *)
(* Implement your error handling here *)
(* In this example: wait for xStart = FALSE and reset *)
IF xStart = FALSE THEN
udtExample.udtMB_TCP_Client.xActivate := FALSE;
udtExample.udtMB_TCP_FC3.xActivate := FALSE;
udtExample.iState := 32000;
END_IF;
32000: (* Finish *)
(* Deactivate client function block *)
IF xStart = FALSE THEN
udtExample.udtMB_TCP_Client.xActivate := FALSE;
udtExample.udtMB_TCP_FC3.xActivate := FALSE;
END_IF;
IF
udtExample.udtMB_TCP_FC3.xActive = FALSE AND
udtExample.udtMB_TCP_Client.xActive = FALSE AND
udtExample.udtMB_TCP_Client.xReady = FALSE
THEN
udtExample.iState := 0;
END_IF;
END_CASE;
iState := udtExample.iState;
Modbus_TCP_7 43/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.

19.1.1 Practical runtime tests


The time starts with the sending of a request by the Modbus client and ends after receiving the response from the
slave with the Done / NDR at the FC function block. The specified times are average times for direct connection of
two network ports with a short network cable over several measurements. The values can differ considerably with
heavy network load or other network infrastructure.

19.1.1.1 Task cycle time 4 ms

Job / Modbus Mode TCP UDP


FC3 read 1 register 42 ms 40 ms
FC3 read 125 registers 42 ms 41 ms
FC16 write 1 register 41 ms 42 ms
FC16 write 123 registers 41 ms 42 ms

19.1.1.2 Task cycle time 8 ms

Job / Modbus Mode TCP UDP


FC3 read 1 register 63 ms 63 ms
FC3 read 125 registers 64 ms 63 ms
FC16 write 1 register 64 ms 65 ms
FC16 write 123 registers 62 ms 64 ms
Modbus_TCP_7 44/50

19.2 Call Modbus data with associated function codes


Modbus_TCP_7 45/50

19.3 IP_CONNECT, IP_URCV, IP_USEND


Diagnosis of firmware function blocks

Error Description Status only for


code
16#0001 Creation of at least one task was unsuccessful.
16#0002 Initialization of the socket interface unsuccessful (Windows NT only).
16#0003 Dynamic memory could not be reserved. IP_USEND, IP_URCV
16#0004 FB cannot be initialized because an error occurred during startup of IP_USEND, IP_URCV,
the asynchronous communication task. IP_TIME, IP_CONNECT
16#0010 Socket initialization unsuccessful.
16#0011 Error while transmitting a message.
16#0012 Error while receiving a message.
16#0013 Unknown service code in the telegram header.
16#0021 Invalid condition switch while establishing a link. IP_CONNECT
16#0030 There are no more free channels available. IP_CONNECT
16#0031 Connection has been terminated. IP_CONNECT
16#0033 General timeout, receiver is no longer answering or the transmitter has IP_USEND, IP_URCV
not finished transmitting.
16#0034 Request for connection was denied. IP_CONNECT
16#0035 Receiver has not confirmed transmission, receiver may be overloaded IP_USEND
(repeat transmission).
16#0040 Partner string is too long (max. 255 characters). IP_CONNECT, IP_TIME
16#0041 The specified IP address is not valid or could not be interpreted IP_CONNECT, IP_TIME
correctly.
16#0042 Invalid port number. IP_CONNECT
16#0044 Wrong digit in some part of the PARTNER string. IP_CONNECT
16#0045 Unknown parameter at input "PARTNER" (maybe upper/lower case or IP_CONNECT, IP_TIME
misspelling).
16#0046 Missing parameter, parameter string is incomplete. IP_CONNECT
16#0047 Missing port while using a serve (/PASSIVE). IP_CONNECT
16#0048 Invalid type. Cannot find any of the allowed connection types. IP_CONNECT
16#0050 Attempt to transmit using invalid link (transmitter or receiver). IP_USEND
16#0053 All available connections are used.
16#0061 Neg. confirmation by receiver. An incorrect sequence number has IP_USEND, IP_URCV
been used.
16#0062 Data types of transmitter and receiver do not match. IP_USEND, IP_URCV
16#0063 Receiver is not yet ready to receive (possible cause: Receiver is IP_USEND
disabled or is currently in transfer mode (NDR=TRUE)).
16#0064 No receiver block with the matching R_ID was found. IP_USEND
16#0065 Another block instance already uses this connection.
16#0070 Partner control system was not configured as the time server.
Modbus_TCP_7 46/50

19.4 Data types


TYPE
(* Byte arrays *)
MB_TCP_ARR_B_1_250 : ARRAY [1..260] OF BYTE;
(* Word arrays *)
MB_TCP_ARR_W_1_125 : ARRAY [1..125] OF WORD;
MB_TCP_ARR_W_0_7167 : ARRAY [0..7167] OF WORD;
MB_TCP_UDT_WINSOCK : STRUCT
DataCnt : WORD;
Transaction_ID : WORD;
Protocol_ID : WORD;
Length : WORD;
arrData : MB_TCP_ARR_B_1_250;
END_STRUCT;
MB_TCP_UDT_READ_WRITE : STRUCT
Write : BOOL; (* FALSE = Read, TRUE = Write *)
xActive : BOOL; (* TRUE: Reading data *)
FC : INT; (* Modbus function code *)
uiUnitIdentifier : BYTE; (* Unit identifier *)
(* Read *)
ReadStartAddress : WORD; (* Address of the first register *)
QuantityToRead : WORD; (* Number of registers *)
arrReadBuffer : MB_TCP_ARR_B_1_250; (* Read buffer *)
ByteCount : BYTE; (* Number of received bytes *)
xNDR : BOOL; (* TRUE (for one cycle): New data available *)
(* Write *)
WriteStartAddress : WORD; (* Address of first register *)
QuantityToWrite : WORD; (* Number of registers *)
arrWriteBuffer : MB_TCP_ARR_B_1_250; (* Write buffer *)
xDone : BOOL; (* TRUE (for one cycle): Write is finished *)
(* Diagnosis *)
xError : BOOL; (* TRUE: Error *)
wDiagCode : WORD; (* Diag code *)
wAddDiagCode : WORD; (* Additional diag code *)
(* Internal *)
StartAction : BOOL; (* Used by scheduler -> TRUE = Read data *)
tUpdateTime : Time; (* Time between two read operations in ms *)
END_STRUCT;
MB_TCP_ARR_READ_WRITE : ARRAY [1..10] OF MB_TCP_UDT_READ_WRITE;
MB_TCP_UDT_COMMUNICATION : STRUCT
ClientReady : BOOL;
xError : BOOL;
wDiagCode : WORD;
wAddDiagCode : WORD;
ReadWriteGroups : MB_TCP_ARR_READ_WRITE;
END_STRUCT;
MB_TCP_UDT_MBS_REQUEST : STRUCT
DATA_CNT: WORD; (* Not part of Modbus protocol *)
Transaction_ID: WORD;
Protocol_ID: WORD;
Length: WORD;
Unit_ID: BYTE;
FC: BYTE;
Modbus_TCP_7 47/50

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:

Development system with


Name (e.g. PC Worx, PLCnext Engineer)
Version (e.g. PLCnext Engineer 2022.0.1 LTS)
Bus structure / plant including all articles with
Name
Order number
Firmware version
External components
Used libraries with
Name
Version (e.g. IOL_Basic_7)
Detailed problem description with
Diag codes of all function blocks in error state

You might also like