Method for improving execution speed of soft PLC (Programmable Logic Controller) code
By using function pointer arrays to record and call application functions in embedded soft PLC systems, combining the 'space-for-time' and 'quadratic compilation' methods, the problem of low execution efficiency in the existing technology is solved, and significant execution speed is achieved promote.
Patent Information
- Application Number
- CN202411241610.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-05
- Publication Date
- 2024-12-27
AI Technical Summary
The prior art has limited effect in improving the code execution efficiency of embedded soft PLC system, especially at the expense of memory space, it is difficult to significantly improve execution speed.
Record and call application functions through a function pointer array, eliminate parsing operations during execution, and adopt the 'space exchange time' and 'secondary compilation' methods to reduce program parsing steps and improve code execution speed.
The execution efficiency of soft PLC code is significantly improved, and the execution speed is improved by reducing the parsing process and data type operations.
Smart Images

Figure CN119201295A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of soft PLC code execution, and in particular to a method for improving the execution speed of soft PLC code. Background Art
[0002] Improving program execution efficiency is the common goal pursued by all code developers. Efficiency refers to the running speed of the current program, the degree of utilization of system resources, etc. High-efficiency C language programs are generally characterized by compact code, fast execution speed, small space occupation, and strong program readability. However, program execution speed and memory space occupation are often two contradictory aspects. In some cases, it is necessary to sacrifice memory space in exchange for improved efficiency.
[0003] In embedded soft PLC systems, program execution speed is particularly important. The existing literature "Wang Lei, Li Xin. A Brief Analysis of Embedded Program Optimization [J]. Computer Knowledge and Technology, 2012, 8(01): 192-193+196" proposes code optimization aspects such as variable processing, appropriate use of macro definitions, array processing, and avoiding the use of division as much as possible to improve system performance based on the characteristics of embedded systems. However, it only optimizes part of the code of the C language program to improve efficiency, and the improvement in optimization effect is very limited. Therefore, the execution efficiency needs to be further improved. Summary of the invention
[0004] The purpose of the present invention is to provide a method for improving the execution speed of soft PLC code to solve the following technical problems:
[0005] How to improve program execution efficiency.
[0006] The purpose of the present invention can be achieved by the following technical solutions:
[0007] A method for improving the execution speed of soft PLC code comprises the following steps:
[0008] S01: After the simulation program is downloaded, the entire PLC simulation file is traversed, and all called application function pointers are recorded by means of a function pointer array;
[0009] S02: When the program is executed, the function pointer array is traversed, the application function pointer is read, and the corresponding application function is executed;
[0010] S03: Determine whether the file parsing is complete, if so, then end; if not, repeat step S02 until the file parsing is complete.
[0011] In a further embodiment of the present invention, the specific implementation method of steps S01-S03 in the soft PLC system is as follows:
[0012] S11: First use typedef to customize a pointer type pointing to a function;
[0013] S12: Then define an array of Funct i onPtr type, each element in the array can store the address of a function that meets these conditions;
[0014] S13: Store the function pointer and increase the count through the record_funct i on_call() function;
[0015] S14: Call the recorded application function pointer through the reply_calls() function.
[0016] In a further solution of the present invention: in step S13, when the application function is called, a set of stored function pointers is obtained through the record_funct i on_call() function.
[0017] In a further solution of the present invention: in step S14, the application function pointers point to different application functions, and the method of calling these application functions includes array indexing.
[0018] In a further embodiment of the present invention, the method of using steps S11-S14 in the embedded soft PLC system code is as follows:
[0019] S21: in the scan_adder_entry() parsing function, add the record_funct ion_call() function after the application function to record the application function pointer called;
[0020] S22: In the prog_process() processing program function, the simulation file is first parsed and traversed to obtain the recorded function pointer array, and then the reply_calls() function is called to traverse the function pointers.
[0021] In a further embodiment of the present invention, the application function comprises the following execution steps:
[0022] SA: Find a free data space in the MCU. After the simulation program is downloaded, parse the simulation program first. After the called application function parses the operation data type, save the operation data type in the aforementioned free data space.
[0023] SB: When executing the called application function, the operation data type is directly read from the free data space and the data operation is performed;
[0024] SC: Determine whether the call of the application function has ended. If so, it ends; if not, repeat SB until the call ends.
[0025] In a further embodiment of the present invention, the specific implementation method of steps SA-SC in the soft PLC system is:
[0026] Step Sa: First find enough free space in the MCU and use the pointer p_s_prog to point to the starting position;
[0027] Step Sb: Use record_type() function to write type and increase p_s_prog pointer;
[0028] Step Sc: Read the value currently pointed to by p_s_prog through the reply_type() function
[0029] Step Sd: Reset the p_s_prog pointer via the reset_prog_pointer() function.
[0030] In a further embodiment of the present invention, the operation steps of steps Sa-Sd in the soft PLC application function involving the data operation part are:
[0031] SM: Add record_type() function to the original data operation function PLC_ReadData16() to record the operation data type value;
[0032] SN: Create a new S_PLC_ReadData16() function to replace the PLC_ReadData16() function for data reading.
[0033] Beneficial effects of the present invention:
[0034] The method for improving the execution speed of soft PLC code of the present invention aims at the problem of low execution efficiency of the soft PLC system. Firstly, the parsing process when executing the application function is omitted by the "space for time" method, thereby improving the code execution speed. On this basis, the process of parsing the operation data type during the execution of the application function is omitted by the "secondary compilation" method, thereby further improving the code execution speed. By reducing the idea of program parsing steps, the execution efficiency of the soft PLC is greatly improved. BRIEF DESCRIPTION OF THE DRAWINGS
[0035] The present invention will be further described below in conjunction with the accompanying drawings.
[0036] Figure 1 It is a flow chart of the execution method of the instruction byte code of the PLC in the prior art;
[0037] Figure 2 Yes Figure 1 The method in is combined into the pseudo code diagram of soft PLC;
[0038] Figure 3 It is a flow chart of a method for improving the execution speed of soft PLC code by using the optimization method of "trading space for time" in Embodiment 1 of the present invention;
[0039] Figure 4 Yes Figure 1 The method is combined with the specific implementation code diagram of the soft PLC system;
[0040] Figure 5 Yes Figure 4 The method is combined with the usage diagram of the embedded soft PLC system code;
[0041] Figure 6 It is a flow chart of parsing instruction data in a simulation program in the prior art;
[0042] Figure 7 yes Figure 6 The method in the above is combined with the pseudo code diagram of the MOV function in the soft PLC program;
[0043] Figure 8 It is a flow chart of a method for improving the execution speed of soft PLC code by the optimization method of "secondary compilation" in Embodiment 2 of the present invention;
[0044] Fig. 9 yes Figure 8 The method is combined with the specific implementation code diagram of the soft PLC system;
[0045] Fig.10 yes Fig. 9 The method in the soft PLC application function is combined with the usage diagram;
[0046] Fig.11 This is the execution result diagram of Mitsubishi GX Works2 using the original program in the test example;
[0047] Fig.12 This is a graph showing the execution results of Mitsubishi GX Works2 using the "space for time" method of Example 1 in the test example;
[0048] Fig.13 This is a diagram showing the execution results of Mitsubishi GX Works2 using the "secondary compilation" method of Example 1 in the test example. DETAILED DESCRIPTION
[0049] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0050] Example 1
[0051] See also Figure 1 In the prior art, the instruction bytecode of a general PLC is actually a string of binary codes. The instruction code, variable address, and constants in the instruction are arranged by bytes (or words) in a certain order. The system will parse the instruction code in the file in order and call the corresponding application function according to the parsing result until the end of the file.
[0052] See also Figure 2 , which is Figure 1 The method in the paper is combined with the pseudo code of the soft PLC, and the specific explanation is as follows:
[0053] 1. The scan_l adder_entry() function is to parse according to the pointer and query which command to execute according to the parsing result;
[0054] 2. The function of prog_process() is to keep reading the file address and passing it into the processing function for processing until the end of the file.
[0055] By reading the code, we can find that when the hardware resources such as MCU are the same, the parsing speed and the number of times the parsing function is called are the main reasons affecting the program execution speed.
[0056] See also Figure 3 In view of the optimizable parts in the prior art, the present invention discloses a method for improving the execution speed of soft PLC code, which improves the program execution speed by using the "space for time" optimization method, including the following steps:
[0057] S01: After the simulation program is downloaded, the entire PLC simulation file is traversed, and all called application function pointers are recorded by means of a function pointer array;
[0058] S02: When the program is executed, the function pointer array is traversed, the application function pointer is read, and the corresponding application function is executed;
[0059] S03: Determine whether the file parsing is complete, if so, then end; if not, repeat step S02 until the file parsing is complete.
[0060] See also Figure 4, which is a specific implementation code based on the optimization method of "space for time" combined with the soft PLC system, and is explained in detail as follows:
[0061] 1. First use typedef to customize a pointer type pointing to a function;
[0062] 2. Then define an array of type Funct i onPtr. Each element in the array can store the address of a function that meets these conditions.
[0063] 3. The record_funct i on_call() function is used to store function pointers and increase the count. The usage is: when calling the application function, this function is used to obtain the set of stored function pointers;
[0064] 4. The reply_calls() function calls the recorded function pointers, and these function pointers point to different functions, which can be called through array indexes when needed.
[0065] See also Figure 5 , which is the method of using the specific embedded soft PLC system code, and is introduced in detail as follows:
[0066] 1. In the scan_adder_entry() parsing function, add the record_funct i on_call() function to record the function pointer after the application function;
[0067] 2. prog_process() is the processing program function. In the function, it first traverses the simulation file by parsing to obtain the record function pointer array, and then calls the reply_calls() function to traverse the function pointer.
[0068] Example 2
[0069] See also Figure 6 In the soft PLC system of the prior art, complex logical operations, data processing and communication functions are realized through soft components, and the soft component addresses are operated in the form of absolute addresses. The types of soft components include data soft components, input and output soft components, counters, etc. Different soft component types have different access addresses, which results in the application function needing to parse the data type of the instruction before performing the corresponding data operation each time when performing the operation; usually in general simulation programs, the operation of parsing the instruction data type is relatively frequent, and the cumulative total is relatively time-consuming.
[0070] See also Figure 7 , which is Figure 6The method in the above is combined with the soft PLC program, specifically the pseudo code of the application instruction MOV function, which is described in detail as follows:
[0071] 1. Both PLC_ReadData16() and PLC_Write_data16() functions are data operation functions, in which type identification is mainly performed through the expression_type() function, and then different addresses are accessed through different types;
[0072] 2. The FNC_12_MOV() function is an application function in the soft PLC system, involving data reading and writing operations.
[0073] It can be seen that when performing data operations, the explain_type() function needs to be used every time to determine the type. After parsing the entire file, the number of calls is large, which is a waste of time.
[0074] See also Figure 8 In view of the optimizable parts in the prior art, this embodiment discloses a method for improving the execution speed of soft PLC code based on Embodiment 1, and further improves the program execution speed through the "secondary compilation" method, including the following steps:
[0075] SA: Find a free data space in the MCU. After the simulation program is downloaded, parse the simulation program first. After the called application function parses the operation data type, save the operation data type in the aforementioned free data space.
[0076] SB: When executing the called application function, the operation data type is directly read from the free data space and the data operation is performed;
[0077] SC: Determine whether the call of the application function has ended. If so, it ends; if not, repeat SB until the call ends.
[0078] See also Fig. 9 , which is based on Figure 8 The method in the above code is combined with the specific implementation code of the soft PLC system. The specific explanation steps are as follows:
[0079] 1. First, find enough free space in the MCU and use the pointer p_s_prog to point to the starting position;
[0080] 2. Use the record_type() function to write type and increase the p_s_prog pointer;
[0081] 3. Read the value currently pointed to by p_s_prog through the reply_type() function
[0082] 4. Reset the p_s_prog pointer through the reset_prog_pointer() function.
[0083] See also Fig.10 , which is Fig. 9 The method in the above is combined with the method of using the soft PLC application function. In the data operation part, the specific operation steps are as follows:
[0084] 1. Add the record_type() function to the original data operation function PLC_ReadData16() to record the operation data type value;
[0085] 2. Create a new S_PLC_ReadData16() function to replace the PLC_ReadData16() function for data reading.
[0086] Through observation, it can be found that the key to method 2 is to add a recording function, and the recording function can customize the information required for recording according to different parsing methods. In other words, the method in embodiment 2 can be applied to a variety of situations and has wide applicability.
[0087] Test example
[0088] Taking the soft PLC with Mitsubishi FX2N instruction set as the instruction system as an example, the test software selects Mitsubishi's GX Works2. By testing 1000 MOV instruction programs as test programs, the execution speed of the simulation program under the original program, the "space for time" method in Example 1, and the "secondary compilation" method in Example 2 is tested; the calculation method is 1000 divided by the D902 value, which is the execution time of a MOV instruction in us.
[0089] See also Figure 11-13 ,in, Fig.11 To use the Mitsubishi GX Works2 execution result under the original program, Fig.12 This is the execution result of Mitsubishi GX Works2 using the "space for time" method in Example 1. Fig.13 To use the Mitsubishi GX Works2 execution result under the "secondary compilation" method in Example 2, Figure 11-13 The execution results in are converted into speed and listed in Table 1. Table 1 is as follows:
[0090] Table 1
[0091] Execution Program Calculation formula: 1000 / D902 Speed / us Original Program 1000 / 336= 2.97us "Space for Time" Method 1000 / 606= 1.65us "Secondary compilation" method 1000 / 643= 1.55us
[0092] By analyzing the data in Table 1, it can be found that the program execution efficiency is improved after using the "space for time" method in Example 1 and the "secondary compilation" method in Example 2. Therefore, the method of improving the execution speed of soft PLC code of the present invention is significantly effective in this case and can improve the execution efficiency of soft PLC code.
[0093] The above is a detailed description of an embodiment of the present invention, but the content is only a preferred embodiment of the present invention and cannot be considered to limit the scope of implementation of the present invention. All equivalent changes and improvements made within the scope of the present invention should still fall within the scope of the patent coverage of the present invention.
Claims
1. A method for improving the execution speed of soft PLC code, characterized in that: The following steps are involved: S01: After the simulation program is downloaded, the entire PLC simulation file is traversed, and all called application function pointers are recorded by means of a function pointer array; S02: When the program is executed, the function pointer array is traversed, the application function pointer is read, and the corresponding application function is executed; S03: Determine whether the file parsing is complete, if so, then end; if not, repeat step S02 until the file parsing is complete.
2. The method for improving the execution speed of soft PLC code according to claim 1, characterized in that: The specific implementation method of steps S01-S03 in the soft PLC system is: S11: First use typedef to customize a pointer type pointing to a function; S12: Then define an array of type Functi onPtr, each element in the array can store the address of a function that meets these conditions; S13: Store the function pointer and increase the count through the record_function_call() function; S14: Call the recorded application function pointer through the replay_calls() function.
3. The method for improving the execution speed of soft PLC code according to claim 2, characterized in that: In step S13, when the application function is called, a set of stored function pointers is obtained through the record_function_call() function.
4. The method for improving the execution speed of soft PLC code according to claim 2, characterized in that: In step S14, the application function pointers point to different application functions, and the method of calling these application functions includes array indexing.
5. The method for improving the execution speed of soft PLC code according to claim 2, characterized in that: The method of using steps S11-S14 in the embedded soft PLC system code is: S21: In the scan_ladder_entry() parsing function, add the record_function_call() function to record the application function pointer after the application function; S22: In the prog_process() processing program function, first parse and traverse the simulation file to obtain the recorded function pointer array, and then call the replay_calls() function to traverse the function pointers.
6. The method for improving the execution speed of soft PLC code according to claim 1, characterized in that: The application function includes the following execution steps: SA: Find a free data space in the MCU. After the simulation program is downloaded, parse the simulation program first. After the called application function parses the operation data type, save the operation data type in the aforementioned free data space. SB: When executing the called application function, the operation data type is directly read from the free data space and the data operation is performed; SC: Determine whether the call of the application function has ended. If so, it ends; if not, repeat SB until the call ends.
7. The method for improving the execution speed of soft PLC code according to claim 6, characterized in that: The specific implementation method of step SA-SC in the soft PLC system is: Step Sa: First find enough free space in the MCU and use the pointer p_s_prog to point to the starting position; Step Sb: Use record_type() function to write type and increase p_s_prog pointer; Step Sc: Read the value currently pointed to by p_s_prog through the replay_type() function Step Sd: Reset the p_s_prog pointer via the reset_prog_pointer() function.
8. The method for improving the execution speed of soft PLC code according to claim 7, characterized in that: The operation steps of steps Sa-Sd involving data operation in the soft PLC application function are as follows: SM: Add record_type() function to the original data operation function PLC_ReadData16() to record the operation data type value; SN: Create a new S_PLC_ReadData16() function to replace the PLC_ReadData16() function for data reading.