Method and device for expanding C language function through PLC programming configuration
By calling C language functions and generating dynamic libraries in PLC programming and configuration software, the problem in the existing technology that PLC programming and configuration software cannot directly use C/C++ programs is solved, and the dynamic loading and operation of C language functions is realized, improving It improves the practicality and flexibility of PLC programming.
Patent Information
- Application Number
- CN202410150287.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-02-02
- Publication Date
- 2024-10-15
- Estimated Expiration
- 2044-02-02
AI Technical Summary
In the process of migrating and transforming existing PLC programming and configuration software to the original non-PLC control system, it cannot directly use or quickly transplant the existing reliable and mature programs written in C/C++ language, and complex logic processing requires calling multiple C/C++ libraries. function, resulting in a large workload and resource investment, and the generated logic call code relies on the original PLC program compiler, which is not very practical.
By calling the C language function in the IEC61131-3 program, create a new C language user function, declare and pre-compile the function number automatically according to the IEC61131-3 specification, generate the registered function address code, and use the cross-compilation tool chain of the PLC runtime to generate extended C The language user function dynamic library initially loads and registers user functions before starting the PLC program task to realize the dynamic loading and operation of C language functions.
It realizes the calling of extended C language functions in PLC programming configuration, improves the practicality and flexibility of programming, breaks through the obstacles between C language functions and IEC61131-3 program variable access, and supports the dynamic loading of multiple C language user functions. It simplifies the C function calling process and improves the scalability of PLC program resources.
Smart Images

Figure CN118778536A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of industrial automation control, and in particular to a method and device for extending C language functions by PLC programming configuration. Background Art
[0002] PLC programming configuration software that complies with IEC61131-3 specifications supports IL instruction list, ST structured text, LD ladder diagram, FBD function block diagram, SFC sequence function Figure 5 A programming language whose program organization unit (POU) includes function, function block and program, where a program can call a function block or function. Although ST structured files are similar to C language, they are obviously different from C language in syntax, conditional statements and loop statements, and lack support for objects such as pointers and classes.
[0003] Only PLC programming configuration and system that conform to IEC61131-3 are supported. Firstly, in the process of migration and transformation to the original non-PLC control system, the existing reliable and mature programs written in C / C++ language cannot be directly used or quickly transplanted. Secondly, the complex logic processing process often requires calling more C / C++ library functions, which makes it difficult to complete all the programming on conventional PLC programming software. Therefore, it is inevitable that configuration engineers and PLC system manufacturers will invest a lot of work and resources in the process of upgrading and transformation.
[0004] Similar patents include "202310514189.5 A firmware library calling method, device, equipment and storage medium". This patent supports the IEC61131-3 language to call the firmware library function of the C language branch. In the PLC program programming software, the function blocks written in C language and solidified into the PLC system can be used. First, this method is mainly used for the construction of the built-in firmware library of PLCCPU, and is aimed at users involved in core programming, rather than ordinary PLC users; secondly, the existing configuration software is greatly changed, involving two programming configuration modes, firmware development mode and engineering mode, which changes the existing PLC programming habits. Third, the generated logic call code needs to rely heavily on the original PLC program compiler to use C language as the intermediate code. At present, most of the existing PLC logic compilers use the lower-level IL code or other custom low-level languages as the intermediate code, which is not very practical. Fourth, the function block that complies with the IEC61131-3 specification contains input parameters, output parameters (can be multiple), and the function (with 1 return value). This patent incorrectly replaces the output parameter of the function block with the function return value of the C language, resulting in only 1 output parameter being implemented. The implementation of the function block export interface does not comply with the IEC61131-3 specification. Summary of the invention
[0005] The purpose of the present invention is to solve the shortcomings existing in the above-mentioned prior art, and to provide a method and device for PLC programming configuration to expand C language functions, the method includes calling C language function method in IEC61131-3 program, and running C language function method in PLC runtime, the device includes PLC program programming host computer software supporting C language function programming, and PLC device CPU module supporting dynamic loading and running of C language function.
[0006] A method for extending C language functions by PLC programming configuration includes the following steps:
[0007] Create a new C language user function in the PLC programming configuration software, declare the C language user function in accordance with the IEC61131-3 standard form, use the C language to write a specific implementation of the function, precompile the code that automatically assigns the C language function number and generates the registration function address, and generate an extended C language user function dynamic library through the cross-compilation tool chain when the PLC is running;
[0008] The C language user function is directly called in the user program of the PLC programming configuration software, the function call is converted into a call according to the C language function number during the pre-compilation, and the compilation and linking generates program resources when the PLC is running;
[0009] Before the PLC program task is started, the PLC runtime initializes and loads the C language user function dynamic library and calls the CLibInit function to register the user function number and address mapping table; when the PLC program task runs and executes the user function number call instruction, it first queries the address mapping table, and then jumps to the specific function address to execute the compiled C language user function.
[0010] Furthermore, creating a new C language user function in the PLC programming configuration software specifically includes:
[0011] Select C as the programming language, input the name of the C language user function, and declare the return value type, input parameters and output parameters of the C language user function.
[0012] Furthermore, after completing the declaration of the C language user function, the PLC programming configuration software automatically generates the code of the basic framework of the C language user function to facilitate processing of the return value, the input parameters and the output parameters.
[0013] Furthermore, the specific implementation of the function written in the C language includes:
[0014] The logic program is written in C language. If the cross-compilation tool chain supports C++ language, the user function here also supports the use of C++ language. The programming environment includes IEC61131-3 variable access interface, network programming interface, file operation interface and peripheral access interface.
[0015] Furthermore, the pre-compilation automatically assigns C language function numbers and generates registration function address codes specifically including:
[0016] The user is allowed to construct multiple C language user functions. Subsequent constructions will add one to the current function number in sequence to generate the C language user function library registration function address code. The function is a C language user function dynamic library. The PLC programming configuration software loads the current C language user function dynamic library and executes the registration function during initialization.
[0017] Furthermore, the C language user function is directly called in the user program of the PLC programming configuration software, and the user program of the PLC programming configuration software includes IEC61131-3 programs, IEC61131-3 functions and IEC61131-3 function blocks, as well as ST programs and LD programs that are consistent with the external characteristics of the IEC61131-3 functions.
[0018] Furthermore, when the function call is converted into a call according to the C language function number during the pre-compilation, the function input, output and return value are transmitted and interacted through the data segment of the C language user function.
[0019] Furthermore, before the PLC program task is started, the program resources of the PLC runtime and the C language user function dynamic library are downloaded to the main controller module of the PLC device.
[0020] Furthermore, the PLC runtime is responsible for executing the operating environment of the logic program generated by the PLC program programming and compilation, and adopts IEC61131-3 as the programming language specification of the PLC program programming software. The IEC61131-3 program supports cyclic execution, periodic execution and event-triggered execution.
[0021] A device for expanding C language functions by PLC programming configuration includes: upper computer PLC programming configuration software, lower computer PLC equipment main controller module;
[0022] The PLC programming configuration software is oriented to the programmable controller PLC, which is a digital operation controller with a microprocessor and is used for automatic control. The PLC programming configuration software includes logic programming, program debugging, variable observation, equipment configuration, and simulator, and provides a user programming language using the IEC61131-3 standard.
[0023] The PLC device main controller module is responsible for executing the PLC program tasks, and has built-in PLC runtime software and C language user function dynamic library. The PLC runtime software is responsible for executing the operating environment of the logic program generated by PLC program programming and compilation, and the C language user function dynamic library is used to register and call the C language user function.
[0024] Compared with the prior art, the present invention has the following beneficial effects:
[0025] (1) The present invention encapsulates C functions into user functions that comply with the IEC61131-3 specification, thereby realizing a programming method that mainly calls extended C language functions using IEC61131-3 programs, thereby improving the practicality and flexibility of PLC programming configuration;
[0026] (2) When the PLC is running, it provides a system function interface for accessing input, output and return values and IEC61131-3 program variables, breaking through the barriers to variable interaction between the C language function and the IEC61131-3 program, making it easier to monitor the operation of the C language function in the existing PLC configuration and programming software.
[0027] (3) The PLC programming configuration project that supports extended C language functions generates PLC program resources and extended C function dynamic libraries. After being downloaded to the main controller module of the PLC, the C function dynamic library is loaded during initialization when the PLC is running, and the relevant C function address registration is performed, thus realizing the dynamic loading of the extended C language function library;
[0028] (4) By using the user C function index number instead of directly calling the function, the extended C function can be dynamically called when the PLC is running and the user C function name cannot be clearly identified. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] The accompanying drawings are used to provide further understanding of the present invention and constitute a part of the specification. They are used to explain the present invention together with the embodiments of the present invention and do not constitute a limitation of the present invention.
[0030] In the attached picture:
[0031] Figure 1 A flow chart of a method for extending C language functions by PLC programming configuration of the present invention;
[0032] Figure 2 A structural block diagram of a device for extending C language functions for PLC programming configuration of the present invention;
[0033] Figure 3 This is a diagram showing an example of a C language user function being declared in a PLC configuration programming software according to the present invention;
[0034] Figure 4 This is an example diagram of directly calling a C language user function in an ST program of a PLC programming configuration software of the present invention;
[0035] Figure 5 This is an example diagram of directly calling a C language user function in an LD program of a PLC programming configuration software of the present invention; DETAILED DESCRIPTION
[0036] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0037] Those skilled in the art will appreciate that, unless otherwise stated, the singular forms "a", "an", "said" and "the" used herein may also include plural forms. It should be further understood that the term "comprising" used in the specification of the present invention refers to the presence of the features, integers, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, integers, steps, operations, elements, components and / or groups thereof.
[0038] The specific implementation of the present invention is described below with reference to the accompanying drawings and embodiments.
[0039] Example 1
[0040] like Figure 1 As shown, a method for extending C language functions of PLC programming configuration provided in this embodiment includes the following steps:
[0041] Create a new C language user function in the PLC programming configuration software, declare the C language user function in accordance with the IEC61131-3 standard form, use the C language to write a specific implementation of the function, precompile the code that automatically assigns the C language function number and generates the registration function address, and generate an extended C language user function dynamic library through the cross-compilation tool chain when the PLC is running;
[0042] The C language user function is directly called in the user program of the PLC programming configuration software, the function call is converted into a call according to the C language function number during the pre-compilation, and the compilation and linking generates program resources when the PLC is running;
[0043] Before the PLC program task is started, the PLC runtime initializes and loads the C language user function dynamic library and calls the CLibInit function to register the user function number and address mapping table; when the PLC program task runs and executes the user function number call instruction, it first queries the address mapping table, and then jumps to the specific function address to execute the compiled C language user function.
[0044] Wherein, creating a new C language user function in the PLC programming configuration software specifically includes:
[0045] Select C as the programming language, input the name of the C language user function, and declare the return value type, input parameters and output parameters of the C language user function.
[0046] Specifically, after completing the declaration of the C language user function, the PLC programming configuration software automatically generates the code of the basic framework of the C language user function to facilitate processing of the return value, the input parameter and the output parameter.
[0047] Furthermore, the specific implementation of the function written in the C language includes:
[0048] The logic program is written in C language. If the cross-compilation tool chain supports C++ language, the user function here can also use C++ language. The programming environment includes IEC61131-3 variable access interface, network programming interface, file operation interface and peripheral access interface.
[0049] Preferably, the pre-compilation automatically assigns C language function numbers and generates registration function address codes specifically including:
[0050] The user is allowed to construct multiple C language user functions. Subsequent constructions will add one to the current function number in sequence to generate the C language user function library registration function address code. The function is a C language user function dynamic library. The PLC programming configuration software loads the current C language user function dynamic library and executes the registration function during initialization.
[0051] Furthermore, the C language user function is directly called in the user program of the PLC programming configuration software, and the user program of the PLC programming configuration software includes IEC61131-3 programs, IEC61131-3 functions and IEC61131-3 function blocks, as well as ST programs and LD programs that are consistent with the external characteristics of the IEC61131-3 functions.
[0052] Furthermore, when the function call is converted into a call according to the C language function number during the pre-compilation, the function input, output and return value are transmitted and interacted through the data segment of the C language user function.
[0053] Wherein, before the PLC program task is started, the program resources of the PLC runtime and the C language user function dynamic library are downloaded to the main controller module of the PLC device.
[0054] Preferably, the PLC runtime is responsible for executing the operating environment of the logic program compiled and programmed by the PLC program, and adopts IEC61131-3 as the programming language specification of the PLC program programming software. The IEC61131-3 program supports cyclic execution, periodic execution and event-triggered execution.
[0055] Specifically, Figure 1 As shown, the specific steps of the method for extending the C language function of the PLC programming configuration of this embodiment are as follows:
[0056] (1) Create a new user function of C language type in the PLC programming software, select "C language" as the programming language, enter the user function name, and declare the function's return value type, input parameters, and output parameters;
[0057] The user function MinDINT, which is used to obtain the minimum value of two input parameters, is used as an example for auxiliary explanation. It returns the minimum value of two inputs, contains one output parameter, and gives the size comparison result of the input parameters.
[0058] An example of declaring a C language user function in a PLC programming software is as follows: Figure 3 shown.
[0059] (2) The PLC programming configuration software automatically generates C language user function basic framework code, which is convenient for users to process input and output parameters and function return values;
[0060] After the user function declaration in C language is completed, the software can automatically generate the following function basic framework sample code as follows:
[0061]
[0062]
[0063] As shown in the above code, the automatically generated code contains input, output parameters and return value definitions. The software should provide input, output and return values and IEC61131-3 program variable access system functions, including GET read value operation function and SET write value operation function. Variable reading and writing involve data types. System functions can be defined according to variable types, such as GETBIT, GETINT, GETDINT, SETBIT, SETINT, SETDINT, etc.
[0064] (3) The software automatically generates IEC61131-3 user function declarations, which serve as interface declarations for user functions provided by existing IEC61131-3 user programs;
[0065] The user function generates an interface that complies with the IEC61131-3 specification. The function declaration example is as follows:
[0066]
[0067] (4) User function definition, write the logic program in C language. The user function is compiled by the cross-compilation tool chain during PLC runtime. If the tool chain supports C++ language, the user function here can use C++ language. The programming environment should include commonly used IEC61131-3 variable access, network programming, file operation, peripheral access and other interfaces.
[0068] The following is an example of a user function definition:
[0069]
[0070] (5) Assign function numbers. For example, if the function number starts from 1000, the user is allowed to build multiple user C language functions, and the subsequent function numbers are incremented by 1.
[0071] (6) Generate user C language function library function registration code.
[0072] The following is an example of function registration code:
[0073]
[0074] This function is a user C function dynamic library. When the PLC is running, it loads the current user C function dynamic library and executes the registration function during initialization.
[0075] (7) Call the PLC runtime cross-compilation tool chain to generate a C function dynamic library
[0076] (8) Calling user functions written in C language in existing IEC61131-3 programs / functions / function blocks
[0077] An example of calling a C language function in an ST program is as follows Figure 4 As shown, it is consistent with the external characteristics of the standard IEC61131-3 function; the example of calling a C language function in an LD program is as follows Figure 5 As shown, it is also consistent with the standard IEC61131-3 function external characteristics.
[0078] (9) Precompile the C function call in the program organization unit POU and convert it into a function number call;
[0079] (10) Compile and link to generate PLC runtime program resources
[0080] (11) Download the "runtime program resources" and "C function dynamic library" to the CPU module of the PLC device;
[0081] (12) Before starting the PLC task, the PLC needs to initialize and load the C function dynamic library when running, and call the CLibInit function to register the functions written in the user's C language;
[0082] (13) When the PLC task runs and executes a C function call statement, it first queries the C function address and then jumps to the corresponding address to execute the C function.
[0083] The program organization unit POU includes three types of PLC logic programs in accordance with the IEC61131-3 standard: function, function block and program.
[0084] like Figure 2 As shown, a device for executing the above method to extend the C language function of PLC programming configuration includes: upper computer PLC programming configuration software, lower computer PLC device main controller module;
[0085] The PLC programming configuration software is oriented to the programmable controller PLC, which is a digital operation controller with a microprocessor and is used for automatic control. The PLC programming configuration software includes logic programming, program debugging, variable observation, equipment configuration, and simulator, and provides a user programming language using the IEC61131-3 standard.
[0086] The PLC device main controller module is responsible for executing the PLC program tasks, and has built-in PLC runtime software and C language user function dynamic library. The PLC runtime software is responsible for executing the operating environment of the logic program generated by PLC program programming and compilation, and the C language user function dynamic library is used to register and call the C language user function.
[0087] The PLC programming configuration software based on the IEC61131-3 standard has expanded the C / C++ user programming language on the basis of the five supported languages of IL, ST, LD, FBD, and SFC. It is exported as a function interface that complies with the IEC61131-3 specification, without changing the existing programming habits. It can be directly called and debugged by the existing PLC programming configuration language that complies with the specification. It is consistent with the external characteristics of the existing standard PLC functions, and has a higher degree of practicality and universality. The cross-compilation tool chain during PLC runtime is directly used to compile user C / C++ language functions, which can more conveniently call interfaces such as variable access, network programming, file operations, and peripheral access.
[0088] The invention discloses a method and device for extending C language functions by programming configuration of a PLC. The method comprises a programming and compiling method of a C language function, a method for calling a C language function in an IEC61131-3 program, and a method for running a C language function during PLC operation. The device comprises a PLC program programming host computer software supporting C language function programming, and a PLC device CPU module supporting dynamic loading and running of C language functions.
[0089] Create a new C language user function in the PLC programming configuration software, declare the user function in the IEC61131-3 standard form, use C or C++ (whether C++ is supported depends on the cross-compilation tool chain when the PLC is running) language to write the specific implementation of the function, automatically assign function numbers and generate registration function address codes during pre-compilation, and generate an extended C function dynamic library through the PLC run-time cross-compilation tool chain; directly call the user C language function in the user program of the PLC programming configuration software, convert the function call into a call by function number during pre-compilation, and the function input, output and return value are transmitted and interacted through the data segment of the user function; when the PLC is running, the user C function dynamic library is dynamically loaded during initialization and the user function number and address mapping table are registered. When the PLC program task runs and executes the C function number call instruction, the C function address table is first queried, and then jumps to the specific function address to execute the compiled C language function.
[0090] Based on the method of the present invention, the control function written in C / C++ language can be encapsulated as a function interface that complies with the IEC61131-3 specification, and the C / C++ language is expanded on the basis of the five programming configuration languages of the PLC specification, thereby improving practicality and flexibility.
[0091] The example device of the present invention involves a relatively complete solution for program programming, program compilation, and program running. The PLC device has been verified on the domestically produced independently controllable Loongson CPU platform, indicating that the method has good compatibility with domestically produced independent systems, can provide an effective reference for PLC program programming of different manufacturers, reduce the design and development costs of PLC system manufacturers, and is conducive to promoting industry development.
[0092] Finally, it should be noted that the above is only a preferred embodiment of the present invention, and the protection scope of the present invention is not limited to the above embodiments. All technical solutions under the concept of the present invention belong to the protection scope of the present invention. It should be pointed out that for ordinary technicians in this technical field, some improvements and modifications without departing from the principle of the present invention should also be regarded as the protection scope of the present invention.
[0093] The technical features of the above-described embodiments may be arbitrarily combined. To make the description concise, not all possible combinations of the technical features in the above-described embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
Claims
1. A method for extending C language functions by PLC programming configuration. It is characterized in that The steps include: Create a new C language user function in the PLC programming configuration software, declare the C language user function in accordance with the IEC61131-3 standard form, use the C language to write a specific implementation of the function, precompile the code that automatically assigns the C language function number and generates the registration function address, and generate an extended C language user function dynamic library through the cross-compilation tool chain when the PLC is running; The C language user function is directly called in the user program of the PLC programming configuration software, the function call is converted into a call according to the C language function number during the pre-compilation, and the compilation and linking generates program resources when the PLC is running; Before the PLC program task is started, the PLC runtime initializes and loads the C language user function dynamic library and calls the CLibInit function to register the user function number and address mapping table; when the PLC program task runs and executes the user function number call instruction, it first queries the address mapping table, and then jumps to the specific function address to execute the compiled C language user function.
2. The method for extending C language functions by PLC programming configuration according to claim 1, It is characterized in that Creating a new C language user function in the PLC programming configuration software specifically includes: Select C as the programming language, input the name of the C language user function, and declare the return value type, input parameters and output parameters of the C language user function.
3. The method for extending C language functions by PLC programming configuration according to claim 2, It is characterized in that After completing the declaration of the C language user function, the PLC programming configuration software automatically generates the code of the basic framework of the C language user function to facilitate processing of the return value, the input parameter and the output parameter.
4. The method for extending C language functions by PLC programming configuration according to claim 1, It is characterized in that The specific implementation of the function written in the C language includes: The logic program is written in C language. If the cross-compilation tool chain supports C++ language, the user function here also supports the use of C++ language. The programming environment includes IEC61131-3 variable access interface, network programming interface, file operation interface and peripheral access interface.
5. The method for extending C language functions by PLC programming configuration according to claim 1, It is characterized in that The pre-compilation automatically assigns C language function numbers and generates registration function address codes specifically including: The user is allowed to construct multiple C language user functions. Subsequent constructions will add one to the current function number in sequence to generate the C language user function library registration function address code. The function is a C language user function dynamic library. The PLC programming configuration software loads the current C language user function dynamic library and executes the registration function during initialization.
6. The method for extending C language functions by PLC programming configuration according to claim 1, It is characterized in that The C language user function is directly called in the user program of the PLC programming configuration software, and the user program of the PLC programming configuration software includes IEC61131-3 program, IEC61131-3 function and IEC61131-3 function block, as well as ST program and LD program consistent with the external characteristics of the IEC61131-3 function.
7. The method for extending C language functions by PLC programming configuration according to claim 1, It is characterized in that When the function call is converted into a call according to the C language function number during the pre-compilation, the function input, output and return value are transmitted interactively through the data segment of the C language user function.
8. The method for extending C language functions by PLC programming configuration according to claim 1, It is characterized in that Before the PLC program task is started, the program resources of the PLC runtime and the C language user function dynamic library are downloaded to the main controller module of the PLC device.
9. The method for extending C language functions by PLC programming configuration according to claim 8, It is characterized in that The PLC runtime is responsible for executing the operating environment of the logic program generated by the PLC program programming and compilation, adopts IEC61131-3 as the programming language specification of the PLC program programming software, and the IEC61131-3 program supports cyclic execution, periodic execution and event-triggered execution.
10. A device for executing the PLC programming configuration extension C language function of the method according to any one of claims 1 to 9, It is characterized in that include: Upper computer PLC programming configuration software, lower computer PLC equipment main controller module; The PLC programming configuration software is oriented to the programmable controller PLC, which is a digital operation controller with a microprocessor and is used for automatic control. The PLC programming configuration software includes logic programming, program debugging, variable observation, equipment configuration, and simulator, and provides a user programming language using the IEC61131-3 standard. The PLC device main controller module is responsible for executing the PLC program tasks, and has built-in PLC runtime software and C language user function dynamic library. The PLC runtime software is responsible for executing the operating environment of the logic program generated by PLC program programming and compilation, and the C language user function dynamic library is used to register and call the C language user function.
Citation Information
Patent Citations
Firmware library calling method and device, equipment and storage medium
CN116483481A
Program generation device, program generation method, and program generation program
CN107615190A
Method for embedding C language in PLC ladder diagram
CN113760298A
Method and system for automatically packaging C language codes into function blocks
CN115033231A
Function calling method, device and equipment and computer readable storage medium
CN115827116A
Cited By
PLC programming method based on C + + language
CN119511931A
