Modbus RTU Programming with TIA V13 SP1

Modbus RTU Programming with TIA V13 SP1The TIA V13 SP1 software provides two versions of Modbus RTU commands:

Modbus RTU Programming with TIA V13 SP1

Figure 1. Two Versions of Modbus RTU Commands

The earlier version of the Modbus RTU command (MODBUS (V2.2) in Figure 1) can only communicate via the CM1241 communication module or the CB1241 communication board.

The new version of the Modbus RTU command (MODBUS(RTU) V3.0 in Figure 1) expands the functionality of Modbus RTU. This command supports not only the CM1241 communication module and CB1241 communication board but also supports Modbus RTU communication via the PTP communication module on the PROFINET or PROFIBUS distributed I/O rack.

The PTP modules supported by the new version of the Modbus RTU command are shown in Figure 2:

Modbus RTU Programming with TIA V13 SP1

Figure 2. PTP Modules Supported by the New Version of Modbus RTU Command

The new version of the Modbus RTU command includes Modbus RTU master and slave commands. This article will introduce the programming steps for master-slave communication of the new version of the Modbus RTU command using the CPU1217C + CM1241 RS422/485 + ET200SP CM PTP module as an example.

In this setup, the CPU rack CM1241 RS422/485 acts as the Modbus RTU slave, while the distributed rack ET200SP with the CMPTP module acts as the Modbus RTU master. The network structure diagram is shown below:

Modbus RTU Programming with TIA V13 SP1

Figure 3. Modbus RTU Network Communication Structure Diagram

The hardware and software used in this project are as follows:

Hardware:

① CPU1217C (Order No.: 6ES7 217-1AG40-0XB0), Firmware Version V4.1.3

② CM1241 RS422/485 Module (Order No.: 6ES7 241-1CH32-0XB0), Firmware Version V2.1

③ 24V Power Supply PS307 (Order No.: 6ES7307-1KA02-0AA0)

④ ET200 SP IM155-6PN HF (Order No.: 6ES7155-6AU00-0CN0)

⑤ CM PTP Module (Order No.: 6ES7137-6AA00-0BA0)

Software:

① TIA V13 SP1 UP 9

1. Device Configuration

a. Configure the CM1241 RS422/485 Module

Open the device view, add the S7-1200 CPU, and find “Communication Module” → “Point-to-Point” → “CM1241 (RS422/485)” in the hardware catalog, then drag this module to the left side of the CPU as shown in Figure 4:

Modbus RTU Programming with TIA V13 SP1

Figure 4. Adding the CM 1241 RS422/485 Module

Note:

The CM 1241 RS422/485 module with firmware version >= V2.1 is required to support the new version of the Modbus RTU command.

Next, in the “Device View”, select the CM1241 (RS422/485) module with the mouse, and configure the hardware interface parameters in “Properties” → “Port Configuration”. In this example, the transmission rate = 9.6Kbps, parity = no parity, data bits = 8 bits, stop bits = 1. The port configuration for the CM 1241 is shown in Figure 5:

Modbus RTU Programming with TIA V13 SP1

Figure 5. CM1241 RS422/485 Module Port Configuration

Finally, confirm that the hardware identifier is 269 in the “Hardware Identifier” (this parameter will be used in programming), as shown in Figure 6:

Modbus RTU Programming with TIA V13 SP1

Figure 6 Hardware Identifier

Additionally, the S7-1200 also provides system and clock memory functions. To facilitate subsequent instructions, it is recommended to enable this function. In the CPU “Properties” → “General” → “System and Clock Memory”, enable the system and clock memory function as shown in Figure 7.

Modbus RTU Programming with TIA V13 SP1

Figure 7. System and Clock Memory Function

b. Configure the ET200 SP CM PtP Module

(1) Insert an ET200SP distributed station.

Open the network view and drag in an ET200SP station, assigning it to the corresponding I/O controller (in this case, the CPU1217C is the I/O controller), as shown in Figure 8.

Modbus RTU Programming with TIA V13 SP1

Figure 8. Inserting ET200SP Station

(2) Configure the ET200SP station.

In the “Device View” environment of ET200SP, add signals, communication modules, and server modules to the ET200SP station. In this example, only the CM PTP module and server module are added.

Note:

The server module in the ET200SP station must be configured. The server module is purchased together with the interface module and does not need to be purchased separately.

The ET200SP interface module needs to be assigned an IP address and Device Name. For detailed steps on configuring the ET200 SP distributed I/O, please refer to the “ET200 SP Quick Start Guide”. This example will not describe the Profinet IO communication settings and steps.

In the “Device View” of ET200SP, select the CM PTP with the mouse, and configure the hardware interface parameters in “Properties” → “General” → “Interface” → “Operating Mode”. In this example, the “Designated Operating Mode”: “Half Duplex (RS485) 2-Wire Operation”; “Initial State of Receive Line”: “None”. As shown in Figure 9:

Modbus RTU Programming with TIA V13 SP1

Figure 9. CM PTP Operating Mode

Next, in “Properties” → “General” → “Interface” → “Port Configuration”, configure the port configuration parameters for this module. In this example, set “Protocol”: “Freeport/Modbus”; Port parameters: transmission rate = 9.6Kbps, parity = no parity, data bits = 8 bits, stop bits = 1. The port configuration is shown in Figure 10:

Modbus RTU Programming with TIA V13 SP1

Figure 10. CM PTP Port Configuration

Finally, confirm the CM PTP module hardware identifier in the “Hardware Identifier”; this parameter will be used in programming.

2. Software Programming

a. Modbus RTU Master Programming

Modbus RTU master programming requires calling the Modbus_Comm_Load instruction and Modbus_Master instruction. The Modbus_Comm_Load instruction configures the communication module via the Modbus RTU protocol, while the Modbus_Master instruction communicates as a Modbus master using the port configured by the Modbus_Comm_Load instruction. The MB_DB parameter of the Modbus_Comm_Load instruction must connect to the (static) MB_DB parameter of the Modbus_Master instruction.

In this example, the CM PTP module in the distributed rack ET200SP acts as the Modbus RTU master, and the relevant programming steps are as follows:

(1) Insert an FC function in OB1, and drag in the Modbus_Comm_Load instruction and Modbus_Master instruction in the function, as shown in Figure 11.

Modbus RTU Programming with TIA V13 SP1

Figure 11. Dragging in Modbus RTU Master Instruction

The parameters of the Modbus_Comm_Load instruction are described in Table 1 below:

Modbus RTU Programming with TIA V13 SP1

Table 1. Parameters of MB_COMM_LOAD Instruction

The parameters of the Modbus_Master instruction are described in Table 2 below:

Modbus RTU Programming with TIA V13 SP1

Table 2. Parameters of Modbus_Master Instruction

Note:

① It is not recommended to call the Modbus_Comm_Load instruction in the startup organization block OB100; it is recommended to call it in OB1. When called in OB1, the input bit “REQ” must be triggered on the rising edge, and in this example, this input bit uses the “FirstScan” system memory bit.

② The static variable “MODE” in the background data block of the Modbus_Comm_Load instruction describes the working mode of the PTP module. Valid working modes include:

  • 0 = Full Duplex (RS232)

  • 1 = Full Duplex (RS422) Four-Wire Mode (Point-to-Point)

  • 2 = Full Duplex (RS 422) Four-Wire Mode (Multi-Master, CM PtP (ET 200SP))

  • 3 = Full Duplex (RS 422) Four-Wire Mode (Multi-Slave, CM PtP (ET 200SP))

  • 4 = Half Duplex (RS485) Two-Wire Mode

The default data for this static variable “MODE” is 0 (RS232 Full Duplex Mode), and it needs to be modified according to the actual configuration of the CM PTP module. In this example, the CM PTP module operates in RS485 half-duplex mode, so this value needs to be modified to 4, as shown in Figure 12.

Modbus RTU Programming with TIA V13 SP1

Figure 12. Modbus_Comm_Load Background Data Block Static Variable “MODE” Modified to 4

③ The “DATA_PTR” parameter of the Modbus_Master instruction points to the address of the data area for writing or reading data. This data area supports optimized access data blocks or non-optimized (standard) data blocks, and non-optimized access data blocks are recommended.

In this example, the data area used is a non-optimized access data block, and to modify the data block to non-optimized access, cancel the “Optimized Block Access” in the properties of the data block (right-click the data block, select “Properties”, and cancel “Optimized Block Access”), as shown in Figure 13.

Modbus RTU Programming with TIA V13 SP1

Figure 13. Set Data Block to Non-Optimized Access

When the “DATA_PTR” parameter of the Modbus_Master instruction points to a non-optimized access data block, this input parameter needs to be filled in using pointer notation, such as P#DB3.DBX0.0 WORD 5.

④ When there are multiple Modbus RTU slaves in the Modbus RTU network, or a single Modbus RTU slave requires both read and write operations simultaneously, multiple Modbus_Master instructions need to be called, and the Modbus_Master instructions should be called in a polling manner.

Figure 14 describes the polling method for two Modbus_Master instructions.

Modbus RTU Programming with TIA V13 SP1

Figure 14. Polling Call Method for Modbus_Master

(2) Insert the “Pull or Plug of Modules” interrupt OB83.

In this example, the Modbus RTU master module is installed on the distributed I/O station, so the program needs to consider faults such as the distributed I/O station failure and the CM PTP module being plugged or unplugged.

Whenever a module is plugged or unplugged in the distributed I/O station, the operating system will call OB83 once. The input variable “16#Event_Class” in the OB83 interface can be used to determine the faulty module and type: Event type 16#39 indicates that the module has been unplugged, while event type 16#38 indicates that the module has been plugged in.

When the CM PTP module is reinserted, the Modbus_Comm_Load instruction must be called in the interrupt OB83 to reconfigure the communication module, as shown in Figure 15.

Modbus RTU Programming with TIA V13 SP1

Figure 15. Calling Modbus_Comm_Load Instruction Again in OB83

Note:

① The background data block called in OB83 for the Modbus_Comm_Load instruction must be the same as the one called in OB1.

② The hardware identifier of the CM PTP module can also be queried in “PLC Variables” → “System Constants”, as shown in Figure 16.

Modbus RTU Programming with TIA V13 SP1

Figure 16. System Constant

(3) Insert the “Rack or Station Failure” interrupt OB86.

Whenever there is a fault or recovery of the distributed I/O station, the operating system will call OB86 once. The input variable “16#Event_Class” in the OB86 interface can be used to determine the faulty module and type: Event type 16#39 indicates a station fault, while event type 16#38 indicates station recovery.

When the I/O station where the CM PTP module is located recovers, the Modbus_Comm_Load instruction must be called in the interrupt OB86 to reconfigure the communication module, as shown in Figure 17.

Modbus RTU Programming with TIA V13 SP1

Figure 17. Calling Modbus_Comm_Load Instruction in OB86

Note:

① The background data block called in OB86 for the Modbus_Comm_Load instruction must be the same as the one called in OB1.

② The hardware identifier of the distributed I/O station can also be queried in “PLC Variables” → “System Constants”.

b. Modbus RTU Slave Programming

Modbus RTU slave programming requires calling the Modbus_Comm_Load instruction and Modbus_Slave instruction. The Modbus_Comm_Load instruction configures the communication module via the Modbus RTU protocol, while the Modbus_Slave instruction communicates as a Modbus slave using the port configured by the Modbus_Comm_Load instruction. The MB_DB parameter of the Modbus_Comm_Load instruction must connect to the (static) MB_DB parameter of the Modbus_Slave instruction.

In this example, the CPU rack CM1241 RS422/485 acts as the Modbus RTU slave, and the relevant programming steps are as follows:

Insert an FC function in OB1, and drag in the Modbus_Comm_Load instruction and Modbus_Slave instruction in the function, as shown in Figure 18.

Modbus RTU Programming with TIA V13 SP1

Figure 18. Dragging in Modbus RTU Slave Instruction

The parameters of the Modbus_Slave instruction are described in Table 3 below:

Modbus RTU Programming with TIA V13 SP1

Table 3. Parameters of Modbus_Slave Instruction

Note:

① It is not recommended to call the Modbus_Comm_Load instruction in the startup organization block OB100; it is recommended to call it in OB1. When called in OB1, the input bit “REQ” must be triggered on the rising edge, and in this example, this input bit uses the “FirstScan” system memory bit.

② The static variable “MODE” in the background data block of the Modbus_Comm_Load instruction describes the working mode of the PTP module. Valid working modes include:

  • 0 = Full Duplex (RS232)

  • 1 = Full Duplex (RS422) Four-Wire Mode (Point-to-Point)

  • 2 = Full Duplex (RS 422) Four-Wire Mode (Multi-Master, CM PtP (ET 200SP))

  • 3 = Full Duplex (RS 422) Four-Wire Mode (Multi-Slave, CM PtP (ET 200SP))

  • 4 = Half Duplex (RS485) Two-Wire Mode

The default data for this static variable “MODE” is 0 (RS232 Full Duplex Mode), and it needs to be modified according to the actual configuration of the CM1241 RS422/485 module. In this example, the CM1241 RS422/485 module operates in RS485 half-duplex mode, so this value needs to be modified to 4, as mentioned in Figure 12.

③ The “MB_HOLD_REG” parameter of the Modbus_Slave instruction points to the address of the data area for Modbus holding register, which supports optimized access data blocks or non-optimized (standard) data blocks, and non-optimized access data blocks are recommended.

In this example, the data area used is a non-optimized access data block, so this input parameter needs to be filled in using pointer notation, such as P#DB6.DBX0.0 WORD 100.

Download the program to the PLC, and use the Profibus DP communication cable to connect the CM1241 RS422/485 with the CM PTP serial module to test the Modbus RTU communication.

3. Common Issues FAQ

1. Are there any limitations when using the new version of the Modbus RTU command?

The new version of the Modbus RTU command requires the following conditions to communicate via the CM1241 communication module or CB1241 communication board:

a. The firmware version of the S7-1200 CPU must not be lower than V4.1;

b. The firmware of the CM1241 communication module or CB1241 communication board must not be lower than V2.1.

The S7-1200 V4.0 firmware CPU can be updated to V4.1, and the V2.0 firmware of the CM1241 communication module can also be updated to V2.1 via firmware updates.

For related firmware update methods, please refer to S7-1200 Firmware Update.

2. What is the function of the static variable “MODE” in the background data block of the Modbus_Comm_Load instruction? Why is it necessary to modify this variable in most projects?

The new version of the Modbus RTU command expands the functionality of Modbus RTU. This command not only supports the CM1241 communication module and CB1241 communication board but also supports PTP communication modules on PROFINET or PROFIBUS distributed I/O racks to achieve Modbus RTU communication.

The PTP communication module on the distributed I/O rack can support multiple working modes. For example, the ET200SP CM PtP module (Order No.: 6ES7137-6AA00-0BA0) can support RS232, RS485, and RS422 working modes.

The static variable “MODE” in the background data block of the Modbus_Comm_Load instruction is used to define the working mode of the PTP module.

The default value of “MODE” is 0, representing “Full Duplex (RS232)” working mode, and it needs to be modified according to the actual working mode in the project configuration.

3. Can the Modbus_Comm_Load instruction be called in the startup organization block OB100?

The Modbus_Comm_Load instruction is not recommended to be called in the startup organization block OB100. The new version of the Modbus RTU command expands the functionality of Modbus RTU, supporting PTP communication modules on PROFINET or PROFIBUS distributed I/O racks to achieve Modbus RTU communication.

The operating system needs to call instructions for reading and writing data records to communicate with the PTP module on the distributed I/O rack. Reading and writing data records are asynchronous instructions, and their execution requires N scanning cycles, so it is not recommended to call the Modbus_Comm_Load instruction in the startup organization block OB100.

4. How to query the error code when Modbus RTU communication fails?

For example, in the Modbus_Master instruction, when a communication error occurs, the “ERROR” output bit of the Modbus_Master instruction will become TRUE but will only remain for one scanning cycle, so it cannot be queried during TIA software program monitoring.

The error code in the “STATUS” parameter of the Modbus_Master instruction is only valid during the scanning cycle when “ERROR” = TRUE. To obtain the error code of the Modbus RTU communication error, we can program as shown in Figure 19.

Modbus RTU Programming with TIA V13 SP1

Figure 19. Obtaining STATUS

Source: Network, Copyright belongs to the original author. If there are copyright issues, please contact us for deletion. Thank you!

    Robot Learning Materials Available for Free ↓

    Modbus RTU Programming with TIA V13 SP1

    Modbus RTU Programming with TIA V13 SP1

    Modbus RTU Programming with TIA V13 SP1

Modbus RTU Programming with TIA V13 SP1

ZHINANCHE

Guidance Car Intelligent Manufacturing Training is positioned to establish an intelligent manufacturing ecosystem service system centered on robotic automation engineers, with an engineering education entry point to deliver quality engineers to the intelligent manufacturing industry. Multiple bases and branches have been established nationwide, including Nanjing, Taizhou, Shanghai, equipped with internationally advanced intelligent manufacturing equipment, including collaborative robots, SCARA robots, Delta robots, card robots, six-axis robots, UR, ABB, KUKA, FANUC, YASKAWA, and Estun brands. There are also multiple intelligent manufacturing production lines, covering from Industry 1.0 to Industry 4.0 production lines and digital twins.Under the background of Industry 4.0 and Made in China 2025, we assist traditional manufacturing enterprises in China to complete the transformation of machine replacement!

Modbus RTU Programming with TIA V13 SP1

Click “Read Original” to sign up for robot courses

Leave a Comment