4.2 Program Unit
Program Unit (PU) contains clearly defined program parts (FBs and FCs in them can be called and executed multiple times), and its purpose is to modularize and structure. PRG can only be called uniquely in Baosky IDE.
- Users can select "Add New Block" under "PLC Software" and "Program Unit" in the project tree, and then the pop-up window can select program (PRG), function block (FB) or function (FC), and the corresponding programming language. After adding, you can view the corresponding attributes in the program unit brackets in the project device tree. PRG is the program, FB is the function block, and FC is the function.
- PU components: PU type and naming, variable declaration part, code instruction part
- Local addressing of bit string type variables
- Data type conversion
4.2.1 Function block (FB)
- Function block converts repeatedly used program blocks into a universal component, which can be called by any programming language in the program
- The call to the function block becomes an instance. It stores the input, output and input/output, internal static variable parameters in a structure variable named variable name of the function block instance with the global variable attribute, so that after the execution of the block, some values are still valid. Therefore, function blocks are also called blocks with memory.
- Temporary variables can be used within function blocks. Temporary variables are not stored in the function block instance name variable, but in a loop
- The execution logic of the function block constitutes its own object behavior characteristics. Therefore, for the input variable values of the same parameters, since there may be different internal state variables, it is of course possible to obtain different calculation results. In the control system, the function block can be a certain control algorithm. For example, the PID function block is used for closed-loop control, and other function blocks can be used for counters, ramps, filtering, etc.
Function block declaration
- Function block declaration is also a function block type declaration, which is similar to variable declaration in a function function manner.
- Define parameters based on the function block UI interface. Its structure includes variable names and related data types such as INPUT, INOUT, OUTPUT, etc. You can choose to declare variables by yourself
- Specifically, Baosky IDE have the following four methods:
- Make a variable (VAR_GLOBAL) declaration for the function block instance variable in GVS; (single instance, global variable)
- Declare variables (VAR) for function block instance variables in other FBs; (multiple instances, FB static variables) Does not support nesting empty member FBs in FBs
- Declare variables (VAR_IN_OUT) for function block instance variables in other FBs; (parameter instances, FB interface variables) Does not support FBs with RETAIN members, and does not support empty member FBs
- Declare a variable (VAR_IN_OUT) for the function block instance variable in FC; (parameter instance, FB interface variable) Does not support FB with RETAIN member, and does not support empty member FB
- The function block name of FB cannot be declared with a return value like the FC function name. It can only be declared without a return value.
Function block call
-
Function block FB can be called by function FC, function block FB, and program PRG
-
When calling FB in textual language ST, it should contain the FB instance name followed by a parameter list
-
When calling FB in graphical language LD, the FB instance name should be located above the function block
-
Function block instance name, which must be declared as a structure variable of the function block data type in Baosky IDE
-
The assignment of inputs and access to outputs of function blocks can be:
- Processed immediately when the function block is called
- Separate from the call
- Unassigned or unconnected function block inputs should retain the initialization value or the value of the latest previous call
-
The name of the function block instance can be used as an interface variable called by other function block instances. At that time, it can only be the VAR_IN_OUT interface variable.
-
FB block instance output value, its structural element name (i.e. member variable) can be passed to other FB blocks for internal access through VAR_INPUT, but cannot be modified by other FBs
-
If an instance of function block A is passed into another function block B through the VAR_IN_OUT structure, another function block B can call function block A internally.
-
Only variables can be passed into the function block through the VAR_IN_OUT construct. And when calling the FB instance, VAR_IN_OUT must be connected to the variable
4.2.2 Function (FC)
Function is a block of code without memory. Since there is no data memory in which the block parameter values can be stored. Therefore, when calling a function, all formal parameters must be assigned actual parameters. Using functions can reduce code duplication rate, make the program modular, and facilitate the reading, modification and improvement of the program.
Function declaration rules
- Return value, function name, formal parameters, function body
Function usage specifications
- Functions with return values can be called in expressions or statements
- Functions without return values should not be called within expressions
- Function (FC) can be called by function FC, function block (FB), and program (PRG)
- Function blocks are different from functions. Function blocks have a storage area, while functions have no storage area.
- An important feature of functions (FC) is that they cannot use internal variables to store values. Internal variables are temporary variables.
- Global variables can be used inside functions (FC)
- The function return value data type only supports basic data types
4.2.3 Program (PRG)
Overview
Program (PRG) is a structure used to organize program code. It usually serves as the main program in the project, responsible for organizing and calling other program units (such as FB function blocks, FC functions, etc.). By using different TASKs to call the program (PRG), the program (PRG) can be executed according to specified rules, thereby ensuring that each part of the project can be executed in a predetermined logical sequence.
- The program can only be called by TASK