s7-1500 Modbusrtu Doku v1d1 en
s7-1500 Modbusrtu Doku v1d1 en
Caution:
The functions and solutions described in this entry are mainly limited to the
realization of the automation task. In addition, please note that suitable security
measures in compliance with the applicable Industrial Security standards must be
taken, if your system is interconnected with other parts of the plant, the companys
network or the Internet. Further information can be found under the Item-ID
50203404.
Copyright
http://support.automation.siemens.com/WW/view/en/50203404
SIMATIC
Master Slave Communication with
Modbus RTU
Task
Solution
Description of the
Modbus RTU Protocol
4
5
1.1
Operation of
the OB Pull
or Plug of
modules
(OB83) and
the OB Rack
or station
failure (OB86)
Background OB83
Copyright
If one of the CM
PtP-modules of the
application example
get pulled/pluged,
the OB83 provides
indirectly another
call of the FB
3
Modbus_Comm_Lo
ad for the
The Application Examples are not binding and do not claim to be complete
regarding the circuits shown, equipping and any eventuality. The application
examples do not represent customer-specific solutions. You are responsible for
ensuring that the described products are used correctly. These Application
Examples do not relieve you of your responsibility to use safe practices in
application, installation, operation and maintenance. When using these
Application Examples, you recognize that we cannot be made liable for any
damage/claims beyond the liability clause described. We reserve the right to
make changes to these Application Examples at any time and without prior
notice. If there are any deviations between the recommendations provided in this
application example and other Siemens publications e.g. catalogs the
contents of the other documents have priority.
We do not accept any liability for the information contained in this document.
Any claims against us based on whatever legal reason resulting from the use of
the examples, information, programs, engineering and performance data etc.,
described in this Application Example shall be excluded. Such an exclusion shall
not apply in the case of mandatory liability, e.g. under the German Product Liability
Act (Produkthaftungsgesetz), in case of intent, gross negligence, or injury of life,
body or health, guarantee for the quality of a product, fraudulent concealment of a
deficiency or breach of a condition which goes to the root of the contract
(wesentliche Vertragspflichten). The damages for a breach of a substantial
contractual obligation are, however, limited to the foreseeable damage, typical for
the type of contract, except in the event of intent or gross negligence or injury to
life, body or health. The above provisions do not imply a change of the burden of
proof to your detriment.
Copyright
Table of Contents
Table of Contents
Caution:......................................................................................................................... 2
Warranty and Liability ................................................................................................. 4
Table of Contents ......................................................................................................... 5
1
Task ..................................................................................................................... 6
Solution............................................................................................................... 7
Copyright
2.1
2.2
3.2
Overview............................................................................................. 15
4.2
4.2.1
4.2.2
4.2.3
4.2.4
4.2.5
4.3
4.3.1
4.3.2
4.3.3
4.4
DB Comm_Data ................................................................................. 29
5.2
5.3
5.4
6.2
6.3
Monitoring ........................................................................................... 40
7.2
Reading data from the Modbus slave to the Modbus master ............ 41
Literature .......................................................................................................... 42
Internet Links ..................................................................................................... 42
History............................................................................................................... 42
1 Task
Task
Introduction
This application shows you how to use the Modbus RTU protocol of the CM PtPs in
the SIMATIC S7-1500 and the distributed I/O system ET 200SP.
Overview of the automation task
The figure below provides an overview of the automation task.
Figure 1-1
S7-1500
Automation system
RS485
Communication interface
ModbusSlaves
Copyright
S7-1500
Controller:
configuration,
data storage
I/O
2 Solution
2.1 Solution overview
Solution
2.1
Solution overview
Schematic layout
Figure 2-1
Engineering-Station
STEP7 V12
Copyright
The following figure gives a schematic overview of the most important components
of the solution:
S7-1500 CPU:
Program for Modbus
master & Modbus slaves
CP PtP HF:
Modbus Master
S7-1500
CM PtP:
Modbus Slave 1
CM PtP:
Modbus Slave 2
ET 200SP
RS485
PROFINET
2 Solution
2.1 Solution overview
Core topics of this application
The following points are presented in this application:
Basics on Modbus RTU protocol
Configuring the hardware environment
Configuring the serial interfaces for the Modbus RTU protocol
Programming the data read in the Modbus RTU master
Programming the Modbus slave functionality in a SIMATIC S7 CPU
In the sample project the CM PtP RS422/485 HF alternately reads eight words of
data as Modbus master from the two slaves (CM PtPs of the ET 200SP).
The user program of the master and the slave is located in the S7-1500 CPU.
Programming the Modbus master with the help of the FB Master_Modbus (FB776)
and
an instance data block (e.g. DB Master_Modbus_DB (DB775))
For the output parameters the DB Output_Data (DB779) with the master
structure
Copyright
Programming the Modbus slave with the help of the FB Slave_Modbus (FB775)
and
an instance data block (e.g. DB Slave_Modbus_DB_1 (DB776))
the DB Comm_Data (DB778)
For the output parameters the DB Output_Data (DB779) with the SlaveX
structure
Advantages
The present application offers you the advantage of fast access to the Modbus
RTU subject in the SIMATIC S7-1500 environment.
You get encapsulated functions for programming either a Modbus slave or a
Modbus master.
2 Solution
2.2 Hardware and software components used
Validity
Software versions from TIA Portal V12
SIMATIC S7-1500 CPUs
CM PtP RS422/485 HF, CM PtP of the ET 200SP
Topics not covered by this application
This application does not contain
an introduction to the issue of SCL programming
basics on TIA Portal V12.
Basic knowledge of these topics is assumed.
2.2
Hardware components
Table 2-1
Copyright
Component
Note
Qty.
Ordering number
Note
6EP1332-4BA00
6ES7516-3AN00-0AB0
CM PtP RS422/485 HF
6ES7541-1AB00-0AB0
ET 200SP
6ES7155-6AU00-0BN0
CM PtP
6ES7137-6AA00-0BA0
BaseUnit
6ES7193-6BP00-0xA0
Server module
6ES7193-6PA00-0AA0
If you are using a different hardware than the one in the sample project, you have to
perform the respective changes in the hardware configuration!
2 Solution
2.2 Hardware and software components used
Standard software components
Table 2-2
Component
STEP 7 V12
(TIA Portal V12)
Qty.
1
Order number
Note
6ES78221AE02-0YA5
Note
68202723_ S71500_ModbusRTU_CODE_V1d0.zap12
68202723_S7-1500_ModbusRTU_
DOKU_V1d0_en.pdf
This document.
Copyright
For further documentation, for example, regarding the distributed I/O ET 200SP,
please note chapter 8 Literature.
10
3.1
Operation of ModbusRTU
Overview
Modbus RTU (Remote Terminal Unit) is a standard protocol for the serial
communication between master and slave.
Other protocols of the Modbus specification, such Modbus ASCII are not supported
by the serial SIMATIC S7-1500 CMs.
Master-Slave relationship
Modbus RTU uses a master/slave relationship in which the entire communication is
based on one single master device, whilst the slaves only respond to the requests
of the master. The master sends a request to a slave address and only the slave
with this slave address replies to the command.
Special case: when using the Modbus slave address 0, the CM PtP sends a
broadcast telegram to all slaves (without receiving a slave reply).
Communication sequence
Siemens AG 2013 All rights reserved
The communication with Modbus RTU always occurs according to the following
scheme:
1. The Modbus master sends a request to a Modbus slave in the network.
2. The slave replies with an answer telegram which includes the requested data
or which acknowledges the receipt of the request.
Copyright
3. If the slave cannot process the request of the master, the slave replies with an
error telegram.
The following table shows the structure of the telegram as an example, when data
is to be read from one or several holding registers of the Modbus slave (Modbus
Standard).
Table 3-1
Message
frame
Byte 0
Byte 1
Byte 2
Byte 3
Byte 4
Byte 5
Request
Slave
address
Function
code
Valid reply
Slave
address
Function
code
Length
Register data
Error message
Slave
address
0x83
Error
code
---
Number of registers
The function code indicates the slave what function it is to execute. Table 3-2 lists
the function codes that can be used with the CM PtPs:
Table 3-2
Function code
Function
01
02
03
11
Function
04
05
06
15
16
11
08
Performance data
Number of devices on the bus
Table 3-3
Modbus standard
Number of addresses
247
65535
For cable lengths over 50m a terminating resistor of approx. 330 Ohm has to be
welded to the receiver side in order to have uninterrupted data traffic.
Data length
Copyright
Table 3-4
Instruction type
Functions codes
Bit instruction
1, 2, 5, 15
1992 bits
Word instruction
3, 4, 6, 16
The values specified are valid for a CM PtP RS422/485 HF and all serial
communication processors of SIMATIC S7-1500.
12
3.2
Overview
The TIA Portal enables the configuration of a Modbus RTU communication. This
chapter shows you
what settings you have to make in the hardware configuration.
what properties the instructions for the Modbus RTU communication have.
Hardware configuration
In the hardware configuration for the CM used, you only set that it is to
communicate via the Modbus protocol. Table 3-5 shows the procedure:
Table 3-5
Copyright
No.
Procedure
1.
2.
Note
13
Copyright
Instruction
14
Description
Modbus_Comm_Load
Modbus_Master
Modbus_Slave
4.1
Overview
Functions
The S7 program realizes the following functions
Configuring the communication module for the communication with Modbus
RTU.
Communication of the S7 CPU as Modbus master for cyclic reading of eight
words each of two Modbus slaves.
Communication of the S7 CPU via the distributed I/O (ET 200SP with CM PtP
modules) as Modbus slave.
The communication program for the Master as well as that for the slaves is stored
in the SIMATIC S7-1500 CPU.
You can adjust the sample program to your requirements. Please note chapter 0.
Program overview
FB775
Master_Modbus
Setting communication
parameters
Modbus_Comm_Load
DB
Comm_Data
Modbus_Master
Copyright
Figure 4-1
Parameters
and data
cyclic
FB776
Slave_Modbus
PULL&PLUG
OB83
ensure call of Modbus_Comm_Load
after pull/plug-alarm
Modbus_Comm_Load
Modbus_Slave
15
16
Description
Main
OB100
Startup
OB83
FB775
Master_Modbus
FB776
Slave_Modbus
DB775
Master_Modbus_DB
DB776
Slave_Modbus_DB_1
Instance DB of FB Slave_Modbus_DB
DB777
Slave_Modbus_DB_2
Instance DB of FB Slave_Modbus_DB
DB778
Comm_Data
Includes
the parameters of the Modbus
communication connection.
an array of Data_for_Master data
type that provides the master with
the required data for the
communication with the slaves.
an array of Data_Slave data type
that provides the slaves with the
required data for communication.
Program call
OB1
In-house development
Symbolic name
Copyright
Element
DB779
Output_Data
FB640
Modbus_Comm_Load
Configuration of a communication
module for the communication with the
Modbus protocol.
FB641
Modbus_Master
Symbolic name
Description
FB642
Modbus_Slave
Other
system
blocks
For example:
Receive_Config
System blocks
Element
System blocks
Copyright
4.2
4.2.1
States
FB Master_Modbus fulfils the following tasks:
Initializing the communication parameters
Configuring the master communication module
Administering the communication jobs belonging to the Modbus slaves
The functionalities are realized in a simple sequence with the following states:
17
initializes
New start
INIT
Sending/receiving data
from/to a slave
configures
Copyright
18
Copyright
Parameter
Type
Note
PORT_MASTER
IN:
HW_SUBMODULE
No_Slaves
IN:
Int
INIT
IN:
Bool
ERROR
OUT
Bool
STATUS
OUT
DWORD
Description
High Word
Shows in which station address (in what slave) the status has occurred.
Low Word
19
4.2.2
"INIT" state
Overview
The INIT state is introduced in the first cycle by calling the FB Master_Modbus in
OB1. The INIT state is also introduced by a positive edge on the INIT input.
In this state the parameters required for the program sequence are initialized.
Description
Table 4-4
No.
1.
2.
3.
4.2.3
Note
"Config_Modbus" state
Overview
After the successful initialization of the parameters, the FB Master_Modbus goes
to the "Config_Modbus" state.
Copyright
4.
Step
20
1.
Copyright
2.
3.
Description
The following step table describes the program code:
Table 4-5
No
.
1.
Step
Presetting public data block
variables.
2.
3.
Note
Variable
Type
Description
ICHAR_GAP
Word
RETRIES
Word
EN_DIAG_ALARM
Word
MODE
USInt
LINE_PRE
USInt
21
Note
4.2.4
"datatransfer" state
Overview
After successful configuration the block of the communication module is in the
"datatransfer" state.
In this state the communication jobs are sent to the Modbus slaves and the
communication is administered.
Program code
Figure 4-5
Copyright
1.
2.
3.
4.
22
Note
1.
2.
3.
4.
Copyright
No.
23
4.2.5
UDT Data_for_Master
Overview
The UDT (User Defined Data Type) Data_for_Master includes all relevant
information for communication with a Modbus slave for the FB Master_Modbus.
Structure
Usage
The sample project includes an array made up of two UDTs in the DB
Comm_Data. An UDT includes parameters for one communication job each of the
Modbus master with one Modbus slave.
Parameters
MODE
DATA_ADDR
Copyright
Figure 4-6
DATA_LEN
specify the job of the master to the slave. Precise information about the Modbus
function code used, in dependence of the parameters, can be found in the help of
STEP 7 V12 for the Modbus_Master instruction.
The buffer area is used as storage of the data that is read by the slave.
If you communicate with other slaves or you want to read/write other data areas,
please observe chapter 0.
24
4.3
4.3.1
Parameter
Overview
The FB Slave_Modbus
initializes a CM (Communication Module)
configures the communication of the CM as Modbus slave.
Parameter of the FB Slave_Modbus
Figure 4-7 shows the call interface of FB Slave_Modbus. The parameters are
described in Figure 4-7.
Copyright
Figure 4-7
Table 4-7
Parameter
Type
Note
Data_Slave
IN:
Int
INIT
IN:
Bool
NDR
OUT
Bool
PR
OUT
Bool
DONE_LOAD
OUT
Bool
ERROR
OUT
Bool
STATUS
OUT
DWORD
25
4.3.2
Description
High Word
Low Word
Assumes the value of the status of the instruction in which the error
occurred.
If the station address (MB_ADDR) or the port is specified with 0 the value
16#FFFE is pending.
Block details
Overview
Program code
Figure 4-8
1.
2.
3.
Copyright
4.
26
Initialization
4.
Station address=0 |
PORT=0
Valid parameter
Configuration
communication
module
2.
Block
slave
1.
Configuration
completed
Copyright
Call
Modbus_Slave
3.
INIT=TRUE
INIT=TRUE
Initialization
Table 4-9
No.
Step
Note
1.
2.
3.
Call of Modbus_Slave.
Refer to the slave buffer
(Comm_Data.Slave[X].Slave_data).
4.
Initialization
For details of the program code, please look in the sample project.
27
4.3.3
UDT Data_Slave
Overview
The UDT Data_Slave includes the relevant information for the FB Slave_Modbus
for setting up the communication with a Modbus master.
At the Slave_Number input the FB Slave_Modbus is informed of what element of
the Slave array the block is to access in DB Comm_Data.
Structure
Usage
In the DB Comm_Data sample project a Slave array of two Data_Slave UDTs is
available that includes, among others, the parameters PORT and MB_ADDR for
the FB Slave_Modbus.
If you want to program other calls of the FB Slave_Modbus, you can attach the
array to other elements in DB Comm_Data. You then have to transfer the number
of the new array element to your FB call.
Copyright
Figure 4-10
28
4.4
DB Comm_Data
Overview
Data is stored in DB Comm_Data for the FBs Master_Modbus and
Slave_Modbus that they need for Modbus RTU communication.
Structure
Figure 4-11
Copyright
Usage
Table 4-10
Name
Data type
Usage
Note
Struct
Master_comm
array[1..2] of
Data_for_Master
Slave
array [1..2] of
Data_Slave
Param
PublicParam
29
4.5
Background OB83
The OB83 Pull or Plug of modules gets called as soon as any module of the S7station gets pulled or pluged.
For the use of the Modbus blocks it is necessary to call the
FB Modbus_Comm_Load instruction once again after pulling and plugging the
CM PtP.
Function OB83
The programmed OB83 of the application example requests, which modul got
pulled or pluged.
If one of the CM PtP-modules of the application example get pulled/pluged, the
OB83 provides indirectly another call of the FB Modbus_Comm_Load for the
pulled/pluged modul.
Copyright
Abbildung 4-12 network for the modbus master modul (HW-ID 259)
OB 86
The OB86 Rack or station failure gets called if there is a failure of the rack or
station.
The programmed behavior is identical to the OB83 with the difference that the HWidentifier of the head of the ET 200SP is concurrent.
30
Overview
If you want to make changes on the STEP 7-V12 project, this chapter provides you
support.
The following adjustment options are documented:
Changing of communication settings, such as, for example, the baud rate on
the Modbus master and on the two Modbus slaves
Changing the existing communication jobs
Adding other slaves to the program.
Adjusting the receive buffer size in order to send or receive data larger than
eight words.
5.1
Overview
Copyright
In DB Comm_Data (DB778) the data for the communication settings are stored.
You can change these parameters. If you have a Modbus slave with fixed
communication settings, you have to adjust the settings of your Modbus master to
these settings.
The FB Master_Modbus as well as the FB Slave_Modbus access these
parameters.
Make sure only to set parameters that are supported by your devices.
Procedure
Table 5-1
No.
Procedure
1.
2.
Note
31
5.2
Overview
The sample project includes two communication jobs due to which the Modbus
master alternately reads 8 words of data each from the two Modbus slaves.
The chapter describes how you change the parameters for the communication
jobs.
Procedure
Table 5-2
No.
Procedure
Open OB 100.
2.
Copyright
1.
Note
Name
MB_ADDR
MODE
DATA_ADDR
DATA_LEN
3.
Meaning
Note
5.3
The data that the master receives from the slave or sends to the slave are
located in the DB Comm_Data in the buffer of the respective communication job
(Master_comm array).
Overview
If you would like to communicate with more than the two slaves configured here,
you have to make changes in the sample project.
Description
The UDT Data_for_Master includes
information relevant for the FB Master_Modbus for the communication with a
Modbus slave.
information relevant for the FB Slave_Modbus for setting up the Modbus RTU
communication as slave.
32
Figure 5-1
Table 5-3
Copyright
Variable
Note
MB_ADDR
MODE
MODE=0 corresponds to
the reading of data
DATA_ADDR
DATA_LEN
Note
Function
Based on the parameters ERROR and STATUS the state of the communication
for the respective slave can be read out.
33
1.
2.
3.
Note
Procedure master
Table 5-5
No.
Procedure
1.
2.
3.
4.
Copyright
No.
Note
34
5.4
Overview
On request, the sample application will read with eight words from a slave.
If you would like to read or write larger data volumes, you have to make changes,
as described in chapter 5.2 and you also have to enlarge the buffers used.
Procedure
Table 5-6
Copyright
No.
Instruction
1.
2.
3.
4.
5.
Note
35
6.1
Overview
The figure below shows the hardware setup of the example.
Figure 6-1
Engineering station
STEP7 V12
S7-1500
CM PtP
ET 200SP
CM PtPs
Copyright
Interconnection
see table 6.2
24V
230V
RS485
PN/IE
The tables that follow describe the procedure for the hardware setup of the project.
Observe the rules for setting up a S7 station.
Hardware setup of the SIMATIC S7-1500 station
Table 6-1
No.
Procedure
1.
2.
3.
36
Note
Procedure
4.
5.
Note
2.
3.
Note
The instructions from the manual
http://support.automation.siemens.com/WW/view/en
/58649293 have to be observed!
Pin
Name
Meaning
12
T(A)/R(A)
Receive/send data
14
T(A)/R(A)
Receive/Send data
Copyright
1.
Procedure
4.
37
6.2
1.
2.
1.
2.
2.
3.
Copyright
Table 6-3
38
6.3
Copyright
No.
Procedure
1.
2.
3.
4.
Note
39
7.1
Monitoring
Overview
Once you have started operating the sample project, your CPU will cyclically
process the user program.
Data with a length of 8 words is read out by the slaves from the arrays
"Comm_Data".Slave[1].slave_data and "Comm_Data".Slave[2].slave_data.
The data read out from the master is stored in array
"Comm_Data".Master_comm[1].buffer or "Comm_Data".Master_comm[2].buffer.
In order to be able to better monitor the actions of the user program, the
Modbus_Overview monitoring table is available to you.
Table 7-1
No.
Variable
Note
Master_Modbus
Copyright
[].stat_save_comm
[].done_count_gen
[].err_count_gen
[].STATUS
[].INIT
[].step
[].number
[].stat_save
[].stat_save
10
[].err_count_gen
11
[].err_count_gen
13
[].MB_ADDR
14
[].STATUS
15
[].buffer[0]
16
[].MB_ADDR
17
[].STATUS
Comm_Data
12
40
Variable
Note
18
[].buffer[0]
19
[].INIT
20
[].Slave_data[0]
21
[].Slave_data[0]
7.2
Table 7-2
Copyright
No.
Procedure
1.
2.
3.
Note
Now you can see the actual values of the monitoring table.
When you have commissioned the application successfully,
the "done_count_gen" variable will be constantly
incremented.
4.
5.
41
8 Literature
Literature
Internet Links
The following list is by no means complete and only provides a selection of
appropriate sources.
Table 8-1
Title
\1\
Link to this
document
http://support.automation.siemens.com/WW/view/en/68202723
\2\
Siemens Industry
Online Support
http://support.automation.siemens.com
\3\
CM PtP
Configurations for
Point to Point
Connections
http://support.automation.siemens.com/WW/view/en/59057093
\4\
S7-1500
Communication
Module CM PtP
RS422/485 HF
http://support.automation.siemens.com/WW/view/en/59061372
\5\
ET 200SP
Communication
Module CM PtP
http://support.automation.siemens.com/WW/view/en/59061378
\6\
ET 200SP
Distributed I/O
system
ET 200SP
http://support.automation.siemens.com/WW/view/en/58649293
Copyright
Topic
History
Table 9-1
42
Version
Date
Modifications
V1.0
03/2013
First version
V1.1
12/2014