SIMATIC - S7-1200 Modbus TCP
SIMATIC - S7-1200 Modbus TCP
http://support.automation.siemens.com/WW/view/en/83130159
This entry is from the Siemens Industry Online Support. The general terms of use
(http://www.siemens.com/terms_of_use) apply.
Caution
The functions and solutions described in this article confine themselves to the
realization of the automation task predominantly. Please take into account
furthermore that corresponding protective measures have to be taken up in the
context of Industrial Security when connecting your equipment to other parts of the
plant, the enterprise network or the Internet. Further information can be found
under the Content-ID 50203404.
Copyright
http://support.automation.siemens.com/WW/view/en/50203404
Table of contents
Table of contents
1
Introduction ........................................................................................................ 4
Copyright
4.1
4.2
1 Introduction
Introduction
Modbus TCP communication between two S7-1200 CPUs is presented. The
instructions "MB_CLIENT" and "MB_SERVER" are called and parameterized in the
user program of the S7-1200 CPU.
The "MB_CLIENT" instruction communicates as Modbus TCP client over the
PROFINET connection of the S7-1200 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 the connection disconnection of the Modbus TCP server.
The "MB_SERVER" instruction communicates as Modbus TCP server over the
PROFINET connection of the S7-1200 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 2 Modbus functions are presented. For each Modbus function a
Modbus TCP connection is established over a Modbus block pair (MB_CLIENT
and MB_SERVER).
Copyright
Figure 1-1 shows an overview of the Modbus functions presented in this example
and the Modbus block pair assignment.
Figure 1-1
Read_
Holding
register
Modbus Function: 03
Read holding register
MB_
CLIENT
MB_
SERVER
Read_
Holding
register
MAIN
[OB 1]
MAIN
[OB 1]
Write_
Holding
Register
User program
S7-1200 CPU
(Modbus client)
MB_
CLIENT
Modbus Function: 16
Write to multiple
holding registers
System blocks
MB_
SERVER
System blocks
Write_
Holding
Register
User program
S7-1200 CPU
(Modbus server)
2.1
Instance DB of the
"MB_CLIENT" instruction
Description
FC1 "Read_HoldingRegister"
DB2 "MB_CLIENT_DB"
Read holding
register
FC4 "Write_HoldingRegister"
DB5 "MB_CLIENT_DB_3"
Write to holding
register
FC1 "Read_HoldingRegister"
The FC1 "Read_HoldingRegister" function calls the "MB_CLIENT" instruction
internally to establish the Modbus TCP connection with ID=1 and read the holding
register.
The communication request to read the holding register is controlled by the marker
M1.0 at the REQ input.
In this example the Modbus TCP connection with connection number=1 is
established to Port 502 of the Modbus TCP server. The Modbus TCP server has
the IP address 192.168.0.30.
5 data words are read out of the holding register. For this you set the input
parameters MB_MODE, MB_DATA_ADDR and MB_DATA_LEN as follows:
MB_MODE = 0
MB_DATA_ADDR = 40001
Copyright
MB_DATA_LEN = 5
Note
Figure 2-1 shows the call and parameters of the "MB_CLIENT" instruction in FC1.
Copyright
Figure 2-1
Note
Section 2.3 Input and Output Parameters of the "MB_CLIENT" Instruction gives
an overview and description of the input and output parameters of the
"MB_CLIENT" instruction.
Receive buffer
At parameter MB_DATA_PTR you specify the buffer for the data received from the
Modbus TCP server. The data read from the holding register is stored in DB1
RD_HoldingRegister starting with address 0.
Table 2-2
Variable name
Read_HoldingRegister
Data type
Array [0..4] of Word
Address in DB1
0.0
Error evaluation
If the "MB_CLIENT" instruction terminates with an error in FC1, the error code of
the STATUS parameter is stored in variable ID1_MB_CLIENT_STATUS_SAVE of
DB7 DATA for error evaluation.
Figure 2-2
2.2
FC4 "Write_HoldingRegister"
The FC4 "Write_HoldingRegister" function calls the "MB_CLIENT" instruction
internally to establish the Modbus TCP connection with ID=4 and write to the
holding register of the Modbus TCP server.
The communication request to write to the holding register is controlled by the
marker M1.3 at the REQ input.
Copyright
Note
Copyright
Figure 2-3 shows the call and parameters of the "MB_CLIENT" instruction in FC4.
Figure 2-3
Note
Section 2.3 Input and Output Parameters of the "MB_CLIENT" Instruction gives
an overview and description of the input and output parameters of the
"MB_CLIENT" instruction.
Send buffer
At parameter MB_DATA_PTR you specify the buffer for the data to be sent to the
Modbus TCP server. The data to be written to the holding register is stored in DB8
WR_HoldingRegister starting with address 0.
Table 2-3
Variable name
Data type
Write_HoldingRegister
Address in DB1
0.0
Error evaluation
If the "MB_CLIENT" instruction terminates with an error in FC4, the error code of
the STATUS parameter is stored in variable ID4_MB_CLIENT_STATUS_SAVE of
DB7 DATA for error evaluation.
Figure 2-4
2.3
Input parameters
The "MB_CLIENT" has the following input parameters.
Table 2-4
Data type
Description
REQ
BOOL
DISCONNECT
BOOL
CONNECT_ID
UINT
IP_OCTET_1
USINT
IP_OCTET_2
USINT
IP_OCTET_3
USINT
IP_OCTET_4
USINT
IP_PORT
UINT
Copyright
Input parameters
Input parameters
Data type
Description
MB_MODE
USINT
MB_DATA_ADDR
UDINT
MB_DATA_LEN
UINT
MB_DATA_PTR
VARIANT
Output parameters
The "MB_CLIENT" instruction has the following output parameters.
Table 2-5
Output parameters
Copyright
2.4
Data type
Description
DONE
Boolean
BUSY
Boolean
ERROR
Boolean
0: No error
1: Error occurred. The cause of the error is
displayed by the STATUS parameter.
STATUS
WORD
10
Table 2-6 shows the relationship between the MB_MODE parameter of the
Modbus function used in this example and the address area.
Table 2-6
Modbus function
03
16
Data length
Function and
data type
MB_DATA_ADDR
1 to 125
Read holding
register
1 to 125
WORDs per
call
40001 to 49999
2 to 123
Write to
multiple
holding
registers
2 to 123
WORDs per
call
40001 to 49999
Copyright
MB_MODE
11
3.1
Instance DB of the
"MB_SERVER"
instruction
Description
FC1 "Read_HoldingRegister"
DB2 "MB_SERVER_DB"
Read holding
register
FC4 "Write_HoldingRegister"
DB5 "MB_SERVER_DB_3"
Write to holding
register
FC1 "Read_HoldingRegister"
Copyright
Figure 3-1
Note
Section 3.3 Input and Output Parameters of the "MB_SERVER" Instruction gives
an overview and description of the input and output parameters of the
"MB_SERVER" instruction.
12
MB_HOLD_REG parameter
The MB_HOLD_REG parameter is a pointer to a data buffer for storing the data
that is read from or is to be written to the Modbus server. You can use a global
data block or a marker as memory area. The table below shows how the Modbus
addresses are mapped to the holding register for the Modbus function 3 (read
WORD).
Table 3-2
Modbus address
Absolute
address
Symbolic name
40001
DB1.DBW0
"HoldingRegister".Read_HoldingRegister[0]
40002
DB1.DBW2
"HoldingRegister".Read_HoldingRegister[1]
40003
DB1.DBW4
"HoldingRegister".Read_HoldingRegister[2]
40004
DB1.DBW6
"HoldingRegister".Read_HoldingRegister[3]
40005
DB1.DBW8
"HoldingRegister".Read_HoldingRegister[4]
Error evaluation
If the "MB_SERVER" instruction terminates with an error in FC1, the error code of
the STATUS parameter is stored in variable ID1_MB_SERVER_STATUS_SAVE
of DB7 DATA for error evaluation.
Copyright
Figure 3-2
3.2
FC4 "Write_HoldingRegister"
The FC4 "Write_HoldingRegister" function calls the "MB_SERVER" instruction
internally to process the connection request to write to the holding register. The
connection request is made over the Modbus TCP connection with ID=4 and Port
505.
13
Figure 3-3 shows the call and parameters of the "MB_SERVER" instruction in FC4.
Copyright
Figure 3-3
Note
Section 3.3 Input and Output Parameters of the "MB_SERVER" Instruction gives
an overview and description of the input and output parameters of the
"MB_SERVER" instruction.
MB_HOLD_REG parameter
The MB_HOLD_REG parameter is a pointer to a data buffer for storing the data
that is read from or is to be written to the Modbus server. You can use a global
data block or a marker as memory area. The table below shows how the Modbus
addresses are mapped to the holding register for the Modbus function 16 (write
multiple WORDs).
Table 3-3
Modbus address
Absolute
address
Symbolic name
40001
DB1.DBW10
"HoldingRegister".Write_HoldingRegister[0]
40002
DB1.DBW12
"HoldingRegister".Write_HoldingRegister[1]
40003
DB1.DBW14
"HoldingRegister".Write_HoldingRegister[2]
40004
DB1.DBW16
"HoldingRegister".Write_HoldingRegister[3]
40005
DB1.DBW18
"HoldingRegister".Write_HoldingRegister[4]
Error evaluation
If the "MB_SERVER" instruction terminates with an error in FC4, the error code of
the STATUS parameter is stored in variable ID4_MB_SERVER_STATUS_SAVE
of DB7 DATA for error evaluation.
14
Figure 3-4
3.3
Input parameters
Input parameters
Data type
Description
DISCONNECT
BOOL
CONNECT_ID
UINT
IP_PORT
UINT
MB_HOLD_REG
VARIANT
Copyright
15
Output parameters
The "MB_SERVER" instruction has the following output parameters.
Table 3-5
Output parameters
Data type
Description
NDR
BOOLEAN
DR
BOOLEAN
Data Read
0: No new data read.
1: Data read by the Modbus client
ERROR
WORD
Copyright
STATUS
16
4.1
Copyright
Figure 4-1
17
Read values from the holding register of the Modbus server and write values to the
holding register of the Modbus server
When the online connection to the CPU 1217C has been established successfully
you click the "Monitor All" button in the "Watchtable_CPU1217C" monitoring table.
The current values are displayed in the "Monitor value" column.
Figure 4-3
Copyright
Figure 4-2
18
1. Set the marker M1.0 "REQ_ID1" to the value "1" to execute the job to read
from the holding register. For this you enter the modify value "TRUE" for the
marker M1.0 "REQ_ID1".
2. Set the marker M1.3 "REQ_ID4" to the value "1" to execute the job to write to
the holding register. For this you enter the modify value "TRUE" for the marker
M1.3 "REQ_ID4".
3. In the CPU 1217C (Modbus client), the values to be written are stored in the
DB8 "WR_HoldingRegister". In the "Modify value" column you enter the values
that the Modbus client writes to the holding register of the Modbus server.
4. Click the "Modify all enabled values "once and immediately"" button to set the
entered modify values as the current values.
5. In the CPU 1217C the read values are stored in the
DB1 RD_HoldingRegister". The read values are displayed in the "Monitor
value" column.
Copyright
Figure 4-4
4.2
19
Copyright
Figure 4-5
When the online connection to the CPU 1211C has been established successfully
you click the "Monitor All" button in the "Watchtable_CPU1211C" monitoring table.
The current values are displayed in the "Monitor value" column.
20
Figure 4-7
2. Click the "Modify all enabled values "once and immediately"" button to set the
entered modify values as the current values.
3. In the CPU 1211C (Modbus server), all the written data is stored in the DB1
starting with address 10. The written values are displayed in the "Monitor
value" column.
Figure 4-8
Copyright
1. In the CPU 1211C (Modbus server), all the data to be read is stored in the
DB1starting with address 0. In the "Modify value" column you enter the values
thatthe Modbus client reads from the holding register of the Modbus server.
21