Abstract:
The present invention provides an event-driven programming method of programmable logic controller (PLC), including: registering at least one event to be detected; storing at least one event handler segment corresponding to the at least one event to be detected respectively; detecting the occurrence of the at least one event to be detected; adding the detected event into an event queue; extracting events from the event queue; and executing the event handler segment corresponding to the extracted event.

Description:
BACKGROUND 
       [0001]    The present invention relates to programmable logic controller, and more particularly, relates to event-driven programmable logic controller and event-driven programming method thereof. 
         [0002]    Programmable logic controller (PLC) plays an important role in the field of industrial control due to its flexibility in being programmable by the user. The programming methods of the existing programmable logic controller and the corresponding user application programs are based on scan cycle. The scan cycle has several steps, comprising: updating the input value, executing the user application, and refreshing the output value. 
         [0003]    However, programming methods according to scan cycle have the following disadvantages: 
         [0004]    Slow response to event. The user needs to detect the input event in application program, and the response cannot be outputted until the end of scan cycle. 
         [0005]    Complicated user applications which are not easy to write. For programmable logic controllers according to scan cycle, the user application is responsible for event detecting and handling. 
         [0006]    Poor readability of user application, which makes it difficult to maintain. For programmable logic controller according to scan cycle, the user needs to integrate the processing procedure of various events such as input/output event, diagnostic event, system event and so on in a single program, therefore the user application is poorly structured. 
       SUMMARY 
       [0007]    The present invention provides event-driven programmable logic controller and event-driven programming method thereof in respect to the above disadvantages of the existing programmable logic controller according to scan cycle and programming method thereof. 
         [0008]    According to an aspect of the invention, provided is an event-driven programming method of programmable logic controller, comprising: registering at least one event to be detected; storing at least one event handler program section corresponding to the at least one event to be detected, respectively; detecting the occurrence of the at least one event to be detected; adding the detected event into an event queue; extracting an event from the event queue; executing an event handler program section corresponding to the extracted event. 
         [0009]    According to an embodiment of the present invention, the at least one event to be detected may include an event selected from at least one predefined event by a user. 
         [0010]    According to an embodiment of the present invention, the at least one event to be detected may include a user customized event inputted by the user. 
         [0011]    According to an embodiment of the present invention, the user customized event may include a logical combination of the predefined event. 
         [0012]    According to an embodiment of the present invention, the predefined event may include input/output event, communication event, diagnostic event, system error event, system idle event. 
         [0013]    According to an embodiment of the present invention, the at least one event handler program section may include a predefined event handler program section. 
         [0014]    According to an embodiment of the present invention, the at least one event handler program section may include a user customized event handler program section inputted by a user. 
         [0015]    The method may further include replacing a corresponding predefined event handler program section by a user customized event handler program section. 
         [0016]    According to an embodiment of the present invention, the event queue may include a plurality of event queues with different priority levels, the registering the at least one event to be detected may further include registering the priority level of the event to be detected, the adding the detected event into the event queue may include adding the event to be detected into a corresponding event queue according to the priority level of the event, the extracting an event from the event queue may include preferentially extracting an event from an event queue with a high priority level. 
         [0017]    According to an embodiment of the present invention, the adding the detected event in the event queue may include discarding a detected event if the event is already in the event queue. 
         [0018]    According to another aspect of the invention, provided is a programmable logic controller, comprising: an event queue for storing the events to be handled; an event detector for registering at least one event to be detected, detecting the occurrence of the at least one event to be detected, and adding the detected event into the event queue; a program memory for storing at least one event handler program section corresponding to the at least one event to be detected, respectively; an event dispatcher for extracting an event from the event queue; and an event handler for executing an event handler program section corresponding to the extracted event. 
         [0019]    According to an embodiment of the present invention, the at least one event to be detected may include an event is selected from at least one predefined event by a user. 
         [0020]    According to an embodiment of the present invention, the at least one event to be detected may include a user customized event inputted by the user. 
         [0021]    According to an embodiment of the present invention, the user customized event may include a logical combination of the predefined event. 
         [0022]    According to an embodiment of the present invention, the predefined event may include input/output event, communication event, diagnostic event, system error event, system idle event. 
         [0023]    According to an embodiment of the present invention, the at least one event handler program section may include a predefined event handler program section. 
         [0024]    According to an embodiment of the present invention, the at least one event handler program section may include a user customized event handler program section inputted by a user. 
         [0025]    According to an embodiment of the present invention, a corresponding predefined event handler program section may be replaced by a user customized event handler program section in the program memory. 
         [0026]    According to an embodiment of the present invention, the event queue may include a plurality of event queues with different priority levels, the event detector may register a priority level of the event to be detected, and add the detected event into a corresponding event queue according to the priority level of the event, the event detector may preferentially extract an event from an event queue with a high priority level. 
         [0027]    According to an embodiment of the present invention, the event detector may discard a detected event if the event is already in the event queue. 
         [0028]    According to the invention, the programming method according to scan cycle is replaced by an event-driven programming method in programmable logic controller, user application is split into event handler program sections with single function, the difficulty of user application programming and the probability of error are reduced, resulting in a well-structured user application with easy maintenance Meanwhile, as handling may be performed in time with respect to specific events, response speed and system performance are improved. 
     
    
     
       BRIEF DESCRIPTION OF THE DRAWINGS 
         [0029]    For a more complete understanding of the present invention, reference is now made to the detailed description of the exemplary embodiment taken in conjunction with the accompanying drawings. It is to be clearly understanded that the exemplary embodiment is only described by way of illustration and example, while the present invention is not limited thereto. The spirit and scope of the present invention is limited by the specific contents of the appended claims. The following describes the brief description of the drawings, including: 
           [0030]      FIG. 1  is a flowchart illustrating an event-driven programming method of programmable logic controller according to an embodiment of the present invention; 
           [0031]      FIG. 2  is a flowchart illustrating an event-driven programming method of programmable logic controller according to another embodiment of the present invention; 
           [0032]      FIG. 3  is a block diagram illustrating a programmable logic controller according to an embodiment of the present invention; and 
           [0033]      FIG. 4  is a bock diagram illustrating a programmable logic controller according to another embodiment of the present invention. 
       
    
    
     DETAILED DESCRIPTION 
       [0034]      FIG. 1  is a flowchart illustrating an event-driven programming method of programmable logic controller according to an embodiment of the present invention. 
         [0035]    Referring to  FIG. 1 , in step S 101 , at least one event to be detected is registered in the programmable logic controller. According to an embodiment, a plurality of predefined events, such as input/output event, communication event, diagnostic event, system error event, system idle event and so on, may be predefined, thereby an interested event may be selected as the event to be detected from these predefined events. In addition, as a complement to the predefined events, the user may also input an user customized event as the event to be detected. The user customized event may be a logical combination of the predefined events. For example, assuming that the predefined events comprise: (E 1 ) an input channel  1  receives data; (E 2 ) a network port receives a message; and (E 3 ) system is idle, then the following user customized events may be defined: E 1  AND E 3 , namely, the input channel  1  receives data and system is idle; Or E 2  OR E 3 , namely, the network port receives message or the system is idle. However, the user customized event of the present invention is not limited to the logical combination of the predefined events, it may also be defined by the user according to the actual requirements. 
         [0036]    In step S 103 , a corresponding event handler program section is stored for each of the events to be detected in the programmable logic controller. According to an embodiment, several predefined event handler program sections may be predefined for default handling of the events. However, when an event has no corresponding predefined event handler program section or requires special handling, a user customized event handler program section may be inputted. When the user input a user customized event handler program section for an event to be detected, if the corresponding predefined event handler program section exists, then the predefined event handler program section may be replaced with the inputted user customized event handler program section. 
         [0037]    In step S 105 , the occurrence of the events to be detected is detected in the programmable logic controller. 
         [0038]    In step S 107 , a detected event is added into an event queue of the programmable logic controller. According to an embodiment, if the detected event is already in the event queue, then the event may be discarded. However, the present invention is not limited thereto. Alternatively, a duplicate event may also be recorded in the event queue. 
         [0039]    In step S 109 , an event is extracted from the event queue. 
         [0040]    In step S 111 , executing an event handler program section corresponding to the extracted event, which is stored in the programmable logic controller. 
         [0041]      FIG. 2  is a flowchart illustrating an event-driven programming method of the programmable logic controller according to another embodiment of the present invention. The difference between the embodiment and the embodiment of  FIG. 1  is mainly in that a plurality of event queues with different priority levels are utilized to manage the queue. 
         [0042]    Referring to  FIG. 2 , in step  201 , at least one event to be detected and the priority level thereof are registered in the programmable logic controller. 
         [0043]    In step S 203 , a corresponding event handler program section for each event to be detected is stored in the programmable logic controller. 
         [0044]    In step S 205 , the occurrence of the events to be detected is detected in the programmable logic controller. 
         [0045]    In step S 207 , a detected event is added into the event queue according to the priority level thereof. 
         [0046]    In the step S 209 , an event is extracted from the event queue according to the priority level, when an event queue with higher priority level is not empty, an event is preferentially extracted from the event queue with higher priority level. Namely, the event queue with the highest priority level is handled first, and the event with the second highest priority level is handled, and so on. 
         [0047]    In step S 211 , an event handler program section stored in the programmable logic controller corresponding to the extracted event is executed. 
         [0048]      FIG. 3  is a block diagram of a programmable logic controller  100  based on an embodiment of the present invention. 
         [0049]    Referring to  FIG. 3 , the programmable logic controller  100  comprises an event detector  101 , a program memory  103 , an event dispatcher  105 , an event handler  107  and an event queue used to store the events to be handled. 
         [0050]    The event detector  101  registers at least one event to be detected, detects the occurrence of the event to be detected, and adds an detected event into the event queue. According to an embodiment, an event existing in the event queue may be discarded or recorded in duplicate. According to an embodiment, the events to be detected may comprise predefined events and user customized events inputted by user. A user customized event may be a logical combination of predefined events, or may be defined by the user himself according to actual needs. 
         [0051]    Program memory  103  stores event handler program sections corresponding to the events to be detected, respectively. According to an embodiment, the event handler program sections may comprise predefined event handler program sections and user customized event handler program section, and a corresponding predefined event handler program section may be replaced with a user customized event handler program section. 
         [0052]    The event dispatcher  105  extracts an event from the event queue and dispatches the event to the event handler  107 . 
         [0053]    The event handler  107  receives an event dispatched by the event dispatcher  105 , and executes an event handler program section stored in the program memory  103  corresponding to the event. 
         [0054]      FIG. 4  is a block diagram of a programmable logic controller  200  based on another embodiment of the present invention. The difference the embodiment and the embodiment of  FIG. 3  is in that a plurality of event queues with different priority levels are utilized to manage the queue. 
         [0055]    Referring to  FIG. 4 , the programmable logic controller  200  comprises an event detector  201 , a program memory  203 , an event dispatcher  205 , an event handler  207 , and a plurality of event queues with distinct priority levels used for storing the event to be handled (for example, a priority level  1  event queue, a priority level  2  event queue, and a priority level  3  event queue). 
         [0056]    The event detector  201  registers at least one event and the priority level thereof, detects the occurrence of the event to be detected, and adds a detected event into the event queue according to the priority level of the event. 
         [0057]    The program memory  203  stores event handler program sections corresponding to the events to be detected, respectively. 
         [0058]    The event dispatcher  205  extracts an event from the event queue according to the priority level and dispatches the event to the event dispatcher  207 , when an event queue with higher priority level is not empty, an event is preferentially extracted from the event queue with higher priority level. Namely, the event queue with the highest priority level is handled first, and the event with the second highest priority level is handled, and so on. 
         [0059]    The event handler  207  receives an event dispatched by the event dispatcher  205 , and executes an event handler program section stored in the program memory  203  corresponding to the event. 
         [0060]    While the invention has been shown and described with reference to certain embodiments thereof, it will be understood by those skilled in the art that various changes and modifications may be made therein without departing from the actual scope of the invention.