Configurable PLC operation zero removal anomaly detection and protection processing method

By extending syntax configuration and constructing abstract syntax trees, flexible detection and protection against division-by-zero anomalies in PLC systems are achieved, solving the problems of high false alarm rate, high false alarm rate and large performance loss in existing technologies, and improving the security and reliability of the system.

CN121579327APending Publication Date: 2026-02-27GUODIAN NANJING AUTOMATION
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202511749694.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-11-26
Publication Date
2026-02-27

AI Technical Summary

Technical Problem

Existing PLC systems suffer from problems such as high false alarm rate, high false alarm rate, lack of flexibility, significant performance loss, inability to cover the entire link, and difficulty in anomaly localization when detecting division by zero.

Method used

By extending the syntax to configure division-by-zero anomaly detection and protection strategies, and using a text programming language to construct an abstract syntax tree, real-time, periodic, and conditional detection can be achieved, and anomaly protection processing can be carried out in conjunction with the PLC runtime scheduling.

Benefits of technology

It enables flexible adjustment of detection strategies, enhances system security and reliability, reduces performance loss, supports fine-grained strategy adjustment, and quickly locates faults and traces the root cause of anomalies.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121579327A_ABST
    Figure CN121579327A_ABST
Patent Text Reader

Abstract

The invention provides a configurable PLC (Programmable Logic Controller) runtime zero removal anomaly detection and protection processing method, which relates to the field of computer software, and comprises the following steps of: configuring a zero removal anomaly detection and protection strategy by utilizing extended grammar, and compiling the zero removal anomaly detection and protection strategy into a PLC text code based on a text programming language; constructing an abstract syntax tree based on the PLC text code, traversing the abstract syntax tree through a preset traversal mode to perform semantic analysis and zero-division abnormal access configuration analysis, and generating an intermediate representation according to an analysis result; and compiling and deploying the intermediate representation into a PLC running program, and running the intermediate representation under the scheduling during the running of the PLC, so as to realize zero-removal exception protection processing. The method supports parameterization configuration and realizes fine-grained strategy adjustment, so that the flexibility and scene adaptability of the system are remarkably improved, and differential requirements of different scenes can be quickly responded.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer software, and more specifically, to a configurable method for detecting and protecting against zero-division anomalies during PLC operation. Background Technology

[0002] A Power Control Library (PCL) is a digital electronic system specifically designed for industrial applications. It uses programmable memory to store instructions for performing logical operations, sequential control, timing, counting, and arithmetic operations. Through digital or analog inputs and outputs, it controls various types of machinery or production processes and is widely used in core systems of energy industries such as power generation. In PLC arithmetic operations, division is one of the most common operations. When the divisor is zero, a division-by-zero error occurs, a common error in computer operations that can easily lead to system failures, causing significant economic losses and social impact.

[0003] The basic principle of division-by-zero exception handling is that when the arithmetic unit detects a division by zero, it triggers the exception handling mechanism. Generally, the system suspends the currently executing computation task and transfers control to a pre-defined exception handler. Static and dynamic detection techniques are commonly used security detection technologies, but they have varying degrees of shortcomings and lack flexibility and configurability in their processing methods.

[0004] Static analysis technology identifies risks by analyzing program code without executing it. Currently, mainstream techniques include data flow analysis, symbolic execution, abstraction interpretation, rule-based inspection, pointer analysis, and cross-process analysis. However, when analyzing denominators of non-constant expression types, the values ​​are only determined at runtime and may continuously change, leading to high false negative and false positive rates. Therefore, static analysis alone is not feasible in demanding fields such as the power industry.

[0005] The core logic of dynamic error detection technology is to perform logical instrumentation in the target code to detect potential errors in real time during program execution. Current mainstream dynamic error detection techniques include binary instrumentation, intermediate representation instrumentation, and source code instrumentation. Because this method can detect various types of runtime information, it can theoretically achieve a detection effect of zero false positives and zero false negatives.

[0006] During the operation of a Programmable Logic Controller (PLC), existing methods for detecting division-by-zero anomalies mainly include static detection and dynamic detection techniques. Static detection techniques, due to their inability to collect runtime state information of the target program, suffer from high false alarm and false negative rates, and therefore cannot be used as the primary detection tool. Known runtime dynamic detection techniques, by logically inserting into the target code, can achieve runtime division-by-zero anomaly detection with zero false alarms and zero false negatives.

[0007] For example, patent application CN114579431B discloses a division-by-zero error detection method based on hybrid analysis. It first registers a custom static analysis division-by-zero error detector to perform static analysis on the target program. Then, using pipe communication, it transmits the detection report generated by the static analysis module to the dynamic analysis instrumentation module, selects the monitoring points for division-by-zero errors, and designs conversion rules. Based on the static analysis detection report and conversion rules, the dynamic analysis instrumentation module selectively instrumentes the monitoring points to obtain a hybrid analysis report. Finally, it generates a division-by-zero detection interface file and the instrumented code file.

[0008] The aforementioned division-by-zero error detection method based on hybrid analysis is limited to the C programming language and primarily focuses on division-by-zero anomaly detection, without providing a detailed description of the processing strategy. This results in a narrow scope of application and poor versatility. In summary, existing technologies have the following technical shortcomings in division-by-zero anomaly detection:

[0009] (1) Known dynamic detection technology uses a uniform detection and processing strategy for all parts of the program. The defect handling method is relatively simple and fixed. For example, it can only replace the exception according to the predetermined strategy and cannot use specific values ​​to replace the exception data access, which lacks flexibility.

[0010] (2) The entire program is used as the processing unit to perform non-discriminatory division by zero anomaly detection at all times. It cannot skip the detection of program segments that are determined to be without problems, nor can it perform periodic and conditional detection, resulting in significant system performance loss.

[0011] (3) The detection of division by zero anomalies is mostly concentrated in a single link (such as only detecting whether the divisor is zero), and the detection logic is not designed independently for divisor reading and writing. This cannot cover the entire link of division by zero anomalies, which may cause the anomaly to spread unnoticed and cause system failure.

[0012] (4) After a division by zero anomaly occurs, there is often a lack of complete records of key information such as the location of the anomaly, the trigger time, and context variables, making it difficult for users to quickly locate the fault and trace the root cause of the anomaly.

[0013] (5) Division-by-zero anomaly detection cannot skip code segments without issues, and the detection frequency cannot be adjusted, resulting in performance degradation and impacting overall detection efficiency. Currently, no effective solution has been proposed to address these issues in the relevant technologies. Summary of the Invention

[0014] In view of this, the present invention provides a configurable method for detecting and protecting against zero division anomalies during PLC operation, in order to solve the aforementioned problems.

[0015] To solve the above problems, the specific technical solution adopted by the present invention is as follows:

[0016] According to a first aspect of the present invention, a configurable method for detecting and protecting against division-by-zero anomalies during PLC operation is provided, comprising the following steps:

[0017] S1. Configure the division-by-zero exception detection and protection strategy using extended syntax, and write the division-by-zero exception detection and protection strategy into PLC text code based on a text programming language.

[0018] S2. Based on PLC text code, construct an abstract syntax tree. Through a preset traversal method, traverse the abstract syntax tree to perform semantic analysis and division-by-zero exception access configuration analysis, and generate an intermediate representation based on the analysis results.

[0019] S3. Compile and deploy the intermediate representation into the PLC runtime program, and run the intermediate representation under the PLC runtime scheduler to achieve division-by-zero exception protection.

[0020] Preferably, configuring the division-by-zero anomaly detection and protection strategy using extended syntax, and writing the division-by-zero anomaly detection and protection strategy into PLC text code based on a text programming language, includes the following steps:

[0021] S11. Based on an annotation-compatible syntax format and combined with preset configuration parameters, construct an extended syntax;

[0022] S12. Based on extended syntax, configure division-by-zero anomaly detection and protection strategies; and use a text programming language to write PLC text code for the configured division-by-zero anomaly detection and protection strategies, thus obtaining PLC text code.

[0023] Preferably, the syntax format compatible with the annotation consists of an annotation start character, a keyword string, configuration parameters, and an annotation end character arranged in sequence, and the configuration parameters are enclosed in parentheses.

[0024] The configuration parameters include operation identification parameters, detection strategy type, protection strategy type, and auxiliary parameters, and these parameters are separated by semicolons.

[0025] Preferably, the division-by-zero anomaly detection and protection strategy includes:

[0026] Skip the real-time detection and processing of all read values ​​in the statement, and directly execute the subsequent statements;

[0027] When an abnormal value is detected in real time during the read / divide operation, the divisor is forcibly set to a preset value.

[0028] The statement checks every ten scan cycles. If an anomaly is detected in writing the divisor variable, the divisor variable is forcibly set to the preset divisor variable.

[0029] Preferably, the step of writing PLC text code for the configured division-by-zero anomaly detection and protection strategy includes:

[0030] A PLC text code for a division-by-zero anomaly detection function is constructed using a general interface and strategy branch architecture. This function is used to transform user-configured detection rules into executable runtime logic through parameterized design and strategy adaptation logic, thereby achieving standardized connection between configuration measurement, detection execution, and result output.

[0031] The PLC text code for the division-by-zero exception protection function is set by extending the syntax, and the division-by-zero exception protection function is stored in a preset symbol table in the form of a structure with standardized parameters. The division-by-zero exception protection function is used to execute the protection logic.

[0032] Preferably, the division-by-zero anomaly detection function includes real-time detection, periodic detection, and conditional detection;

[0033] The real-time detection is used to determine the divisor value of zero each time a protected division instruction is executed;

[0034] The periodic detection is used to perform sampling detection at preset detection intervals;

[0035] The conditional detection is used to perform a division by zero check when a pre-defined Boolean conditional expression is true.

[0036] Preferably, the division-by-zero exception protection function includes: returning a default value, triggering an alarm, pausing the program, and skipping execution;

[0037] The return default value is used to force the divisor to be set to the user-preset default value when a division by zero exception is detected;

[0038] The alarm trigger is used to call the PLC hardware interface to send an alarm, and at the same time write the alarm information to the log.

[0039] The pause procedure is used to modify the program state variable to pause the execution of the current program block and wait for a manual reset command.

[0040] The "skip execution" option is used to ignore the current division operation and directly jump to execute subsequent statements without performing the current division operation.

[0041] Preferably, the step of constructing an abstract syntax tree based on PLC text code, performing semantic analysis and division-by-zero exception access configuration analysis by traversing the abstract syntax tree through a preset traversal method, and generating an intermediate representation based on the analysis results includes the following steps:

[0042] S21. Use compiler development tools to read PLC text code and construct an abstract syntax tree through syntax analysis;

[0043] S22. By using a preset traversal method, combined with semantic analysis and division-by-zero exception access configuration analysis, the semantic analysis results and division-by-zero exception access configuration analysis results are obtained by traversing the abstract syntax tree multiple times.

[0044] S23. Generate an intermediate representation based on the semantic analysis results and the division-by-zero exception access configuration analysis results.

[0045] Preferably, the step of obtaining the semantic analysis results and the division-by-zero exception access configuration analysis results by traversing the abstract syntax tree multiple times through a preset traversal method, combined with semantic analysis and division-by-zero exception access configuration analysis, includes the following steps:

[0046] S221. Perform an initial traversal of the abstract syntax tree, identify statement nodes containing division operations through semantic analysis, extract division-to-zero exception configuration annotation information associated with the statement nodes containing division operations, and fill them into a preset symbol table for storage.

[0047] S222. Perform a second traversal of the abstract syntax tree and, based on the preset verification rules, verify the validity of the parameters of the division-by-zero exception configuration annotation information.

[0048] S223. Perform three traversals of the abstract syntax tree, associate the configuration information that passes the verification with the existing program symbols in the preset symbol table, and store the complete configuration information in the symbol table in the form of a structure.

[0049] Preferably, the step of compiling and deploying the intermediate representation into the PLC runtime program and running the intermediate representation under the PLC runtime scheduling to achieve division-by-zero exception protection includes the following steps:

[0050] S31. Configure error handling functions for division-by-zero exception read and write access in the intermediate representation; the error handling functions for division-by-zero exception read and write access are used to record error information when a division-by-zero exception read or write access occurs;

[0051] S32. For the configured intermediate representation, an executable file that can run on the PLC is generated through compilation and linking.

[0052] S33. Deploy the executable file to the PLC through a download operation, and run it under the scheduling of the PLC runtime.

[0053] According to a second aspect of the present invention, a configurable PLC runtime zero-division anomaly detection and protection system is provided, the system comprising:

[0054] The text code writing module is used to configure division-by-zero exception detection and protection strategies using extended syntax, and to write these strategies into PLC text code based on a text programming language.

[0055] The intermediate representation generation module is used to construct an abstract syntax tree based on PLC text code. It traverses the abstract syntax tree through a preset traversal method to perform semantic analysis and division-by-zero exception access configuration analysis, and generates an intermediate representation based on the analysis results.

[0056] The intermediate representation deployment module is used to compile and deploy the intermediate representation into the PLC runtime program, and run the intermediate representation under the PLC runtime scheduling to achieve division-by-zero exception protection.

[0057] According to a third aspect of the present invention, an electronic device is provided, the electronic device comprising: one or more processors and a memory, the memory being used to store one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors execute the programs to implement the steps of the above-described method.

[0058] According to a fourth aspect of the present invention, a computer-readable storage medium is provided, wherein a computer program is stored therein, wherein the steps of the above-described method are implemented when the computer program controls the device in which the computer-readable storage medium is located to execute during runtime.

[0059] The beneficial effects of this invention are as follows:

[0060] 1. This invention supports configurable design. Users can customize detection and protection strategy types by extending the ST language syntax. Protection rules can be quickly adjusted without modifying the main program. It supports three detection types: real-time detection, periodic detection, and conditional detection, as well as four protection strategies: skip detection, return to preset value, trigger alarm, and pause program. It supports parameterized configuration to achieve fine-grained strategy adjustment, thereby significantly improving the system's flexibility and scenario adaptability, and can quickly respond to the differentiated needs of different scenarios.

[0061] 2. This invention designs independent detection logic for divisor read and write operations, forming a full-link protection from preventing zero value input at the source of divisor write operations to intercepting division by zero in real time during divisor read operations. This achieves dual blocking of anomaly generation and propagation, greatly enhancing system security and reliability and preventing system failures. This invention also supports periodic detection, conditional detection, and skip detection protection strategies, reducing unnecessary performance overhead and avoiding significant system performance degradation. Furthermore, it supports recording error information at each division-by-zero anomaly location, significantly improving system intelligence and maintainability, thereby solving the problem of users finding it difficult to quickly locate faults and trace the root cause of anomalies. Attached Figure Description

[0062] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the embodiments will be briefly described below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort. In the drawings:

[0063] Figure 1 This is a flowchart illustrating the principle of a configurable PLC runtime division-by-zero anomaly detection and protection method according to an embodiment of the present invention.

[0064] Figure 2 This is a technical architecture diagram of a configurable PLC runtime division-by-zero anomaly detection and protection method according to an embodiment of the present invention;

[0065] Figure 3 This is a schematic diagram of a configurable PLC runtime zero division anomaly detection and protection system according to an embodiment of the present invention.

[0066] Figure 4 This is a flowchart of PLC text code generation in a configurable PLC runtime division-by-zero anomaly detection and protection method according to an embodiment of the present invention;

[0067] Figure 5 This is a hardware structure block diagram of the host device in a configurable PLC runtime division-by-zero anomaly detection and protection method according to an embodiment of the present invention.

[0068] In the picture:

[0069] 1. Text code writing module; 2. Intermediate representation generation module; 3. Intermediate representation deployment module. Detailed Implementation

[0070] To enable those skilled in the art to better understand the technical solutions in this application, the technical solutions in the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of this application, and not all of the embodiments. Based on the embodiments in this application, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of this application.

[0071] The methods and embodiments provided in this application can be executed on a host device or a similar computing device. Taking running on a host device as an example, Figure 5 This is a hardware structure block diagram of a host device for a configurable PLC runtime division-by-zero anomaly detection and protection method according to an embodiment of this application. Figure 5 As shown, the host device may include one or more ( Figure 5 Only one is shown in the diagram. The processor (which may include, but is not limited to, a microprocessor (MCU) or programmable logic device (FPGA), etc.) and storage for storing data are also shown. The host device may further include transmission devices for communication functions and input / output devices. Those skilled in the art will understand that... Figure 5 The structure shown is for illustrative purposes only and does not limit the structure of the host device described above. For example, the host device may also include components that are larger than... Figure 5 The more or fewer components shown, or having the same Figure 5 The different configurations shown.

[0072] The memory can be used to store computer programs, such as application software programs and modules, like the computer program corresponding to the exception handling method in this embodiment. The processor executes various functional applications and data processing by running the computer program stored in the memory, thus implementing the above-described method. The memory may include high-speed random access memory (RAM) and non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory may further include memory remotely located relative to the processor, and these remote memories can be connected to the host device via a network. Examples of such networks include, but are not limited to, the Internet, corporate intranets, local area networks (LANs), mobile communication networks, and combinations thereof.

[0073] Transmission devices are used to receive or send data over a network. Specific examples of the network described above may include a wireless network provided by a communication provider for the host device. In one example, the transmission device includes a Network Interface Controller (NIC), which can connect to other network devices via a base station to communicate with the Internet. In another example, the transmission device may be a Radio Frequency (RF) module used for wireless communication with the Internet.

[0074] According to an embodiment of the present invention, a configurable method for detecting and protecting against zero-division anomalies during PLC operation is provided.

[0075] Specifically, the overall architecture of this configurable PLC runtime division-by-zero anomaly detection and protection method is as follows: Figure 2 As shown, it mainly consists of three parts: a PLC programming module, a compiler module, and a PLC execution module. The PLC programming module allows users to write PLC programs containing division-by-zero exception configuration information. Users can set division-by-zero exception detection and protection strategies through the extended PLC text language syntax. The compiler module is responsible for reading and processing the written PLC program, generating an executable program that can run on the PLC. It sequentially performs lexical analysis and syntax analysis to construct an Abstract Syntax Tree (AST), then performs semantic analysis and division-by-zero exception access configuration analysis on multiple passes of the AST, storing semantic information. The IR generated by traversing the AST is compiled and linked to generate an executable program that can run on the PLC. The PLC execution module is used to execute the generated executable program, implementing division-by-zero exception detection, protection, and recording functions during operation. The interaction of these three modules realizes the configurable PLC division-by-zero exception access runtime detection and protection functions.

[0076] To enable configuration for division-by-zero anomaly detection and handling, this invention extends the syntax of the PLC text language (Structured Text ST, Instruction List IL), adopting a comment-compatible format. By using this extended syntax, users can configure corresponding division-by-zero anomaly detection and protection strategies for one or more division read or write operations in each line of a PLC program, according to actual needs.

[0077] The present invention will now be further described in conjunction with the accompanying drawings and specific embodiments, such as... Figure 1As shown, the process executes from left to right and top to bottom, starting from the beginning node and ending at the end node. Diamond-shaped boxes represent conditional decision nodes, and elliptical boxes represent the beginning or end nodes. Solid unidirectional arrows indicate the execution order, while dashed arrows originating from process nodes and their text labels represent data or file operations performed during the execution of those nodes.

[0078] The entire process is divided into three stages: PLC program writing, compiler processing, and PLC execution. The first stage after the start node is the PLC program writing stage, where the user writes the PLC program code and configures the division-by-zero exception operation. The second stage is the compiler processing stage, which is the core conversion link of the technical solution. Through lexical analysis, syntax analysis, semantic analysis, division-by-zero exception configuration analysis, and traversal of the AST to generate the IR, the user-written source code is finally compiled and linked into an executable program with division-by-zero protection logic. The last stage is the PLC execution stage, which realizes the conversion from the compilation result to actual operation. During runtime, the process nodes related to division-by-zero exception access detection and protection are executed.

[0079] According to a first embodiment of the present invention, a configurable method for detecting and protecting against division-by-zero anomalies during PLC operation is provided, comprising the following steps:

[0080] S1. Configure the division-by-zero exception detection and protection strategy using extended syntax, and write the division-by-zero exception detection and protection strategy into PLC text code based on a text programming language.

[0081] As a preferred embodiment, such as Figure 4 As shown, the steps for configuring the division-by-zero anomaly detection and protection strategy using extended syntax, and for writing the division-by-zero anomaly detection and protection strategy into PLC text code based on a text programming language, include:

[0082] S11. Based on an annotation-compatible syntax format and combined with preset configuration parameters, construct an extended syntax;

[0083] In a preferred embodiment, the annotation-compatible syntax format consists of a comment start character, a keyword string, configuration parameters, and a comment end character arranged in sequence, and the configuration parameters are enclosed in parentheses; the configuration parameters include operation identifier parameters, detection strategy types, protection strategy types, and auxiliary parameters, and the operation identifier parameters, detection strategy types, protection strategy types, and auxiliary parameters are separated by semicolons.

[0084] It should be noted that the extended syntax format is as follows:

[0085] / *DIV_ZERO_HANDLER(Operation Identifier Parameter; Detection Strategy Type; Auxiliary Parameter; Protection Strategy Type; Auxiliary Parameter)* / ;

[0086] This extended syntax is comment-compatible and uses the standard comment format. Compilers that do not support this syntax will handle the comments. The syntax begins with " / *", followed by the constant keyword string DIV_ZERO_HANDLER, used by the compiler to identify division-by-zero exception configuration information. Finally, the configuration parameters are enclosed in parentheses, including operation identifier parameters, detection strategy types, protection strategy types, and auxiliary parameters, separated by semicolons. Optional configuration parameter information is shown in Table 1 below:

[0087] Table 1 Optional Configuration Parameter Information

[0088] S12. Based on extended syntax, configure division-by-zero anomaly detection and protection strategies; and use a text programming language to write PLC text code for the configured division-by-zero anomaly detection and protection strategies, thus obtaining PLC text code.

[0089] As a preferred embodiment, the division-by-zero anomaly detection and protection strategy includes:

[0090] Skip the real-time detection and processing of all read values ​​in the statement, and directly execute the subsequent statements;

[0091] When an abnormal value is detected in real time during the read / divide operation, the divisor is forcibly set to a preset value.

[0092] The statement checks every ten scan cycles. If an anomaly is detected in writing the divisor variable, the divisor variable is forcibly set to the preset divisor variable.

[0093] The example configuration for the divide-by-zero anomaly detection protection is as follows:

[0094] Skip all real-time checks for dividing VALUE1 by a value in the statement and execute the subsequent statements directly:

[0095] " / *DIV_ZERO_HANDLER(R_VALUE1;RT;SKIP_EXE)* / ".

[0096] The statement detects an anomaly when reading VALUE1 and dividing it by a value, and forces the divisor to the preset value of 255.

[0097] " / *DIV_ZERO_HANDLER(R_VALUE1;RT;RETURN_DEF;default_value=255)* / ".

[0098] The statement checks every ten scan cycles. If an exception occurs when writing to the divisor variable VALUE2, the divisor variable is forcibly set to variable VALUE3.

[0099] " / *DIV_ZERO_HANDLER(W_VALUE2;CLE;cycle_interval=10;RETURN_DEF;default_value=VALUE3)* / ".

[0100] In addition, when writing PLC code, users can refer to the above examples and use extended syntax in statements containing division, divisors, and similar cases to adjust parameter settings for targeted detection and protection strategies for divisor reads or writes.

[0101] For divisor reads and writes, you can set detection policy types (RT real-time, CLE periodic, and COND conditional) and protection policy types (SKIP_EXE skip execution, TRIGGER_MSG trigger alarm and display error message, RETURN_DEF force the divisor to a preset value, the return value of a variable or a function, and PAUSE_WAIT pause the current program block and wait for reset).

[0102] For statements containing multiple divisor read and write operations, different detection and protection strategies can be set for each read and write operation.

[0103] In a preferred embodiment, the step of writing PLC text code for the configured division-by-zero anomaly detection and protection strategy, resulting in PLC text code, includes:

[0104] A PLC text code for a division-by-zero anomaly detection function is constructed using a general interface and strategy branch architecture. This function is used to transform user-configured detection rules into executable runtime logic through parameterized design and strategy adaptation logic, thereby achieving standardized connection between configuration measurement, detection execution, and result output.

[0105] As a preferred embodiment, the division-by-zero anomaly detection function includes real-time detection, periodic detection, and conditional detection;

[0106] The real-time detection is used to determine the divisor value of zero each time a protected division instruction is executed;

[0107] The periodic detection is used to perform sampling detection at preset detection intervals;

[0108] The conditional detection is used to perform a division by zero check when a pre-defined Boolean conditional expression is true.

[0109] It should be noted that defining and configuring the division-by-zero anomaly detection function is the core step in implementing configurable division-by-zero anomaly detection and protection in this invention. This function, through parameterized design and policy adaptation logic, transforms user-configured detection rules (real-time, periodic, conditional detection) into executable runtime logic, achieving a standardized connection between "configuration policy—detection execution—result output". The function adopts a "general interface + policy branch" architecture, receiving all the information required for detection through input parameters. The specific definition and pseudocode are as follows:

[0110] The detection strategy enumeration definition includes: real-time detection (RT), periodic detection (CLE), and conditional detection (COND).

[0111] Real-time detection (RT): Every time a protected division instruction is executed, a zero-value check for the divisor is performed immediately.

[0112] Periodic inspection (CLE): Sampling inspection is performed at fixed periodic intervals.

[0113] Conditional check (COND): The division by zero check is performed only if a predefined Boolean condition expression is true.

[0114] “enum DetectStrategy {

[0115] RT, / / Real-time detection

[0116] CLE, / / Periodic detection

[0117] COND / / Conditional Detection

[0118] }".

[0119] Real-time detection: When the PLC is running, the program executes the target division instruction. Before the division operation, the compiler calls a real-time detection function (such as RTDetect), passing the divisor as a parameter. The function performs a strict zero-value check, verifying that the passed divisor parameter `divisor` is zero. If an anomaly is detected, the function immediately records the anomaly details (such as operation identifier, strategy type, actual divisor value, etc.) and returns a boolean value.

[0120] If true, it means an anomaly has been detected, and the program flow will jump to the protection logic.

[0121] If the result is false, it means that the divisor is normal, and the program continues to perform normal division operations.

[0122] The pseudocode for the real-time detection function is as follows:

[0123] "bool RTDetect(double divisor) {

[0124] / / Check if the divisor is zero (supports integers and floating-point numbers)

[0125] if (divisor == 0 || fabs(divisor) < 1e-9) {

[0126] / / Record exception details

[0127] LogException("DIVISOR_READ", "RT", 0, false, divisor);

[0128] return true; / / Division by zero exception detected

[0129] }

[0130] return false; / / No exception detected

[0131] }".

[0132] Periodic detection: The user configures the detection period (cycle_interval) in the configuration. The compiler initializes a dedicated cycle counter (cycle_counter) for this division operation and sets it to 0. When the PLC runs, the counter increments by 1 each time the division instruction is executed.

[0133] If (counter%cycle_interval) != 0, skip the check and directly perform the division operation.

[0134] If (counter%cycle_interval) == 0, then rely on the modulo operation loop (or reset the counter) and perform the zero value judgment and recording for this time (same as real-time detection).

[0135] The subsequent anomaly detection and handling logic is the same as the real-time detection strategy. If an anomaly occurs, the protection logic is triggered; if normal, the division is performed.

[0136] The pseudocode for the periodicity detection function is as follows:

[0137] "bool CLEDetect(double divisor, int cycle_counter, int cycle_interval) {

[0138] / / Detection is performed only when the current period is an integer multiple of the detection period.

[0139] if (cycle_counter % cycle_interval == 0) {

[0140] / / Check if the divisor is zero

[0141] if (divisor == 0 || fabs(divisor) < 1e-9) {

[0142] LogException("DIVISOR_READ","CLE",cycle_counter, false,divisor);

[0143] return true; / / Division by zero exception detected

[0144] }

[0145] return false; / / No exception detected

[0146] }

[0147] return false; / / Not yet the detection period has ended

[0148] cycle_counter++; / / Increment the counter by one

[0149] }".

[0150] Condition detection: The user configures a Boolean condition expression in the division-by-zero configuration (e.g., condition_result="Mode==MANUAL"). Before executing the division instruction, the PLC runtime first parses and calculates the value of this condition expression. This requires reading the current value of the variables (such as Mode) referenced in the expression in real time.

[0151] If the condition result is true, then the same real-time detection strategy is used to determine, record, identify, and process the division by zero value.

[0152] If the condition result is false, the zero value check is skipped and the division operation is performed directly.

[0153] The statement triggers a conditional detection when the real-time pressure detection value is greater than a preset threshold. If the reading of the pressure gain divisor value is abnormal, the value of the divisor pressure gain is forcibly set to a preset random number function, and an alarm is triggered. The alarm error message is set to "pressure gain value abnormal".

[0154] For example, in the statement, when current_press > 5, a condition check is triggered. If the reading of press_gain is abnormal when divided by a value, the value of the divisor press_gain is forcibly set to the function RAND, and an alarm is triggered with the error message "press_gain value is abnormal".

[0155] "press_adjust := (max_press - current_press) / (max_press - current_press) / press_gain;

[0156] / *DIV_ZERO_HANDLER(R_press_gain;COND;condition="current_press>5";RETURN_DEF;default_value=RAND();TRIGGER_MSG;msg_info="press_gain value is abnormal")* / ".

[0157] The pseudocode for the conditional detection function is as follows:

[0158] "bool CONDDetect(double divisor, bool condition_result) {

[0159] / / Perform the check only if the condition result is true

[0160] if (condition_result) {

[0161] / / Check if the divisor is zero

[0162] if (divisor == 0 || fabs(divisor) < 1e-9) {

[0163] LogException("DIVISOR_READ","COND",0,true, divisor);

[0164] return true; / / Division by zero exception detected

[0165] }

[0166] }

[0167] return false; / / Condition not met or no exception

[0168] }".

[0169] Additionally, the division-by-zero anomaly detection function is defined as follows:

[0170] “bool DivZeroDetect(

[0171] float divisor, / / The divisor to be detected

[0172] DetectStrategy strategy, / / Detection strategy type

[0173] int cycle_counter, / / Cycle detection counter (current scan cycle number)

[0174] int cycle_interval, / / The interval between periodic checks (e.g., checking once every 5 cycles).

[0175] bool condition_result / / The result of the precondition for the condition check (e.g., the result of judging if temp>80).

[0176] ) {

[0177] / / Function internal logic

[0178] switch(strategy) {

[0179] case RT:

[0180] return RTDetect(divisor); / / Real-time detection

[0181] case CLE:

[0182] return CLEDetect(divisor, cycle_counter, cycle_interval); / / Cycle detection

[0183] case COND:

[0184] return CONDDetect(divisor, condition_result); / / Conditional detection

[0185] default:

[0186] return false; / / No exception is allowed if the strategy is invalid.

[0187] }

[0188] }".

[0189] The compiler automatically inserts function call code before the division operation based on the user-configured detection strategy. When the PLC runs, the function is triggered during the program scan cycle, reads the current value of the divisor from memory, passes in the corresponding strategy parameters, and then executes the internal branch logic of the function. Based on the output detection results, it drives the protection strategy (such as executing an alarm when there is an abnormality and executing the operation when there is no abnormality).

[0190] The PLC text code for the division-by-zero exception protection function is set by extending the syntax, and the division-by-zero exception protection function is stored in a preset symbol table in the form of a structure with standardized parameters. The division-by-zero exception protection function is used to execute the protection logic.

[0191] In a preferred embodiment, the division-by-zero exception protection function includes: returning a default value, triggering an alarm, pausing the program, and skipping execution;

[0192] The return default value is used to force the divisor to be set to the user-preset default value when a division by zero exception is detected;

[0193] The alarm trigger is used to call the PLC hardware interface to send an alarm, and at the same time write the alarm information to the log.

[0194] The pause procedure is used to modify the program state variable to pause the execution of the current program block and wait for a manual reset command.

[0195] The "skip execution" option is used to ignore the current division operation and directly jump to execute subsequent statements without performing the current division operation.

[0196] It should be noted that when writing PLC programs, users can set configurable division-by-zero exception access protection strategies through extended syntax. These protection strategies are stored as standardized parameters in a symbol table in the form of a structure. The core definition (pseudocode) is as follows:

[0197] (1) Definition of protection strategy enumeration:

[0198] “enum ProtectStrategy {

[0199] RETURN_DEF, / / Returns the default value

[0200] TRIGGER_MSG, / / Trigger alarm

[0201] PAUSE_WAIT, / / Pause the program

[0202] SKIP_EXE / / Skip execution

[0203] }".

[0204] (2) Definition of the protection strategy parameter structure:

[0205] “struct ProtectConfig {

[0206] ProtectStrategy strategy; / / Protection strategy type

[0207] float default_value; / / Default value for the RETURN_DEF strategy

[0208] char msg_info

[128] ; / / Alarm information

[0209] }".

[0210] Users can customize their protection strategy by configuring parameters using extended syntax. The compiler converts the configured protection strategy into a ProtectConfig structure and stores it in the symbol table, associating it with the corresponding divisor. When the compiler detects a division-by-zero exception (e.g., the DivZeroDetect function returns true), it generates protection logic code based on the ProtectConfig in the symbol table. At this point, the runtime system calls the ProtectHandler function. This function executes the corresponding protection logic based on the strategy configured in the ProtectConfig structure associated with the current division operation, retrieved from the symbol table.

[0211] The core logic and pseudocode of the division-by-zero exception handling function are as follows:

[0212] / / Division-to-zero exception handling function

[0213] void ProtectHandler(ProtectConfig config, float* result, int*program_state) {

[0214] switch (config.strategy) {

[0215] case RETURN_DEF:

[0216] ReturnDefaultHandler(config, result);

[0217] break

[0218] case TRIGGER_MSG:

[0219] TriggerMessageHandler(config);

[0220] break

[0221] case PAUSE_WAIT:

[0222] PauseWaitHandler(config, program_state);

[0223] break

[0224] case SKIP_EXE:

[0225] SkipExecutionHandler(program_state);

[0226] break

[0227] }

[0228] }".

[0229] Among them, (1) Return default value (RETURN_DEF): When a division by zero exception is detected, the divisor is forcibly set to the user-preset default value without interrupting program execution, ensuring that subsequent processes continue to run based on the preset value. The pseudocode is as follows:

[0230] / / Return default value handling function

[0231] void ReturnDefaultHandler(ProtectConfig config, float* result) {

[0232] *result = config.default_value; / / Assign a default value

[0233] / / Record exceptions (write to log)

[0234] LogException("Division by zero exception, returns default value: %f", config.default_value);

[0235] }".

[0236] (2) Trigger Alarm (TRIGGER_MSG): Calls the PLC hardware interface to send an alarm and writes the alarm information to the log. The pseudocode is as follows:

[0237] / / Trigger alarm handling function

[0238] void TriggerAlarmHandler(ProtectConfig config,int alarm_level,) {

[0239] / / Send alarm signal to PLC output module (hardware interface call)

[0240] PLC_SendAlarm(alarm_level, config.msg_info);

[0241] / / Record alarm logs via PLC internal interface

[0242] LogAlarm("Alarm Level: %d, Message: %s", alarm_level, config.alarm_msg);

[0243] }".

[0244] (3) Pause the program (PAUSE_WAIT): Modify the program status variable to pause the execution of the current program block and wait for a manual reset instruction. The pseudocode is as follows:

[0245] "void PauseProgramHandler(ProtectConfig config, int* program_state) {

[0246] *program_state = 0; / / Set the program state to paused (0 - paused, 1 - running)

[0247] LogException("Division by zero exception, program paused");

[0248] }".

[0249] (4) Skip execution (SKIP_EXE); Ignore the current division operation and jump directly to execute the subsequent statements without performing the current division operation. The pseudocode is as follows:

[0250] / / Skip execution of the processing function

[0251] void SkipExecutionHandler(int* program_state) {

[0252] *program_state = 2; / / Mark as skipped (2-skipped)

[0253] LogException("Division by zero exception, skip the current operation");

[0254] }".

[0255] Specifically, users can configure detection and protection strategies for divisor read and divisor write operations by extending the ST syntax. The configuration distinguishes operation types using operation identifier parameters, and defines processing rules based on the detection and protection strategy types. The compiler parses this information into structured data and stores it in the symbol table. Configuration information is embedded in the program as comments, as shown in the example code below:

[0256] / / Divisor write operation: prohibit writing zero; in case of an exception, write the divisor value to VALUE2.

[0257] / *DIV_ZERO_HANDLER(W_VALUE1;CLE;cycle_interval=10;RETURN_DEF;default_value=VALUE2)* /

[0258] / / Divisor read operation: Real-time check for zero; returns default value 1 if an error occurs.

[0259] result := 100 / divisor;

[0260] / / DIV_ZERO_HANDLER(R_divisor;RT;RETURN_DEF;default_value=1}".

[0261] When the compiler traverses the AST, it extracts the operation identifier parameters, detection / protection strategy types and their auxiliary parameters from the extended syntax, generates the OperationConfig structure, inserts detection function and protection function call code before and after each configuration operation node, and finally executes the scheduler at runtime.

[0262] (1) Divisor write operation: Prevent zero input at the source; the divisor write operation is the "source" of the division by zero exception, so it is necessary to check whether the written value is zero. The core logic is as follows:

[0263] / / Divisor write operation detection function

[0264] bool DetectDivisorWrite(DIV_ZERO_CONFIG config) {

[0265] switch(config.detect_strategy) {

[0266] case RT:

[0267] return RTDetect(divisor); / / Real-time detection

[0268] case CLE: / / Periodic detection

[0269] return CLEDetect(divisor, cycle_counter, cycle_interval);

[0270] case COND:

[0271] return CONDDetect(divisor, condition_result); / / Conditional detection

[0272] default:

[0273] return false; / / Strategy invalid

[0274] }

[0275] }

[0276] / / Divisor write protection function

[0277] void ProtectDivisorWrite(DIV_ZERO_CONFIG config) {

[0278] switch(config.protect_strategy) {

[0279] case RETURN_DEF:

[0280] ReturnDefaultHandler(config, result);

[0281] break

[0282] case TRIGGER_MSG:

[0283] TriggerMessageHandler(config);

[0284] break

[0285] case PAUSE_WAIT:

[0286] PauseWaitHandler(config, program_state);

[0287] break

[0288] case SKIP_EXE:

[0289] SkipExecutionHandler(program_state);

[0290] break

[0291] }

[0292] }".

[0293] (2) Divisor read operation: Core division by zero detection. The divisor read operation is the "core detection point" for division by zero anomalies. It is necessary to determine whether the read value is zero. The logic is as follows:

[0294] 1) Divisor read operation detection function:

[0295] "bool DetectDivisorRead(DIV_ZERO_CONFIG config) {

[0296] switch(config.detect_strategy) {

[0297] case RT:

[0298] return RTDetect(divisor); / / Real-time detection

[0299] case CLE: / / Periodic detection

[0300] return CLEDetect(divisor, cycle_counter, cycle_interval);

[0301] case COND:

[0302] return CONDDetect(divisor, condition_result); / / Conditional detection

[0303] default:

[0304] return false; / / Strategy invalid

[0305] }

[0306] }".

[0307] 2) Divisor read operation protection function:

[0308] "float ProtectDivisorRead(DIV_ZERO_CONFIG config) {

[0309] switch(config.protect_strategy) {

[0310] case RETURN_DEF:

[0311] ReturnDefaultHandler(config, result);

[0312] break

[0313] case TRIGGER_MSG:

[0314] TriggerMessageHandler(config);

[0315] break

[0316] case PAUSE_WAIT:

[0317] PauseWaitHandler(config, program_state);

[0318] break

[0319] case SKIP_EXE:

[0320] SkipExecutionHandler(program_state);

[0321] break

[0322] }

[0323] }".

[0324] S2. Based on PLC text code, construct an abstract syntax tree. Through a preset traversal method, traverse the abstract syntax tree to perform semantic analysis and division-by-zero exception access configuration analysis, and generate an intermediate representation based on the analysis results.

[0325] As a preferred embodiment, the step of constructing an abstract syntax tree based on PLC text code, performing semantic analysis and division-by-zero exception access configuration analysis by traversing the abstract syntax tree through a preset traversal method, and generating an intermediate representation based on the analysis results includes the following steps:

[0326] S21. Use compiler development tools to read PLC text code and construct an abstract syntax tree through syntax analysis;

[0327] It should be noted that compiler development tools such as Antlr (ANother Tool for Language Recognition, a compiler development tool) are used to read PLC text code written in ST and IL languages, and generate AST through lexical analysis and syntax analysis.

[0328] S22. By using a preset traversal method, combined with semantic analysis and division-by-zero exception access configuration analysis, the semantic analysis results and division-by-zero exception access configuration analysis results are obtained by traversing the abstract syntax tree multiple times.

[0329] In a preferred embodiment, the step of obtaining the semantic analysis results and the division-by-zero exception access configuration analysis results by traversing the abstract syntax tree multiple times through a preset traversal method, combined with semantic analysis and division-by-zero exception access configuration analysis, includes the following steps:

[0330] S221. Perform an initial traversal of the abstract syntax tree, identify statement nodes containing division operations through semantic analysis, extract division-to-zero exception configuration annotation information associated with the statement nodes containing division operations, and fill them into a preset symbol table for storage.

[0331] S222. Perform a second traversal of the abstract syntax tree and, based on the preset verification rules, verify the validity of the parameters of the division-by-zero exception configuration annotation information.

[0332] S223. Perform three traversals of the abstract syntax tree, associate the configuration information that passes the verification with the existing program symbols in the preset symbol table, and store the complete configuration information in the symbol table in the form of a structure.

[0333] S23. Generate an intermediate representation based on the semantic analysis results and the division-by-zero exception access configuration analysis results.

[0334] It should be noted that semantic analysis of the AST is performed through multiple traversals, and then the results are stored. Specifically, this includes:

[0335] The AST is traversed to identify all statement nodes containing division operations (for statement nodes in the AST), and their associated division-to-zero exception configuration annotation information is extracted and populated into the symbol table for storage. Then, the AST is traversed again to specifically verify the parameter validity of the division-to-zero exception configuration annotation information. The core is to ensure the matching of the division-to-zero exception configuration strategy type with its corresponding parameters and the completeness of the required parameters. Based on the values ​​of the required parameters, the auxiliary parameters are checked for matching (auxiliary parameters that do not correspond to the strategy are invalid and only issue a warning), as shown in Table 2 below:

[0336] Table 2 Validation Rules

[0337] The third pass associates program symbols and stores configuration information. The configuration information that passes verification (divided by zero) is associated with existing program symbols in the symbol table, and the complete configuration information is stored in the symbol table in the form of a structure (struct, the structure data type in C language), as shown below:

[0338] “struct DIV_ZERO_CONFIG{

[0339] char op_id

[32] ; / / Operation identifier, such as "R_press_gain"

[0340] char related_var

[32] ; / / Related divisor variable, such as "press_gain" in "(max_press - current_press) / press_gain"

[0341] char detect_strategy

[16] ; / / Detection strategy, such as "RT"

[0342] char protect_strategy

[16] ; / / Protection strategy, such as "TRIGGER_MSG"

[0343] / / Auxiliary parameters (dynamically stored according to strategy type)

[0344] int cycle_interval; / / Only valid when detect_strategy is "CLE"

[0345] char condition

[64] ; / / Only valid when detect_strategy is "COND"

[0346] float default_value; / / Only valid when protect_strategy is "RETURN_DEF"

[0347] char msg_info

[128] ; / / Only valid when protect_strategy is "TRIGGER_MSG"

[0348] }".

[0349] Furthermore, when traversing the AST to generate the IR, the compiler backend traverses the AST from top to bottom and left to right, combining the symbol table and semantic information to generate the IR (Intermediate Representation). When generating other IR formats, the IR generation code may need to be adjusted according to the characteristics of the target IR. During IR generation, except for statement nodes containing division read and write operations, other nodes in the AST do not require special processing. Figure 1 The flowchart can be used to generate the IR (Input Retrieval System) using standard compiler code generation techniques, such as... Figure 1 As shown in the flowchart, the semantics are determined by recursively traversing the AST and querying the symbol table, generating directly corresponding IR instructions for most nodes.

[0350] In this invention, the Indicator (IR) is generated by combining the symbol table and semantic information. When a variable is encountered, the compiler uses the variable name as the key to look up the symbol table and obtain the variable type, storage class, and memory address. Based on this information, the compiler generates a load instruction to read the value of the variable type from the memory address into a register. The core of this invention lies in changing the default decision path when handling division nodes through the early collection and storage of configuration information. Instead of simply issuing a division instruction, it issues a complete "detection-processing-merging" logic based on additional semantic information (configuration strategy), thus integrating runtime protection capabilities during the code generation stage.

[0351] S3. Compile and deploy the intermediate representation into the PLC runtime program, and run the intermediate representation under the PLC runtime scheduler to achieve division-by-zero exception protection.

[0352] In a preferred embodiment, the step of compiling and deploying the intermediate representation into the PLC runtime program and running the intermediate representation under the PLC runtime scheduling to achieve division-by-zero anomaly protection includes the following steps:

[0353] S31. Configure error handling functions for division-by-zero exception read and write access in the intermediate representation; the error handling functions for division-by-zero exception read and write access are used to record error information when a division-by-zero exception read or write access occurs;

[0354] S32. For the configured intermediate representation, an executable file that can run on the PLC is generated through compilation and linking.

[0355] S33. Deploy the executable file to the PLC through a download operation, and run it under the scheduling of the PLC runtime.

[0356] It should be noted that the error handling function for division-by-zero exception read / write access is defined in IR: LogException is used to log error information when a division-by-zero exception read / write access occurs. When a division-by-zero exception is detected, the error handling function is called, and the context information of the division-by-zero exception is passed to the function as a parameter. The function will record the code line and column number of the division-by-zero exception, the detection and protection strategy for the division-by-zero exception, and the error message set by the user through configuration parameters.

[0357] In addition, the generated IR is compiled and linked to produce an executable file that can run on the PLC. The generated executable file is then downloaded to the PLC and run under the PLC's runtime scheduler.

[0358] Among them, when entering the PLC operation phase, a division-by-zero detection is performed (the divisor is monitored in real time).

[0359] Determine if the divisor is zero: If not, proceed with the subsequent logic normally and the process ends. If yes, execute the corresponding protection strategy, including converting the divisor to zero, triggering an alarm and recording the abnormal information; return to the preset threshold or perform other processing.

[0360] It should be understood that the compiler front-end in this invention is described using Antlr (a compiler development tool) as the development tool. However, the implementation of this functionality is not limited to Antlr; other compiler development tools can also be used. The back-end is described using LLVM (a low-level virtual machine, a compiler framework) and IR (Introduction Redirection) as the target generation language, but it is not limited to a specific IR language. Other target languages ​​can also be used, and the implementation of detection and protection functions can be adjusted according to the characteristics of the target language.

[0361] This invention defines a specific format of extended compatible syntax to describe and define division-by-zero anomaly detection and protection configuration strategies. The extended syntax type, extended syntax format, division-by-zero anomaly detection, protection strategy type, and parameters are not limited.

[0362] like Figure 3 As shown, according to a second embodiment of the present invention, a configurable PLC runtime zero-division anomaly detection and protection system is provided, the system comprising:

[0363] Text code writing module 1 is used to configure division-by-zero exception detection and protection strategies using extended syntax, and to write the division-by-zero exception detection and protection strategies into PLC text code based on a text programming language;

[0364] Intermediate representation generation module 2 is used to construct an abstract syntax tree based on PLC text code. It traverses the abstract syntax tree through a preset traversal method to perform semantic analysis and division-by-zero exception access configuration analysis, and generates an intermediate representation based on the analysis results.

[0365] Intermediate representation deployment module 3 is used to compile and deploy the intermediate representation into the PLC runtime program, and run the intermediate representation under the PLC runtime scheduling to achieve division-by-zero exception protection processing.

[0366] According to a third embodiment of the present invention, an electronic device is provided, the electronic device comprising: one or more processors and a memory, the memory being used to store one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors cause the one or more processors to perform the steps in any of the above method embodiments.

[0367] According to a fourth embodiment of the present invention, a computer-readable storage medium is provided, wherein a computer program is stored in the computer-readable storage medium, wherein the computer program, when running, controls the device where the computer-readable storage medium is located to perform the steps in any of the above method embodiments.

[0368] In one exemplary embodiment, the aforementioned computer-readable storage medium may include, but is not limited to, various media capable of storing computer programs, such as a USB flash drive, read-only memory (ROM), random access memory (RAM), portable hard disk, magnetic disk, or optical disk.

[0369] 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, optical storage, etc.) containing computer-usable program code.

[0370] The specific embodiments described above further illustrate the purpose, technical solution, and beneficial effects of the present invention. It should be understood that the above descriptions are merely specific embodiments of the present invention and are not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.

Claims

1. A configurable PLC runtime zero division exception detection and protection handling method, characterized in that, Includes the following steps: S1. Configure the division-by-zero exception detection and protection strategy using extended syntax, and write the division-by-zero exception detection and protection strategy into PLC text code based on a text programming language. S2. Based on PLC text code, construct an abstract syntax tree. Through a preset traversal method, traverse the abstract syntax tree to perform semantic analysis and division-by-zero exception access configuration analysis, and generate an intermediate representation based on the analysis results. S3. Compile and deploy the intermediate representation into the PLC runtime program, and run the intermediate representation under the PLC runtime scheduler to achieve division-by-zero exception protection.

2. The configurable PLC runtime zero-divide exception detection and prevention process method of claim 1, wherein, The steps for configuring division-by-zero anomaly detection and protection strategies using extended syntax, and for writing these strategies into PLC text code based on a text programming language, include: S11. Based on an annotation-compatible syntax format and combined with preset configuration parameters, construct an extended syntax; S12. Based on extended syntax, configure division-by-zero anomaly detection and protection strategies; and use a text programming language to write PLC text code for the configured division-by-zero anomaly detection and protection strategies, thus obtaining PLC text code.

3. The configurable PLC runtime zero-divide exception detection and prevention process of claim 2, wherein, The syntax format compatible with the annotation consists of the annotation start character, keyword string, configuration parameters and annotation end character arranged in sequence, and the configuration parameters are enclosed in parentheses. The configuration parameters include operation identification parameters, detection strategy type, protection strategy type, and auxiliary parameters, and these parameters are separated by semicolons.

4. A configurable PLC runtime division-by-zero anomaly detection and protection method according to claim 3, characterized in that, The division-by-zero anomaly detection and protection strategy includes: Skip the real-time detection and processing of all read values ​​in the statement, and directly execute the subsequent statements; When an abnormal value is detected in real time during the read / divide operation, the divisor is forcibly set to a preset value. The statement checks every ten scan cycles. If an anomaly is detected in writing the divisor variable, the divisor variable is forcibly set to the preset divisor variable.

5. A configurable PLC runtime division-by-zero anomaly detection and protection method according to claim 4, characterized in that, The PLC text code for writing the configured division-by-zero anomaly detection and protection strategy includes: A PLC text code for a division-by-zero anomaly detection function is constructed using a general interface and strategy branch architecture. This function is used to transform user-configured detection rules into executable runtime logic through parameterized design and strategy adaptation logic, thereby achieving standardized connection between configuration measurement, detection execution, and result output. The PLC text code for the division-by-zero exception protection function is set by extending the syntax, and the division-by-zero exception protection function is stored in a preset symbol table in the form of a structure with standardized parameters. The division-by-zero exception protection function is used to execute the protection logic.

6. A configurable PLC runtime division-by-zero anomaly detection and protection method according to claim 5, characterized in that, The division-by-zero anomaly detection function includes real-time detection, periodic detection, and conditional detection. The real-time detection is used to determine the divisor value of zero each time a protected division instruction is executed; The periodic detection is used to perform sampling detection at preset detection intervals; The conditional detection is used to perform a division by zero check when a pre-defined Boolean conditional expression is true.

7. A configurable PLC runtime division-by-zero anomaly detection and protection method according to claim 6, characterized in that, The division-by-zero exception protection function includes: returning a default value, triggering an alarm, pausing the program, and skipping execution; The return default value is used to force the divisor to be set to the user-preset default value when a division by zero exception is detected; The alarm trigger is used to call the PLC hardware interface to send an alarm, and at the same time write the alarm information to the log. The pause procedure is used to modify the program state variable to pause the execution of the current program block and wait for a manual reset command. The "skip execution" option is used to ignore the current division operation and directly jump to execute subsequent statements without performing the current division operation.

8. A configurable PLC runtime division-by-zero anomaly detection and protection method according to claim 1, characterized in that, The process of constructing an abstract syntax tree based on PLC text code, performing semantic analysis and division-by-zero exception access configuration analysis by traversing the abstract syntax tree through a preset traversal method, and generating an intermediate representation based on the analysis results includes the following steps: S21. Use compiler development tools to read PLC text code and construct an abstract syntax tree through syntax analysis; S22. By using a preset traversal method, combined with semantic analysis and division-by-zero exception access configuration analysis, the semantic analysis results and division-by-zero exception access configuration analysis results are obtained by traversing the abstract syntax tree multiple times. S23. Generate an intermediate representation based on the semantic analysis results and the division-by-zero exception access configuration analysis results.

9. A configurable PLC runtime division-by-zero anomaly detection and protection method according to claim 8, characterized in that, The process of obtaining semantic analysis results and division-by-zero exception access configuration analysis results by traversing the abstract syntax tree multiple times through a preset traversal method, combined with semantic analysis and division-by-zero exception access configuration analysis, includes the following steps: S221. Perform an initial traversal of the abstract syntax tree, identify statement nodes containing division operations through semantic analysis, extract division-to-zero exception configuration annotation information associated with the statement nodes containing division operations, and fill them into a preset symbol table for storage. S222. Perform a second traversal of the abstract syntax tree and, based on the preset verification rules, verify the validity of the parameters of the division-by-zero exception configuration annotation information. S223. Perform three traversals of the abstract syntax tree, associate the configuration information that passes the verification with the existing program symbols in the preset symbol table, and store the complete configuration information in the symbol table in the form of a structure.

10. A configurable PLC runtime division-by-zero anomaly detection and protection method according to claim 1, characterized in that, The process of compiling and deploying the intermediate representation into the PLC runtime program, and running the intermediate representation under the PLC runtime scheduling to achieve division-by-zero exception protection includes the following steps: S31. Configure error handling functions for division-by-zero exception read and write access in the intermediate representation; the error handling functions for division-by-zero exception read and write access are used to record error information when a division-by-zero exception read or write access occurs; S32. For the configured intermediate representation, an executable file that can run on the PLC is generated through compilation and linking. S33. Deploy the executable file to the PLC through a download operation, and run it under the scheduling of the PLC runtime.