A method and system for integrating execution of python scripts in programmable controllers

By integrating Python function blocks into the PLC and adopting a compile-time registration-runtime execution mechanism, the limitations of traditional PLC programming languages ​​in complex algorithms and IT system integration are solved, achieving seamless integration of Python and PLC, improving development efficiency and system stability, and meeting the high real-time requirements of industrial control.

CN122219291APending Publication Date: 2026-06-16NR ELECTRIC CO LTD +2

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
NR ELECTRIC CO LTD
Filing Date
2026-03-04
Publication Date
2026-06-16

AI Technical Summary

Technical Problem

Traditional PLC programming languages ​​have limitations in implementing complex algorithms, data analysis, and integration with IT systems, resulting in low development efficiency, resource scarcity, and cumbersome integration. Existing solutions cannot achieve seamless, native, and standardized integration between Python and PLC.

Method used

It adopts a two-stage mechanism of compile-time registration and runtime execution. During the compilation stage, it binds the Python script to the PLC function block interface. At runtime, it performs bidirectional safe bridging of data types and script execution, provides initialization and execution functions for Python function blocks, and achieves seamless integration of Python with the IEC 61131-3 environment through an enhanced integrated development environment and runtime system.

🎯Benefits of technology

It achieves deep integration of Python capabilities with PLC control logic, lowers the integration threshold, improves development efficiency and system stability, meets high real-time requirements, and provides rich ecosystem resources and flexibility.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122219291A_ABST
    Figure CN122219291A_ABST
Patent Text Reader

Abstract

The application discloses a method and system for integrating and executing a Python script in a programmable controller and relates to the technical field of industrial automation control. The method comprises the following steps: compiling a function block for writing a Python script in an integrated development environment to generate corresponding initialization functions and execution functions; the initialization functions comprise codes for registering function block interface variable definition data and script content to a controller runtime system; the execution functions comprise codes for calling the runtime system to execute the script and inputting a function block instance pointer; during runtime of the programmable controller, the initialization functions are executed to complete registration of the function block in the runtime system; and the function block is called in program task scheduling to execute the execution functions and output execution results. The application can realize seamless integration of Python and an IEC environment, and can balance development convenience, execution safety and system real-time performance.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to a method and system for integrating Python script execution into a programmable controller, belonging to the field of industrial automation control technology. Background Technology

[0002] Traditional programmable logic controller (PLC) programming development strictly follows the IEC 61131-3 international standard, supporting five dedicated industrial programming languages: Structured Text (ST), Ladder Diagram (LD), Function Block Diagram (FBD), Sequential Function Chart (SFC), and Instruction List (IL). These languages ​​have undergone long-term industrial validation and have the advantages of high stability, strong real-time performance, and intuitive logic expression, which can meet the needs of most conventional industrial control scenarios.

[0003] However, with the trend of industrial automation upgrading towards intelligence and digitalization, the demand for complex algorithms, data analysis, deployment of artificial intelligence models, and integration with upper-level IT systems (such as cloud platforms and relational databases) in industrial control scenarios is increasing. At this point, the limitations of traditional IEC 61131-3 proprietary languages ​​are becoming increasingly apparent.

[0004] Complex algorithms are difficult to implement: For complex numerical calculations such as matrix operations, data fitting, and signal processing, the IEC language lacks dedicated library support, requiring engineers to manually write a large amount of low-level code, which is inefficient and prone to errors.

[0005] Limited ecological resources: Compared to general programming languages, the IEC language has very few third-party library resources, making it impossible to quickly reuse mature data analysis, machine learning, and network communication capabilities;

[0006] Integration with IT systems is cumbersome: When interacting with cloud platforms and databases, it is necessary to rely on dedicated communication modules or extended commands, which are complex to configure and lack flexibility.

[0007] On the other hand, Python, as an interpreted, high-level general-purpose programming language, is characterized by its concise syntax, ease of learning, and extremely rich ecosystem. It boasts powerful data processing libraries such as NumPy and Pandas, machine learning frameworks such as TensorFlow and PyTorch, and convenient network and database interaction libraries such as Requests and SQLAlchemy. It possesses natural technical advantages in data analysis, machine learning, and network communication, effectively compensating for the shortcomings of traditional PLC languages.

[0008] Currently, some solutions in the industry have attempted to introduce Python into industrial control scenarios, mainly falling into two categories:

[0009] External collaboration solution: A Python program runs independently on a PC or edge server, interacting with the PLC via industrial communication protocols such as OPC UA and Modbus. This solution has significant drawbacks: it adds extra hardware nodes, increasing deployment costs and complexity; unavoidable communication delays prevent it from meeting the demands of high real-time scenarios; and it introduces a point of failure—a problem with the communication link or edge server will directly impact the entire control process.

[0010] The script interpreter embedding solution involves directly embedding a standalone Python interpreter into the PLC runtime system, allowing users to upload and execute Python scripts. However, this solution is typically disconnected from the IEC 61131-3 programming environment. Data exchange between the Python script and the PLC main program requires users to manually write data reading and writing code, lacking a standardized interface. Scheduling and synchronization of the two programs also require manual management by the user, resulting in an inconsistent development experience, difficult debugging, and difficulty in ensuring that the execution of the Python script does not affect the real-time performance and stability of the PLC main program.

[0011] Therefore, the industry urgently needs a technical solution that can seamlessly, natively, and in a standardized manner integrate Python's powerful capabilities with the IEC 61131-3 standard PLC programming environment, enabling industrial control engineers to conveniently and safely call Python's ecosystem capabilities just like using traditional standard function blocks, without having to worry about complex underlying interaction details. Summary of the Invention

[0012] The purpose of this invention is to provide a method and system for integrating and executing Python scripts into a programmable controller, creating a new type of "Python function block". Through a two-stage mechanism of "compile-time registration-runtime execution", the binding and registration code generation of function block interface variable data with Python scripts are completed during the compilation stage, and the bidirectional secure bridging of data types and the controllable execution of Python scripts are completed during the runtime system execution stage, so as to achieve deep integration of Python capabilities and PLC control logic.

[0013] To achieve the above objectives, the present invention is implemented using the following technical solution.

[0014] On one hand, the present invention provides a method for integrating the execution of Python scripts in a programmable controller, comprising:

[0015] Compile the functional blocks used to write Python scripts in the integrated development environment, and generate the corresponding initialization and execution functions;

[0016] The initialization function includes code for registering the function block interface variable definition data and Python script content with the controller runtime system; the execution function includes code for calling the runtime system to execute the Python script and passing in a function block instance pointer.

[0017] During programmable controller operation, the initialization function is executed to complete the registration of the function block in the runtime system;

[0018] The function block is invoked in the program task scheduler to execute the execution function and output the execution result.

[0019] Optionally, the process of executing the execution function is as follows:

[0020] Based on the registered function block interface variable definition data, convert the IEC 61131-3 format data in the function block into Python format data;

[0021] Execute the registered script in the Python interpreter, and take the converted Python formatted data as the script input;

[0022] Convert the Python format data output after the script is executed back to IEC 61131-3 format and write it back to the output variable of the function block instance.

[0023] Optionally, the execution process of the execution function further includes: the runtime system capturing runtime exceptions during the execution of the Python script, encapsulating the exception type, exception information and the location of the exception as the execution status of the function block, and returning it to the upper-level PLC program logic for subsequent fault tolerance processing.

[0024] Optionally, the conversion rules include: converting BOOL (Boolean) type in IEC 61131-3 format to bool (Boolean) type in Python format; converting INT / DINT (16-bit signed integer / 32-bit signed integer) type in IEC 61131-3 format to int (integer) type in Python format; converting REAL (32-bit floating-point number) type in IEC 61131-3 format to float (64-bit double-precision floating-point number) type in Python format; converting STRING (single-byte string) type in IEC 61131-3 format to str (string) type in Python format; converting ARRAY (array) type in IEC 61131-3 format to list (list) type in Python format; and converting STRUCT (struct) type in IEC 61131-3 format to custom class instance in Python format.

[0025] Optionally, when compiling the functional block, the Python script is also subjected to syntax verification. If a syntax error is detected, the compilation process is interrupted and a clear error message is output in the integrated development environment. The syntax error includes keyword spelling errors, indentation errors, and incomplete syntax structure.

[0026] Optionally, the function block interface definition conforms to the IEC 61131-3 standard interface; the interface variables conforming to the IEC 61131-3 standard include input variables, output variables, and internal variables; the variable types include at least one of BOOL (Boolean), SINT (8-bit signed integer), USINT (8-bit unsigned integer), INT (16-bit signed integer), UINT (16-bit unsigned integer), DINT (32-bit signed integer), UDINT (32-bit unsigned integer), LINT (64-bit signed integer), ULINT (64-bit unsigned integer), REAL (32-bit floating-point number), LREAL (64-bit floating-point number), STRING (single-byte string), WSTRING (double-byte string), BYTE (byte type), WORD (word), DWORD (double word), LWORD (quad word), ENUM (enumeration), ARRAY (array), and STRUCT (structure).

[0027] Optionally, the execution function specifies the data instance to be processed by passing a pointer or memory address of the function block instance to the runtime system; the runtime system locates the input, output, and internal variable data areas of the specific instance based on the pointer or memory address.

[0028] Secondly, the present invention provides a programmable controller system, including an enhanced integrated development environment and an operation control system;

[0029] The enhanced integrated development environment includes a dedicated Python function block editor, an enhanced compiler, and a project management module; it is used for creating, editing, and compiling Python function blocks.

[0030] The dedicated editor supports simultaneous editing of IEC 61131-3 standard interfaces and Python script logic; the enhanced compiler integrates a Python syntax checker and a registration code generator on the basis of the traditional IEC compiler, used to complete the syntax checking of Python scripts, as well as the automatic generation of Python function block initialization functions and execution functions; the project management module is responsible for managing PLC project files containing Python function blocks, supporting the saving, importing, exporting and version control of Python function blocks, and is consistent with the project management logic of traditional IEC function blocks.

[0031] The operation control system includes a Python execution engine, a type bridging module, a function block registration management module, and an exception handling module; it is used to complete the registration of Python function blocks and data conversion.

[0032] The Python execution engine is used to execute user-written Python scripts; the type bridging module is used for bidirectional conversion between IEC 61131-3 data types and Python data types; the function block registration management module is used to maintain a mapping table between registered Python function block variable data and scripts, and to handle registration and execution requests for Python function blocks; the exception handling module is used to capture various exceptions during the execution of Python scripts, encapsulate exception information and return it to the upper-level logic, and also provides exception logging functionality.

[0033] Thirdly, the present invention provides a computer-readable storage medium having a computer program / instructions stored thereon, which, when executed by a processor, implements the steps of the method for integrating and executing Python scripts in a programmable controller as described in any of the first aspects.

[0034] Fourthly, the present invention provides a computer device / equipment / system, characterized in that it comprises:

[0035] Memory, used to store computer programs / instructions;

[0036] A processor for executing the computer program / instructions to implement the steps of integrating the execution of a Python script in a programmable controller as described in any of the first aspects.

[0037] Compared with the prior art, the beneficial effects achieved by the present invention are as follows:

[0038] 1. This invention achieves seamless integration of Python with the IEC 61131-3 environment, balancing development convenience, execution security, and system real-time performance. It provides a native and efficient implementation path for directly integrating advanced intelligent algorithms at the edge control layer, breaking through the technical bottleneck of traditional PLCs in intelligent applications. It is a key enabling technology for the upgrading of industrial automation to intelligence and digitalization, and has broad prospects for industrial applications.

[0039] 2. This invention seamlessly integrates and lowers the application threshold: Python is embedded into the PLC programming environment in the form of function blocks conforming to the IEC 61131-3 standard, so that industrial control engineers do not need to master complex external communication technologies or low-level data interaction logic. They can directly utilize Python's rich ecosystem resources, which greatly reduces the integration threshold of advanced algorithms in industrial control scenarios and realizes "using Python like using standard function blocks".

[0040] 3. This invention provides a smooth development experience and improves development efficiency: It provides an integrated editing and compilation environment, maintains the engineering and modular characteristics of PLC programming, and avoids the fragmented feeling of traditional Python integrated solutions;

[0041] 4. This invention ensures safe and controllable execution and guarantees system stability: The type bridging module ensures the security of data conversion, and the exception capture mechanism provides fault tolerance, ensuring that the execution of Python scripts will not affect the determinism, real-time performance and stability of the PLC main program, thus meeting the high reliability requirements of industrial control scenarios.

[0042] 5. This invention balances performance and flexibility: The Python script is executed inside the PLC controller, avoiding communication delays associated with external collaborative solutions and meeting the requirements of high real-time scenarios; at the same time, Python's dynamic syntax and rich ecosystem enable the rapid implementation of complex data analysis, machine learning inference, and other functions, improving the flexibility and scalability of the PLC control system. Attached Figure Description

[0043] Figure 1 This is a flowchart illustrating the method for integrating Python script execution into a programmable controller according to the present invention.

[0044] Figure 2 This is a schematic diagram of the Python function block editor interface of the present invention;

[0045] Figure 3 This is a flowchart illustrating the processing of Python function blocks during the compilation phase of this invention;

[0046] Figure 4 This is an internal data flow diagram of the Python function blocks executed by the system during the runtime of this invention;

[0047] Figure 5 This is a schematic representation of the conversion between IEC data types and Python data types in this invention;

[0048] Figure 6 This is a schematic diagram of the programmable controller system architecture of the present invention. Detailed Implementation

[0049] The technical solution of the present invention will be described in detail below with reference to the accompanying drawings and specific embodiments. It should be understood that the embodiments of the present invention and the specific features in the embodiments are detailed descriptions of the technical solution of the present invention, rather than limitations thereof. In the absence of conflict, the embodiments of the present invention and the technical features in the embodiments can be combined with each other.

[0050] Example 1:

[0051] This embodiment describes a method for integrating Python script execution into a programmable logic controller (PLC), such as... Figure 1 As shown, it includes:

[0052] Compile the functional blocks used to write Python scripts in the integrated development environment, and generate the corresponding initialization and execution functions;

[0053] The function block interface definition conforms to the IEC 61131-3 standard interface; the initialization function includes code for registering the function block interface variable definition data and Python script content with the controller runtime system; the execution function includes code for calling the runtime system to execute the Python script and passing in a function block instance pointer;

[0054] During programmable controller operation, the initialization function is executed to complete the registration of the function block in the runtime system;

[0055] The function block is invoked in the program task scheduler to execute the execution function and output the execution result.

[0056] 1) Editing and Definition Stage

[0057] In a PLC integrated development environment (IDE) conforming to the IEC 61131-3 standard, a dedicated editor for "creating new Python function blocks" is provided, as shown in the interface diagram below. Figure 2 As shown, this editor uses a dual-region layout to integrate interface definition and script editing:

[0058] Interface Definition Area: In this area, users define standardized interfaces for Python function blocks according to the IEC 61131-3 standard, including input variables, output variables, and internal variables. All variable data types strictly adhere to the IEC 61131-3 standard, supporting common data types such as BOOL, SINT, USINT, INT, UINT, DINT, UDINT, LINT, ULINT, REAL, LREAL, STRING, WSTRING, BYTE, WORD, DWORD, LWORD, ENUM, ARRAY, and STRUCT. Variable naming conventions are also consistent with traditional IEC function blocks.

[0059] Python Script Editing Area: Within the same editor, a Python script editing area with syntax highlighting and code completion is provided. Users can write the core business logic of function blocks using standard Python syntax in this area, and can directly reference IEC standard interface variables created in the interface definition area through predefined variable names (without manually writing data read / write code). For example, if a user creates an input variable IN_REAL_TEMPERATURE (REAL type) and an output variable OUT_BOOL_ALARM (BOOL type) in the interface definition area, they can directly obtain the input temperature value through IN_REAL_TEMPERATURE in the Python script and output the alarm status by assigning a value to OUT_BOOL_ALARM.

[0060] 2) Compilation and registration code generation stage

[0061] Once the user finishes editing the Python function block and triggers compilation, the PLC enhanced compiler will perform special processing on that Python function block. The processing flow is as follows: Figure 3 As shown, the specific steps include:

[0062] Python script syntax validation: The compiler calls the embedded lightweight Python syntax checker to perform static syntax analysis on the user-written Python script. If a syntax error is detected (such as misspelled keywords, indentation errors, incomplete syntax, etc.), the compilation process is immediately interrupted, and the error message is marked in the corresponding position in the IDE's Python script editing area. At the same time, a detailed error description is output in the output window to help the user locate and correct the error.

[0063] Generate standard IEC function block framework: The compiler generates an external framework for Python function blocks that is consistent with traditional function blocks in accordance with the IEC 61131-3 standard, ensuring that the function block can be recognized, called and scheduled by other PLC programs.

[0064] Generate registration code and execution functions: The compiler automatically generates two core functions for this Python function block: an initialization function and an execution function.

[0065] Initialization function: It contains code that calls the PLC Runtime System (RTE) registration interface. This code registers the unique identifier of the Python function block, the complete variable information of each IEC interface variable (including variable name, data type, variable type (input / output / local), instance data area address offset), and the entire text content of the Python script into the Python function block management module of the runtime system.

[0066] Execution function: It contains code that calls the PLC runtime system execution interface. When this code is called, it passes the pointer of the current Python function block instance (or the starting memory address of the instance data area) to the runtime system to locate the specific instance data and receives the script execution result (normal completion or exception information) returned by the runtime system.

[0067] 3) Runtime System Execution Phase

[0068] Once the compiled PLC project is downloaded to the programmable controller and started running, the Python function blocks will be executed under the control of the runtime system, and the internal data flow will be as follows: Figure 4 As shown, it is divided into two steps: initialization and execution.

[0069] Initialization steps: When the PLC program starts, the runtime system will call the initialization functions of all function blocks (including traditional IEC function blocks and Python function blocks) in execution order. For Python function blocks, the runtime system's function block registration management module will receive the registration information passed by the initialization function and complete the following operations:

[0070] Parse the interface variable data, establish a one-to-one mapping relationship between IEC variable names, memory addresses and Python variable names, form a mapping table and cache it;

[0071] Once the Python function block's state initialization is complete, mark it as "ready" and wait for task scheduling.

[0072] Execution steps: When the PLC task scheduler schedules this Python function block, it will call its execution function, triggering the runtime system to execute the following core processes:

[0073] Data export and type conversion: The runtime system's type bridging module, based on the mapping table established during the initialization phase, locates the instance data area through the function block instance pointer, reads the current values ​​of all input variables, and then performs type conversion according to preset rules (such as...). Figure 5 As shown, this method enables bidirectional secure conversion of IEC 61131-3 formatted data to Python native data types. For example, it converts the IEC REAL type to a Python float type, the IEC ARRAY type to a Python list type, and the IEC STRUCT type to a Python custom class instance.

[0074] Python script execution: The type bridging module injects the converted Python format data into the script's execution context, and then the Python engine executes the Python script.

[0075] Data import and result write-back: After the Python script finishes execution, the type bridging module reads the final values ​​of all output variables from the execution context, converts the Python format data back to the IEC 61131-3 standard format according to the reverse type conversion rules, and then writes the data back to the corresponding position in the function block instance data area through the instance pointer.

[0076] Exception capture and status return: The runtime system captures runtime exceptions that may occur during the execution of the Python script (such as numerical calculation overflow, undefined variables, abnormal data types, etc.), encapsulates the exception type, exception information and the script line number where the exception occurred into the execution status of the function block, writes it back into the function block's dedicated status variable, and returns it to the upper-level PLC program logic for users to write fault-tolerant processing logic.

[0077] Example 2:

[0078] This embodiment describes the specific implementation process of the present invention in detail, based on a specific application scenario: industrial field temperature anomaly alarm processing based on Python function blocks.

[0079] 1. Overview of Implementation Scenarios

[0080] Temperature data moving average filtering based on NumPy – Raw temperature data collected by industrial field temperature sensors is susceptible to fluctuations due to electromagnetic interference. By implementing a moving average filtering algorithm using Python's NumPy library, noise reduction is achieved in the raw temperature data, improving the stability and accuracy of temperature monitoring (in complex numerical calculation scenarios).

[0081] Create a Python function block FB_PY_TEMP_FILTER, which focuses on implementing the moving average filtering function for temperature data and is called by the loop task (100 milliseconds period) of the PLC main program.

[0082] 2. Implementation of the editing and definition phase

[0083] 1) Open the enhanced PLC integrated development environment, create a new Python function block, and name it FB_PY_TEMP_FILTER.

[0084] 2) In the interface definition area, define the interface variables according to the IEC 61131-3 standard, as shown in Table 1:

[0085] Table 1 Interface Variable Definitions

[0086]

[0087] In the Python script editing area, write the core business logic: First, verify that the window size is between 3 and 10. Then, manage the latest temperature data through the internal cache INT_ARRAY_RAW_CACHE. When the cache is not full, append new data to the end. When the cache is full, shift left and discard the oldest data before appending. Then, determine the amount of cached data. If it is greater than or equal to the window size, take the corresponding number of data, calculate the average value using NumPy, and output the filtered result with 2 decimal places. If it is insufficient, directly output the original data and mark the filtering as not ready.

[0088] 2. Implementation of the compilation and registration code generation phase

[0089] The PLC project compilation is triggered, and the enhanced compiler begins processing the FB_PY_TEMP_FILTER function block:

[0090] 1) The Python syntax checker is called to perform syntax analysis on the written script. No syntax errors are detected, and compilation continues.

[0091] 2) Generate an external framework for the function block that conforms to the IEC 61131-3 standard, so that FB_PY_TEMP_FILTER can be called by the PLC main program.

[0092] 3) Generate the initialization function FFB_PY_TEMP_FILTER_INIT(), which contains code that calls the runtime system's RegisterPythonFB() interface to register the function block identifier, five interface variable data (name, data type, variable type, address offset), and the complete Python script content to the runtime system.

[0093] 4) Generate the execution function FB_PY_TEMP_FILTER_EXEC(), which contains code that calls the runtime system's ExecutePythonFB() interface, passes a function block instance pointer, and receives the execution result.

[0094] Compilation complete, generating a target project file that can be downloaded to the PLC.

[0095] 3. Implementation during runtime execution phase

[0096] Initialization steps: Download the compiled project file to the PLC controller and start running it. During PLC runtime, the system performs the following operations:

[0097] The task scheduler calls the FB_PY_TEMP_FILTER_INIT() initialization function.

[0098] The function block registration management module receives registration information, parses interface variable data, establishes and caches a mapping table, caches function block Python scripts, and waits for task scheduling.

[0099] Periodic execution steps (PLC cyclic task cycle is 100 milliseconds):

[0100] The task scheduler calls the FB_PY_TEMP_FILTER_EXEC() function to execute the function, passing a pointer to the function block instance.

[0101] The type bridging module reads the current values ​​of input variables (such as IN_REAL_TEMP_RAW=85.36℃, IN_DINT_WINDOW_SIZE=5) and internal variables according to the mapping table, converts them into Python format data (REAL→float, DINT→int), and injects them into the script execution context.

[0102] The Python execution engine executes the script: validates the window size parameter (if the input is 2, it is automatically corrected to 3); updates the internal cache array (appends new original data, and shifts left to discard old data when the cache is full); when the cached data reaches the window size (5), it calls numpy.mean() to calculate the average value and completes the filtering.

[0103] The type bridging module converts the output variables (OUT_REAL_TEMP_FILTERED=85.42℃, OUT_BOOL_FILTER_READY=True) back to IEC 61131-3 format and writes them back to the instance data area; modifications to internal variables (such as INT_DINT_CACHE_LEN auto-increment and INT_ARRAY_RAW_CACHE element update) are directly synchronized to the instance data area for use in the next cycle.

[0104] The exception handling module captures potential exceptions (such as NumPy array operation exceptions), encapsulates them as execution status returns, and does not affect the operation of the PLC main program; if no exception is captured, the function block execution status is marked as "normal completion".

[0105] In this embodiment, the Python function block FB_PY_TEMP_FILTER can be seamlessly called by the PLC main program, realizing the core function of temperature data moving average filtering. Throughout the development process, engineers do not need to worry about details such as underlying data type conversion and cache storage. They only need to define the interface according to the development habits of traditional IEC function blocks and write concise filtering logic with the help of the NumPy library, successfully achieving seamless integration of Python numerical calculation capabilities with the PLC control environment.

[0106] Example 3:

[0107] This embodiment describes a programmable controller system, such as... Figure 6 As shown, it consists of two main parts: an enhanced integrated development environment and a programmable logic controller (PLC) runtime system. These two parts work together to achieve seamless integration of Python scripts with the PLC environment.

[0108] 1) Enhanced Integrated Development Environment

[0109] This environment is an extension of the traditional IEC 61131-3 PLC development environment, primarily used for creating, editing, and compiling Python function blocks. Its core components include:

[0110] A dedicated Python function block editor: It adopts a dual-area layout, supports integrated IEC standard interface definition and Python script editing, and provides convenient functions such as syntax highlighting, code completion, and error annotation, maintaining the same operating experience as traditional PLC development.

[0111] Enhanced compiler: Based on the traditional IEC compiler, it integrates a Python syntax checker and a registration code generator, which can perform syntax checking of Python scripts and automatically generate Python function block initialization functions and execution functions, while ensuring that the generated function block framework conforms to the IEC 61131-3 standard.

[0112] Project Management Module: Responsible for managing PLC project files containing Python function blocks, supporting the saving, importing, exporting and version control of Python function blocks, and maintaining consistency with the project management logic of traditional IEC function blocks.

[0113] 2) Programmable Controller Runtime System

[0114] This system is deployed inside the PLC controller and is the core supporting the operation of Python function blocks. It has built-in lightweight components to avoid consuming too many controller resources. The core components include:

[0115] Python Execution Engine: An instance of the Python interpreter that can execute user-written Python scripts.

[0116] Type bridging module: Responsible for bidirectional safe conversion between IEC 61131-3 data types and Python data types, maintaining a preset type conversion rule table, supporting conversion of basic data types, arrays and structures, and ensuring the accuracy and efficiency of data conversion.

[0117] Function block registration management module: Maintains a mapping table between registered Python function block variable data and scripts, and is responsible for handling registration and execution requests of Python function blocks. It is the core scheduling center of the runtime system.

[0118] The exception handling module is responsible for capturing various exceptions during the execution of Python scripts, encapsulating exception information and returning it to the upper-level logic. It also provides exception logging functionality to facilitate subsequent debugging and troubleshooting for engineers.

[0119] Example 4:

[0120] Based on the same inventive concept as other embodiments, this embodiment describes a computer-readable storage medium having a computer program / instructions stored thereon, which, when executed by a processor, implements the steps of the method for integrating and executing Python scripts in a programmable controller as described in any of Embodiment 1.

[0121] Example 5:

[0122] Based on the same inventive concept as other embodiments, this embodiment introduces a computer device / apparatus / system, characterized in that it includes:

[0123] Memory, used to store computer programs / instructions;

[0124] A processor for executing the computer program / instructions to implement the steps of integrating the execution of Python scripts in a programmable controller as described in any of Embodiment 1.

[0125] Those skilled in the art will understand that embodiments of the present invention can be provided as methods, systems, or computer program products. Therefore, the present invention can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the present invention can take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0126] This invention is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart illustrations and / or block diagrams. Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.

[0127] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.

[0128] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.

[0129] The embodiments of the present invention have been described above with reference to the accompanying drawings. However, the present invention is not limited to the specific embodiments described above. The specific embodiments described above are merely illustrative and not restrictive. Those skilled in the art can make many other forms under the guidance of the present invention without departing from the spirit and scope of the claims. All of these forms are within the protection scope of the present invention.

Claims

1. A method for integrating the execution of Python scripts into a programmable logic controller (PLC), characterized in that, include: Compile the functional blocks used to write Python scripts in the integrated development environment, and generate the corresponding initialization and execution functions; The initialization function includes code for registering the function block interface variable definition data and Python script content with the controller runtime system; the execution function includes code for calling the runtime system to execute the Python script and passing in a function block instance pointer. During programmable controller operation, the initialization function is executed to complete the registration of the function block in the runtime system; The function block is invoked in the program task scheduler to execute the execution function and output the execution result.

2. The method for integrating Python script execution into a programmable controller according to claim 1, characterized in that, The process of executing the aforementioned function is as follows: Based on the registered function block interface variable definition data, convert the IEC 61131-3 format data in the function block into Python format data; Execute the registered script in the Python interpreter, and take the converted Python formatted data as the script input; Convert the Python format data output after the script is executed back to IEC 61131-3 format and write it back to the output variable of the function block.

3. The method for integrating Python script execution into a programmable controller according to claim 2, characterized in that, The execution process of the execution function also includes: the runtime system captures runtime exceptions during the execution of the Python script, encapsulates the exception type, exception information and the location of the exception as the execution status of the function block, and returns it to the upper-level PLC program logic for subsequent fault tolerance processing.

4. The method for integrating Python script execution in a programmable controller according to claim 2, characterized in that, The conversion rules include: converting BOOL type in IEC 61131-3 format to bool type in Python format, INT / DINT type in IEC 61131-3 format to int type in Python format, REAL type in IEC 61131-3 format to float type in Python format, STRING type in IEC 61131-3 format to str type in Python format, ARRAY type in IEC 61131-3 format to list type in Python format, and STRUCT type in IEC 61131-3 format to custom class instance in Python format.

5. The method for integrating Python script execution in a programmable controller according to claim 1, characterized in that, When compiling the functional block, the Python script is also subjected to syntax verification. If a syntax error is detected, the compilation process is interrupted and a clear error message is output in the integrated development environment. The grammatical errors include keyword spelling errors, indentation errors, and incomplete grammatical structures.

6. The method for integrating Python script execution in a programmable controller according to claim 1, characterized in that, The function block interface definition conforms to the IEC 61131-3 standard interface; the interface variables conforming to the IEC 61131-3 standard include input variables, output variables, and internal variables; the variable types include at least one of BOOL, SINT, USINT, INT, UINT, DINT, UDINT, LINT, ULINT, REAL, LREAL, STRING, WSTRING, BYTE, WORD, DWORD, LWORD, ENUM, ARRAY, and STRUCT.

7. The method for integrating Python script execution in a programmable controller according to claim 1, characterized in that, The execution function specifies the data instance to be processed by passing a pointer or memory address of the function block instance to the runtime system; the runtime system locates the input, output, and internal variable data areas of the specific instance based on the pointer or memory address.

8. A programmable logic controller (PLC) system for implementing the method of integrating Python script execution in a PLC as described in any one of claims 1 to 7, characterized in that, This includes an enhanced integrated development environment and a runtime control system; The enhanced integrated development environment includes a dedicated Python function block editor, an enhanced compiler, and a project management module; it is used for creating, editing, and compiling Python function blocks. The dedicated editor supports simultaneous editing of IEC 61131-3 standard interfaces and Python script logic; the enhanced compiler integrates a Python syntax checker and a registration code generator on the basis of the traditional IEC compiler, used to complete the syntax checking of Python scripts, as well as the automatic generation of Python function block initialization functions and execution functions; the project management module is responsible for managing PLC project files containing Python function blocks, supporting the saving, importing, exporting and version control of Python function blocks, and is consistent with the project management logic of traditional IEC function blocks. The operation control system includes a Python execution engine, a type bridging module, a function block registration management module, and an exception handling module. Used to complete the registration of Python function blocks and data transformation; The Python execution engine is used to execute user-written Python scripts; the type bridging module is used for bidirectional conversion between IEC61131-3 data types and Python data types; the function block registration management module is used to maintain a mapping table between registered Python function block variable data and scripts, and to handle registration and execution requests for Python function blocks; the exception handling module is used to capture various exceptions during the execution of Python scripts, encapsulate exception information and return it to the upper-level logic, and also provides exception logging functionality.

9. A computer-readable storage medium having a computer program / instructions stored thereon, characterized in that, When the computer program / instructions are executed by the processor, they implement the steps of the method for integrating the execution of a Python script in a programmable controller as described in any one of claims 1 to 7.

10. A computer device / equipment / system, characterized in that, include: Memory, used to store computer programs / instructions; A processor for executing the computer program / instructions to implement the steps of the method for integrating the execution of a Python script in a programmable controller as described in any one of claims 1 to 7.