Pin-based ladder diagram program grammar checking method, system and equipment and medium
Through the pin-based ladder diagram program syntax check method, the problem of failure to effectively handle the overloadable and extensible characteristics of block elements in the prior art is solved, and a comprehensive syntax check of ladder diagram program is realized, which improves the accuracy of detection and efficiency, supporting the overloadable and extensible features of the IEC61131 standard.
Patent Information
- Application Number
- CN202410935036.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-12
- Publication Date
- 2024-11-08
AI Technical Summary
The existing ladder program syntax check algorithm failed to effectively consider the overloadable and extensible characteristics of block elements, resulting in syntax check defects when processing functional blocks specified in IEC61131-3 standard, and pin binding cannot be detected correctly. Variable errors, pin connection errors, and feedback loops.
Using the pin-based ladder diagram program syntax check method, traversing and combing all elements and pins in the ladder diagram, topological sorting is performed based on the pin connection relationship, deducing the data types of pins in the sorted element, and Combining overloadable and extensible features, checking the syntax of each pin, supporting the overloadable and extensible features defined in the IEC61131 standard.
It realizes detection of pin binding variable errors, pin connection errors, and feedback loops of ladder diagram programs, improves the accuracy and efficiency of syntax checks, and can be executed regularly during the ladder diagram editing stage, assisting developers to write the correct ladder Graphic program.
Smart Images

Figure CN118916034A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of program development, and in particular to a pin-based ladder diagram program syntax checking method, system, device and medium. Background Art
[0002] In PLC development, ladder diagram programs are a common and intuitive programming method for industrial automation control. Current technical means usually require that all the primitives in the ladder diagram be obtained first. These primitives include networks, contacts, coils, jumps, return primitives, block primitives, and power rails. Performing syntax checks on each primitive one by one is a key step to ensure the correctness and reliability of the program. However, the current syntax checking scheme has certain limitations, especially the failure to fully consider the reloadable and scalable characteristics of block primitives, which are clearly stipulated in the IEC61131-3 standard, resulting in defects in the existing syntax checking algorithm when processing block primitives.
[0003] First, the syntax check of the contact element focuses on ensuring that the variable type associated with it is Boolean (BOOL), because contacts are usually used to represent switch states or conditional judgments and must be Boolean values. If the contact element is associated with a non-Boolean variable, it will cause a logical judgment error. Similarly, the coil element must also be associated with a Boolean variable. Coils are usually used to control the start and stop or state retention of output devices. Non-Boolean associations will result in the inability to control the output device normally. The syntax check of the jump element and the return element focuses on the correctness of the label. The jump element is used to implement the jump logic of the program, and the associated label must exist and be unique in the program. If the jump element is associated with a non-existent label, an error will occur when the program is executed that the target location cannot be found, resulting in an interruption of the control logic. Block elements (such as function blocks FB) play an important role in PLC programming, and the variable type bound to their pins must be consistent with the definition. For example, if the input pin of a function block is defined as an integer (INT), the bound variable must also be an integer. If the bound variable type is incorrect, the function block will not be executed correctly.
[0004] However, the existing scheme has deficiencies in the syntax checking of block primitives, mainly because it does not consider the overloading and extensible characteristics of block primitives. The IEC61131-3 standard clearly stipulates that function blocks can have different overloaded versions, and the number and type of parameters of each version may be different. In addition, the extended characteristics of function blocks allow multiple functions to be implemented by configuring different parameters in different application scenarios. In current technical means, such as Figure 2As shown, it is necessary to first obtain all the primitives in the ladder diagram, including networks, contacts, coils, jumps, return primitives, block primitives, and power rails, and then perform syntax checks on each primitive in turn, such as syntax problems such as contact primitives associated with non-BOOL type variables, block primitive pin binding variable type errors, coils associated with non-BOOL type variables, and jump primitive associated labels that do not exist, and finally output syntax error information. However, the scheme does not take into account the overloadable and extensible characteristics of block primitives, which are clearly defined in the IEC61131-3 standard, resulting in the syntax check algorithm not supporting block primitives with overloadable and extensible characteristics. Summary of the invention
[0005] Based on this, it is necessary to provide a pin-based ladder diagram program syntax checking method, system, device and medium to address the above technical problems.
[0006] In a first aspect, the present invention provides a pin-based ladder diagram program syntax checking method, the method comprising the following steps:
[0007] S1. Traverse all networks in the ladder diagram and sort out all the graphics elements and pins in the network;
[0008] S2. Topologically sort all the primitives in the network based on the connection relationship of the pins;
[0009] S3. Based on the overloadable and extensible characteristics, deduce the data type of the pins in the sorted primitives;
[0010] S4. Based on the deduced result of the data type, check the syntax of each pin.
[0011] Furthermore, based on the connection relationship of the pins, topological sorting of all the primitives in the network includes the following steps:
[0012] S21, extracting a network in the ladder diagram in order;
[0013] S22, based on the number of pins and connection relationship in the primitive, check the validity of the pins;
[0014] S23, introducing the position relationship of the pins and performing topological sorting of all the primitives in the network;
[0015] S24, setting feedback loop detection to remove graphics elements with loop errors.
[0016] Furthermore, based on the number of pins and the connection relationship in the primitive, checking the validity of the pins includes the following steps:
[0017] S221, traverse the primitives, and extract the input pins and output pins stored in each primitive;
[0018] S222, counting and identifying the number of input pins and output pins included in each primitive, and the connection relationship between each input pin and output pin;
[0019] S223. Check the validity of the pins of all graphics elements according to the preset pin quantity rule and pin connection rule, and filter out the valid pins that do not meet the rules.
[0020] Furthermore, the position relationship of the pins is introduced, and the topological sorting of all the primitives in the network is performed, including the following steps:
[0021] S231, based on the connection relationship between pins and the subordinate relationship between pins and primitives, calculate the predecessor primitive list and the successor primitive list of each primitive in order from top to bottom and from left to right;
[0022] S232, checking the number of predecessor primitive lists and the number of successor primitive lists, if the number of predecessor primitive lists or successor primitive lists is more than one, introducing the position information of the primitives, and sorting the predecessor primitive lists and the successor primitive lists from top to bottom by comparing the y-axis coordinate data;
[0023] S233, finding a graph element whose number of predecessor graph element lists is zero, defining the in-degree of the graph element as zero, adding the graph element to the sorting result set, removing the graph element from the network, and then recalculating the in-degree and out-degree of the graph element in the new network;
[0024] S234. Repeat step S233 until there are no primitives with zero in-degree, and stop sorting.
[0025] Furthermore, setting feedback loop detection and removing primitives with loop errors includes the following steps:
[0026] S241, comparing the number of graph elements in the initial network with the number of graph elements in the sorting result set, if the number of graph elements in the initial network is greater than the number of graph elements in the sorting result set, determining that a feedback loop is formed between the graph elements, if the number of graph elements in the initial network is less than or equal to the number of graph elements in the sorting result set, determining that no feedback loop is formed between the graph elements;
[0027] S242. Identify and record the feedback loops that exist during the detection process, and return error information.
[0028] Furthermore, based on the overloadable and extensible characteristics, deriving the data type of the pins in the sorted primitives includes the following steps:
[0029] S31, obtaining a sorted primitive list, detecting whether there are unprocessed primitives in the network, and if so, returning an error message; if not, extracting a primitive in sequence;
[0030] S32, detecting the type of the extracted primitive, and identifying the reloadable and extensible characteristics of the primitive;
[0031] S33, verifying whether all pins in the primitive are correctly connected. If there are incorrectly connected pins, an error message is returned. If there are no incorrectly connected pins, step S34 is continued.
[0032] S34. Based on the primitive type, different derivation algorithms are called to derive the data type of each pin.
[0033] Furthermore, according to the primitive type, different derivation algorithms are called to derive the data type of each pin, including the following steps:
[0034] S341. If the type of the primitive is a non-block primitive, the data type of the pin is a Boolean type;
[0035] S342. If the type of the primitive is a block primitive, call the block primitive derivation algorithm to derive the data type of each pin in the block primitive in turn.
[0036] Further, calling the block primitive derivation algorithm to sequentially derive the data type of each pin in the block primitive includes the following steps:
[0037] S3421, obtain all pins of the block primitive and basic information of the corresponding program organization unit;
[0038] S3422, check whether there are unprocessed pins in the primitive, if yes, return an error message, if no, continue to execute step S3423;
[0039] S3423, extract all pins in the block primitive, traverse them in sequence, deduce the formal parameter data type and actual parameter data type of each pin, if the formal parameter data type conflicts with the actual parameter data type, return an error message, if the formal parameter data type conflicts with the actual parameter data type, then output the data type derivation result of the formal parameter data type;
[0040] S3424. If the pin is an input pin, obtain the actual parameter data type of the pin, and determine whether the formal parameter data type of the pin is an overloaded type. If not, set the data type of the pin to the formal parameter data type. If so, determine whether the conversion relationship between the actual parameter data type and the formal parameter data type is legal. If not, return an error message. If legal, set the data type of the pin to the actual parameter data type and return to step S3422.
[0041] Further, based on the derivation result of the data type, checking the syntax of each pin includes the following steps:
[0042] S41, obtain all pins in the network, check whether there is an unprocessed pin, if so, return an error message, if not, extract a pin in the primitive;
[0043] S42, detecting whether the data type of the pin is an overloaded type, if it is an overloaded type, reporting an error, if not an overloaded type, determining the type of the pin, and executing steps S43 and S44 in sequence;
[0044] S43, if the pin is an output pin, determine whether the output pin is directly bound to a variable, if not directly bound to a variable, return to step S41, if directly bound to a variable, determine whether the data type of the output pin is consistent with the variable data type, if not, return an error message, if consistent, return to step S41;
[0045] S44. If the pin is an input pin, determine whether the input pin is directly bound to a variable. If it is directly bound to a variable, determine whether the data type of the input pin is consistent with the variable data type. If not, return an error message. If they are consistent, return to step S41. If the variable is not directly bound, determine whether the data type of the input pin is consistent with the data type of the connected pin. If not, return an error message. If they are consistent, return to step S41.
[0046] In a second aspect, the present invention further provides a pin-based ladder diagram program syntax checking system, the system comprising:
[0047] The traversal and combing module is used to traverse all networks in the ladder diagram and comb all the graphics elements and pins in the network;
[0048] The topological sorting module is used to topologically sort all the primitives in the network based on the connection relationship of the pins;
[0049] The pin derivation module is used to derive the data type of the pins in the sorted primitives based on the overloadable and extensible characteristics;
[0050] The result checking module checks the syntax of each pin based on the deduced result of the data type.
[0051] Further, the topological sorting module includes: a network extraction unit, a validity check unit, a sorting execution unit and a loop detection unit;
[0052] Wherein, the network extraction unit is used to extract a network in the ladder diagram in order;
[0053] A validity check unit, used to check the validity of pins based on the number of pins and connection relationships in the primitive;
[0054] A sorting execution unit is used to introduce the position relationship of the pins and perform topological sorting of all the primitives in the network;
[0055] The loop detection unit is used to set feedback loop detection and eliminate primitives with loop errors.
[0056] Further, the pin derivation module includes: a list detection unit, a type identification unit, a connection verification unit and a type derivation unit;
[0057] The list detection unit is used to obtain the sorted primitive list and detect whether there are unprocessed primitives in the network. If so, an error message is returned. If not, a primitive is extracted in sequence.
[0058] A type identification unit, used to determine the type of the extracted primitive and identify the reloadable and extensible characteristics of the primitive;
[0059] The connection verification unit is used to verify whether all pins in the primitive are correctly connected. If there are incorrectly connected pins, an error message is returned. If there are no incorrectly connected pins, the type deduction unit continues to be called;
[0060] The type deduction unit is used to call different deduction algorithms according to the primitive type to deduce the data type of each pin.
[0061] Further, the result checking module includes: a network checking unit, a reload judgment unit, a binding judgment unit and a variable verification unit;
[0062] The network checking unit is used to obtain all pins in the network and check whether there are unprocessed pins. If there are unprocessed pins, an error message is returned. If not, a pin in the primitive is extracted.
[0063] The overload judgment unit is used to detect whether the data type of the pin is an overload type. If it is an overload type, an error is reported. If it is not an overload type, the type of the pin is determined, and the binding judgment unit and the variable verification unit are called in sequence;
[0064] A binding judgment unit, for judging whether the output pin is directly bound to a variable if the pin is an output pin, and returning to the network inspection unit if the pin is not directly bound to a variable; and for judging whether the data type of the output pin is consistent with the data type of the variable if the pin is directly bound to a variable, and returning an error message if the data type is inconsistent, and returning to the network inspection unit if the data type is consistent;
[0065] The variable verification unit is used to determine whether the input pin is directly bound to the variable if the pin is an input pin. If it is directly bound to the variable, it is used to determine whether the data type of the input pin is consistent with the variable data type. If not, an error message is returned. If they are consistent, it returns to the network inspection unit. If the variable is not directly bound, it is used to determine whether the data type of the input pin is consistent with the data type of the connected pin. If not, an error message is returned. If they are consistent, it returns to the network inspection unit.
[0066] In a third aspect, the present invention further provides a computer device.
[0067] In some embodiments, the computer device includes a memory and a processor, the memory stores a computer program, and the processor implements the steps of the above method when executing the computer program.
[0068] In a fourth aspect, the present invention also provides a computer-readable storage medium.
[0069] In one embodiment, a computer program is stored on a computer-readable storage medium, and when the computer program is executed by a processor, the steps of the above method are implemented.
[0070] The beneficial effects of the present invention are as follows: a set of ladder diagram syntax checking algorithms is designed based on the connection relationship between pins and the subordinate relationship between pins and graphic elements. The algorithm supports the overloadable and extensible characteristics defined in the IEC61131 standard. By combining the overloadable and extensible characteristics of the ladder diagram program, three types of errors, namely pin binding variable errors, pin connection errors, and feedback loop errors, of the ladder diagram program can be detected, thereby detecting more syntax errors in the early syntax checking stage of the ladder diagram and avoiding errors from being brought into the later stage of compilation. Since the syntax checking algorithm of the present invention has a fast execution speed and a small burden on computing resources, it can be executed regularly in the ladder diagram editing stage, which can assist developers in writing ladder diagram programs. BRIEF DESCRIPTION OF THE DRAWINGS
[0071] The drawings described herein are used to provide a further understanding of the present invention and constitute a part of the present invention. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation of the present invention. In the drawings:
[0072] Figure 1 is a flowchart of a pin-based ladder diagram program syntax checking method according to an embodiment of the present invention;
[0073] Figure 2 It is a flow chart of steps of the existing method for checking the syntax of a gradient graph program;
[0074] Figure 3 It is a schematic diagram of the organization structure of ladder diagram data in a pin-based ladder diagram program syntax checking method according to an embodiment of the present invention;
[0075] Figure 4 It is a flow chart of a syntax checking algorithm of a gradient diagram program in a pin-based syntax checking method of a ladder diagram program according to an embodiment of the present invention;
[0076] Figure 5 is a flowchart of a pin-based topological sorting algorithm in a pin-based ladder diagram program syntax checking method according to an embodiment of the present invention;
[0077] Figure 6 is a flow chart of feedback loop detection in a pin-based ladder diagram program syntax checking method according to an embodiment of the present invention;
[0078] Figure 7 It is a flow chart of a pin data type derivation algorithm in a pin-based ladder diagram program syntax checking method according to an embodiment of the present invention;
[0079] Figure 8 It is a flow chart of an algorithm for deducing pin data types of block primitives in a pin-based ladder diagram program syntax checking method according to an embodiment of the present invention;
[0080] Fig. 9 is a data type relationship diagram in a pin-based ladder diagram program syntax checking method according to an embodiment of the present invention;
[0081] Fig.10 It is a flow chart of a pin connection legality verification algorithm in a pin-based ladder diagram program syntax checking method according to an embodiment of the present invention;
[0082] Fig.11 is a principle block diagram of a pin-based ladder diagram program syntax checking system according to an embodiment of the present invention;
[0083] Fig.12 is a schematic diagram of the structure of a computer device according to an embodiment of the present invention;
[0084] Fig.13 is a schematic diagram of a ladder diagram primitive according to an embodiment of the present invention;
[0085] Fig.14 2 is a schematic diagram of a pin structure according to an embodiment of the present invention.
[0086] Figure numbers: 1. Traversal and combing module; 2. Topological sorting module; 3. Pin derivation module; 4. Result checking module. DETAILED DESCRIPTION
[0087] In order to make the purpose, technical solution and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention.
[0088] See also Figure 1-Figure 10 , a pin-based ladder diagram program syntax checking method is provided, the method comprising the following steps:
[0089] S1. Traverse all networks in the ladder diagram and sort out all the graphics elements and pins in the network.
[0090] Among them, ladder diagram is a PLC programming language, also known as ladder logic. It is called ladder diagram because it is composed of horizontal lines and looks like a ladder. Ladder diagram was invented by electrical engineers and is a graphical programming language.
[0091] like Figure 3 As shown, the present invention is based on Figure 3 The ladder diagram data is organized in a structure. Each ladder diagram contains at least one network, such as Fig.13 As shown in the figure, each network consists of contacts, coils, jump primitives, return primitives, block primitives and power rails, and each primitive has at least one pin. The primitive contains the ID number, primitive type, pin list, predecessor primitive list, and successor primitive list information.
[0092] like Fig.14 As shown, the pins are divided into input pins and output pins.
[0093] S2. Based on the connection relationship of the pins, topologically sort all the elements in the network.
[0094] In the description of the present invention, topological sorting of all the primitives in the network based on the connection relationship of the pins includes the following steps:
[0095] S21. Extract a network in the ladder diagram in order.
[0096] S22. Check the validity of the pins based on the number of pins and the connection relationship in the graphic element.
[0097] In the description of the present invention, based on the number of pins and the connection relationship in the primitive, checking the validity of the pins includes the following steps:
[0098] S221. Traverse the primitives and extract the input pins and output pins stored in each primitive.
[0099] The metadata stores its own input pin and output information, and the input pin stores the output pin ID connected to it.
[0100] S222: Count and identify the number of input pins and output pins included in each primitive, and the connection relationship between each input pin and output pin.
[0101] S223. Check the validity of the pins of all graphics elements according to the preset pin quantity rule and pin connection rule, and filter out the valid pins that do not meet the rules.
[0102] Specifically, the pin number rules are: the left power rail has only 1 output pin, the right power rail has only 1 input pin, the contact, coil, jump, and return primitives each have 1 input and 1 output pin, and the block primitive has ≥1 input and output pins.
[0103] Pin connection rules: Input pins must be connected to ≥1 output pin, and output pins do not need to be connected to other pins.
[0104] S23. Introduce the position relationship of the pins and perform topological sorting of all the elements in the network.
[0105] Specifically, the present invention formulates the following rules and requires that the ladder diagram data be managed according to the rules.
[0106] Rule 1: The left power rail has only 1 output pin, the right power rail has only 1 input pin, the contact, coil, jump, and return primitives each have 1 input and 1 output pin, and the block primitive has ≥ 1 input and output pin.
[0107] Rule 2: The output pins of jump and return primitives must be connected to the right power rail. This rule only constrains the primitive data storage of the ladder diagram, and does not constrain the display of the primitive.
[0108] Rule 3: The execution order of the ladder diagram is from top to bottom and from left to right.
[0109] Rule 4: For pins in the same primitive, first execute the primitive's input pins from top to bottom, and then execute the primitive's output pins according to the same rule; for multiple pins connected to the same pin, execute them in order from top to bottom.
[0110] Rule 5: A pin can be directly bound to a variable, or connected to another pin, but only one of the two.
[0111] The purpose of executing the pin-based topological sorting algorithm is to infer the topological relationship between the disordered primitives in the network according to the connection relationship of the pins, and then sort the primitives according to the topological relationship.
[0112] In the description of the present invention, Figure 5 As shown, the position relationship of the pins is introduced, and the topological sorting of all the elements in the network is performed, which includes the following steps:
[0113] S231. Based on the connection relationship between pins and the subordinate relationship between pins and primitives, calculate the predecessor primitive list and the successor primitive list of each primitive in order from top to bottom and from left to right (following rules 3 and 4).
[0114] S232. Check the number of predecessor primitive lists and successor primitive lists. If the number of predecessor primitive lists or successor primitive lists is more than one, introduce the position information of the primitives, and sort the predecessor primitive lists and successor primitive lists from top to bottom by comparing the y-axis coordinate data (each primitive stores the center point coordinates, the smaller the y coordinate, the higher the primitive position).
[0115] S233, find a graph element with zero number in the predecessor graph element list, define the in-degree of the graph element as zero, add the graph element to the sorting result set, remove the graph element from the network, and then recalculate the in-degree and out-degree of the graph element in the new network.
[0116] S234. Repeat step S233 until there are no primitives with zero in-degree, and stop sorting.
[0117] S24, setting feedback loop detection to remove graphics elements with loop errors.
[0118] Among them, the ladder diagram is a sequential execution programming language. The feedback loop refers to the reverse link from the output pin to the input pin in the ladder diagram. Under normal circumstances, in the topological relationship, the output pin of the left element should be connected to the input pin of the right element. However, if a reverse link appears, it means that there is a feedback loop error in the ladder diagram program. This error will cause the program to fail to end normally, but will continue to loop in a certain link, causing the program to get stuck.
[0119] In the description of the present invention, Figure 6 As shown in the figure, setting feedback loop detection and removing primitives with loop errors includes the following steps:
[0120] S241. Compare the number of graph elements in the initial network with the number of graph elements in the sorting result set. If the number of graph elements in the initial network is greater than the number of graph elements in the sorting result set, determine that a feedback loop is formed between the graph elements (because once a feedback loop is formed, during the sorting algorithm, the in-degree of each graph element in the loop cannot be reduced to 0, and thus cannot participate in the sorting process). If the number of graph elements in the initial network is less than or equal to the number of graph elements in the sorting result set, determine that no feedback loop is formed between the graph elements.
[0121] S242. Identify and record the feedback loops that exist during the detection process, and return error information.
[0122] The final result set will lack cyclic primitives, so that the error of "feedback loop" mentioned in the present invention can be identified.
[0123] S3. Based on the overloadable and extensible characteristics, derive the data type of the pins in the sorted primitives.
[0124] According to the description of overloadable and extensible features in the IEC61131-3 standard, if the data type uses ANY_INT, ANY_NUM, and other data types starting with ANY, the language element has the overloadable feature. For example, if the ADD function block uses ANY_NUM, then REAL, SINT, LINT, and other data types can be used to pass values. Extensible means that the number of pins of the block element is variable. For example, when calling the ADD function block, you can ADD(1, 2) or ADD(1, 2, 3, 4).
[0125] The pin data type derivation algorithm considers the overloadable and extensible characteristics, derives the data types of all pins in the network, and obtains the data type of each pin in actual use. The pin situation of each primitive has been described in rules 1, 2, and 5 of the topological sorting algorithm. In order to ensure the correct execution of the pin data type derivation algorithm, the following rules are formulated in combination with the IEC61131-3 standard.
[0126] Rule 1: If it is EN and ENO pins, the data type of the pin is derived as BOOL.
[0127] Rule 2: If the POU corresponding to the block element is a function type, there is a default OUT pin in the ladder diagram to represent the function return value, and the data type of the pin is deduced from the return value type of the POU.
[0128] Rule 3: If the POU corresponding to the block element has the extensible feature, the pin names are incremented in sequence from IN1, IN2, and IN3, and the data types of the expanded pins are exactly the same.
[0129] Rule 4: If the POU corresponding to a block element has an overloadable feature, then only one overloaded data type can exist in the block element; if the data type of the output pin is an overloaded type, then there must be an input pin of the same overloaded type. When in use, the data types of the variables connected to the pins of each overloaded type of the same block element must be consistent.
[0130] Rule 5: The data type of the pins of power rails, contacts, coils, jumps, and return primitives is derived as BOOL.
[0131] Rule 6: Input pins must be directly bound to variables or connected to other pins; output pins do not have to be bound to variables or connected to other pins.
[0132] In the description of the present invention, Figure 7As shown, based on the overloadable and extensible characteristics, the data type of the pins in the sorted primitives is derived by the following steps:
[0133] S31. Obtain a sorted list of primitives, and check whether there are any unprocessed primitives in the network. If so, return an error message; if not, extract a primitive in sequence.
[0134] S32. Detect the type of the extracted graphic element and identify the overloadable and extensible characteristics of the graphic element.
[0135] S33, verify whether all pins in the primitive are correctly connected. If there are incorrectly connected pins, return an error message. If there are no incorrectly connected pins, continue to execute step S34.
[0136] S34. Based on the primitive type, different derivation algorithms are called to derive the data type of each pin.
[0137] Specifically, the pin data type derivation algorithm flow is as follows: Figure 7 As described, the error reporting in this process will identify errors such as "pin binding variable error" and "pin connection error" mentioned in the present invention, but only errors such as unbound variables or pins not connected as required will be identified. This algorithm relies on the previous topological sorting algorithm. The execution order of the ladder diagram is from left to right and from top to bottom. Therefore, each pin must be derived from left to right and from top to bottom according to the topological relationship. The previous pin derivation result as input will affect the subsequent pin derivation result. The pin connection rules and pin data type derivation methods of each primitive are different. Therefore, when deriving the data type of the pin, first obtain the type of the primitive to which the pin belongs.
[0138] When verifying whether the pins are correctly connected, input pins must be bound to variables or connected to other pins, while users can do nothing with output pins, because in the actual use of ladder diagrams, some block primitives have many pins, and users only want to execute the block primitives, and do not want to obtain output or only want to obtain partial output. When deriving the pin data type, the pins of the power rail, contact, coil, jump, and return primitives are derived according to Rule 5. This part is relatively simple and will not be described in detail here; the pin data type derivation algorithm of the block primitive is the most complex, and the present invention will focus on it.
[0139] In the description of the present invention, different derivation algorithms are called according to the primitive type, and the data type of each pin is derived, including the following steps:
[0140] S341. If the type of the primitive is a non-block primitive (including power rails, contacts, coils, jumps, and return primitives), the data type of the pin is Boolean.
[0141] S342. If the type of the primitive is a block primitive, call the block primitive derivation algorithm to derive the data type of each pin in the block primitive in turn.
[0142] In the description of the present invention, Fig. 9 As shown, calling the block primitive derivation algorithm to derive the data type of each pin in the block primitive in turn includes the following steps:
[0143] S3421. Obtain all pins of the block primitive and the basic information of the corresponding program organization unit, including POU type (divided into function and function block), pin list (including pin ID, pin name, pin input and output attributes, pin data type), whether it is expandable, and return value data type.
[0144] S3422. Check whether there are any unprocessed pins in the graphic element. If so, return an error message. If not, continue to execute step S3423.
[0145] S3423. Extract all the pins in the block primitive, traverse them one by one, and deduce the formal parameter data type and actual parameter data type of each pin. If the formal parameter data type conflicts with the actual parameter data type, an error message is returned. If the formal parameter data type does not conflict with the actual parameter data type, the data type derivation result of the formal parameter data type is output.
[0146] Specifically, if the formal parameter data type does not conflict with the actual parameter data type, the data type derivation result of the formal parameter data type is output, including the following situations:
[0147] 1. If the pin name is EN or ENO, the pin's parameter data type is directly derived as BOOL.
[0148] 2. If the block element is a function, there is an OUT pin, and the parameter data type Param of the pin is deduced to be the return value type of the function.
[0149] 3. If the block primitive is expandable, the data type of the expanded pins is exactly the same, and the Param of the expanded pins uses the data type of the IN1 pin.
[0150] S3424. If the pin is an input pin, obtain the actual parameter data type of the pin, and determine whether the formal parameter data type of the pin is an overloaded type. If not, set the data type of the pin to the formal parameter data type. If so, determine whether the conversion relationship between the actual parameter data type and the formal parameter data type is legal. If not, return an error message. If legal, set the data type of the pin to the actual parameter data type and return to step S3422.
[0151] Specifically, if the pin is an input type, then after the previous inspection, the pin has the following three situations.
[0152] Case 1: The pin is directly bound to a variable. Get the data type of the variable according to the variable ID and fill it into the actual parameter data type Arg of the pin;
[0153] Case 2: Connect other pins, the number of pin connections is 1. Arg is filled with the data type of the connected pin;
[0154] Case 3: Connect other pins, the number of pin connections is greater than 1. Arg is directly set to BOOL.
[0155] If Param is of an overloaded type, you need to determine whether the conversion relationship between Param and Arg is legal.
[0156] The conversion relationship is as follows Fig. 9 The data correspondence is shown in Table 1. The left side of the arrow is Param, and the right side of the arrow is Arg. If it is legal, set Arg as the data type of the pin.
[0157] If Param is not an overloaded type, the data type of the pin is directly set to Param.
[0158] If the pin is an output type, the following judgment is made based on whether Param is an overload type.
[0159] If Param is an overloaded type, according to Rule 4, there must be an input pin whose Param type is the same as the pin, so the data type of the pin is deduced to the deduced type of the input pin.
[0160] If Param is not an overloaded type, the data type of the pin is directly set to Param.
[0161] Table 1: Data type correspondence table
[0162]
[0163]
[0164] S4. Based on the deduced result of the data type, check the syntax of each pin.
[0165] Specifically, the pin connection legitimacy verification algorithm is a process of traversing the pins and comparing the data types. The specific process is as follows: Fig.10 As shown. After the previous algorithm, the data types of all pins are derived and determined, and there is no longer an overloadable type such as ANY. Therefore, at this stage, if the data type of a pin is found to be an overloadable type, it means that there is a problem in the previous data type derivation process, and an error will be reported. In order to simplify the algorithm, the present invention adopts the following optimization strategy.
[0166] Optimization strategy: Input pins need to verify the legitimacy of both directly bound variables and connected pins; output pins only need to verify the legitimacy of directly bound variables. Because if output pin A is connected to an input pin B, according to the optimization strategy, the legitimacy of the AB connection will not be verified when verifying pin A, but when verifying pin B, according to the verification strategy of input pins, the legitimacy of the AB connection will be verified.
[0167] If a pin is bound to a variable, the data type of the variable will be compared with the data type of the pin to see if they are the same. If they are not the same, an error will be reported. If the pin is connected to other pins, it will be determined whether the data types of the two pins are consistent. If they are not consistent, an error will be reported. The error reporting in this process will identify the two types of errors mentioned in the present invention, namely "pin binding variable error" and "pin connection error", with a focus on identifying data type mismatch problems.
[0168] In the description of the present invention, based on the derivation result of the data type, checking the syntax of each pin includes the following steps:
[0169] S41. Get all the pins in the network and check whether there are any unprocessed pins. If so, return an error message. If not, extract a pin from the primitive.
[0170] S42, check whether the data type of the pin is an overloaded type. If it is an overloaded type, report an error. If it is not an overloaded type, determine the type of the pin and execute steps S43 and S44 in sequence.
[0171] S43. If the pin is an output pin, determine whether the output pin is directly bound to a variable. If not, return to step S41. If it is directly bound to a variable, determine whether the data type of the output pin is consistent with the variable data type. If not, return an error message. If consistent, return to step S41.
[0172] S44, if the pin is an input pin, determine whether the input pin is directly bound to a variable, if it is directly bound to a variable, determine whether the data type of the input pin is consistent with the variable data type, if not, return an error message, if consistent, return to step S41. If the variable is not directly bound, determine whether the data type of the input pin is consistent with the data type of the connected pin, if not, return an error message, if consistent, return to step S41.
[0173] See also Fig.11 The present invention also provides a pin-based ladder diagram program syntax checking system, the system comprising:
[0174] The traversal and combing module 1 is used to traverse all networks in the ladder diagram and comb all graphics elements and pins in the network.
[0175] The topological sorting module 2 is used to topologically sort all the graphics elements in the network based on the connection relationship of the pins.
[0176] The pin derivation module 3 is used to derive the data type of the pins in the sorted primitives based on the overloadable and extensible characteristics.
[0177] The result checking module 4 checks the syntax of each pin based on the derivation result of the data type.
[0178] In the description of the present invention, the topological sorting module 2 includes: a network extraction unit (not shown in the figure), a validity check unit (not shown in the figure), a sorting execution unit (not shown in the figure) and a loop detection unit (not shown in the figure).
[0179] The network extraction unit is used to sequentially extract a network in the ladder diagram.
[0180] The validity check unit is used to check the validity of the pins based on the number of pins and the connection relationship in the primitive.
[0181] The sorting execution unit is used to introduce the position relationship of the pins and perform topological sorting of all the primitives in the network.
[0182] The loop detection unit is used to set feedback loop detection and eliminate primitives with loop errors.
[0183] In the description of the present invention, the pin derivation module 3 includes: a list detection unit (not shown in the figure), a type identification unit (not shown in the figure), a connection verification unit (not shown in the figure) and a type derivation unit (not shown in the figure).
[0184] Among them, the list detection unit is used to obtain the sorted primitive list and detect whether there are unprocessed primitives in the network. If so, an error message is returned; if not, an primitive is extracted in sequence.
[0185] The type identification unit is used to determine the type of the extracted primitive and identify the overloadable and extensible characteristics of the primitive.
[0186] The connection verification unit is used to verify whether all pins in the primitive are correctly connected. If there are incorrectly connected pins, an error message is returned. If there are no incorrectly connected pins, the type deduction unit continues to be called.
[0187] The type deduction unit is used to call different deduction algorithms according to the primitive type to deduce the data type of each pin.
[0188] In the description of the present invention, the result checking module 4 includes: a network checking unit (not shown in the figure), a reload judgment unit (not shown in the figure), a binding judgment unit (not shown in the figure) and a variable verification unit (not shown in the figure).
[0189] The network checking unit is used to obtain all the pins in the network and check whether there are any unprocessed pins. If so, an error message is returned. If not, a pin in the primitive is extracted.
[0190] The overload judgment unit is used to detect whether the data type of the pin is an overloaded type. If it is an overloaded type, an error is reported. If it is not an overloaded type, the type of the pin is determined, and the binding judgment unit and the variable verification unit are called in sequence.
[0191] The binding judgment unit is used to judge whether the output pin is directly bound to a variable if the pin is an output pin. If not, it returns to the network inspection unit. If it is directly bound to a variable, it judges whether the data type of the output pin is consistent with the variable data type. If not, it returns an error message. If consistent, it returns to the network inspection unit.
[0192] The variable verification unit is used to determine whether the input pin is directly bound to the variable if the pin is an input pin. If the pin is directly bound to the variable, it is used to determine whether the data type of the input pin is consistent with the variable data type. If not, it returns an error message. If consistent, it returns to the network inspection unit. If the pin is not directly bound to the variable, it is used to determine whether the data type of the input pin is consistent with the data type of the connected pin. If not, it returns an error message. If consistent, it returns to the network inspection unit.
[0193] To sum up, with the help of the above-mentioned technical scheme of the present invention, a set of ladder diagram syntax checking algorithms are designed based on the connection relationship between pins and the subordinate relationship between pins and graphic elements. The algorithm supports the overloadable and extensible characteristics defined in the IEC61131 standard. By combining the overloadable and extensible characteristics of the ladder diagram program, the three types of errors of the ladder diagram program, namely pin binding variable errors, pin connection errors, and feedback loops, can be detected, thereby detecting more syntax errors in the early syntax checking stage of the ladder diagram and avoiding errors from being brought into the later stage of compilation. Since the syntax checking algorithm of the present invention has a fast execution speed and a small burden on computing resources, it can be executed regularly in the ladder diagram editing stage, which can assist developers in writing ladder diagram programs.
[0194] In one embodiment, a computer device is provided. The computer device may be a server, and its internal structure diagram may be as follows: Fig.12As shown. The computer device includes a processor, a memory and a network interface connected through a system bus. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database of the computer device is used to store static information and dynamic information data. The network interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, the steps in the above method embodiment are implemented.
[0195] Those skilled in the art will understand that Fig.12 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present invention, and does not constitute a limitation on the computer device to which the solution of the present invention is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.
[0196] In addition, the present invention also provides a computer device, including a memory and a processor, wherein a computer program is stored in the memory, and the processor implements the steps in the above method embodiment when executing the computer program.
[0197] In addition, the present invention further provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the steps in the above method embodiment are implemented.
[0198] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided by the present invention can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory or optical memory, etc. Volatile memory can include random access memory (RAM) or external cache memory. As an illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM).
[0199] It should be understood that, although the steps in the flowchart of the accompanying drawings are displayed in sequence as indicated by the arrows, these steps are not necessarily executed in sequence in the order indicated by the arrows. Unless otherwise specified herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least a part of the steps in the flowchart of the accompanying drawings may include multiple sub-steps or multiple stages, and these sub-steps or stages are not necessarily executed at the same time, but can be executed at different times, and their execution order is not necessarily sequential, but can be executed in turn or alternately with other steps or at least a part of the sub-steps or stages of other steps.
Claims
1. A pin-based ladder diagram program syntax checking method, characterized in that: The method comprises the following steps: S1. Traverse all networks in the ladder diagram and sort out all the graphics elements and pins in the network; S2. Topologically sort all the primitives in the network based on the connection relationship of the pins; S3. Based on the overloadable and extensible characteristics, deduce the data type of the pins in the sorted primitives; S4. Based on the deduced result of the data type, check the syntax of each pin.
2. The pin-based ladder diagram program syntax checking method according to claim 1, characterized in that: The topological sorting of all the graphics elements in the network based on the connection relationship of the pins includes the following steps: S21, extracting a network in the ladder diagram in order; S22, based on the number of pins and connection relationship in the primitive, check the validity of the pins; S23, introducing the position relationship of the pins and performing topological sorting of all the primitives in the network; S24, setting feedback loop detection to remove graphics elements with loop errors.
3. The pin-based ladder diagram program syntax checking method according to claim 2, characterized in that: The checking of the validity of the pins based on the number of pins and the connection relationship in the primitives includes the following steps: S221, traverse the primitives, and extract the input pins and output pins stored in each primitive; S222, counting and identifying the number of input pins and output pins included in each primitive, and the connection relationship between each input pin and output pin; S223. Check the validity of the pins of all graphics elements according to the preset pin quantity rule and pin connection rule, and filter out the valid pins that do not meet the rules.
4. The pin-based ladder diagram program syntax checking method according to claim 2, characterized in that: The position relationship of the introduced pins and the topological sorting of all the primitives in the network include the following steps: S231, based on the connection relationship between pins and the subordinate relationship between pins and primitives, calculate the predecessor primitive list and the successor primitive list of each primitive in order from top to bottom and from left to right; S232, checking the number of predecessor primitive lists and the number of successor primitive lists, if the number of predecessor primitive lists or successor primitive lists is more than one, introducing the position information of the primitives, and sorting the predecessor primitive lists and the successor primitive lists from top to bottom by comparing the y-axis coordinate data; S233, finding a graph element whose number of predecessor graph element lists is zero, defining the in-degree of the graph element as zero, adding the graph element to the sorting result set, removing the graph element from the network, and then recalculating the in-degree and out-degree of the graph element in the new network; S234. Repeat step S233 until there are no primitives with zero in-degree, and stop sorting.
5. The pin-based ladder diagram program syntax checking method according to claim 2, characterized in that: The setting of feedback loop detection and elimination of primitives with loop errors comprises the following steps: S241, comparing the number of graph elements in the initial network with the number of graph elements in the sorting result set, if the number of graph elements in the initial network is greater than the number of graph elements in the sorting result set, determining that a feedback loop is formed between the graph elements, if the number of graph elements in the initial network is less than or equal to the number of graph elements in the sorting result set, determining that no feedback loop is formed between the graph elements; S242. Identify and record the feedback loops that exist during the detection process, and return error information.
6. The pin-based ladder diagram program syntax checking method according to claim 1, characterized in that: The derivation of the data type of the pins in the sorted primitive based on the overloadable and extensible characteristics includes the following steps: S31, obtaining a sorted primitive list, detecting whether there are unprocessed primitives in the network, and if so, returning an error message; if not, extracting a primitive in sequence; S32, detecting the type of the extracted primitive, and identifying the reloadable and extensible characteristics of the primitive; S33, verifying whether all pins in the primitive are correctly connected. If there are incorrectly connected pins, an error message is returned. If there are no incorrectly connected pins, step S34 is continued. S34. Based on the primitive type, different derivation algorithms are called to derive the data type of each pin.
7. The pin-based ladder diagram program syntax checking method according to claim 6, characterized in that: The method of calling different derivation algorithms according to the primitive type to derive the data type of each pin includes the following steps: S341. If the type of the primitive is a non-block primitive, the data type of the pin is a Boolean type; S342. If the type of the primitive is a block primitive, call the block primitive derivation algorithm to derive the data type of each pin in the block primitive in turn.
8. The pin-based ladder diagram program syntax checking method according to claim 7, characterized in that: The calling of the block primitive derivation algorithm to sequentially derive the data type of each pin in the block primitive comprises the following steps: S3421, obtain all pins of the block primitive and basic information of the corresponding program organization unit; S3422, check whether there are unprocessed pins in the primitive, if yes, return an error message, if no, continue to execute step S3423; S3423, extract all pins in the block primitive, traverse them in sequence, deduce the formal parameter data type and actual parameter data type of each pin, if the formal parameter data type conflicts with the actual parameter data type, return an error message, if the formal parameter data type conflicts with the actual parameter data type, then output the data type derivation result of the formal parameter data type; S3424. If the pin is an input pin, obtain the actual parameter data type of the pin, and determine whether the formal parameter data type of the pin is an overloaded type. If not, set the data type of the pin to the formal parameter data type. If so, determine whether the conversion relationship between the actual parameter data type and the formal parameter data type is legal. If not, return an error message. If legal, set the data type of the pin to the actual parameter data type and return to step S3422.
9. The pin-based ladder diagram program syntax checking method according to claim 1, characterized in that: Based on the derivation result of the data type, checking the syntax of each pin includes the following steps: S41, obtain all pins in the network, check whether there is an unprocessed pin, if so, return an error message, if not, extract a pin in the primitive; S42, detecting whether the data type of the pin is an overloaded type, if it is an overloaded type, reporting an error, if not an overloaded type, determining the type of the pin, and executing steps S43 and S44 in sequence; S43, if the pin is an output pin, determine whether the output pin is directly bound to a variable, if not directly bound to a variable, return to step S41, if directly bound to a variable, determine whether the data type of the output pin is consistent with the variable data type, if not, return an error message, if consistent, return to step S41; S44. If the pin is an input pin, determine whether the input pin is directly bound to a variable. If it is directly bound to a variable, determine whether the data type of the input pin is consistent with the variable data type. If not, return an error message. If they are consistent, return to step S41. If the variable is not directly bound, determine whether the data type of the input pin is consistent with the data type of the connected pin. If not, return an error message. If they are consistent, return to step S41.
10. A pin-based ladder diagram program syntax checking system, used to implement the pin-based ladder diagram program syntax checking method according to any one of claims 1 to 9, characterized in that: The system includes: The traversal and combing module is used to traverse all networks in the ladder diagram and comb all the graphics elements and pins in the network; The topological sorting module is used to topologically sort all the primitives in the network based on the connection relationship of the pins; The pin derivation module is used to derive the data type of the pins in the sorted primitives based on the overloadable and extensible characteristics; The result checking module checks the syntax of each pin based on the deduced result of the data type.
11. The pin-based ladder diagram program syntax checking system according to claim 10, characterized in that: The topological sorting module includes: a network extraction unit, a validity check unit, a sorting execution unit and a loop detection unit; Wherein, the network extraction unit is used to extract a network in the ladder diagram in order; The validity checking unit is used to check the validity of the pins based on the number and connection relationship of the pins in the primitive; The sorting execution unit is used to introduce the position relationship of the pins and execute the topological sorting of all the primitives in the network; The loop detection unit is used to set feedback loop detection and eliminate primitives with loop errors.
12. The pin-based ladder diagram program syntax checking system according to claim 10, characterized in that: The pin derivation module includes: a list detection unit, a type identification unit, a connection verification unit and a type derivation unit; The list detection unit is used to obtain the sorted primitive list, detect whether there are unprocessed primitives in the network, and if so, return an error message; if not, extract a primitive in order; The type identification unit is used to determine the type of the extracted primitive and identify the reloadable and extensible characteristics of the primitive; The connection verification unit is used to verify whether all pins in the primitive are correctly connected. If there are incorrectly connected pins, an error message is returned. If there are no incorrectly connected pins, the type derivation unit continues to be called; The type derivation unit is used to call different derivation algorithms according to the primitive type to derive the data type of each pin.
13. The pin-based ladder diagram program syntax checking system according to claim 10, characterized in that: The result checking module includes: a network checking unit, a reload judging unit, a binding judging unit and a variable verifying unit; The network checking unit is used to obtain all pins in the network, check whether there are unprocessed pins, and if so, return an error message; if not, extract a pin in the primitive; The overload judgment unit is used to detect whether the data type of the pin is an overload type. If it is an overload type, an error is reported. If it is not an overload type, the type of the pin is determined, and the binding judgment unit and the variable verification unit are called in sequence; The binding judgment unit is used to judge whether the output pin is directly bound to a variable if the pin is an output pin, and return to the network inspection unit if the pin is not directly bound to a variable; if the pin is directly bound to a variable, judge whether the data type of the output pin is consistent with the variable data type, and return an error message if the data type is inconsistent; and return to the network inspection unit if the data type is consistent; The variable verification unit is used to determine whether the input pin is directly bound to the variable if the pin is an input pin, and if it is directly bound to the variable, then determine whether the data type of the input pin is consistent with the variable data type, if not, return an error message, if they are consistent, return to the network inspection unit; if the variable is not directly bound, then determine whether the data type of the input pin is consistent with the data type of the connected pin, if not, return an error message, if they are consistent, return to the network inspection unit.
14. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 9 are implemented.
15. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 9 are implemented.
