Thursday, June 9, 2011

How do you establish MODBUS-RTU communication with STEP 7 Basic V10.5 for the SIMATIC S7-1200?Go to beginning
Display part number

Description
The SIMATIC S7-1200 enables point-to-point communication using the communication modules CM 1241 RS485 and CM 1241 RS232.
You can add up to three communication modules to each S7-1200 controller.
Using the MODBUS library integrated in STEP 7 Basic V10.5 you can define each communication module as a MODBUS master or slave.
If you define the CM 1241 RS232 as MODBUS master, then there is a physical limitation that permits communication with one slave only.
If you define the CM 1241 RS485 as MODBUS master, then it is possible to communicate with up to 32 slaves one after the other.
We will take a sample project to describe the configuration procedure in STEP 7 Basic V10.5 for communication with multiple slaves using the CM 1241 RS485.

Instructions
We have taken the example of MODBUS communication between a master and a slave.
There is alternate writing to the holding register of the slave (function code 16) and reading from the holding register of the slave (function code 03).
Since the RS485 has a two-wire (half-duplex) interface, writing and reading must be conducted consecutively.
By the same principle, the address of the slave to be addressed can be changed between two actions (here writing and reading), thus making it possible to exchange data with multiple slaves.


Fig. 01

The CM 1241 RS485 communication modules are linked to each other using a PROFIBUS cable.
You can continue with the PROFIBUS cable to connect up to 31 additional MODBUS slaves to the master.
The configuration is done in STEP 7 Basic V10.5 SP2. The program code is transferred to the controllers with the switch CSM 1277.

MB_COMM_LOAD
The configuration block MB_COMM_LOAD is required on both sides (master and slave) for MODBUS communication.


Fig. 02

The MB_COMM_LOAD block is used to select the communication module, set the communication parameters and establish the connection with the master or slave parameters.
The MB_COMM_LOAD block must be called in the first program cycle (by activating the system marker M1.0 in the hardware settings or by a call in Startup OB 100).
After inserting the communication module in the hardware configuration you can select the symbolic name of the communication module at the PORT parameter.
The communication parameters BAUD (transmission rate) and PARITY (parity) must be identical for all nodes. The port configuration of the RS485 interface in the STEP 7 V10.5 device view is irrelevant here.
The instance data block of the master or slave block is transferred at the MB_DB parameter and thus defines the communication module (PORT parameter) as MODBUS master or slave.

MB_MASTER
You use the MB_MASTER block to define the communication module selected with the MB_COMM_LOAD configuration block as MODBUS master.


Fig. 03

The MB_MASTER block is used to select the MODBUS slave to be addressed, select the function code (read/write, MODBUS start address and data length) and define the local data storage area.
The communication is triggered by the REQ parameter.
The MODBUS-RTU station address to be addressed is transferred at the MB_ADDR parameter.
The MODE parameter specifies the direction of transmission ("0" = read, "1" = write or additional diagnostics functions).
The MB_MASTER block must be called in the first program cycle (by activating the system marker M1.0 in the hardware settings or by a call in Startup OB 100).
The DATA_ADDR and DATA_LEN parameters are for setting the MODBUS start address and the data length (bits or words depending on the MODBUS start address selected).
The DATA_PTR parameter defines the local receive and send data storage areas of the master. DATA_PTR must refer to a global data block created with the "Symbolic access only" option disabled. When multiple elements are to be transferred (bits or words), the symbolic name of an array or struct that covers this data storage area (DATA_ADDR and DATA_PTR) must be transferred here.

MB_SLAVE
You use the MB_SLAVE block to define the communication module selected with the MB_COMM_LOAD configuration block as MODBUS slave.


Fig. 04

The MB_SLAVE block is used to define the MODBUS-RTU station address and specify the local data storage area for the holding register data transfer.
The local MODBUS-RTU station address is transferred at the MB_ADDR parameter. The MODBUS master identifies the slave with this.
The MB_HOLD_REG parameter defines the local holding register data storage area of the slave. MB_HOLD_REG must refer to a global data block created with the "Symbolic access only" option disabled. When multiple registers (words) are to be transferred, the symbolic name of an array or struct that covers the specified data volume of the master (DATA_ADDR and DATA_PTR) must be transferred here. The first word of this array or struct corresponds to the MODBUS holding register start address 40001.

The parameters can be switched with the inputs and outputs of the blocks or by accessing the instance data block concerned.

Sample project
In the master project, the alternate writing to / reading from the holding register of the slave is done using a sequencer.


Fig. 05

The MB_COMM_LOAD configuration block is called in the initialization step 0.
In Step 1 the MB_Master with the communication parameters for writing to the slave (MODE = 1) is called and executed (REQ = 1).
The transition to Step 2 is made by the positive (DONE) or negative (ERROR) confirmation from the MB_MASTER.
The transition resets the execution of the MB_MASTER (REQ = 0).
In Step 2 the communication parameters for reading from the slave (MODE = 0) are written using the MB_MASTER in the same instance data block as from Step 1 (MB_DB parameter of the MB_COMM_LOAD block).
Execution and transition are the same as in Step 1.

The figure below shows a step (networks 6 and 7) with transition (network 8).


Fig. 06

The MB_ADDR parameter (see Network 6) can therefore also be used to change the slave to be addressed per step, which permits data to be exchanged with up to 32 slaves.

Download
The compressed file "MODBUS_RTU.zip" contains the sample project "MODBUS_RTU.ap10" for alternate data transfer between a master and a slave.
Unpack the "MODBUS_RTU.zip" file and open the sample project "MODBUS_RTU.ap10" with STEP 7 Basic V10.5 (>= SP2).
The version of the firmware of the SIMATIC S7-1200 controllers used is V1.0.2.
Download the project folders ("Master" and "Slave") into the controllers.
You can track the data transfer in the monitoring table.


For more information, check out Siemens website at http://support.automation.siemens.com/WW/view/en/47755811