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 (TIA Portal) you can define each communication module as a MODBUS master or slave. If you select MODBUS Master, you can communicate
- With only one slave using the CM 1241 RS232 (physical limitation).
- With up to 32 slaves one after the other using the CM 1241 RS485.
We will take a sample project to describe the configuration procedure in STEP 7 (TIA Portal) V11 for communication with multiple slaves using the CM 1241 RS485.
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 interface (half-duplex), 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.
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 made in STEP 7 (TIA Portal) and the program code is transferred to the controllers with the switch CSM 1277.
The configuration block MB_COMM_LOAD is called on both sides (master and slave) for MODBUS communication.
The MB_COMM_LOAD block is used to select the communication module, set the communication parameters and parameterize 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 (TIA Portal) 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.
You use the MB_MASTER block to define the communication module selected with the MB_COMM_LOAD configuration block as MODBUS master.
The MB_MASTER block is used to select the MODBUS slave to be addressed, select the function code and define the local data storage area. The table below explains the parameters.
||MODBUS-RTU station address.|
||Selects the direction of transmission ("0" = read, "1" = write or additional diagnostics functions).|
||Specifies the MODBUS start address.|
||Specifies the MODBUS data length.|
||Defines the local receive and send data areas of the master. The DATA_PTR parameter must refer to a global data block created with the disabled option "Symbolic access only".|
The "MB_MASTER" must be called in the first program cycle
- by activating the system marker M1.0 in the hardware settings
- or by a call in the Startup OB 100.
You use the MB_SLAVE block to define the communication module selected with the MB_COMM_LOAD configuration block as MODBUS slave.
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.
||Transfers the MODBUS-RTU station address.|
||Holding register of the slave. The MB_HOLD_REG parameter must refer to a global data block created with the disabled option "Symbolic access only".|
When multiple registers (words) are to be transferred, with the "MB_HOLD_REG" parameter 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. 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.
In the master project, the alternate writing to / reading from the holding register of the slave is done using a sequencer.
- 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).
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.
The compressed file "STEP7_V11_Modbus_RTU.zip" contains the sample project "MODBUS_RTU_V11 SP2 Upd5.ap11" for alternating data transfer between a master and a slave. Unpack the "STEP7_V11_Modbus_RTU.zip" file and open the sample project "MODBUS_RTU_V11 SP2 Upd5.ap11" with STEP 7 (TIA Portal) V11+SP2+Update5. The firmware of the CPU modules is V2.2. Download the project folders ("Master" and "Slave") into the controllers. You can follow the data transfer in the monitoring (watch) tables.
STEP7_V11_Modbus_RTU.zip ( 994 KB )
More information about MODBUS library operations is available in the SIMATIC S7-1200 Programmable Controller System Manual (Entry ID: 36932465).
Proceed as follows to update the blocks of your MODBUS-RTU library (Entry ID 59421832).