Modbus Communication Case in PLC Programming

Modbus Communication Case in PLC Programming

1. Hardware Setup
The Modbus communication in the program is conducted between the communication port 0 of two S7-200 CPUs (it is best for each CPU to have two communication ports). On the master side, you can also use the corresponding library files “MBUS_CTRL_P1” and “MBUS_MSG_P1” to communicate through communication port 1. Communication port 1 establishes a connection with PG or PC via Micro/WIN, while the communication port 0 of the two CPUs connects through Profibus cable (the cable pin connections are 3, 3, 8, 8 -> see Figure 01). Additionally, it is necessary to ensure that they are logically connected.

Modbus Communication Case in PLC Programming

2. Parameter Matching
For MODBUS communication, the master side requires the program libraries “MBUS_CTRL” and “MBUS_MSG”, while the slave side requires the program libraries “MBUS_INIT” and “MBUS_SLAVE”. In Micro/WIN, you need to create a new project for both master and slave, as shown in Figure 02. It is essential to ensure that the parameters for “Baud” and “Parity” on the master and slave sides are consistent, and the “Slave” address in the program block “MBUS_MSG” must match the “Addr” set in the program block “MBUS_INIT” (see Figure 02). The baud rate set for communication port 0 in the “system block” of Micro/WIN is irrelevant to the MODBUS protocol (“Mode” = “1”).

Modbus Communication Case in PLC Programming

The table below lists the various parameter options in the program block and their meanings. Master station MBUS_CTRL

Modbus Communication Case in PLC Programming

Modbus Communication Case in PLC Programming

MBUS_MSG

Modbus Communication Case in PLC Programming

Modbus Communication Case in PLC Programming

Modbus Communication Case in PLC Programming

Modbus Communication Case in PLC Programming

Slave station MBUS_INIT

Modbus Communication Case in PLC Programming

MBUS_SLAVE

Modbus Communication Case in PLC Programming

3. Library Storage Address
After completing the project, it is necessary to define the storage address of the library in Micro/WIN. Once the storage area is defined, it must ensure that it cannot be used by other programs under any circumstances (Master side: “DataPtr” + “Count”; Slave side: “HoldStart” + “MaxHold”).

Modbus Communication Case in PLC Programming

4. Transmission of Holding Register Values
After downloading the program to the corresponding CPU, you can assign values to the V storage area on the master side in the status table and then monitor the changes in values on the slave side. When the I0.0 of the master is enabled, the contents of VW2 are sent to the slave and written into the VW2 of the slave. The transmission of holding register values is shown in Figure 04. The pointer “DataPtr” represents the starting address of the V area being read. The parameter “Count” specifies the number of words to be written to the address “Addr” = “4xxxx” (holding register). The corresponding variable in the V storage area will be written to the starting address of the holding register “Addr” = “40002” (“RW” = “1”). Holding registers are transmitted in units of words, corresponding to the V area address of the slave. The pointer “HoldStart” specifies the initial address of the V storage area corresponding to the holding register starting address 40001. The target pointer for the V area of the slave can be calculated using the following formula: 2 * (Addr – 40001) + HoldStart = 2 * (40002 – 40001) + &VB0 = &VB2. Additionally, it is necessary to ensure that the data area to be written by the master is included within the data area defined by “MaxHold”: MaxHold >= Addr – 40001 + Count = 40002 – 40001 + 1 = 2.

Modbus Communication Case in PLC Programming

End

Source: Network, This article is reprinted from the internet, and the copyright belongs to the original author. However, due to numerous reprints, it is impossible to confirm the true original author, hence the source is only indicated. If the videos, images, or text used in this article involve copyright issues, please inform us immediately, and we will confirm the copyright based on the materials you provide and pay remuneration according to national standards or delete the content immediately! The content of this article reflects the views of the original author and does not represent the views of this public account or its authenticity.

Leave a Comment