Method and system for realizing PLC (Programmable Logic Controller) from single task to multi-thread task

By creating PLC task threads in the RTOS and using task switching hook functions to save and restore the context, the problem of insufficient real-time performance in PLC single-task mode is solved, and efficient scheduling and safe switching of multi-threaded tasks in the RTOS are achieved.

CN122064442APending Publication Date: 2026-05-19ZHEJIANG SUPCON RES
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202610109147.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-01-27
Publication Date
2026-05-19

AI Technical Summary

Technical Problem

Existing PLC program logic still runs in a single-task cycle scanning mode, which cannot meet the requirements of complexity and real-time performance, and small and medium-sized manufacturers find it difficult to implement multi-threaded task scheduling in RTOS.

Method used

By acquiring PLC task scheduling information, creating RTOS threads and allocating stack space, using task switching hook functions to save and restore PLC program unit contexts, and combining this with the RTOS priority scheduling mechanism, the conversion from a single PLC task to a multi-threaded task is achieved.

Benefits of technology

It achieves real-time performance and reliability for PLC programs, avoids deep dependence on operating system vendors, and improves the execution efficiency and security of multi-threaded tasks.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122064442A_ABST
    Figure CN122064442A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of automation control, and discloses a PLC single task to multi-thread task realization method and system, and the method comprises the steps: obtaining PLC task arrangement information which comprises at least one PLC task and a corresponding program organization unit POU; based on the PLC task arrangement information, creating a real-time operating system (RTOS) thread corresponding to each PLC task in the RTOS, and distributing a corresponding stack space for each RTOS thread; when the RTOS task scheduler executes task switching, calling a preset task switching hook function to carry out stack pressing storage on the additional context of the PLC program unit corresponding to the currently running RTOS thread, and carrying out elastic stack recovery on the additional context of the PLC program unit corresponding to the to-be-running RTOS thread, according to the method for transforming the PLC single task into the RTOS-based multi-thread task, the practical requirement that multi-thread task transformation needs to be carried out due to the fact that the real-time performance of a single task mode of a traditional PLC manufacturer is limited is met.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of automation control technology, and in particular to a method and system for implementing PLC single-task to multi-threaded tasks. Background Technology

[0002] PLCs are widely used in industrial settings. Currently, the operating mechanism of most PLC program logic is still a single-task cycle sequential scan execution mode. As PLC program logic becomes more complex and larger, some PLC program logic needs to be executed quickly in real time, while others only need to be executed slowly. In some cases, an error in the execution of one task may block other tasks from executing. Therefore, this mode, which completes the entire PLC program logic in one scan cycle in a single task and does not distinguish between the actual situation of the user's site, is increasingly unable to meet the needs of users.

[0003] Embedded operating systems are now widely used, and the real-time multithreading concept in RTOS is being applied to industrial PLC program logic control. There are two ways to transform the traditional single-task mode of a PLC into a multithreaded task mode using an RTOS: one is to treat the original PLC task as a single thread of the RTOS; the other is to convert the original PLC task into multiple threads of the RTOS. Major international manufacturers support real-time multithreaded task control modes, implementing multithreaded task scheduling through the RTOS. This involves using a priority-based preemptive scheduling mechanism to rationally plan and manage PLC program tasks. However, most major manufacturers are deeply integrated with operating system vendors, which is not friendly to small and medium-sized manufacturers. In most cases, even when domestic manufacturers incorporate an RTOS into the PLC controller itself, many still treat the PLC task as a single thread of the operating system, essentially maintaining a single-task cycle scanning mode, and failing to truly achieve real-time multithreaded preemptive scheduling execution.

[0004] It is evident that most PLC manufacturers' PLC programs still operate in a single-task cycle scanning mode. Transforming PLC programs into a real-time, multi-threaded task mode is a practical need, but specific implementation methods are still rare or not publicly available. According to publicly available information, some methods either differentiate task types based on the original single-task PLC based on its operating mode and use interrupt calls to prioritize task calls—essentially still a time-sharing mechanism; others propose general methodologies and implementation goals, but lack specific implementation measures and core details.

[0005] Therefore, developing a general, feasible, and feasible implementation method that can smoothly and safely transform traditional PLC single-task functions into priority-driven multi-threaded tasks based on RTOS has become a technical problem that urgently needs to be solved in this field. Summary of the Invention

[0006] The purpose of this invention is to overcome the shortcomings of the prior art and provide a method and system for implementing PLC single-task to multi-threaded task based on RTOS. Through this method, a single task of PLC can be decomposed and mapped to a multi-threaded task of RTOS. The scheduling mode of the mapped PLC multi-threaded task is completely taken over by RTOS, which eliminates the need to design an additional multi-task scheduling mechanism and meets the real-time requirements.

[0007] On the one hand, a method for implementing PLC single-task to multi-threaded task conversion is provided, including the following steps: S1: Obtain PLC task scheduling information, wherein the PLC task scheduling information includes at least one PLC task and its corresponding program organization unit (POU); S2: Based on the PLC task scheduling information, create an RTOS thread corresponding to each PLC task in the real-time operating system (RTOS), and allocate a corresponding stack space for each RTOS thread. S3: When the RTOS task scheduler performs task switching, it calls the preset task switching hook function to push and save the additional context of the PLC program unit corresponding to the currently running RTOS thread onto the stack, and to pop and restore the additional context of the PLC program unit corresponding to the RTOS thread that is about to run.

[0008] Furthermore, before proceeding to step S1, PLC task scheduling is also included: Based on the execution attributes of multiple program organization units (POUs) in the PLC program, the POUs are divided into several PLC tasks. The execution attributes include at least one of task type, execution cycle, event trigger source, priority, and watchdog timeout parameter. The task types include cyclic tasks, timed tasks, and event tasks.

[0009] Preferably, the basis for dividing the POU into several PLC tasks is as follows: Based on real-time requirements and task execution attributes, POUs with high real-time requirements are assigned to high-priority PLC tasks; POUs with average real-time requirements are assigned to low-priority PLC tasks.

[0010] Furthermore, in step S3, the additional context includes at least the register status information related to the execution of the POU in the PLC runtime system, specifically including at least one of the program pointer, segment pointer, data segment pointer, accumulator, and additional registers of the POU associated with the current PLC task.

[0011] Furthermore, in step S3, the invocation of the preset task switching hook function specifically includes: Before the RTOS task scheduler performs the task switching stack operation, the first hook function is called to save the additional context of the PLC program unit corresponding to the current RTOS thread. After the RTOS task scheduler performs the task switching stack pop operation, the second hook function is called to restore the additional context of the PLC program unit corresponding to the RTOS thread that is about to run.

[0012] Furthermore, in step S2, the stack space allocated to each RTOS thread comprises two parts: The first part is used to store the context of the RTOS thread itself, which is automatically managed by the RTOS kernel; The second part is used to store the additional context of the PLC program unit according to claim 4, which is managed through the hook function.

[0013] Preferably, the method further includes: S4: Based on the RTOS priority scheduling mechanism, multiple RTOS threads are scheduled so that the RTOS thread corresponding to a high-priority PLC task can preempt the execution right of the RTOS thread corresponding to a low-priority PLC task.

[0014] On the other hand, the present invention provides a system for implementing PLC single-task to multi-threaded task transformation, comprising: The task scheduling acquisition module is used to acquire PLC task scheduling information, which includes at least one PLC task and its corresponding program organization unit (POU). The thread creation and allocation module is used to create an RTOS thread corresponding to each PLC task in the real-time operating system (RTOS) based on the PLC task orchestration information, and allocate a corresponding stack space for each RTOS thread. The context hook management module is used to call preset task switching hook functions to push and save the additional context of the PLC program unit corresponding to the currently running RTOS thread onto the stack when the RTOS task scheduler performs task switching, and to pop and restore the additional context of the PLC program unit corresponding to the RTOS thread that is about to run from the stack.

[0015] In addition, a computer-readable storage medium is provided, on which a computer program is stored, which, when executed by a processor, implements the PLC single-task to multi-threaded task implementation method described in any of the above claims.

[0016] Meanwhile, an electronic device is provided, comprising: one or more processors; and a storage device for storing one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement the PLC single-task to multi-threaded task implementation method described in any of the preceding claims.

[0017] Compared with the prior art, the beneficial effects of the present invention are: Through the task orchestration acquisition module and thread creation and allocation module, this invention maps PLC user tasks to independent threads of RTOS based on their priority, execution cycle and other attributes, changing the mode in which all tasks must be queued for execution in traditional single-task cyclic scanning, so that critical tasks can be responded to with minimal delay. This invention utilizes the hook interface provided during RTOS task switching to automatically and accurately save and restore the unique program unit (POU) attached context for each PLC thread while the RTOS kernel performs thread context push and pop operations, ensuring the correctness and reliability of PLC program execution in a multi-threaded environment. This invention, through a specific and implementable task orchestration-thread mapping-context hook management method, can successfully upgrade a PLC from a single-task mode to a true multi-threaded concurrent mode. Attached Figure Description

[0018] The accompanying drawings are provided to further illustrate the invention and form part of the specification. They are used in conjunction with embodiments of the invention to explain the invention and do not constitute a limitation thereof. In the drawings: Figure 1 This is a flowchart illustrating a method for implementing a PLC single-task to multi-threaded task approach according to the present invention. Figure 2 This is a schematic diagram illustrating the principle of PLC multi-threaded task switching according to the present invention. Figure 3 This is a schematic diagram illustrating the PLC task scheduling principle of the present invention; Figure 4 This is a flowchart of a PLC task creation process according to the present invention; Figure 5 This is a schematic diagram illustrating the principle of PLC multi-threaded task creation according to the present invention; Figure 6 This is a schematic diagram of the additional context push and pop operation when a PLC is multithreaded according to the present invention. Figure 7 This is a schematic diagram of an embodiment of an electronic device according to the present invention. Detailed Implementation

[0019] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.

[0020] The objective of this invention is to decompose, transform, and map PLC user tasks into multi-threaded tasks of an RTOS through a technical implementation scheme, and then realize the real-time and multi-threaded nature of PLC user tasks by leveraging the real-time multi-task scheduling mechanism of the RTOS.

[0021] The specific embodiments of the present invention will be described below with reference to the accompanying drawings and examples.

[0022] Example 1 This embodiment is based on an RTOS, and its main goal is to map single PLC tasks to multi-threaded RTOS tasks. The implementation involves two stages: first, organizing and orchestrating the PLC tasks, decomposing them into independent, mappable PLC tasks; and second, mapping these decomposed PLC tasks to RTOS tasks in an appropriate manner. After the PLC tasks are mapped to RTOS tasks, the original PLC tasks can be scheduled and managed through the RTOS's multi-task scheduling mechanism, and the isolation and determinism of RTOS task execution ensure the safety and real-time performance of the PLC tasks. This section includes three parts: an overall scheme introduction, the PLC task orchestration implementation stage, and the RTOS multi-threaded mapping implementation stage.

[0023] Please see Figure 1 The technical solution for implementing a PLC single-task to multi-threaded task conversion method provided in this embodiment includes the following steps: S1: Obtain PLC task scheduling information, wherein the PLC task scheduling information includes at least one PLC task and its corresponding program organization unit (POU); S2: Based on the PLC task scheduling information, create an RTOS thread corresponding to each PLC task in the real-time operating system (RTOS), and allocate a corresponding stack space for each RTOS thread. S3: When the RTOS task scheduler performs task switching, it calls the preset task switching hook function to push and save the additional context of the PLC program unit corresponding to the currently running RTOS thread onto the stack, and to pop and restore the additional context of the PLC program unit corresponding to the RTOS thread that is about to run.

[0024] The overall principle diagram of the scheme is as follows: Figure 2As shown in the figure, the PLC program is set with four POU units. The goal is to orchestrate them into two PLC tasks and then map them to create two RTOS real-time tasks.

[0025] The upper-level PLC runtime system is responsible for executing the PLC program logic. Typically, a PLC program consists of Point-of-Use (POU) program units, which are the smallest units of execution in a PLC program. In the traditional single-task PLC operation mode, the execution of the four POUs in the PLC runtime system is a sequential scan, with an example execution flow of POU1->POU2->POU3->POU4. If one POU times out or gets stuck, other POU tasks will be unable to execute in real time, severely impacting real-time performance. However, when using a multi-threaded task mode based on an RTOS, the POU units are arranged into PLC tasks of different priorities and mapped to two RTOS multi-threaded tasks. When the RTOS task scheduler switches to different thread tasks, the corresponding PLC task gains access to the PLC runtime system and executes the POU resources within its own PLC task, achieving RTOS-based PLC multi-threaded task scheduling. A typical PLC runtime system includes a program parser. The core of the parser is similar to the basic unit of a central processing unit, including elements such as a program pointer, segment pointer, stack pointer, accumulator, and additional registers. Since this parser is shared by all PLC tasks, the POU unit needs to perform additional switching management when switching between multi-threaded tasks in the PLC.

[0026] The intermediate layer describes the mapping process from PLC tasks to RTOS tasks. After a PLC task is mapped to an RTOS task, it is actually converted into an RTOS multi-threaded task, and the RTOS task scheduler is responsible for managing and scheduling it. PLC tasks can be organized and arranged using Point of Interest (POUs) as units; POU units can be organized arbitrarily. Figure 2 In this case, POU1 and POU4 are assigned as PLC task 1, and POU2 and POU3 are assigned as PLC task 2.

[0027] The lower-level RTOS task scheduler is responsible for the scheduling and management of RTOS tasks. Its main tasks include: 1) selecting which RTOS task to start execution based on the scheduling policy; 2) determining whether to use priority or time-slicing for task switching during scheduling; and 3) protecting and restoring the context of different tasks during task switching. Normally, these tasks are automatically managed by the operating system after RTOS tasks are created. However, as described above, the program units of a PLC task in the PLC runtime system are similar to the execution process of a central processing unit, also possessing registers such as program pointers, segment pointers, and accumulators. Therefore, these elements must be properly protected to ensure the correctness of execution during PLC task switching. Based on this, this work can be accomplished in the context protection and restoration during RTOS task switching, which is crucial for converting a single PLC task into a multi-threaded task.

[0028] Specifically, before obtaining the orchestration information in step S1, task orchestration is performed first, including: Based on the execution attributes of multiple program organization units (POUs) in the PLC program, the POUs are divided into several PLC tasks. The execution attributes include at least one of task type, execution cycle, event trigger source, priority, and watchdog timeout parameter. The task types include cyclic tasks, timed tasks, and event tasks.

[0029] The basis for dividing the POU into several PLC tasks is as follows: Based on real-time requirements and task execution attributes, POUs with high real-time requirements are assigned to high-priority PLC tasks; POUs with average real-time requirements are assigned to low-priority PLC tasks.

[0030] The PLC task scheduling flowchart is as follows: Figure 3 As shown. Figure 3 The PLC program is configured with four Program Units (POUs). According to the definition of the PLC task model in IEC 61131-3, the PLC task is the caller of the program unit (POU) and is responsible for the execution and operation of the program unit. The program unit (POU) is the smallest basic unit of the user program, organized and managed by the PLC task. Therefore, a PLC task can contain at least one POU. A POU program unit is a piece of program code, so each POU program unit has attributes such as program segment (CS), data segment (DS), program pointer (SP), accumulator (AE), and additional accumulators (AEs). These attributes need to be specially managed during switching.

[0031] After the PLC program is written, PLC tasks can be organized and arranged. Based on the execution method of PLC tasks in practical applications, PLC tasks are typically divided into cyclic tasks, timed tasks, and event tasks. Each type can have parameters set according to task attributes, such as priority, timing cycle, event trigger source, and watchdog timeout monitoring. These attribute parameters are set and sent to the PLC controller during the host computer software arrangement. Then, each time the PLC controller starts, it recognizes the configuration and creates RTOS tasks according to the PLC task configuration parameters. The specific process is as follows: Figure 4 As shown.

[0032] After organizing and orchestrating the PLC tasks, the key step is to create the orchestrated PLC tasks as RTOS multi-threaded tasks, which is the core of this solution.

[0033] In step S2, the stack space allocated to each RTOS thread comprises two parts: The first part is used to store the context of the RTOS thread itself, which is automatically managed by the RTOS kernel; The second part is used to store the additional context of the PLC program unit according to claim 4, which is managed through the hook function.

[0034] In addition to steps S1 to S3, the method of the present invention also includes S4: Based on the RTOS priority scheduling mechanism, multiple RTOS threads are scheduled so that the RTOS thread corresponding to a high-priority PLC task can preempt the execution right of the RTOS thread corresponding to a low-priority PLC task.

[0035] Specifically, such as Figure 5 As shown, this embodiment is based on an RTOS. The RTOS itself provides multithreading functionality and a priority-based task scheduling mechanism to ensure that each task is executed within the expected time period. The essence of multithreading is that each thread has its own independent control block and stack space. The former determines the thread's name, priority, task state, stack pointer, and other attributes when it is created, while the latter determines the protection and restoration address of the thread context during task switching. The underlying implementation mechanism of the priority-based multitasking mode is the saving and restoring of the task context in interrupt mode. When a higher-priority task preempts a lower-priority task, the lower-priority task first saves its own context and then relinquishes execution rights. The higher-priority task then gains execution rights, restores its own context, and begins execution. This embodiment mainly modifies and utilizes the thread stack space, dividing it into two cases: one is creating regular RTOS thread tasks, and the other is creating PLC thread tasks.

[0036] When creating regular tasks based on an RTOS, the context protection and recovery during task scheduling are automatically managed and maintained by the operating system, such as... Figure 5 The user does not need to participate in the "thread's own context" protection of the middle stack space.

[0037] When creating multi-threaded tasks for a PLC based on an RTOS, it's essential to consider the specific parameters of each PLC program unit, such as its code segment (CS), data segment (DS), program pointer (SP), and accumulator (AE), because these PLC program units will share a single PLC runtime parser. The parser operates similarly to a central processing unit, relying on registers for the code segment (CS), data segment (DS), program pointer (SP), and accumulator (AE). If the register pointers cannot correctly point to the corresponding POU program unit during task switching, program execution will malfunction. Therefore, context protection is needed for various pointers and addresses of the POU during task switching. This part is discussed in... Figure 5 This is referred to as "user-attached context".

[0038] Therefore, to protect and restore the context of various pointers in the POU, it is necessary to consider performing this action at an appropriate time. RTOS (such as uCOS / FreeRTOS / vxWorks / ReWorks, etc.) typically have a type of API function interface called HOOK functions, which are generally not used. However, if HOOK functions are enabled, the operating system will call the corresponding HOOK function when specific events occur. In particular, the hook functions provided by the operating system's task scheduler before and after task switching events provide the operation of pushing the original running thread onto the stack and popping the newly ready thread from the stack. This provides the feasibility of context protection during PLC runtime system task execution. In this solution, the user-operable pushed and popped data mainly consists of various pointers related to the POU program unit. Figure 6 A flowchart illustrating the process of pushing and popping context from the stack for a PLC program unit.

[0039] In step S3, the additional context includes at least the register status information related to the execution of the POU in the PLC runtime system, specifically including at least one of the program pointer, segment pointer, data segment pointer, accumulator and additional registers of the POU associated with the current PLC task.

[0040] Furthermore, step S3, which involves calling a preset task switching hook function, specifically includes: Before the RTOS task scheduler performs the task switching stack operation, the first hook function is called to save the additional context of the PLC program unit corresponding to the current RTOS thread. After the RTOS task scheduler performs the task switching stack pop operation, the second hook function is called to restore the additional context of the PLC program unit corresponding to the RTOS thread that is about to run.

[0041] In this embodiment, Figure 6 Compared to traditional task switching, this new approach adds the push and pop operations of PLC POU program unit information onto and off the stack. Without the task switching hook interface, the RTOS-managed tasks can automatically perform push and pop operations on the task stack during switching, but it cannot operate on context-protected content added during PLC runtime. This causes a single PLC task to fail to run correctly in a multi-threaded PLC environment. When using the task switching hook interface, users can manually add POU context information during the push and pop operations of the task switching process. This allows the PLC to correctly restore the POU program unit running in the original thread mode after a multi-threaded task switch.

[0042] In summary, the method for transforming a PLC from a single-task PLC to a multi-threaded PLC based on an RTOS provided by this invention can solve the practical needs of traditional PLC manufacturers who require multi-threaded PLC transformation due to limited real-time performance in the single-task mode. It also avoids the technical risk that PLC manufacturers' multi-threaded PLC transformation requires deep customization and binding by the operating system manufacturer.

[0043] Based on the above method, this embodiment also provides a system for implementing PLC single-task to multi-threaded task transformation, including: The task scheduling acquisition module is used to acquire PLC task scheduling information, which includes at least one PLC task and its corresponding program organization unit (POU). The thread creation and allocation module is used to create an RTOS thread corresponding to each PLC task in the real-time operating system (RTOS) based on the PLC task orchestration information, and allocate a corresponding stack space for each RTOS thread. The context hook management module is used to call preset task switching hook functions to push and save the additional context of the PLC program unit corresponding to the currently running RTOS thread onto the stack when the RTOS task scheduler performs task switching, and to pop and restore the additional context of the PLC program unit corresponding to the RTOS thread that is about to run from the stack.

[0044] It should be noted that the steps in the PLC single-task to multi-threaded task implementation method provided in this embodiment can be implemented based on the corresponding modules in the PLC single-task to multi-threaded task implementation system. Those skilled in the art can refer to the technical solution of the system to implement the steps of the method. That is, the embodiment in the system can be understood as a preferred example of implementing the method, and will not be elaborated here.

[0045] This embodiment also provides an electronic device, such as... Figure 7 As shown, the electronic device includes a processor 14 and a memory 13. The memory 13 stores machine-executable instructions that can be executed by the processor 14, which executes the machine-executable instructions to implement the above-described audio control method.

[0046] Furthermore, Figure 7 The electronic device shown also includes a bus 12 and a communication interface 11, with the processor 14, the communication interface 11 and the memory 13 connected via the bus 12.

[0047] The memory 13 may include high-speed random access memory (RAM) and may also include non-volatile memory, such as at least one disk storage device. Communication between this system network element and at least one other network element is achieved through at least one communication interface 11 (which can be wired or wireless), such as the Internet, wide area network, local area network, metropolitan area network, etc. The bus 12 may be an ISA bus, PCI bus, or EISA bus, etc. The bus can be divided into address bus, data bus, control bus, etc. For ease of representation, Figure 7 The symbol is represented by a single double-headed arrow, but this does not mean that there is only one bus or one type of bus.

[0048] Processor 14 may be an integrated circuit chip with signal processing capabilities. In implementation, each step of the above method can be completed by the integrated logic circuitry in the hardware of processor 14 or by instructions in software form. Processor 14 can be a general-purpose processor, including a Central Processing Unit (CPU), a Network Processor (NP), etc.; it can also be a Digital Signal Processor (DSP), an Application Specific Integrated Circuit (ASIC), a Field-Programmable Gate Array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. It can implement or execute the methods, steps, and logic block diagrams disclosed in this embodiment. The general-purpose processor can be a microprocessor or any conventional processor. The steps of the method disclosed in this embodiment can be directly manifested as execution by a hardware decoding processor, or execution by a combination of hardware and software modules in the decoding processor. The software modules can reside in random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, registers, or other mature storage media in the art. The storage medium is located in memory 13. The processor 14 reads the information in memory 13 and, in conjunction with its hardware, completes the steps of the method for implementing a PLC single-task to multi-threaded task.

[0049] This disclosure also provides a computer-readable storage medium, which can be a non-volatile computer-readable storage medium or a volatile computer-readable storage medium, storing a computer program that, when run on a computer, causes the computer to execute steps of a method for implementing a PLC single-task to multi-threaded task.

[0050] Finally, it should be noted that the above description is only a preferred embodiment of the present invention, and the scope of protection of the present invention is not limited to the above embodiments. All technical solutions falling within the scope of the present invention's concept are within the scope of protection of the present invention. It should be pointed out that for those skilled in the art, any improvements and modifications made without departing from the principle of the present invention should also be considered within the scope of protection of the present invention.

[0051] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

Claims

1. A method for implementing PLC single-task to multi-threaded task conversion, characterized in that, Includes the following steps: S1: Obtain PLC task scheduling information, wherein the PLC task scheduling information includes at least one PLC task and its corresponding program organization unit (POU); S2: Based on the PLC task scheduling information, create an RTOS thread corresponding to each PLC task in the real-time operating system (RTOS), and allocate a corresponding stack space for each RTOS thread. S3: When the RTOS task scheduler performs task switching, it calls the preset task switching hook function to push and save the additional context of the PLC program unit corresponding to the currently running RTOS thread onto the stack, and to pop and restore the additional context of the PLC program unit corresponding to the RTOS thread that is about to run.

2. The method for implementing PLC single-task to multi-threaded task conversion according to claim 1, characterized in that, Before step S1, PLC task scheduling is also included: Based on the execution attributes of multiple program organization units (POUs) in the PLC program, the POUs are divided into several PLC tasks. The execution attributes include at least one of task type, execution cycle, event trigger source, priority, and watchdog timeout parameter. The task types include cyclic tasks, timed tasks, and event tasks.

3. The method for implementing PLC single-task to multi-threaded task conversion according to claim 2, characterized in that, The basis for dividing the POU into several PLC tasks is as follows: Based on real-time requirements and task execution attributes, POUs with high real-time requirements are assigned to high-priority PLC tasks; POUs with average real-time requirements are assigned to low-priority PLC tasks.

4. The method for implementing PLC single-task to multi-threaded task conversion according to claim 1, characterized in that, In step S3, the additional context includes at least the register status information related to the execution of the POU in the PLC runtime system, specifically including at least one of the program pointer, segment pointer, data segment pointer, accumulator and additional registers of the POU associated with the current PLC task.

5. The method for implementing PLC single-task to multi-threaded task conversion according to claim 1, characterized in that, In step S3, the invocation of the preset task switching hook function specifically includes: Before the RTOS task scheduler performs the task switching stack operation, the first hook function is called to save the additional context of the PLC program unit corresponding to the current RTOS thread. After the RTOS task scheduler performs the task switching stack pop operation, the second hook function is called to restore the additional context of the PLC program unit corresponding to the RTOS thread that is about to run.

6. The method for implementing PLC single-task to multi-threaded task conversion according to claim 1, characterized in that, In step S2, the stack space allocated to each RTOS thread comprises two parts: The first part is used to store the context of the RTOS thread itself, which is automatically managed by the RTOS kernel; The second part is used to store the additional context of the PLC program unit according to claim 4, which is managed through the hook function.

7. The method for implementing PLC single-task to multi-threaded task conversion according to claim 1, characterized in that, The method further includes: S4: Based on the RTOS priority scheduling mechanism, multiple RTOS threads are scheduled so that the RTOS thread corresponding to a high-priority PLC task can preempt the execution right of the RTOS thread corresponding to a low-priority PLC task.

8. A system for implementing PLC single-task to multi-threaded task conversion, characterized in that, include: The task scheduling acquisition module is used to acquire PLC task scheduling information, which includes at least one PLC task and its corresponding program organization unit (POU). The thread creation and allocation module is used to create an RTOS thread corresponding to each PLC task in the real-time operating system (RTOS) based on the PLC task orchestration information, and allocate a corresponding stack space for each RTOS thread. The context hook management module is used to call preset task switching hook functions to push and save the additional context of the PLC program unit corresponding to the currently running RTOS thread onto the stack when the RTOS task scheduler performs task switching, and to pop and restore the additional context of the PLC program unit corresponding to the RTOS thread that is about to run from the stack.

9. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the program is executed by the processor, it implements the PLC single-task to multi-threaded task implementation method as described in any one of claims 1-7.

10. An electronic device, characterized in that, include: One or more processors; A storage device for storing one or more programs, which, when executed by one or more processors, cause the one or more processors to implement the PLC single-task to multi-threaded task implementation method as described in any one of claims 1-7.