33
Library for Modbus RTU (LMRTU) STEP 7 (TIA Portal) V14, SIMATIC S7-1200/1500 https://support.industry.siemens.com/cs/ww/en/view/109747565 Siemens Industry Online Support

Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

Embed Size (px)

Citation preview

Page 1: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

Library for Modbus RTU (LMRTU)

STEP 7 (TIA Portal) V14, SIMATIC S7-1200/1500

https://support.industry.siemens.com/cs/ww/en/view/109747565

Siemens Industry Online Support

Page 2: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

Warranty and Liability

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

Warranty and Liability

Note 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. They are only intended to provide support for typical applications. You are responsible for ensuring that the described products are used correctly. These Application Examples do not relieve you of the 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 without prior notice. If there are any deviations between the recommendations provided in these Application Examples 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. Any form of duplication or distribution of these Application Examples or excerpts hereof is prohibited without the expressed consent of the Siemens AG.

Security informa-tion

Siemens provides products and solutions with industrial security functions that support the secure operation of plants, systems, machines and networks. 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 Security RSS Feed under http://www.siemens.com/industrialsecurity.

Page 3: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

Table of Contents

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 3

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

Table of Contents Warranty and Liability ................................................................................................. 2

1 Introduction ........................................................................................................ 4

1.1 General ................................................................................................. 4 1.2 Hardware and software requirements .................................................. 4

2 Working with the libraries ................................................................................. 5

3 Blocks of the Library ......................................................................................... 6

3.1 Overview............................................................................................... 6 3.2 Scanning the bus for accessible slaves ............................................... 7 3.2.1 Description ........................................................................................... 7 3.2.2 Mode of operation .............................................................................. 10 3.2.3 Integration into the user project .......................................................... 13 3.2.4 Operation ............................................................................................ 17 3.3 Redundant communication................................................................. 18 3.3.1 Description ......................................................................................... 18 3.3.2 Mode of operation .............................................................................. 21 3.3.3 Integration into the user project .......................................................... 23 3.3.4 Operation ............................................................................................ 27 3.4 Data types .......................................................................................... 28

4 Valuable Information ....................................................................................... 30

4.1 Fundamentals of the Modbus RTU .................................................... 30 4.2 Modbus RTU in TIA Portal ................................................................. 31

5 Appendix .......................................................................................................... 32

5.1 Service and support ........................................................................... 32 5.2 Links and literature ............................................................................. 33 5.3 Change documentation ...................................................................... 33

Page 4: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

1 Introduction

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 4

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

1 Introduction

1.1 General

The "LMRTU" block library contains blocks for the communication via Modbus RTU with SIMATIC S7-1200 and S7-1500.

It comprises the following functions:

Scanning the bus for accessible slaves

Redundant communication with two CMs

The finished functions are freely adjustable and can thus be used universally.

The library described here is version-numbered and is continually expanded.

1.2 Hardware and software requirements

Hardware

The "LMRTU" library can be used with all SIMATIC S7-1200 and S7-1500 CPUs and with one of the following CMs:

Table 1-1: Compatible CMs

Designation Article number Comment

CM 1241 RS422/485 6ES7241-1CH32-0XB0 V2.1 and higher

CM PtP RS422/485 HF 6ES7541-1AB00-0AB0 --

Software

This library is valid for STEP 7 Basic V14 SP1 and higher.

Page 5: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

2 Working with the libraries

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 5

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

2 Working with the libraries

All blocks in the "LMRTU" library are freely usable in connection with S7-1200 and S7-1500 controllers.

Most blocks are stored as type in the library. The blocks are therefore version-numbered and can exploit the following advantages:

Central update function of library elements

Versioning of library elements

Note Information on the general use of libraries can be found in the S7-1200/1500 program guide under the chapter "libraries".

https://support.industry.siemens.com/cs/ww/en/view/81318674

Note All blocks in the library have been created according to the programming style guide.

https://support.industry.siemens.com/cs/ww/en/view/81318674

Further information on libraries in the TIA portal:

How do you open libraries in STEP 7 (TIA Portal)? https://support.industry.siemens.com/cs/ww/en/view/37364723

Automation in less than 10 minutes TIA portal: Time Savers – Global Libraries https://support.industry.siemens.com/cs/ww/en/view/78529894

Which elements of STEP 7 (TIA Portal) can you store in a library as Type or as Master Copy? https://support.industry.siemens.com/cs/ww/en/view/109476862

How can you automatically open a global library upon starting up TIA portal from V13 or higher, and how can you use it, for example, as a company library? https://support.industry.siemens.com/cs/ww/en/view/100451450

Page 6: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

3 Blocks of the Library

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 6

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

3 Blocks of the Library

3.1 Overview

The "LMRTU" library consists of the following blocks and data types.

Function blocks and functions

Table 3-3: FBs and FCs of the library

Name Type Version Description

LMRTU_Scan FB V1.0.0 Scans the bus for accessible slaves.

LMRTU_RedMaster FB V1.0.0 Realizes a redundant Modbus RTU communication via two CMs

LMRTU_ResetSlaveList FC V1.0.0 Resets the list of scanned slaves.

Data types

Table 3-3: Data types of the library

Name Version Description

LMRTU_typeCommParam V1.0.0 Data type with the parameters for "Modbus_Comm_Load".

LMRTU_typeMbControl V1.0.0 Data type for the interconnection of the Modbus instructions.

LMRTU_typeScanParam V1.0.0 Data type with the parameters for "LMRTU_Scan".

LMRTU_typeSlaveList V1.0.0 Array that determines the size of the slave list for "LMRTU_Scan".

LMRTU_typeSlaveRed V1.0.0 Data type with the properties of a slave for "LMRTU_RedMaster".

LMRTU_typeSlaveScan V1.0.0 Data type with the properties of a slave for "LMRTU_Scan".

Page 7: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

3 Blocks of the Library

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 7

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

3.2 Scanning the bus for accessible slaves

3.2.1 Description

Short description

With the "LMRTU_Scan" function block, a Modbus master can scan the bus for accessible slaves. During operation, the master can thereby determine, if all known slaves are still accessible, or if new slaves have been added.

Figure 3-1: Overview

Block

The following figure shows the call interface of the "LMRTU_Scan" FB. The table describes the parameters of the function block.

Figure 3-2: "LMRTU_Scan"

LMRTU_Scan

Bool

execute done

Bool

Bool

stopReset busy

Bool

PORT

hwID error

Bool

USInt

mode status

Word

current BaudRate

UDInt

current

Address

USInt

numSlaves

Found

USInt

"LMRTU_typeParam" param

"LMRTU_typeParam"

"LMBSCAN_typeSlaveList" slaveList

"LMBSCAN_typeSlaveList"

Modbus_Master instModbusMaster

Modbus_Master

Page 8: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

3 Blocks of the Library

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 8

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

Table 3-1: Parameter of "LMRTU_Scan"

Name P type Data type Comment

execute IN Bool Execute scan

stopReset IN Bool Stop scan and reset slave list

hwID IN PORT Hardware ID of the CM.

mode IN USInt Duplex operation mode1)

.

done OUT Bool Job successfully completed.

busy OUT Bool Job is being processed.

error OUT Bool An error occurred.

status OUT Word Specifies the error.

currentBaudRate OUT UDInt Baud rate that is currently being scanned.

currentAddress OUT USInt Modbus address that is currently being scanned.

numSlavesFound OUT USInt Number of found slaves. The value remains set until the next scan.

param IN_OUT "LMRTU_typeParam" Parameter of the FB (see 3.4).

slaveList IN_OUT "LMRTU_typeSlaveList" Slave list in form of an array of the "LMRTU_slave" type.

The slave list is reset at every scan.

instModbusMaster IN_OUT "Modbus_Master" Parameter instance of the subordinate "Modbus_Master" FB, so that a possibly already existing instance of "Modbus_Master" can be used.

1)

Permissible operation modes are:

0: Full duplex (RS232)

1: Full duplex (RS422) Four-wire mode (point-to-point)

2: Full duplex (RS422) Four-wire mode (multi-point master CM PtP (ET 200SP))

3: Full duplex (RS422) Four-wire mode (multi-point slave CM PtP (ET 200SP))

4: Half duplex (RS485) Two-wire mode

Status and error displays

After completion of the job, or in the event of an error, the outputs "done", "error", "status" and "numSlavesFound" remain set for as long as "execute" is set, or for the duration of one cycle.

The outputs "currentAddress" and "currentBaudrRate" are reset upon completion of the scan. In the event of an error, the outputs remain set until the error is corrected.

Page 9: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

3 Blocks of the Library

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 9

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

The table below shows the meaning of the "status" output parameter.

Table 3-2: Status codes

status Meaning

16#0000 Job successfully completed.

16#7000 Function block is waiting for job.

16#7001 Job started.

16#7002 Job is being processed.

16#7011 Job is stopped.

16#7012 Job has been stopped and function block has been reset.

16#8xxx An error occurred at the "Modbus_Comm_Load" or "Modbus_Master" function block. The status code of the concerned function block is output.

The meaning of the respective status codes can be found in the TIA Portal information system.

Note Interconnect the output parameters in order to recognize an error of the block and to perform adequate error handling.

Figure 3-3: Behavior of the outputs at brief "execute"

Figure 3-4: Behavior of the outputs at continuing "execute"

Page 10: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

3 Blocks of the Library

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 10

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

Figure 3-5: Behavior of the outputs in the event of an error

3.2.2 Mode of operation

The user determines the address range and the baud rate that are to be scanned. Optionally, all common baud rates can also be scanned successively.

The "LMRTU_Scan" function block successively sends a telegram with the Modbus function "8" (diagnosis) to each address of the specified address range.

If a slave with the respective address exists, it responds and the master saves the slave’s address and baud rate in a data block. If the master does not receive an answer within a given time, it proceeds with the next address.

An output of the "LMRTU_Scan" function block outputs the number of found slaves during the scan.

Page 11: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

3 Blocks of the Library

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 11

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

Process flow

The "LMRTU_Scan" FB is realized in a state machine. The figure below shows the simplified program flow:

Figure 3-6: Program flow

IDLE

Input parameters

are saved

"execute"

is set

Slave list is reset

Port is configured for baud rate m

Telegram is sent

to slave n

Slave n responds

Slave is written to slave list,"numSlavesFound" is

incremented

n = last

address

n is incremented

or decremented

m is incremented, n is reset to start

address

"scanAllBaud

Rates" activated

DONE

m = last

baud rate

Yes

No

No No

No

YesYes

Yes

For the Modbus communication, the "LMRTU_Scan" FB uses the "Modbus_Comm_Load" and "Modbus_Master" instructions.

If all baud rates are scanned, the "Modbus_Comm_Load" instruction will be called up multiple times and the CM will be reconfigured for each baud rate.

Page 12: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

3 Blocks of the Library

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 12

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

Instance with standard access (no optimized access)

In the "LMRTU_Scan" FB, the optimized access is deactivated since the Modbus RTU instruction "Modbus_Master", that is used in the FB, requires the standard access.

If you create your own instance data block for the "LMRTU_Scan" FB, it will automatically be created with standard access.

If you call up the "LMRTU_Scan" FB as multi-instance, you need to deactivate the optimized access for the calling block.

Slave list

The slave list at the "slaveList" parameter is written to during the scan process.

At each new scan process, the slave list is reset and refilled.

Figure 3-7: Example of a slave list after the scan process

Parameter instance of "Modbus_Master"

A CM must not be used by several "Modbus_Master" instances at once. If you use the "Modbus_Master" FB several times for the same CM in your project, the "Modbus_Master" call-ups need to use the same instance.

This is why the "Modbus_Master" instance is set up as parameter instance in the "LMRTU_Scan" FB. If you already use an instance of "Modbus_Master" in your project, connect this instance to the "instModbusMaster" parameter.

Page 13: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

3 Blocks of the Library

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 13

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

3.2.3 Integration into the user project

Opening the library and loading it into the project

Table 3-3: Opening the library and loading it into the project

No. Procedure

1. Download the "LMRTU" library and unzip the file.

2. Open TIA Portal V14 and your existing project.

3. Go to the project view.

4. Open the "Libraries" pane in the right part of the display.

5. Expand the "Global libraries" tab.

6. Click on the "Open global library" icon and select the extracted library.

7. Select the "LMRTU.al14" global library from the extracted folder.

8. Use drag and drop to move the "Scan" folder from the "Types" folder to the "Program blocks" folder of your Modbus master.

The data types are automatically stored in the "PLC data types" folder.

Changes to the Modbus slaves are not necessary.

9. Use drag and drop to move the data blocks from the "Master copies > Scan" folder to the "Program blocks" folder of your Modbus master.

10. Use drag and drop to move the "LMRTU_Scan" watch table from the "Master copies > Scan" folder to the "Watch and force tables" folder of your Modbus master.

Page 14: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

3 Blocks of the Library

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 14

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

Interconnecting LMRTU_Scan

Table 3-4: Interconnecting "LMRTU_Scan"

No. Procedure

1. Open the OB1 of your Modbus master, add the "LMRTU_Scan" FB to an empty network and create an instance data block.

Note: In the "LMRTU_Scan" FB, the optimized access is deactivated, since the Modbus RTU instruction "Modbus_Master" that is used in the FB, requires the standard access.

If you create your own instance data block for the "LMRTU_Scan" FB, it will automatically be created with standard access.

If you call up the "LMRTU_Scan" FB as multi-instance, you need to deactivate the optimized access for the calling block.

2. Interconnect the inputs and outputs of the block with the tags of the "LMRTU_ScanControl" data block.

Page 15: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

3 Blocks of the Library

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 15

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

No. Procedure

3. Set the hardware identifier of your CM at the "hwID" input, e.g. via the corresponding system constant.

4. Specify the duplex mode at the "mode" input, e.g. "4" for half duplex (RS485) two-wire mode (see Table 3-1).

5. If you have not yet created an instance of the "Modbus_Master" FB in your project, create a new data block of the "Modbus_Master" type.

Page 16: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

3 Blocks of the Library

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 16

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

No. Procedure

6. Set the already existing or just created instance of "Modbus_Master" at the "instModbusMaster" parameter.

Interconnecting LMRTU_ResetSlaveList (optional)

To reset the slave list during a CPU restart, proceed as follows:

Table 3-5: Interconnecting "LMRTU_ResetSlaveList"

No. Action

1. Insert the OB100 (Startup) into your Modbus master.

2. Open OB100 and insert the "LMRTU_ResetSlaveList" function into an empty network.

3. Interconnect the "slaves" parameter with the array of the slave list.

Page 17: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

3 Blocks of the Library

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 17

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

3.2.4 Operation

Prerequisite

Modbus slaves are parameterized and ready for operation.

Modbus master and Modbus slaves are connected to each other via a bus.

During the scan, the used CM is not used by other calls of the "Modbus_Master" (e.g. reading of hold registers).

Scanning the Modbus for accessible slaves

To scan the bus for accessible slaves, compile and download the project and proceed as follows:

Table 3-6: Scanning the Modbus for accessible slaves

No. Action

1. Open the watch table "LMRTU_Scan" and activate monitoring.

2. To scan the bus for a certain number of slaves, specifiy this number via the "slavesToCount" tag.

To scan the whole address range, set the "slavesToCount" tag to "0".

3. Specify the address range to be scanned via the "startingAddress" and "endingAddress" tags.

To perform the scan in reverse, from the highest to the lowest address, specify a higher number at the "startingAddress" tag than at the "endingAddress" tag.

4. Determine the baud rate to be scanned via the "baudRate" tag.

To scan all common baud rates consecutively, set the "scanAllBaudRates" tag to "true".

5. Specify the parity with the "parity" tag (0: none, 1: odd, 2: even).

6. Specify the time, how long the master shall wait for a response from the slave, via the "timeout" tag.

7. Specify the number of attempts to access a slave via the "retries" tag.

8. To start the scan process, set the "execute" tag to "true".

9. To abort the scan process, set the "stopReset" tag to "true".

Page 18: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

3 Blocks of the Library

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 18

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

3.3 Redundant communication

3.3.1 Description

Short description

With the "LMRTU_RedMaster" function block, you can implement a redundant communication via Modbus RTU. For this, you add two CMs and interconnect the Modbus slaves as a ring.

If the bus is interrupted at one point and the slave cannot be accessed by the master CM any more, the FB tries to access the slave via the reserve CM.

Figure 3-8: Overview

Note The "LMRTU_RedMaster" FB does not check periodically, whether the bus is interrupted. If a timeout occurs during the request of a slave, the slave will be addressed by the reserve CM. The switchover occurs within a few milliseconds after the timeout.

Page 19: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

3 Blocks of the Library

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 19

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

Block

The following figure shows the call interface of the "LMRTU_RedMaster" FB. The table describes the parameters of the function block.

Figure 3-9: LMRTU_RedMaster

LMRTU_RedMaster

Bool

init done

Bool

Bool

req busy

Bool

UInt

mbAddr error

Bool

USInt

mode statusID

USInt

UDInt

dataAddr status

Word

UInt

dataLen reserveActive

Bool

PORT

hwIDMaster

PORT

hwIDReserve

Time recheck

Interval

Variant dataPtr

Variant

"LMRTU_typeCommParam" commParam

"LMRTU_typeCommParam"

Array[*] of "LMRTU_typeSlaveRed"

slaves Array[*] of "LMRTU_typeSlaveRed"

Table 3-7: Parameter of LMRTU_RedMaster

Name P type Data type Comment

init IN Bool Re-initialize the communication and resets the use of the reserve CM.

req IN Bool Send data to the Modbus slave.

mbAddr IN UInt Modbus address of the slave.

mode IN USInt Selecting the mode (read, write, diagnosis)

1).

dataAddr IN UDInt Start address of the data in the slave

1).

dataLen IN UInt Length of the data to be written, or data to be read 1)

.

hwIDMaster IN PORT Hardware identifier of the master CM.

hwIDReserve IN PORT Hardware identifier of the reserve CM.

recheckInterval IN Time Interval, in which an inaccessible slave is checked.

T#0s: no periodic check.

Page 20: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

3 Blocks of the Library

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 20

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

Name P type Data type Comment

done OUT Bool Job successfully completed.

busy OUT Bool Job is being processed.

error OUT Bool An error occurred.

statusID OUT USInt Specifies the source of the error.

status OUT Word Specifies the error.

reserveActive OUT Bool Interruption detected. Reserve CM is being used.

dataPtr IN_OUT Variant Points to the data to be written or to be read.

commParam IN_OUT "LMRTU_typeCommParam" Parameter for the Modbus communication.

slaves IN_OUT Array[*] of "LMRTU_typeSlaveRed"

Array of the known slaves.

1)

These parameters correspond to the parameters of the used "Modbus_Master" instruction. The meaning of the parameters can be found in the TIA Portal information system under "Modbus_Master".

Status and error displays

After completion of the job, or in the event of an error, the outputs "done", "error", "statusID" and "status" remain set for as long as "req" is set, or for the duration of one cycle.

The output "statusID" indicates, which of the instructions used ("Modbus_Comm_Load" or "Modbus_Master") triggered the error that is returned at the "status" output.

The table below shows the meaning of the "statusID" and "status" outputs.

Table 3-8: Status codes

statusID status Meaning

0 16#0000 Job successfully completed.

0 16#7000 Function block is waiting for job.

0 16#7001 Job started.

0 16#7002 Job is being processed.

1 16#8xxx An error occurred at the "Modbus_Master" instruction of the master port. The status code of this instruction is being output.

The meaning of the respective status codes can be found in the TIA Portal information system.

2 16#8xxx An error occurred at the "Modbus_Master" instruction of the reserve port. The status code of this instruction is being output.

The meaning of the respective status codes can be found in the TIA Portal information system.

3 16#8xxx An error occurred at the "Modbus_Comm_Load" instruction of the master port. The status code of this instruction is being output.

The meaning of the respective status codes can be found in the TIA Portal information system.

4 16#8xxx An error occurred at the "Modbus_Comm_Load" instruction of the

Page 21: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

3 Blocks of the Library

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 21

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

statusID status Meaning

reserve port. The status code of this instruction is being output.

The meaning of the respective status codes can be found in the TIA Portal information system.

Note Interconnect the output parameters in order to recognize an error of the block and to perform adequate error handling.

3.3.2 Mode of operation

In the "LMRTU_RedMaster" FB, the Modbus instructions "Modbus_Comm_Load" and "Modbus_Master" are called up respectively for the master and the reserve CM.

During the program start, both ports are configured for the communication. In general, a slave is addressed via the master CM. If a slave does not respond within the parameterized time period, the same job will be sent again via the reserve CM.

If the slave responds to the request from the reserve CM, the "accessibleByMaster" tag in the array at the "slaves" parameter will be reset for this slave. Further requests to this slave will then be immediately sent via the reserve CM.

If the slave does not respond to the request from the reserve CM either, an error will be output.

Page 22: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

3 Blocks of the Library

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 22

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

Process flow

The figure below shows the simplified program flow:

Figure 3-10: Program flow

Stop

Start

New

request

“accessible

ByMaster” is

TRUE?

Telegram is sent

via master port

Telegram is sent

via reserve port

Slave

responds?

Slave

responds?

Output

time out

error

“accessibleBy

Master” is set to

FALSE

Yes

No

NoYes

Yes

No

Periodic check of inaccessible slaves

If you specify a time at the "recheckInterval" parameter that is unequal to "0", the FB (if it is not processing a job at this time) checks in this interval, whether the inaccessible slaves are accessible again via the master CM.

During this, the master sends a diagnostic telegram to the first inaccessible slave within the array at the "slaves" parameter. If the slave responds to the request from the master CM, the "accessibleByMaster" tag in the array will be set again for this slave. Further requests to this slave will then be sent via the master CM again.

Page 23: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

3 Blocks of the Library

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 23

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

3.3.3 Integration into the user project

Opening the library and loading it into the project

Table 3-9: Opening the library and loading it into the project

No. Procedure

1. Download the "LMRTU" library and unzip the file.

2. Open TIA Portal V14 and your existing project.

3. Go to the project view.

4. Open the "Libraries" pane in the right part of the display.

5. Expand the "Global libraries" tab.

6. Click on the "Open global library" icon and select the extracted library.

7. Select the "LMRTU.al14" global library from the extracted folder.

8. Use drag and drop to move the "RedundantMaster" folder from the "Types" folder to the "Program blocks" folder of your Modbus master.

The data types are automatically stored in the "PLC data types" folder.

Changes to the Modbus slaves are not necessary.

9. Use drag and drop to move the "LMRTU_RedMasterControl" data block from the "Master copies > RedundantMaster" folder to the "Program blocks" folder of your Modbus master.

10. Use drag and drop to move the "LMRTU_RedMaster" watch table from the "Master copies > RedundantMaster" folder to the "Watch and force tables" folder of your Modbus master.

Page 24: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

3 Blocks of the Library

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 24

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

Creating data buffer and slave array

Table 3-10: Creating data buffer and slave array

No. Procedure

1. Create a new global data block as data buffer for the data to be written or read.

2. Create an array of the "Word" type in the data block for the data to be written or read.

3. Open the properties of the data block and deactivate the optimized access under "Attributes".

Confirm the dialog and close the window by pressing "OK".

4. Create a new global data block for the slave array.

5. Create an array of the "LMRTU_typeSlaveRed" type in the data block. The number of your Modbus slaves determines the size of the array.

6. Enter the Modbus addresses of your Modbus slaves into the array.

Page 25: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

3 Blocks of the Library

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 25

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

Determining the communication parameters

The communication parameters for the configuration of the CM are transmitted to the "LMRTU_RedMaster" FB via the "commParam" parameter. For the communication parameters, the "LMRTU_typeCommParam" data type is included in the library.

Table 3-11: Determining the communication parameters

No. Procedure

1. Open the "LMRTU_RedMasterControl" data block.

2. Expand the "commParam" variable.

3. Define the parameters for your application.

A detailed description of the parameters can be found in the TIA Portal information system under "Modbus_Comm_Load".

Interconnecting LMRTU_RedMaster

Table 3-12: Interconnecting "LMRTU_RedMaster"

No. Procedure

1. Open the OB1 of your Modbus master, add the "LMRTU_RedMaster" FB to an empty network and create an instance data block.

Page 26: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

3 Blocks of the Library

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 26

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

No. Procedure

2. Interconnect the inputs and outputs of the FB with the tags of the "LMRTU_RedMasterControl" DB.

3. Set the hardware identifier of your CMs at the "hwIDMaster" and "hwIDReserve" inputs, e.g. via the corresponding system constant.

4. If the FB shall check periodically, whether the bus interruption has been resolved and inaccessible slaves are accessible again via the Master CM, you need to set a time at the "recheckInterval" input that is greater than "0", e.g. "T#10m".

5. Set the array at the IN-OUT parameter "dataPtr" for the data to be written or read.

6. Set the slave array at the IN-OUT parameter "slaves".

Page 27: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

3 Blocks of the Library

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 27

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

Note To configure both CMs, the Modbus instruction "Modbus_Comm_Load" is called up within the "LMRTU_RedMaster" FB. You do not need to call up the instruction separately.

Setting further slaves to "inaccessible" (optional)

If you enter your slaves into the slave array in the order in which they are attached to the bus, and if a slave is inaccessible, you can also set all slaves to "inaccessible" that are at the bus behind (in the array beneath). Hence, the "LMRTU_RedMaster" FB will not attempt to access each of these slaves via the Master CM first.

To be able to use the FB as universally as possible, this function has not been realized in the FB. However, it can easily be added.

For this, monitor the "reserveActive" output and, via an FOR loop, set the "accessibleByMaster" tags of the slaves behind the inaccessible slave in the slave array to "FALSE".

With a positive edge at the "init" input of the "LMRTU_RedMaster" FB, the "accessibleByMaster" tag of all slaves is set to "TRUE" again.

3.3.4 Operation

Prerequisite

Modbus slaves are parameterized and ready for operation.

Modbus master and Modbus slaves are connected to each other in a ring.

The known slaves are entered into the slave array at the "slaves" parameter.

Sending a request to the slave

You can send a request to the slave with the "LMRTU_RedMaster" FB, just like with the "Modbus_Master" instruction. The FB has the same parameters for this.

The only difference is that after completion of the job, or in the event of an error, the outputs "done", "error" and "status" remain set for as long as "req" is set, or for the duration of one cycle.

Table 3-13: Scanning the Modbus for accessible slaves

No. Action

1. Open the watch table "LMRTU_RedMaster" and activate monitoring.

2. Control the "mbAddr", "mode", "dataAddr" and "dataLen" tags for the respective request.

The meaning of the parameters can be found in the TIA Portal information system under "Modbus_Master".

3. To write data, control the respective tag of the data buffer.

4. Set the "req" tag to "TRUE".

5. Reset the "req" tag.

Page 28: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

3 Blocks of the Library

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 28

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

3.4 Data types

This chapter explains the data types that are used by the blocks of the library.

LMRTU_typeCommParam

The "LMRTU_typeCommParam" data type summarizes the communication parameters for the "Modbus_Comm_Load" instruction.

Table 3-14: "LMRTU_typeCommParam" tags

Name Data type

Comment

baud UDInt Selecting the transmission rate (baud rate).

parity UInt Selecting the parity:

0: none

1: odd

2: even

flowCtrl UInt Selecting the flow control:

0: none

1: Hardware flow control with RTS always ON

2: Hardware flow control switched with RTS

rtsOnDelay UInt Selecting RTS switch-on delay:

0: No delay

1 to 65535: Delay in ms

rtsOffDelay UInt Selecting RTS switch-off delay:

0: No delay

1 to 65535: Delay in ms

respTo UInt Time (in ms) the "Modbus_Master" waits for a response from the slave.

mode USInt Duplex operation mode:

0: Full duplex (RS232)

1: Full duplex (RS422) Four-wire mode (point-to-point)

2: Full duplex (RS422) Four-wire mode (multi-point master CM PtP (ET 200SP))

3: Full duplex (RS422) Four-wire mode (multi-point slave CM PtP (ET 200SP))

4: Half duplex (RS485) Two-wire mode

retries USInt Number of retries performed by the Master.

Further information on these parameters can be found in the TIA Portal information system under "Modbus_Comm_Load".

Note With the "Modbus_Comm_Load" instruction, the "mode" and "retries" parameters are not transmitted to the interface but written directly into the instance data block. The meaning of both of these parameters can be found on the same page in the TIA Portal information system under "Modbus_Comm_Load data block tags".

Page 29: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

3 Blocks of the Library

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 29

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

LMRTU_typeMbControl

This data type contains tags that are frequently required for the interconnection of the "Modbus_Comm_Load" and "Modbus_Master" instructions.

LMRTU_typeScanParam

The following table describes the "LMRTU_typeScanParam" data type which summarizes the parameters for the "LMRTU_Scan" FB.

Table 3-15: "LMRTU_typeScanParam" tags

Name Data type

Comment

slavesToCount USInt Number of slaves that are to be found.

startingAddress USInt Modbus address at which the scan starts.

endingAddress USInt Modbus address at which the scan ends.

If "endingAddress" is lower than "startingAddress", the scan will be performed from the highest to the lowest address.

baudRate UDInt Baud rate with which the scan is performed.

scanAllBaudRates Bool Option that all common baud rates are scanned successively.

parity USInt Parity (0: none, 1: odd, 2: even).

timeout UInt Time (in ms) the Master waits for a response from the slave.

retries USInt Number of retries that the Master peforms per slave.

LMRTU_typeSlaveRed

The following table describes the "LMRTU_typeSlaveScan" data type which is used for the slave array for "LMRTU_RedMaster".

Table 3-16: "LMRTU_typeSlaveRed" tags

Name Data type

Comment

mbAddr USInt Modbus address of the slave.

accessibleByMaster Bool Determines, whether the slave is addressed via the master or the reserve CM.

LMRTU_typeSlaveScan

The following table describes the "LMRTU_typeSlaveScan" data type which is used for the slaves list for "LMRTU_Scan".

Table 3-17: "LMRTU_typeSlaveScan" tags

Name Data type

Comment

mbAddress USInt Modbus address of the slave.

baudRate UDInt Baud rate used by the slave.

Page 30: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

4 Valuable Information

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 30

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

4 Valuable Information

4.1 Fundamentals of the Modbus RTU

Overview

Modbus RTU (Remote Terminal Unit) is a standard protocol for serial communication between master and slave.

Master-slave relationship

Modbus RTU utilizes a master-slave relation in which the entire communication is effected from one single master unit. The master sends a request to a slave address and only the slave with this slave address responds to the command.

Special case: When using the slave address "0", the Master sends a broadcast telegram to all slaves (without receiving a slave reply).

Communication procedure

The communication procedure with Modbus RTU is as follows:

1. The Modbus master sends a request to a Modbus slave.

2. The slave responds with a response telegram, or, if the slave cannot process the request of the master, with an error telegram.

Frame structure

As an example, the following table shows the structure of a telegram if data are read from one or several holding register(s) of the Modbus slave.

Table 4-1: Telegram structure

Telegram Byte 0 Byte 1 Byte 2 Byte 3 Byte 4 Byte 5 …

Query Slave address

Function code

Start address (from what holding register

is to be read)

No. of registers ---

Valid response Slave address

Function code

Length Register data

Error message Slave address

0x83 Error code

---

Page 31: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

4 Valuable Information

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 31

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

4.2 Modbus RTU in TIA Portal

TIA Portal provides Modbus RTU instructions to enable communication with SIMATIC components via Modbus RTU. You can find these instructions in the TIA Portal in the "Instructions" task card under "Communication > Communication processors > MODBUS (RTU)".

The table below lists the Modbus RTU instructions that are available in TIA Portal.

Table 4-2: Modbus RTU instruction in TIA Portal

Instruction Description

Modbus_Comm_Load Configures the communication module for Modbus

Modbus_Master Communicating as Modbus master

Modbus_Slave Communicating as Modbus slave

Below, you find important notes on the use of the Modbus RTU instruction in TIA Portal.

Configuring the CM for the Modbus RTU protocol

Before the "Modbus_Master" and "Modbus_Slave" instructions can be used for communication, the CM needs to be configured for the Modbus RTU protocol with the "Modbus_Comm_Load" instruction.

For the configuration of every communication port that is used for the Modbus communication, an execution of "Modbus_Comm_Load" must be used.

Note When using the "Modbus_Comm_Load" instruction, certain parameters that do not change during the program runtime (e.g. "MODE") Need to be set directly in the static tags of the corresponding instance.

The parameters that need to be set in the instance can be found in the TIA Portal Online Help under "Modbus_Comm_Load".

Modbus function codes in TIA Portal

The "Modbus_Master" instruction uses a "MODE" input instead of a function code input. The combination of "MODE" and "DATA_ADDR" specifies the function code that is used in the actual Modbus telegram.

The assignment between the "MODE" parameter, the Modbus function code and the Modbus address range in "DATA_ADDR" can be found in the TIA Portal Online Help under "Modbus_Master".

Page 32: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

5 Appendix

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 32

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

5 Appendix

5.1 Service and support

Industry Online Support

Do you have any questions or need support?

Siemens Industry Online Support offers access to our entire service and support know-how as well as to our services.

Siemens Industry Online Support is the central address for information on our products, solutions and services.

Product information, manuals, downloads, FAQs and application examples – all information is accessible with just a few mouse clicks at https://support.industry.siemens.com

Technical Support

Siemens Industry's Technical Support offers quick and competent support regarding all technical queries with numerous tailor-made offers – from basic support right up to individual support contracts.

Please address your requests to the Technical Support via the web form: www.siemens.com/industry/supportrequest

Service offer

Our service offer comprises, among other things, the following services:

Product Training

Plant Data Services

Spare Parts Services

Repair Services

On Site and Maintenance Services

Retrofit and Modernization Services

Service Programs and Agreements

Detailed information on our service offer is available in the Service Catalog: https://support.industry.siemens.com/cs/sc

Industry Online Support app

Thanks to the "Siemens Industry Online Support" app, you will get optimum support even when you are on the move. The app is available for Apple iOS, Android and Windows Phone. https://support.industry.siemens.com/cs/ww/en/sc/2067

Page 33: Library for Modbus RTU (LMRTU) and Liability Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 2 G 7 d Warranty and Liability Note The Application Examples are not binding

5 Appendix

Bibliothek LMRTU Library Entry ID: 109747565, V1.0.0, 09/2017 33

S

iem

en

s A

G 2

01

7 A

ll ri

gh

ts r

ese

rve

d

5.2 Links and literature

Table 5-1: Links and literature

No. Topic

\1\ Siemens Industry Online Support

https://support.industry.siemens.com

\2\ Link to the entry page of the library

https://support.industry.siemens.com/cs/ww/en/view/109747565

5.3 Change documentation

Table 5-5: Change documentation

Version Date Modifications

V1.0 09/2017 First version