Method, device and electronic equipment for storing and loading a plc program
By using an SQLite database to store and load PLC programs, the problems of slow loading speed and difficult code maintenance are solved, achieving the effects of fast loading and secure storage.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- ZHUHAI RUIHE ELECTRIC CO LTD
- Filing Date
- 2023-03-15
- Publication Date
- 2026-05-26
AI Technical Summary
Existing PLC program storage methods have shortcomings in terms of loading speed and maintainability, especially encrypted INI files which are slow and custom binary formats which are difficult to maintain.
The PLC program is stored in an SQLite database. The program is loaded by decryption and version comparison, and upgrades are performed when the versions do not match. SQL statements are used for storage and retrieval.
It improves the loading speed and maintainability of PLC programs, ensures storage security and compatibility, and simplifies code maintenance.
Smart Images

Figure CN116431603B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of PLC technology, and in particular to a method, apparatus, electronic device, and storage medium for storing and loading PLC programs. Background Technology
[0002] The storage and loading of PLC (Programmable Logic Controller) programs is an indispensable part of PLC programming software. This part involves comprehensive information covering all aspects of the PLC programming system, including PLC model information, system configuration, module units, ladder diagram components, component annotations, and register status monitoring. The storage of PLC programs must first ensure reliability, requiring no information loss during storage; secondly, it must ensure security, as the sensitivity of industry data necessitates that stored files cannot be easily viewed or restored using other tools. Furthermore, with the rapid development of the electronics and electrical industry and the emergence of various new technologies, new concepts and elements are constantly being added to PLC programs or PLC programming software, leading to increasingly frequent version updates. This requires the storage file format of PLC programs to be compatible and extensible, meaning the file format design needs to adapt to constantly changing requirements, thus maintaining maintainability is also a consideration. Moreover, as PLC programs become increasingly complex, their code size is gradually increasing, which also places new demands on their storage and loading speed. Summary of the Invention
[0003] This invention aims to solve at least one of the technical problems existing in the prior art. To this end, this invention proposes a method, apparatus, electronic device, and storage medium for storing and loading PLC programs, which features fast loading speed and strong maintainability.
[0004] On one hand, the method for storing and loading a PLC program according to an embodiment of the present invention includes the following steps:
[0005] Open the database file of the PLC program and decrypt the database file of the PLC program; the database of the PLC program is an SQLite database, and the database file of the PLC program includes a system information table, which records the original version information and the modifiable version information of the PLC program database.
[0006] The version of the modifiable version information is detected, and the version of the modifiable version information is compared with the latest version that can be matched by the PLC programming software;
[0007] If the version of the modifiable version information is not lower than the latest version that the PLC programming software can match, then the PLC program is loaded.
[0008] According to some embodiments of the present invention, the method further includes the following steps:
[0009] If the version of the modifiable version information is lower than the latest version that the PLC programming software can match, then an upgrade operation is performed on the database of the PLC program.
[0010] Modify the modifiable version information of the upgraded PLC program's database to the version information of the PLC programming software's template database.
[0011] According to some embodiments of the present invention, after the step of using the modifiable version information of the upgraded PLC program database as the version information of the template database of the PLC programming software, the method further includes:
[0012] Write the relevant information for this upgrade operation into the log table.
[0013] According to some embodiments of the present invention, the method further includes the following steps:
[0014] If the PLC program fails to load, an error report will be displayed;
[0015] Close the database file of the PLC program.
[0016] According to some embodiments of the present invention, the method further includes a storage step of the PLC program, the storage step comprising:
[0017] Convert the contents of the PLC program into SQL statements;
[0018] Invoke the command to start a transaction and execute the SQL statement;
[0019] After successful execution, execute the commit transaction command to complete the storage of the PLC program.
[0020] According to some embodiments of the present invention, the storage step further includes:
[0021] If the SQL statement fails to execute, the transaction start command will be stopped.
[0022] This displays the reason why the SQL statement failed to execute.
[0023] According to some embodiments of the present invention, the method further includes a query step of the PLC program, the query step comprising:
[0024] Use SQL statements to query various data in the PLC program.
[0025] On the other hand, the PLC program storage and loading device according to an embodiment of the present invention includes:
[0026] The acquisition module is used to open the database file of the PLC program and decrypt the database file of the PLC program; the database of the PLC program is an SQLite database, and the database file of the PLC program includes a system information table, which records the original version information and the modifiable version information of the PLC program database.
[0027] The detection module is used to detect the version of the modifiable version information and compare the version of the modifiable version information with the latest version that can be matched by the PLC programming software;
[0028] The loading module is used to load the PLC program when the version of the modifiable version information is not lower than the latest version that the PLC programming software can match.
[0029] On the other hand, an electronic device according to an embodiment of the present invention includes:
[0030] Memory, used to store program instructions;
[0031] The processor is used to call the program instructions stored in the memory and execute the above-described method for storing and loading the PLC program according to the obtained program instructions.
[0032] On the other hand, according to an embodiment of the present invention, the storage medium stores computer-executable instructions, which are used to cause a computer to execute the above-described method for storing and loading PLC programs.
[0033] The PLC program storage and loading method, apparatus, electronic device, and storage medium according to embodiments of the present invention have at least the following beneficial effects: using an SQLite database to store the PLC program can improve the speed of loading PLC program files; using SQLite encryption can ensure the security requirements of PLC program storage; and by controlling the version upgrade of the SQLite database, the identification and compatibility problem of file storage between previous and subsequent versions can be solved, thereby improving maintainability.
[0034] Additional aspects and advantages of the invention will be set forth in part in the description which follows, and in part will be obvious from the description, or may be learned by practice of the invention. Attached Figure Description
[0035] The above and / or additional aspects and advantages of the present invention will become apparent and readily understood from the description of the embodiments taken in conjunction with the following drawings, in which:
[0036] Figure 1 This is a flowchart illustrating the steps of a PLC program storage and loading method according to an embodiment of the present invention.
[0037] Figure 2 This is a flowchart illustrating the loading process of the PLC program according to an embodiment of the present invention.
[0038] Figure 3 This is a flowchart illustrating the upgrade process of the PLC program database in an embodiment of the present invention.
[0039] Figure 4 This is a flowchart illustrating the stored procedure of a PLC program according to an embodiment of the present invention.
[0040] Figure 5 This is a schematic diagram of the structure of a PLC program storage and loading device according to an embodiment of the present invention;
[0041] Figure label:
[0042] Acquisition module 100, detection module 200, loading module 300. Detailed Implementation
[0043] This section will describe in detail specific embodiments of the present invention. Preferred embodiments of the present invention are shown in the accompanying drawings. The purpose of the drawings is to supplement the textual description with graphics, so that people can intuitively and vividly understand each technical feature and overall technical solution of the present invention, but they should not be construed as limiting the scope of protection of the present invention.
[0044] In the description of this invention, "several" means one or more, "more than" means two or more, "greater than," "less than," and "exceeding" are understood to exclude the stated number, while "above," "below," and "within" are understood to include the stated number. The use of "first" and "second" in the description is merely for distinguishing technical features and should not be construed as indicating or implying relative importance, or implicitly indicating the number of indicated technical features, or implicitly indicating the order of the indicated technical features.
[0045] In the description of this invention, unless otherwise explicitly defined, terms such as "set up," "install," and "connect" should be interpreted broadly, and those skilled in the art can reasonably determine the specific meaning of the above terms in this invention in conjunction with the specific content of the technical solution.
[0046] In existing technologies, there are two main methods for storing PLC programs:
[0047] 1. Use encrypted Ini files for file storage; Ini files refer to Initialization Files, which are the storage format used by many operating system configuration files and manage various configurations of the operating system;
[0048] 2. Use a custom binary format for file storage.
[0049] While the first method described above, which uses encrypted INI files, is very secure and has good scalability, its speed performance is unsatisfactory, especially when the PLC program file is very large.
[0050] Regarding the second method mentioned above, the custom-formatted binary storage method has no issues in terms of security and performs well in terms of speed. However, this storage method has low maintainability, and the PLC program becomes increasingly difficult to maintain over time. For example, consider a ladder diagram element. Suppose that in the initial version 1.0, describing the ladder diagram element using N dimensions is sufficient. Then, in subsequent versions, new dimensions need to be added. The corresponding change in the file is that the element's record structure grows due to the addition of new fields. At this point, the format of the file used to store the PLC program must be modified, requiring a version upgrade to 1.1. Simultaneously, software typically needs to be compatible with previous versions of the storage file. Therefore, when new programming software loads, it must be able to parse both version 1.0 and version 1.1 files. Version switches must be added to the software's source code to accommodate multiple versions. With many versions, the source code becomes increasingly complex and difficult to maintain.
[0051] To address both the long PLC program loading time of the first method and the code maintenance difficulties associated with multiple versions of the second method, this embodiment of the invention employs an SQLite database to store the PLC program. SQLite is a small, fast, and reliable database that supports multiple operating systems, integrates with many programming languages, and offers fast processing speeds. Furthermore, SQLite provides ACID (Atomicity, Isolation, Consistency, Durability) transaction functionality and requires zero configuration, eliminating the need for additional installation and management. Using SQLite improves the speed of loading PLC program files; its encryption ensures the security of stored PLC programs; and version upgrade control resolves compatibility issues between different versions, thereby enhancing maintainability.
[0052] like Figure 1 As shown, the method for storing and loading a PLC program according to an embodiment of the present invention includes the following steps:
[0053] Step S100: Open the database file of the PLC program and decrypt the database file of the PLC program; the database of the PLC program is an SQLite database, and the database file of the PLC program includes a system information table, which records the original version information and the modifiable version information of the PLC program database.
[0054] Step S200: Detect the version of the modifiable version information and compare the version of the modifiable version information with the latest version that can be matched by the PLC programming software;
[0055] Step S300: If the version of the modifiable version information is not lower than the latest version that the PLC programming software can match, then load the PLC program.
[0056] Specifically, such as Figure 2 As shown, firstly, the PLC programming software calls the `sqlite3_open` function to open the PLC program's database file; after a successful connection, it calls the `sqlite3_key` function to decrypt the PLC program's database file, because the PLC program's database file is stored encrypted. In addition to business information tables including program model information, program unit tables, and ladder diagram component tables, the PLC program's database also contains a system information table. This table describes the original version information and the modifiable version information of the PLC program's database. Initially, the two versions are identical; however, by convention, the latter can be modified, while the former is read-only and cannot be modified. One specific implementation of the system information table is shown in Table 1.
[0057] Field Name Data types describe Remark sParaName Varchar(255) Parameter name Primary key sParaValue Varchar(255) Parameter values sParaDesc VarChar(255)
[0058] Table 1
[0059] This table has fields for parameter name, parameter value, and parameter description, and includes the original version information and modifiable version information as shown below:
[0060]
[0061] The PLC program database has a built-in PLC program unit information table (UnitInfo), as shown in Table 2. This table has fields such as program unit ID, program unit name, unit language type, ladder diagram type, program type, and unit comments to store information such as the ID, name, language type, and description of the PLC program unit.
[0062]
[0063]
[0064] Table 2
[0065] The PLC program database has a built-in PLC program component information table, as shown in Table 3. This table is used to store PLC component information in the ladder diagram, including the program unit to which it belongs, component identifier, component category, coordinate information, whether pulses are enabled, and the names of various parameters of the component.
[0066]
[0067]
[0068] Table 3
[0069] The PLC program's database has a built-in device annotation information table (ElementHint), as shown in Table 4. This table is used to store device annotations and prompts.
[0070] Field Name Data types describe Remark sNo Varchar(30) Device Identification Primary key sPostil Varchar(255) Device annotations sHint Varchar(100) Device prompts
[0071] Table 4
[0072] When PLC programming software is released, it comes with a corresponding version of the database as a template database. The programming software also internally stores its own latest compatible PLC program database version (this version is identical to the version in the template database). When the PLC programming software opens the PLC program's database file, it automatically detects the modifiable version information of the database file and compares it with the latest compatible database version. If the former is not lower than the latter, the PLC program's database file is considered to have been generated by a newer version of the programming software. The structure of a newer version's database is more complex than previous versions, with more fields, etc., thus older versions of PLC programming software can still parse it. Therefore, if the modifiable version information is not lower than the latest compatible version of the PLC programming software, a select statement is executed to read and process model information, unit lists, ladder diagram component lists, annotation information, etc., from the database, converting them into the internal structure of the PLC programming system. After reading, the sqlite3_close function is called to close the PLC program's database file, completing the loading of the PLC program. If an error occurs during the loading process (such as incomplete data) and the PLC program fails to load successfully, an error report will be displayed and the PLC program's database file will be closed.
[0073] like Figure 3 As shown, the method for storing and loading PLC programs according to an embodiment of the present invention further includes the following steps:
[0074] If the version of the modified version information is lower than the latest database version that the PLC programming software can match, perform an upgrade operation on the PLC program's database.
[0075] Modify the editable version information of the upgraded PLC program's database to match the version information of the PLC programming software's template database.
[0076] Specifically, when the programming software detects that the version of the modifiable version information is lower than the latest database version that the PLC programming software can match, it will issue a prompt and automatically upgrade the PLC program's database to keep it synchronized with the version of the PLC programming software's template database. After the upgrade, the modifiable version information of the upgraded PLC program's database will be modified to match the version information of the programming software's template database, and the relevant information of this upgrade will be written to the log table.
[0077] like Figure 4 As shown, the method for storing and loading a PLC program according to an embodiment of the present invention further includes a storage step for the PLC program, which includes:
[0078] Convert the contents of the PLC program into SQL statements;
[0079] Invoke the command to start a transaction and execute the SQL statement;
[0080] After successful execution, execute the commit transaction command to complete the storage of the PLC program.
[0081] Specifically, such as Figure 4 As shown, firstly, the `sqlite3_open` function is called to open the database file of the PLC program; after a successful connection, the `sqlite3_key` function is called to decrypt the database file; then, the contents of the PLC program, such as model information, unit list, ladder diagram component list, annotation information, etc., are converted into SQL statements; then, the start transaction command is called to execute the SQL statements; after successful execution, the commit transaction command is executed to complete the storage of the PLC program; finally, the `sqlite3_close` function is called to close the database file of the PLC program.
[0082] like Figure 4 As shown, in some embodiments of the present invention, the above-described storage step further includes:
[0083] If the SQL statement fails to execute, the command to start the transaction will be stopped.
[0084] Display the reason why the SQL statement failed to execute, and close the database file of the PLC program.
[0085] The method for storing and loading a PLC program according to an embodiment of the present invention further includes a query step for the PLC program, wherein the query step includes: querying various data of the PLC program using SQL statements.
[0086] When the PLC program file is very large, querying various data in the PLC program file using SQL statements is more convenient and faster, as it eliminates the need to write additional statistical code in other programming languages.
[0087] The PLC program storage and loading method according to embodiments of the present invention significantly speeds up PLC program loading by using an SQLite database to store the SQL program. Furthermore, during version updates, the code for loading the PLC program does not require a version switch, making the code easier to maintain. Using the primary key and restrictions of the SQLite database ensures that if the data generated by the PLC programming system is incorrect (e.g., duplicate component IDs in the component list or duplicate addresses in the register list), storage will fail and the reason for failure will be reported. This, in turn, verifies the correctness of the data and structure processed by the PLC programming system, promoting the software quality of the PLC programming system. When the PLC program file is large, this method allows for more convenient, faster, and more flexible querying of various data within the PLC program file using SQL statements, without the need to write additional statistical code in other programming languages.
[0088] On the other hand, based on the above-mentioned method for storing and loading PLC programs, this embodiment of the invention also proposes a device for storing and loading PLC programs, such as... Figure 5 As shown, the device includes:
[0089] The acquisition module 100 is used to open the database file of the PLC program and decrypt the database file of the PLC program; the database of the PLC program is an SQLite database, and the database file of the PLC program includes a system information table, which records the original version information and the modifiable version information of the database of the PLC program.
[0090] The detection module 200 is used to detect the version of the modifiable version information and compare the version of the modifiable version information with the latest version of the PLC programming software.
[0091] The loading module 300 is used to load the PLC program when the version of the modifiable version information is not lower than the latest version matched by the PLC programming software.
[0092] It should be noted that the content of the above method embodiments is applicable to the present device embodiments. The specific functions implemented by the present device embodiments are the same as those of the above method embodiments, and the beneficial effects achieved are also the same as those achieved by the above method embodiments.
[0093] On the other hand, embodiments of the present invention also propose an electronic device comprising:
[0094] Memory, used to store program instructions;
[0095] The processor is used to call the program instructions stored in the memory and execute the above-described method for storing and loading the PLC program according to the obtained program instructions.
[0096] On the other hand, embodiments of the present invention also propose a storage medium storing computer-executable instructions, which are used to cause a computer to execute the above-described method for storing and loading PLC programs.
[0097] Although specific embodiments are described herein, those skilled in the art will recognize that many other modifications or alternative embodiments are also within the scope of this disclosure. For example, any of the functions and / or processing capabilities described in connection with a particular device or component can be performed by any other device or component. Furthermore, while various exemplary embodiments and architectures have been described according to embodiments of this disclosure, those skilled in the art will recognize that many other modifications to the exemplary embodiments and architectures described herein are also within the scope of this disclosure.
[0098] The foregoing description, with reference to block diagrams and flowcharts of systems, methods, systems, and / or computer program products according to exemplary embodiments, has described certain aspects of this disclosure. It should be understood that one or more blocks in the block diagrams and flowcharts, as well as combinations of blocks in the block diagrams and flowcharts, can be implemented by executing computer-executable program instructions, respectively. Similarly, according to some embodiments, some blocks in the block diagrams and flowcharts may not need to be executed in the order shown, or may not all need to be executed. Furthermore, additional components and / or operations beyond those shown in the blocks in the block diagrams and flowcharts may exist in some embodiments.
[0099] Therefore, blocks in block diagrams and flowcharts support combinations of means for performing a specified function, combinations of elements or steps for performing a specified function, and program instruction means for performing a specified function. It should also be understood that each block in a block diagram and flowchart, and combinations of blocks in block diagrams and flowcharts, can be implemented by a dedicated hardware computer system or a combination of dedicated hardware and computer instructions that performs a specific function, element, or step.
[0100] The program modules, applications, etc., described herein may include one or more software components, including, for example, software objects, methods, data structures, etc. Each such software component may include computer-executable instructions that, in response to execution, cause at least a portion of the functionality described herein (e.g., one or more operations of the exemplary methods described herein) to be performed.
[0101] Software components can be coded using any of a variety of programming languages. An exemplary programming language could be a low-level programming language, such as assembly language associated with a specific hardware architecture and / or operating system platform. Software components including assembly language instructions may need to be converted into executable machine code by an assembler before being executed by the hardware architecture and / or platform. Another exemplary programming language could be a higher-level programming language that is portable across multiple architectures. Software components including higher-level programming languages may need to be converted into an intermediate representation by an interpreter or compiler before execution. Other examples of programming languages include, but are not limited to, macro languages, shell or command languages, job control languages, scripting languages, database query or search languages, or report writing languages. In one or more exemplary embodiments, a software component containing instructions from one of the above-described programming language examples can be executed directly by the operating system or other software components without first being converted into another form.
[0102] Software components can be stored as files or other data storage structures. Software components of similar type or related function can be stored together in a specific directory, folder, or library. Software components can be static (e.g., pre-defined or fixed) or dynamic (e.g., created or modified at runtime).
[0103] The embodiments of the present invention have been described in detail above with reference to the accompanying drawings. However, the present invention is not limited to the above embodiments. Within the scope of knowledge possessed by those skilled in the art, various changes can be made without departing from the spirit of the present invention.
Claims
1. A method for storing and loading PLC programs, characterized in that, Applied to PLC programming software, the method includes the following steps: Open the database file of the PLC program and decrypt the database file of the PLC program; the database of the PLC program is an SQLite database, and the database file of the PLC program includes a system information table, which records the original version information and the modifiable version information of the PLC program database. The version of the modifiable version information is detected, and the version of the modifiable version information is compared with the latest version that can be matched by the PLC programming software; If the version of the modifiable version information is not lower than the latest version that the PLC programming software can match, then the PLC program is loaded; The method further includes a step of storing the PLC program, the storage step comprising: Convert the contents of the PLC program into SQL statements; Invoke the command to start a transaction and execute the SQL statement; After successful execution, execute the commit transaction command to complete the storage of the PLC program.
2. The method for storing and loading PLC programs according to claim 1, characterized in that, The method further includes the following steps: If the version of the modifiable version information is lower than the latest version that the PLC programming software can match, then an upgrade operation is performed on the database of the PLC program. Modify the modifiable version information of the upgraded PLC program's database to the version information of the PLC programming software's template database.
3. The method for storing and loading PLC programs according to claim 2, characterized in that, After the step of using the modifiable version information of the upgraded PLC program database as the version information of the template database of the PLC programming software, the method further includes: Write the relevant information for this upgrade operation into the log table.
4. The method for storing and loading PLC programs according to claim 1, characterized in that, The method further includes the following steps: If the PLC program fails to load, an error report will be displayed; Close the database file of the PLC program.
5. The method for storing and loading PLC programs according to claim 1, characterized in that, The storage step further includes: If the SQL statement fails to execute, the transaction start command will be stopped. This displays the reason why the SQL statement failed to execute.
6. The method for storing and loading PLC programs according to claim 1, characterized in that, The method further includes a query step for the PLC program, the query step comprising: Use SQL statements to query various data in the PLC program.
7. A device for storing and loading PLC programs, characterized in that, include: The acquisition module is used to open the database file of the PLC program and decrypt the database file of the PLC program; the database of the PLC program is an SQLite database, and the database file of the PLC program includes a system information table, which records the original version information and the modifiable version information of the PLC program database. The detection module is used to detect the version of the modifiable version information and compare the version of the modifiable version information with the latest version that can be matched by the PLC programming software; A loading module is used to load the PLC program when the version of the modifiable version information is not lower than the latest version that the PLC programming software can match; The module for storing PLC programs is used to convert the contents of the PLC program into SQL statements, call and execute a start transaction command, execute the SQL statements, and after successful execution, execute a commit transaction command to complete the storage of the PLC program.
8. An electronic device, characterized in that, include: Memory, used to store program instructions; A processor is configured to invoke program instructions stored in the memory and execute the storage and loading method of the PLC program according to any one of claims 1-6.
9. A storage medium, characterized in that, The storage medium stores computer-executable instructions, which are used to cause a computer to execute the storage and loading method of the PLC program according to any one of claims 1-6.