Professional Documents
Culture Documents
STEP 7–Micro/WIN Instruction Libraries makes communicating to Modbus master devices easier by
including pre-configured subroutines and interrupt routines that are specifically designed for Modbus
communications. With the Modbus Slave Protocol Instructions, you can configure the S7-200 to act as a
Modbus RTU slave device and communicate to Modbus master devices.
You find these instructions in the Libraries folder of the STEP 7–Micro/WIN instruction tree. With these
new instructions you can make the S7-200 act as a Modbus slave. When you select a Modbus Slave
instruction, one or more associated subroutines are automatically added to your project.
In This Chapter
Requirements for Using the Modbus Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
Initialization and Execution Time for the Modbus Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
Modbus Addressing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Using the Modbus Slave Protocol Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Instructions for the Modbus Slave Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
329
S7-200 Programmable Controller System Manual
- Initializing the Modbus Slave Protocol dedicates Port 0 for Modbus Slave Protocol communications.
When Port 0 is being used for Modbus Slave Protocol communications, it cannot be used for any
other purpose, including communications with STEP 7–Micro/WIN. The MBUS_INIT instruction
controls assignment of Port 0 to Modbus Slave Protocol or PPI.
- The Modbus Slave Protocol instructions affect all of the SM locations associated with Freeport
communications on Port 0.
- The Modbus Slave Protocol instructions use 3 subroutines and 2 interrupts.
- The Modbus Slave Protocol instructions require 1857 bytes of program space for the two Modbus
Slave instructions and the support routines.
- The variables for the Modbus Slave Protocol instructions require a 779-byte block of V memory. The
starting address for this block is assigned by the user and is reserved for Modbus variables.
Tip
To change the operation of Port 0 back to PPI so that you can communicate with STEP 7-Micro/WIN,
use another MBUS_INIT instruction to reassign Port 0.
You can also set the mode switch on the S7-200 to STOP mode. This resets the parameters for Port 0.
The scan time is extended when the MBUS_SLAVE subroutine services a request. Since most of the time
is spent calculating the Modbus CRC, the scan time is extended by about 650 microseconds for every
byte in the request and in the response. A maximum request/response (read or write of 120 words)
12 extends the scan time by approximately 165 milliseconds.
330
Using the Modbus Protocol Library Chapter 12
Modbus Addressing
Modbus addresses are normally written as 5 or 6 character values containing the data type and the offset.
The first one or two characters determine the data type, and the last four characters select the proper
value within the data type. The Modbus master device then maps the addresses to the correct functions.
The following addresses are supported by the Modbus Slave instructions:
- 000001 to 000128 are discrete outputs mapped to Q0.0 - Q15.7
- 010001 to 010128 are discrete inputs Table 12-1 Mapping Modbus Address to the S7-200
mapped to I0.0 - I15.7
Modbus Address S7-200 Address
- 030001 to 030032 are analog input 000001 Q0.0
registers mapped to AIW0 to AIW62
000002 Q0.1
- 040001 to 04xxxx are holding registers
000003 Q0.2
mapped to V memory.
... ...
All Modbus addresses are one-based.
Table 12-1 shows the mapping of Modbus 000127 Q15.6
addresses to the S7-200 addresses. 000128 Q15.7
You should assign a starting V location for the table which occupies 779 bytes. Be sure that the
assignment of the Modbus Slave symbols do not overlap with the V memory assigned to the Modbus
holding registers with the HoldStart and MaxHold parameters on the MBUS_INIT instruction. If there is any
overlap of the memory areas, the MBUS_INIT instruction returns an error.
331
S7-200 Programmable Controller System Manual
1. Insert the MBUS_INIT instruction in your program and execute the MBUS_INIT instruction for one
scan only. You can use the MBUS_INIT instruction either to initiate or to change the Modbus
communications parameters.
When you insert the MBUS_INIT instruction, several hidden subroutines and interrupt routines are
automatically added to your program.
2. Assign a starting address for the 779 bytes of consecutive V memory required for Modbus Slave
Protocol instructions.
3. Place only one MBUS_SLAVE instruction in your program. This instruction is called every scan to
service any requests that have been received.
4. Connect the communications cable between Port 0 on the S7-200 and the Modbus master devices.
Caution
Interconnecting equipment with different reference potentials can cause unwanted currents to flow
through the interconnecting cable. These unwanted currents can cause communications errors or
damage equipment.
Ensure that all equipment that is connected with a communications cable either shares a common
circuit reference or is isolated to prevent unwanted current flows.
The accumulators (AC0, AC1, AC2, AC3) are utilized by the Modbus slave instructions and appear in the
Cross Reference listing. Prior to execution, the values in the accumulators of a Modbus Slave instruction
are saved and restored to the accumulators before the Modbus Slave instruction is complete, ensuring
that all user data in the accumulators is preserved while executing a Modbus Slave instruction.
The Modbus Slave Protocol instructions support the Modbus RTU protocol. These instructions utilize the
Freeport utilities of the S7-200 to support the most common Modbus functions. The following Modbus
functions are supported:
12 2 Read single/multiple contact (discrete input) status. Function 2 returns the on/off status of any number of
input points (Is).
3 Read single/multiple holding registers. Function 3 returns the contents of V memory. Holding registers are
word values under Modbus and allow you to read up to 120 words in one request.
4 Read single/multiple input registers. Function 4 returns Analog Input values.
5 Write single coil (discrete output). Function 5 sets a discrete output point to the specified value. The point
is not forced and the program can overwrite the value written by the Modbus request.
6 Write single holding register. Function 6 writes a single holding register value to the V memory of the
S7-200.
15 Write multiple coils (discrete outputs). Function 15 writes the multiple discrete output values to the Q image
register of the S7-200. The starting output point must begin on a byte boundary (for example, Q0.0 or
Q2.0) and the number of outputs written must be a multiple of eight. This is a restriction for the Modbus
Slave Protocol instructions. The points are not forced and the program can overwrite the values written by
the Modbus request.
16 Write multiple holding registers. Function 16 writes multiple holding registers to the V memory of the
S7-200. There can be up to 120 words written in one request.
332
Using the Modbus Protocol Library Chapter 12
The parameter Parity is set to match the parity of the Modbus master. The accepted values are:
- 0-no parity 12
- 1-odd parity
- 2-even parity
The parameter Delay extends the standard Modbus end-of-message timeout condition by adding the
specified number of milliseconds to the standard Modbus message timeout. The typical value for this
parameter should be 0 when operating on a wired network. If you are using modems with error correction,
set the delay to a value of 50 to 100 milliseconds. If you are using spread spectrum radios, set the delay to
a value of 10 to 100 milliseconds. The Delay value can be 0 to 32767 milliseconds.
The parameter MaxIQ sets the number of I and Q points available to Modbus addresses 00xxxx and
01xxxx at values of 0 to 128. A value of 0 disables all reads and writes to the inputs and outputs. The
suggested value for MaxIQ is 128, which allows access to all I and Q points in the S7-200.
333
S7-200 Programmable Controller System Manual
The parameter MaxAI sets the number of word input (AI) registers available to Modbus address 03xxx at
values of 0 to 32. A value of 0 disables reads of the analog inputs. The suggested value for MaxAI to allow
access to all of the S7-200 analog inputs, is as follows:
The parameter HoldStart is the address of the start of the holding registers in V memory. This value is
generally set to VB0, so the parameter HoldStart is set to &VB0 (address of VB0). Other V memory
addresses can be specified as the starting address for the holding registers to allow VB0 to be used
elsewhere in the project. The Modbus master has access to MaxHold number of words of V memory
starting at HoldStart.
When the MBUS_INIT instruction completes, the Done output is turned on. The Error output byte contains
the result of executing the instruction. Table 12-5 defines the error conditions that could result from
executing the instruction.
12
334
Using the Modbus Protocol Library Chapter 12
MBUS_SLAVE Instruction
The MBUS_SLAVE instruction is used to service a request from the
Modbus master and must be executed every scan to allow it to
check for and respond to Modbus requests.
The instruction is executed on each scan when the EN input is on.
The MBUS_SLAVE instruction has no input parameters.
The Done output is on when the MBUS_SLAVE instruction
responds to a Modbus request. The Done output is turned off if
there was no request serviced.
The Error output contains the result of executing the instruction.
This output is only valid if Done is on. If Done is off, the error
parameter is not changed. Table 12-5 defines the error conditions
that could result from executing the instruction.
335
S7-200 Programmable Controller System Manual
12
336