You are on page 1of 6

Instructions: Taking the following example you can setup a simple MODBUS master-slave communication between two S7-200

CPUs. The example concerns the MODBUS function code 6 (Write to slave-holding register) and can be considered as a basis for step-by-step changing of the parameters for the other function codes supported: 1, 2, 3, 4, 5, 15 and 16. Requirement: To operate the MODBUS protocol you must first procure and install first the STEP 7 Micro/WIN command library, then STEP 7 Micro/WIN (Entry ID 17470979). The MODBUS-Master protocol is supported only from STEP 7 Micro/WIN V4.0 Service Pack 5 onwards.

1. Hardware setup 2. Parameter matching 3. Memory addressing for the libraries 4. Holding register value transfer

1. Hardware setup The example is set up for a MODBUS communication between the Ports 0 of two S7-200 CPUs (preferably each with 2 communication interfaces). On the master side it would also be possible to select Port 1 with the corresponding library block selection "MBUS_CTRL_P1" and "MBUS_MSG_P1". Port 1 is connected in each case with a PC or PG with the Micro/WIN software. Communication between the CPUs via Port 0 is made via a PROFIBUS cable (serial connection of Pins 3, 3, 7 and 8 -> see Fig. 01). In addition, you must make sure that the encoder ground (M) is connected.

Fig. 01 2. Parameter matching For MODBUS communication, on the master side you need library blocks "MBUS_CTRL" and "MBUS_MSG", and "MBUS_INIT" and "MBUS_SLAVE" on the slave side. In Micro/WIN you create a new project for the master and the slave with the networks and parameters shown in Fig. 02. Here you must make sure that the "Baud" and "Parity" parameters are identical and that the "Slave" address of the "MBUS_MSG" block matches the "Addr" parameter of the "MBUS_INIT" block (see Fig. 02). The baud rate setting for the Port 0 interface in the "System data block" view in Micro/WIN is irrelevant for the MODBUS protocol ("Mode" = "1").

Fig. 02 The following tables describe the meanings and selection options of the library block parameters. MASTER MBUS_CTRL

Parameter Meaning EN Release Mode Baud Parity Timeout Done Error Table 01
1)

Selection options 0=PPI, 1=MODBUS 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200 0=none, 1=odd, 2=even

Protocol specification Transmission rate in kbps Parity Maximum time in ms for the slave's response 'Finished' message Error code

1)

See STEP 7 Micro/WIN Help: "Error codes for the execution of the MODBUS master operation MBUS_MSG"

MBUS_MSG

Parameter Meaning EN Release First Slave RW Addr Activation Slave address "Read" or "Write" MOBDUS initial address

Selection options

0=read, 1=write 0 .. 128 = digital outputs Q0.0 .. Q15.7 10001 .. 10128 = digital inputs I0.0 .. I15.7 30001 .. 30092 = analog inputs AIW0 .. AIW62 40001 .. 49999 = holding register 2

Count DataPtr Done Error Table 02


1)

Number of bits (0xxxx, 1xxxx) / words (3xxxx, 4xxxx) Pointer to start of variable memory 'Finished' message Error code
1)

See STEP 7 Micro/WIN Help: "Error codes for the execution of the MODBUS master operation MBUS_MSG"

SLAVE MBUS_INIT

Parameter Meaning EN Release Mode Addr Baud Parity Delay MaxIQ MaxAI MaxHold HoldStart Done Protocol specification Slave address Transmission rate in kbps Parity Signal stretching in ms Number of available digital inputs and outputs Number of available analog input words Max. number of holding register word entries Pointer to the start of the holding register (40001) 'Finished' message

Selection options 0=PPI, 1=MODBUS 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200 0=none, 1=odd, 2=even
2) 2) 2)

Error Table 03
2) 3)

Error code

3)

The maximum addresses that can be really used depends on the CPU type used and its maximum values. See STEP 7 Micro/WIN Help: "Error codes for the execution of the MODBUS slave protocol"

MBUS_SLAVE

Parameter EN Done Error Table 04


3)

Meaning Release 'Finished' message Error code

Selection options

3)

See STEP 7 Micro/WIN Help: "Error codes for the execution of the MODBUS slave protocol"

3. Memory addressing for the libraries After completion of the projects you must define the memory for the library in the Micro/WIN program block container. When reserving memory for the libraries, make sure that the area selected in each case does not overlap the variable memory area for the data (for the master: "DataPtr" + "Count" and for the slave: "HoldStart" + "MaxHold").

Fig. 03 4. Holding register value transfer After loading the projects into the appropriate CPU you can write to the selected master variable memory area by activating the table status and monitor on the slave side. When the master input I0.0 is activated, the content of variable word VW2 is sent to the slave CPU and written here to the variable word VW2. This holding register value transfer is shown in Fig. 04. The pointer "DataPtr" represents the start of the variable memory to be read. The "Count" parameter specifies the number of words to be written to the address "Addr" = "4xxxx" (holding register). The variable memory area selected will be written to the holding register start address "Addr" = "40002" ("RW" = "1").

The holding register works word for word as a placeholder to the slave variable memory area. The "HoldStart" pointer specifies the initial address of the variable memory equivalent to the holding register start address 40001. You can calculate the slave variable memory target pointer as follows: 2 * (Addr - 40001) + HoldStart = 2 * (40002 - 40001) + &VB0 = &VB2 In addition, you must make sure that the maximum number of holding register word entries "MaxHold" also covers this area: MaxHold >= Addr - 40001 + Count = 40002 - 40001 + 1 = 2

Fig. 04 More information on the STEP 7 Micro/WIN MOBDUS library is available in the S7-200 system manual ( Entry ID 1109582) and in the STEP 7 Micro/WIN Help.

You might also like