Modbus TCP with instructions MB_CLIENT and MB_SERVER
Modbus TCP with instructions MB_CLIENT and MB_SERVER
Modbus TCP with instructions MB_CLIENT and MB_SERVER
instructions
"MB_CLIENT" and
"MB_SERVER"
Siemens
S7-1500 CPU and S7-1200 CPU Industry
Online
https://support.industry.siemens.com/cs/ww/en/view/102020340 Support
This entry is from the Siemens Industry Online Support. The general terms of use
(http://www.siemens.com/terms_of_use) apply.
Security Siemens provides products and solutions with industrial security functions that
informa- support the secure operation of plants, systems, machines and networks.
tion In order to protect plants, systems, machines and networks against cyber
threats, it is necessary to implement – and continuously maintain – a holistic,
state-of-the-art industrial security concept. Siemens’ products and solutions only
form one element of such a concept.
Customer is responsible to prevent unauthorized access to its plants, systems,
machines and networks. Systems, machines and components should only be
connected to the enterprise network or the internet if and to the extent necessary
and with appropriate security measures (e.g. use of firewalls and network
segmentation) in place.
Additionally, Siemens’ guidance on appropriate security measures should be
taken into account. For more information about industrial security, please visit
http://www.siemens.com/industrialsecurity.
Siemens’ products and solutions undergo continuous development to make them
more secure. Siemens strongly recommends to apply product updates as soon
as available and to always use the latest product versions. Use of product
versions that are no longer supported, and failure to apply latest updates may
increase customer’s exposure to cyber threats.
To stay informed about product updates, subscribe to the Siemens Industrial
© Siemens AG 2019 All rights reserved
1 Introduction
Modbus/TCP communication between S7-1500 CPU and S7-1200 CPU is
presented.
The instructions "MB_CLIENT" and "MB_SERVER" are called and parameterized
in the user program of the S7-1200 CPU and the S7-1500 CPU.
The "MB_CLIENT" instruction communicates as Modbus/TCP client over the
PROFINET interface of the CPU. You do not need any additional hardware to use
the instruction. You use the "MB_CLIENT" instruction to establish a connection
between the client and the server, send requests and receive responses, and
control disconnection of the connection.
The "MB_SERVER" instruction communicates as Modbus/TCP server over the
PROFINET interface of the CPU. You do not need any additional hardware to use
the instruction. The "MB_SERVER" instruction processes connection requests of a
Modbus/TCP client, receives requests from Modbus functions and sends response
messages.
In this example, two Modbus functions are connections.
The S7-1500 CPU establishes the first connection as Modbus TCP client. The
S7-1200 CPU is Modbus TCP server.
The S7-1200 CPU establishes the second connection as Modbus TCP client. The
S7-1500 CPU is Modbus TCP server.
© Siemens AG 2019 All rights reserved
Figure 1-1
Modbus/TCP communication
Industrial Ethernet
The Modbus/TCP connections are established each via a Modbus instruction pair
(MB_CLIENT and MB_SERVER).
Inst
Modbus
Client
Holding
Register
Write
Modbus MB_
Client CLIENT
© Siemens AG 2019 All rights reserved
Main Modbus
[OB1] Data
Modbus MB_
Server SERVER
Holding
Register
Read
Inst
Modbus
Server
As Modbus TCP client, the S7-1500 CPU establishes the connection to the
Modbus TCP server (S7-1200 CPU) and sends the request to write the holding
register.
Table 2-1
ID Call of the Instance DB of the FB Description
"MB_CLIENT" "ModbusClient"
instruction
1 FB1 "ModbusClient" DB1 "InstModbusClient" Modbus function 16
(Write holding
register)
As Modbus TCP server the S7-1500 CPU processes the connection request of the
Modbus TCP client (S7-1200 CPU) and receives the request to read the holding
register.
Table 2-2
ID Call of the Instance DB of the FB Description
"MB_SERVER "ModbusServer"
instruction"
2 FB2 "ModbusServer" DB2 "InstModbusServer" Modbus function 3
(Read holding
register)
Use the following structure for connection description according to TCON_IP_v4 for
programmed connections at the "connectParamClient" input.
• Make sure that you specify connections only of the TCP type in the
TCON_IP_v4 structure.
• The connection must not use the following TCP port numbers: 20, 21, 25, 80,
© Siemens AG 2019 All rights reserved
At the "dataBuffer" parameter you specify the data area for storing the data that is
sent to the Modbus TCP server. The data that is written to the holding register of
the Modbus TCP server is stored in the data block DB3 "HoldingRegisterWrite".
Table 2-3
Variablenname Datentyp Hinweis
holdingRegister Array [0 .. 65535] of Word Entspricht dem
Gesamtadressbereich des
Halteregisters (0 bis 65535)
© Siemens AG 2019 All rights reserved
Abbildung 2-4
© Siemens AG 2019 All rights reserved
Use the following structure for connection description according to TCON_IP_v4 for
programmed connections at the "connectParamServer" input.
• Make sure that you specify connections only of the TCP type in the
TCON_IP_v4 structure.
• The connection must not use the following TCP port numbers: 20, 21, 25, 80,
102, 123, 5001, 34962, 34963 and 34964.
The figure below shows the structure of TCON_IP_v4 with the name
"connectParamServer". You specify this structure at the "connectParamServer"
parameter of the FB "ModbusServer". A description of the parameters of the
"TCON_IP_v4" structure is available in chapter 5.
Figure 2-5
© Siemens AG 2019 All rights reserved
The "dataBuffer" parameter is a pointer to a data buffer for storing the data that is
read from or written to the Modbus server. You can use a global data block or a
marker as memory area.
The data that is read is stored in the data block DB4 "HoldingRegisterRead".
Table 2-4
Tag name Data type Note
holdingRegister Array [0 .. 65535] of Word Corresponds to the total address
area of the holding register (0 to
65535)
The table below shows how the Modbus addresses are mapped to the holding
register for the Modbus function 3 (read Word).
Table 2-5
Modbus address dataBuffer
0 "holdingRegisterRead".holdingRegister[0]
1 "holdingRegisterRead".holdingRegister[1]
2 "holdingRegisterRead".holdingRegister[2]
3 "holdingRegisterRead".holdingRegister[3]
© Siemens AG 2019 All rights reserved
4 "holdingRegisterRead".holdingRegister[4]
5 "holdingRegisterRead".holdingRegister[5]
6 "holdingRegisterRead".holdingRegister[6]
7 "holdingRegisterRead".holdingRegister[7]
8 "holdingRegisterRead".holdingRegister[8]
9 "holdingRegisterRead".holdingRegister[9]
Inst
Modbus
Server
Holding
Register
Write
Modbus MB_
Server SERVER
© Siemens AG 2019 All rights reserved
Main Modbus
[OB1] Data
Modbus MB_
Client CLIENT
Holding
Register
Read
Inst
Modbus
Client
As Modbus TCP server, the S7-1200 CPU processes the connection request of the
Modbus TCP client (S7-1500 CPU) and receives the request to write the holding
register.
Table 3-1
ID Call of the Instance DB of the FB Description
"MB_SERVER" "ModbusServer"
instruction
1 FB2 "ModbusServer" DB2 "InstModbusServer" Modbus function 16
(Write holding
register)
As Modbus TCP client, the S7-1200 CPU establishes the connection to the
Modbus TCP server (S7-1500 CPU) and sends the request to read the holding
register.
Table 3-2
ID Call of the Instance DB of the FB Description
"MB_CLIENT" "ModbusClient"
instruction
2 FB1 "ModbusClient" DB1 "InstModbusClient" Modbus function 3
(Read holding
register)
Use the following structure for connection description according to TCON_IP_v4 for
programmed connections at the "connectParamServer" input.
• Make sure that you specify connections only of the TCP type in the
TCON_IP_v4 structure.
• The connection must not use the following TCP port numbers: 20, 21, 25, 80,
102, 123, 5001, 34962, 34963 and 34964.
The figure below shows the structure of TCON_IP_v4 with the name
"connectParamServer". You specify this structure at the "connectParamServer"
parameter of the FB "ModbusServer". A description of the parameters of the
"TCON_IP_v4" structure is available in chapter 5.
Figure 3-3
© Siemens AG 2019 All rights reserved
The "dataBuffer" parameter is a pointer to a data buffer for storing the data that is
read from or written to the Modbus server. You can use a global data block or a
marker as memory area.
The data is written to the data block DB3 "HoldingRegisterWrite" and stored.
Table 3-3
Tag name Data type Note
holdingRegister Array [0 .. 4999] of Word -
The table below shows how the Modbus addresses are mapped to the holding
register for the Modbus function 16 (write Word).
Table 3-4
Modbus address dataBuffer
0 "holdingRegisterWrite".holdingRegister[0]
1 "holdingRegisterWrite".holdingRegister[1]
2 "holdingRegisterWrite".holdingRegister[2]
3 "holdingRegisterWrite".holdingRegister[3]
4 "holdingRegisterWrite".holdingRegister[4]
5 "holdingRegisterWrite".holdingRegister[5]
6 "holdingRegisterWrite".holdingRegister[6]
7 "holdingRegisterWrite".holdingRegister[7]
8 "holdingRegisterWrite".holdingRegister[8]
9 "holdingRegisterWrite".holdingRegister[9]
Figure 3-4
Use the following structure for connection description according to TCON_IP_v4 for
programmed connections at the "connectParamClient" input.
Make sure that you specify connections only of the TCP type in the TCON_IP_v4
structure.
The connection must not use the following TCP port numbers: 20, 21, 25, 80, 102,
© Siemens AG 2019 All rights reserved
At the "dataBuffer" parameter you specify the data area for storing the data that is
received from the Modbus TCP server. The data that is read from the holding
register of the Modbus TCP server is stored in the data block DB4
"HoldingRegisterRead".
Table 3-5
Tag name Data type Note
holdingRegister Array [0 .. 4999] of Word -
© Siemens AG 2019 All rights reserved
Outputs
The following table shows the outputs of the function block FB1 "ModbusClient".
Table 4-2
Output Data Description
type
done Bool The bit at the "done" output is set to "1" as soon as
the last job has been executed without error.
busy Bool • 0: no Modbus request is being processed
• 1: Modbus request is being processed
error Bool • 0: no error
• 1: error occurred. The cause of the error is
displayed at the "status" output.
status Word Detailed Status information of the "MB_CLIENT"
instruction.
Outputs
The table below shows the outputs of the function block FB2 "ModbusServer".
Table 4-4
© Siemens AG 2019 All rights reserved
The table below shows the relationship between the inputs of the function block
© Siemens AG 2019 All rights reserved
address 0 to
65534.
5 Structure of "TCON_IP_v4"
Folgende Tabelle beschreibt die Parameter der Struktur "TCON_IP_v4".
Table 5-1
Byte Parameter Datentyp Beschreibung
0 to 1 InterfaceID HW_ANY Hardware ID of the local interface (value range: 0 to
65535).
The hardware ID is to be found in the device
configuration of the CPU. Mark the PROFINET
interface to display the properties of the PROFINET
interface in the inspector window. In the "General" tab
you navigate to "HW Identifier" to determine the
hardware ID.
2 to 3 ID CONN_OUC Reference to this connection (value range: 1 to 4095).
The parameter uniquely identifies a connection in the
CPU. Each single instance of the "MB_CLIENT" and
"MB_SERVER" instructions must use a unique ID.
4 ConnectionType BYTE Connection type
Select 11 (decimal) for TCP. Other connection types
are not permissible.
5 ActiveEstablished BOOL ID for how the connection is established.
True: active connection establishment
False: passive connection establishment
© Siemens AG 2019 All rights reserved