Modbus RTU Communication In Siemens TIA Portal

Modbus-RTU-Configuration-In-TIA-Portal-Example

27th  October, 2024.

In this post, we will see the concept of configuring Modbus RTU communication in Siemens TIA Portal.

Modbus RTU communication is a widely used protocol in industrial automation for connecting devices together. Be it a single PLC and multiple VFD’s, or a multiple PLC’s and a SCADA system, this communication is very cheap to implement and also effective for shorter distances (though has a very less speed compared to Ethernet and is preferred in applications where slow speed is acceptable). One of the most used software in industrial automation is TIA Portal of Siemens make. In this post, we will see how to configure Modbus RTU communication in TIA Portal.



Does the S7-1200 support Modbus RTU?


Yes, the S7-1200 series supports Modbus RTU communication. There are some select models in this series which either have an embedded port or can be used by expansion modules. Let us have a look at the below drawing sample for working on our system. We have an S7-1200 CPU (1214C) with two Modbus ports - one is an embedded Modbus RS485 port (CB1241) and the other is an expansion module (CM1241). The embedded port will be used as Modbus slave and the expansion port will be used as Modbus master. The slave is connected to a SCADA PC and the master is connected to three VFD’s. Remember that in Modbus communication, one port can be used only as a master or slave, and not both. So, we require two ports for our case. The slave will be used to send data from PLC to SCADA, and the master will be used to fetch data from three slave devices to the PLC. Whenever you are configuring and commissioning Modbus communication, please keep an USB-to-RS485 converter and Modbus Poll (which serves as master) / Modbus Slave (which serves as slave) software ready for troubleshooting.



How-To-Configure-Modbus-RTU-In-TIA-Portal

How to configure Modbus port in S7-1200?


Refer to the below image. Double click the port and you have to just enter the following parameters - baud rate, parity, data bits, wait time and stop bits. This setting is shown for CB1241. Refer to the second image for the settings of CM1241. As this port supports both RS485 and RS422 protocols, you have to first select the required protocol and then choose the remaining settings for RS485 like baud rate, parity, data bits, wait time and stop bits. Once done, there is no need to configure other settings, as Siemens already has set the best possible default parameters and it works absolutely fine with them.


CB1241-Modbus-Settings

CM1241-Modbus-Settings

How to connect Modbus to Siemens PLC?


Once you have configured the Modbus ports, you now need to write the program for the same to connect Modbus communication with Siemens PLC. You need to have three blocks in our program for the application we require - MB_COMM_LOAD, MB_MASTER and MB_SLAVE. MB_COMM_LOAD defines the configuration of the port according to the hardware you configured earlier. We then have to link a variable to this block, which will then be used to link master or slave blocks used next in the program. Means for example, if you use a slave block in our program and link this variable to that block, that means the particular port will function as a slave. Refer to the below images step by step. We will first write the program for Master port.



1. MB_Comm_Load:


In the main OB, you have to use the MB_COMM_LOAD block. You can take these blocks from the communication section as shown in the image. The REQ pin will be linked to a system variable named First_Scan, which will execute only during warm start or cold start of the PLC. Then, this block is not required in the program. The PORT pin will be linked to the Modbus port you want to use. You can find the name of this port by double clicking that port in the hardware configuration and then clicking system constants. Enter the values in BAUD and PARITY that you have set in the hardware configuration earlier. The MB_DB pin will be linked to a data block that will be used as reference for the master block in the next part. You can optionally link variables to the output pins of this block, and it is not compulsory.


MB-Comm-Load-TIA-Portal

2. MB_Master:

Next, we will use master blocks in the program as shown below. Remember to use the same data block for as many master blocks that you use in the program. The REQ pin needs to be called every time in a pulse, so we use a counter in the logic (named MFM_Trigger) which has a setpoint of 6. The counter is incremented every 10 milliseconds. When the current value of this counter is 0, this block will be executed. The MB_ADDR pin is used to enter the slave address of the VFD. The MODE pin is used to determine whether you are reading or writing from that VFD. The DATA_ADDR pin is used to give the starting register address from where the data will be written or read. The DATA_LEN pin is used to depict the total number of registers from the starting address that will be communicated. The DATA_PTR pin needs to be linked to the local variable in the PLC from where the data will be read or written. If you see the next two images, you will find that the instance name or data block is the same; only the linking of the input pins will vary according to your actual device. So, the important points to note when configuring master blocks are - call every instance in different intervals or else it will not communicate, each slave address will have it’s own instance, if there is a gap between lengths in a single slave device, use different instances for such and each slave instance needs to have it’s own local tag in the PLC.



What-Is-The-Description-Of-MB-Master

What-Is-The-Description-Of-MB-Master

How-To-Configure-MB-Master-In-TIA-Portal


3. MB_Slave:


Next, we will use the slave block in the program. Another block of MB_COMM_LOAD will come in the program, with it’s corresponding port settings as discussed earlier. The MB_SLAVE block will be defined with the data block that you named in MB_COMM_LOAD. The MB_ADDR pin will define the slave address of the Modbus port of the PLC. The MB_HOLD_REG pin will be linked to the local PLC tag, where registers will be read or written by the Master SCADA PC. This variable, if using data block, needs to untick the option general - attributes - optimized block access in it’s properties.


MB-Comm-Load-Code

Mb-Slave-TIA-Portal




Also, have a look at the below image in the holding register data block we used. The offset values define the holding register address. So, 0.0 means 40001, 2.0 means 40002, 4.0 means 40003 and so on (because 0.0 to 1.0 depicts one integer as Siemens uses byte formatting). 


Holding-Register-TIA-Portal-Example

I have covered a general theory related to Modbus RTU communication in TIA Portal. I have also not attempted to cover all the topics, as it can vary from systems to systems. Once you are familiar with this engineering, you can easily tackle all types of problems in it. 


Thank you for reading the post. I hope you liked it and will find a new way in this type of technology.


Comments