Python language integration method based on IEC61131-3 programming system

CN122672798APending Publication Date: 2026-09-01CLP INTELLIGENT TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202610774138.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-06-01
Publication Date
2026-09-01

Smart Images

  • Figure CN122672798A_ABST
    Figure CN122672798A_ABST
Patent Text Reader

Abstract

This invention discloses a Python language integration method for an IEC61131-3 programming system in the PLC field, solving the problem that Python language cannot be logically integrated into IEC61131-3 programs in the form of function interfaces. The method steps are as follows: 1. Create a Python language project on the IEC61131-3 programming system, edit Python functions according to requirements, and specify interface information such as interface names. After creation, corresponding function interfaces will be generated; 2. Build a Python language project on the IEC61131-3 programming system. After the construction is completed, a C++ dynamic library is generated. This dynamic library has built-in CPython to implement the calling of Python programs; 3. Create an IEC program on the IEC61131-3 programming system. This IEC program should include program fragments that call the function interfaces in step 1. Compile the IEC program and generate a compiled file; 4. Download and run the IEC program on the IEC61131-3 programming system. Download the compiled file generated in step 3 and the C++ dynamic library and Python program from step 2 to the PLC. During runtime, load the dynamic library, resolve the function address, and fill the address back to the IEC program call location for execution.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of programmable logic controller (PLC) programming, specifically to a Python language integration method based on the IEC61131-3 programming system. Background Technology

[0002] The IEC 61131-3 standard is an international standard for programming programmable logic controllers (PLCs). It specifies the types, syntax, and usage rules of PLC programming languages, mainly including five programming languages: Ladder Diagram (LD), Function Block Diagram (FBD), Structured Text (ST), Sequential Function Chart (SFC), and Instruction List (IL). Programs developed based on these five programming languages ​​are called IEC programs, and IEC 61131-3 programming systems typically need to support the editing and compilation of IEC programs.

[0003] As industrial automation demands increasingly higher levels of intelligence, more and more industrial systems require the integration of PLC systems with the Python language to expand their data analysis and computational interaction capabilities. Python has been widely adopted in various fields such as data processing, machine learning, and automated testing, providing developers with rich computing power and extended interfaces. Especially in complex control algorithms and real-time data processing, Python can significantly improve the functionality and operational efficiency of PLC systems.

[0004] Regarding Python support, existing IEC61131-3 programming systems such as Twincat, CodeSys, and Inovance support Python through external integration or internal interaction. Twincat uses the ADS protocol to interact with Python programs on external devices; CodeSys introduces IronPython to support Python access to internal interfaces, implementing an automated script management tool; Inovance primarily uses industrial protocols such as Modbus to interact with external Python processes. In summary, current PLC platforms do not integrate Python at the logic level; they exist as external systems or development tools, relying on communication protocols for long call chains, which can easily lead to low operating efficiency. They also cannot process process data during PLC program execution in a timely manner, nor can they directly edit Python language projects within the IEC61131-3 programming system.

[0005] In summary, existing IEC 61131-3 programming systems generally do not support Python logic integration, making it impossible to independently edit and compile Python language projects. Therefore, this invention proposes a Python language integration method based on the IEC 61131-3 programming system to improve the system's support for the Python language. Summary of the Invention

[0006] The purpose of this invention is to provide a Python language integration method based on the IEC61131-3 programming system. This invention enables the integration of the Python language into the IEC61131-3 programming system (including Python language editing and the calling of ST function interfaces exported from the Python language by the IEC program, and successful construction, download, and normal operation on the PLC), and solves the problem of existing programmable system Python language integration lacking Python language editing capabilities and unable to call Python interfaces within the IEC program.

[0007] To achieve the above objectives, the present invention provides the following technical solution: a Python language integration method based on the IEC61131-3 programming system, the steps of which are as follows:

[0008] Step 1: Create a Python language project on the IEC61131-3-based programming system. Based on the project configuration, generate the corresponding ST function interface declaration. This declaration is visible in the PLC program, but the function implementation address is filled in at runtime. The programming system backend generates a C++ program responsible for loading the specified Python program and interacting with the Python runtime through the mechanism provided by CPython (an external library that provides an interface for C / C++ languages ​​to call Python code). Specifically, when the PLC calls this interface, the C++ dynamic library converts the passed parameters into Python objects, then calls the corresponding Python function, and converts the return value into a data type recognizable by the PLC. This process requires the user to pre-specify the Python environment path in the project configuration to ensure that the Python program can be correctly initialized and run.

[0009] Step 2: Build a Python language project on the IEC61131-3 programming system. After the build operation, the programming system backend compiles the C++ program generated in Step 1 into a dynamic library. This dynamic library contains the calling logic for the Python program interface and exports function symbols consistent with the ST interface declaration. When the project is online, this dynamic library is downloaded to the PLC along with other compiled files. During the IEC program compilation process, a function symbol is temporarily filled into all call instructions for Python exported functions in the compiled file. The compilation toolchain records the mapping relationship between these symbols and the actual functions in the dynamic library, generating relocation information. In runtime, the programming system uses symbol resolution to fill the actual addresses of the functions in the dynamic library back into the PLC compiled file. This process ensures that the PLC program can correctly jump to the C++ dynamic library functions and thus interact with the Python program.

[0010] Step 3: Create an IEC program that calls a Python program interface on the IEC61131-3 programming system. The IEC program loads and calls the exported function interface of the Python language project. Then, the IEC program is compiled and a compiled file is generated.

[0011] Step 4: Download and run the IEC program on the IEC61131-3-based programming system. Download the compiled file generated in Step 3 and the C++ dynamic library from Step 2 to the PLC. During runtime, load the dynamic library, obtain the actual addresses of functions in the library through symbol resolution, fill the addresses back into the ST function interface declaration, and run the program. A multi-threaded call mechanism based on sub-interpreters (a sub-interpreter is an independent execution environment within the Python runtime, allowing multiple threads to safely execute Python code concurrently) is used. The C++ dynamic library employs sub-interpreters to achieve thread isolation. When a task thread of the PLC executes an instruction to call a Python function, a sub-interpreter is created, and thread-local storage is used to maintain the mapping relationship between the thread and the sub-interpreter. During periodic calls, the interpreter is directly reused. The sub-interpreter is created and destroyed along with the task thread's lifecycle, and resources are released when the thread terminates, ensuring no memory leaks.

[0012] The most significant feature of step 1 is the provision of a Python language editing environment and dependency configuration. A Python language project is created based on the IEC61131-3 programming system. After creation, the external Python environment path is configured (to support the operation of the Python language project). Multiple Python files can be created. Users write business logic by editing Python scripts, creating the necessary exported function interfaces according to the functions, specifying interface information such as function names, parameter types, and return types. Then, the IEC61131-3 programming system converts these into ST language function interfaces (function interfaces for IEC programs to call).

[0013] The main feature of step 2 is the automatic generation of a C++ program based on user-created function interface information. This program is compiled into a C++ dynamic library when building the Python language project. The C++ program depends on the CPython library (an officially released external library that provides an interface for C language to call Python code) and contains function interfaces for interacting with the Python language project. The programming system backend converts C++-style parameter types to IEC-style parameter types to enable the passing of parameter data through the interface provided by the C++ dynamic library via the ST language interface. In the C++ program, functions in the Python language project are accessed through methods provided by CPython, and the return values ​​of the Python functions are received. Specifically, when the PLC program calls a function in the C++ dynamic library, the C++ side wraps the passed parameters into a Python object, calls the corresponding function in the Python process, obtains the calculation result, and returns it to the PLC program, thus realizing the interaction between Python and the PLC system.

[0014] The main feature of step 3 is that the programming system downloads the compiled files generated in step 3 and the C++ dynamic library and Python program generated in step 2 to the PLC. The compiled files are then packaged to ultimately produce a compiled file for downloading to the PLC.

[0015] The main feature of step 4 is the isolation and integration of PLC multithreaded tasks and the Python runtime based on CPython sub-interpreters. Specifically, this is achieved by creating a dedicated sub-interpreter for each PLC task thread, maintaining a one-to-one mapping between a thread and a sub-interpreter through thread-local storage, thus reusing the Python execution environment; the sub-interpreters are dynamically created and destroyed throughout the thread's lifecycle, ensuring the reclamation of Python runtime resources. This allows PLC periodic tasks to call Python functions in real-time and safely, balancing real-time performance and independence. During runtime, the PLC system loads C++ dynamic library files from the Python user library. It parses and backfills the actual function interface addresses provided in the dynamic library, generates machine code, and executes it.

[0016] The advantages and positive effects of the invention of a Python language integration method based on the IEC61131-3 programming system are:

[0017] The Python language integration method for the IEC61131-3 programming system can support the editing of Python language and generate C++ dynamic libraries to parse and call interfaces in Python programs, pass the return values ​​of Python interfaces to the IEC program, and finally be successfully compiled, downloaded to PLC and run normally.

[0018] Compared with existing PLC programming systems, the Python language integration method for the IEC61131-3 programming system avoids the low real-time performance problem caused by relying on communication protocols to interact with external Python processes. The method of this invention can efficiently and in real-time interact with Python processes. Attached Figure Description

[0019] Figure 1 This is an overall schematic diagram of a Python language integration method based on the IEC61131-3 programming system described in this invention;

[0020] Figure 2 This is a Python example program diagram of a Python language integration method based on the IEC61131-3 programming system described in this invention;

[0021] Figure 3 This is a schematic diagram illustrating the process of converting a Python language project based on the IEC61131-3 programming system into a C++ dynamic library, as described in this invention.

[0022] Figure 4 This is an IEC program diagram illustrating the calling of the Python program interface in a Python language integration method based on the IEC61131-3 programming system described in this invention.

[0023] Figure 5 This is a schematic diagram of the IEC program calling Python function interface and compilation process of the Python language integration method based on the IEC61131-3 programming system described in this invention;

[0024] Figure 6 This is a schematic diagram of the process of downloading and compiling the Python language integration method based on the IEC61131-3 programming system described in this invention and running it on a PLC.

[0025] Figure 7 This is a schematic diagram illustrating the running results of the Python language integration method based on the IEC61131-3 programming system described in this invention; Detailed Implementation

[0026] The technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings of the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of the present invention.

[0027] This invention provides a Python language integration method based on the IEC61131-3 programming system. The overall idea of ​​the solution is as follows: Figure 1 As shown, a Python user project and function interface are created and edited on the IEC61131-3 programming system. The interface information must be consistent with the function interfaces that need to be exported in the Python program. The created Python project is then built to generate user library files and C++ dynamic libraries. An IEC program that needs to call the Python interface is created and edited on the IEC61131-3 programming system. After completion, the IEC program is compiled to generate a compiled file. Subsequently, the IEC program compiled file (including C++ dynamic libraries and Python program code) is downloaded to the PLC. The PLC parses the downloaded file and reads the actual function addresses, finally generating machine code to execute the program. Specifically, the following steps are included:

[0028] Step 1: Create a Python language project named "Library0" on the IEC61131-3 based programming system, such as... Figure 2 As shown, the project structure includes Python source code files and exported function interfaces. First, create a Python file at the Python source code file node, such as... Figure 5 As shown, a linear fitting prediction function is implemented using a Python program. This function receives the current input value, fits a linear model using historical data, and predicts the output for the next period. The PLC main program will periodically call this interface. After editing, create the function interface to be exported (DINT datapredict(DINT,DINT,DINT)). At this point, an ST language function interface with the same name needs to be created in the Python language project according to the parameter types, function name, and return type expected in the design phase of the "datapredict" interface. After the user creates this function interface on the programming system interface, the system backend will convert the data type to the IEC program variable type, thereby ensuring that the interface can be called correctly in the IEC program.

[0029] Step 2: Build a Python language project on the IEC61131-3 programming system. After the build is complete, a C++ dynamic library (.dll or .so format) will be generated, such as... Figure 3As shown, after completing the Python program writing and creating the function interface (defdatapredict), the programming system generates a corresponding C++ program based on the interface information. This C++ program contains a C++ interface named "datapredict". The interface parameter types and return value types are consistent with the expected types of the Python interface with the same name. The C++ interface (int datapredict(int,int,int)) uses CPython to convert the C++ variable type to the Python variable type. Then, it calls the Python interface with the same name through the method provided by CPython, obtains the return value, converts the return value type to the PLC variable type, and then sends it back to the PLC program.

[0030] Step 3: Create an ST type program in the IEC61131-3-based programming system, call the "datapredict" function interface exported by the Python language project, and compile it. The specific calling method is as follows: Figure 4 As shown, four DINT type variables are created: "current_x", "current_y", "next_x", and "predict_next_y". The program first sets the value of "current_y" to twice the value of "current_x", and sets "next_x" to "current_x + 1" to represent the x of the next cycle. These variables are used as input to call the "datapredict" interface, and the variable named "predict_next_y" receives the return value of this interface. If the return value is greater than 10000, an alarm operation is executed. Figure 5 As shown, the IEC program that calls the IEC function interface in step 1 is compiled and a compiled file (.prg file) is generated. This compiled file contains the virtual address of the IEC function interface. When the PLC parses the compiled file, the actual function address will be filled back into this virtual address.

[0031] Step 4: Deploy and verify the system on the IEC61131-3 programming system, such as... Figure 6 As shown, the PLC compilation file, C++ dynamic library, and Python program generated in step 3 are deployed to the PLC. The PLC parses the compilation file and searches the function name symbol table. If the "datapredict" interface name corresponding to the virtual address in step 3 is found in the symbol table, the C++ dynamic library is dynamically loaded, the actual function interface address is obtained, and this address is filled back into the virtual address in the compilation file. After parsing, machine code is generated, and the PLC executes the machine code. Figure 7As shown, actual operation verification shows that when the input value is x, the predicted output is 2x, which conforms to the linear relationship preset in the ST program. This proves that the Python algorithm can be correctly embedded into the PLC cycle task and output results stably, realizing the complete integration and real-time calling of the Python scientific computing library by the IEC61131-3 programming system.

[0032] The above description is merely a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the present invention should be covered within the scope of protection of the claims of the present invention.

Claims

1. A Python language integration method based on the IEC61131-3 programming system, characterized in that: Step 1: Create a Python language project on the IEC61131-3 programming system, edit the Python program to implement specific functional modules, such as data processing and algorithm implementation. During the configuration process, create the Python function interface to be exported, and specify the interface name, parameter information, and return type. After creation, the corresponding ST language (a PLC programming language based on the IEC61131-3 standard) exported function interface (the function interface for the PLC program to call) will be generated. Step 2: Build a Python language project on the IEC61131-3 programming system. Then build the Python language project from Step 1. After the project is completed, a C++ dynamic library (.dll or .so format) will be generated. This C++ library contains the implementation of the ST function interface from Step 1. Step 3: Create an IEC program calling Python program interface on the IEC61131-3 programming system. The IEC program should include a program that calls the ST language exported function interface in Step 1. The program loads and calls the functions in the dynamic library in Step 2, compiles the IEC program, and generates a compiled file. Step 4: Download and run the IEC program on the IEC61131-3 programming system. Download the compiled file generated in Step 3 and the C++ dynamic library in Step 2 to the PLC. Load the dynamic library at runtime, obtain the actual address of the function in the library through symbol resolution, fill the address back into the ST function interface declaration and run.

2. Step 1 according to claim 1, characterized in that: It provides a Python language editing environment and dependency configuration, and creates Python language projects based on the IEC61131-3 programming system. After creation, the external Python environment path is configured (to support external libraries in the Python ecosystem). It supports the creation of multiple Python files. Users write business logic by editing Python scripts, create the function interfaces that need to be exported according to the functions, and specify interface information such as function name, parameter type, and return type. Then, it converts them into ST language function interfaces (function interfaces for IEC programs to call) through the IEC61131-3 programming system.

3. Step 2 according to claim 1, characterized in that: The system automatically generates a C++ program based on user-created function interface information. This program is compiled into a C++ dynamic library when building a Python language project. The C++ program depends on the CPython library (an officially released external library that provides an interface for C language to call Python code) and contains function interfaces for interacting with the Python language project. The ST language interface allows the PLC program to pass parameter data by calling the interface provided by the C++ dynamic library. In the C++ program, functions in the Python language project are accessed through methods provided by CPython, and the return values ​​of Python functions are received. Specifically, when the PLC program calls a function in the C++ dynamic library, the C++ side wraps the passed parameters into a Python object, calls the corresponding function in the Python process, obtains the calculation result, and returns it to the PLC program to realize the interaction between Python and the PLC system.

4. Step 3 according to claim 1, characterized in that: The IEC program that calls the ST language interface provided in step 1 is compiled using the IEC61131-3 programming system, and the compiled file is generated and packaged to finally produce a compiled file for downloading the PLC.

5. Step 4 according to claim 1, characterized in that: The programming system downloads the compiled file generated in step 3 and the C++ dynamic library and Python program generated in step 2 to the PLC. The PLC system loads the C++ dynamic library file in the Python user library, parses the actual function interface address provided in the dynamic library and backfills it, and then generates machine code and executes it.