摘自STEP_7_WinCC_V15_1_enUS_en-US 12 Programming the PLC 1.3 Block calls
Principles of block calls
For your blocks to be executed in the user program, they need to be called from another block.
When one block calls another block, the instructions of the called block are executed. Only when execution of the called block has been completed does execution of the calling block resume. The execution is continued with the instruction that follows on the block call.
When a block is called, you must assign values to the parameters in the block interface. By providing input parameters you specify the data with which the block is executed. By providing the output parameters you specify where the execution results are saved.
Nesting depth for blocks
The order and nesting of block calls is referred to as the call hierarchy. The permissible nesting depth for blocks depends on the CPU that is used.
Nesting depth for structures
Structures (STRUCT) and PLC data types (UDT) can be nested to a depth of 8. This nesting depth is independent of the CPU used.
Instances
After a function block is called, it needs memory for its working data. This data is referred to as an instance.
Instances have the following properties:
● Instances are always assigned to an FB.
● The structure of an instance is derived from the interface of the associated FB and can only be changed there.
● Instances are created automatically when a function block is called.
You have the following options for storing instances:
● Single instance
● Multi-instance
● Parameter instance
Single instances
An instance of a function block (FB) that is located in its own instance DB is referred to as a single instance. The instance DB thus contains the working data for an individual block call.
The use of single instances provides the following advantages:
● Reusability of the function blocks
● Good structuring options for simple programs
The following figure shows an FB that uses another FB ("FB_Valve"). "FB_Valve" is called as a single instance, i.e. it stores its data in its own instance DB.
The instance DB contains the following data:
● Block parameters
The block parameters in the "Input", "Output" and "InOut" sections form the interface of the block for the call in the program.
● Static local data
Static local data in the "Static" section is used for permanently storing intermediate results beyond the current program cycle, for example for storing the signal state for an edge evaluation.
When a single instance is called, the assigned instance data block is indicated.
The following figure shows the call of the "Called_Block" block as a single instance in LAD.The instance data block "DB_Called_Block" is above the call.
Multi-instances
When a function block (FB) calls another FB, its instance data can also be stored in the instance DB of the calling FB. This type of block call is referred to as a multi-instance.
The use of multi-instances provides the following advantages:
● Good structuring options for complex blocks
● Lower number of instance DBs
● Easy programming of local subprograms, for example for local timers or edge evaluations.
The following figure shows an FB that uses another FB ("FB_Valve"). "FB_Valve" is called as a multi-instance, i.e. it stores its data in the instance DB of the calling FB. Multi-instance data is located in the "Static" section of the calling block. In CPUs of the S7-1200/S7-1500 series, the instance can also be located in the instance DB of another function block.
You have the following options for creating a multi-instance:
● When you call an FB in the program, the "Call options" dialog appears. Here, you can specify whether you want to call the FB as a single instance, multi-instance or parameter instance.
● You declare the multi-instance directly in the interface of the calling block.
● You declare the multi-instance in the interface of another function block (S7-1200/S7-1500).
Multi-instances can also be created as an ARRAY. You can address the individual ARRAY elements using a variable index, for example when processing program loops.
You declare ARRAYs of multi-instances directly in the interface of a function block.
When a multi-instance is called, the assigned instance is indicated.
In the following example, the instance is located in the instance DB "MyOtherFB" (S7-1200/S7-1500):
In the following example, the instance is located in an ARRAY of multi-instances in instance DB "MyOtherFB" (S7-1200/S7-1500):
Parameter instances
The parameter instance provides a special option for instantiation: Here, you transfer the block instance to be used to the calling block as an in-out parameter(InOut) during runtime.
The use of parameter instances provides the following advantages:
● At runtime, you can define which instance is currently being used.
● You can process different instances iteratively in program loops.
The following figure shows the "FB_Caller", which uses another FB ("Valve"). An instance of "Valve" is transferred as a parameter instance. To do this, define an in-out parameter ("#Valve_Instance") that will be used to transfer the specific instance at runtime.
Parameter instances are defined when a function block is called: When you call an FB, a dialog appears in which you can specify whether the FB will be called as a single instance, multiinstance or parameter instance. As an alternative, you also have the option of entering parameter instances manually directly in the block interface.
Each time the higher-level FB is called ("FB_Caller" in the example) transfer an instance for the called FB (in the example "Valve"). You can transfer the following types of instances:
● Single instance
● Multi-instance
● Individual element of an ARRAY of multi-instances
Example of the use of parameter instances
The use of parameter instances allows you to transfer the instance of a function block to another block (FB or FC) for further processing, for example, a data query or an error analysis, or even have the function block executed with the passed instance.
The ARRAY multi-instances can be used to combine objects of the same type and process their instances indexed in a program loop. Indexed ARRAY elements can also be passed to another block as a parameter instance.
To illustrate this, the object "Valve" (FB_Valve) was selected in the following example. On this basis, you are provided with all required information on valve processing in a program block, "FB_ValveControl" in this case.
The two functions "FC_StatusValve" and "FC_MaintainValve" process a parameter instance of the "FB_Valve" program block which they received in the call.
In the "FB_ValveControl" program block, you use the ARRAY multi-instances on the one hand to declare the number of existing valves and, on the other hand, to manage all of the valves in a loop by means of various functions.
The following program blocks are required to implement this example:
Create "FB_Valve"
To create the SCL function block, follow these steps:
1. Double-click the "Add new block" command.
The "Add new block" dialog opens.
2. Click on the "Function block (FB)" button.
3. Enter the name "FB_Valve".
4. Select "SCL" as the language.
5. Click "OK".
6. The declaration of the block interface is based on a valve which can open and close, and could look like this, for example:
7. The program code for controlling the valves could look like this, for example:
Create "FC_StatusValve"
The declaration of the block interface with integration of the parameter instance "FB_Valve" could look like this, for example:
Write the following program code, for example:
Create "FC_MaintainValve"
The declaration of the block interface with integration of the parameter instance "FB_Valve" could look like this, for example:
Write the following program code, for example:
Create "FB_ValveControl"
Define the user constant:
The user constant "vmax" is used in this example in order to easily adapt the program to a different number of valves.
The declaration of the block interface with integration of the valve instances from "FB_Valve" could look like this, for example:
Write the following program code, for example:
Call of "FB_ValveControl" in OB 1
To call the "FB_ValveControl" function block in OB 1, follow these steps:
1. Open the "Main [OB1]" block with a double-click.
2. Use a drag-and-drop operation to add the "FB_ValveControl" function block to OB1.
Result
One valve is processed in each program cycle. The respective valve selected by index is processed, its status queried and maintenance is performed, if needed.