Programming method and device of programmable logic controller and storage medium
By writing PLC control logic code in an easy-to-learn programming language and converting it into PLC executable machine code, the existing PLC programming methods are solved, and efficient and real-time PLC programming and device debugging are achieved.
Patent Information
- Application Number
- CN202411891947.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-20
- Publication Date
- 2025-03-25
- Estimated Expiration
- 2044-12-20
AI Technical Summary
Existing PLC programming methods are inefficient and complex in debugging, making it difficult to achieve efficient and rapid programming and manufacturing, especially in large systems.
Write the first control logic code in an easy-to-learn first programming language and convert it into PLC executable machine code through a pre-built syntax map, deploy to the PLC's real-time operating system, and run the code. At the same time, the device parameters and control instructions input by the user are received and the protocol is converted to ensure compatibility with the PLC hardware and network communication protocols.
It improves programming efficiency, reduces dependence on professional PLC programming knowledge, meets the real-time requirements in the field of industrial automation, and simplifies the debugging and maintenance of various hardware equipment in industrial control systems.
Smart Images

Figure CN119689969A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of industrial control technology, and in particular to a programming method, device and storage medium for a programmable logic controller. Background Art
[0002] In the field of industrial automation control, Programmable Logic Controller (PLC) plays a core role in realizing complex automation control of mechanical and electrical processes. PLC can be programmed to respond to various input signals and control output devices according to preset logic.
[0003] In the prior art, Ladder Diagram (LD) is the most commonly used graphical language in PLC programming, which consists of a series of horizontal lines (ladders), and each horizontal line can be used to place control elements such as input, output, and logic gates. This programming method is intuitive and easy to understand, but the programming efficiency is low and the result interpretation is cumbersome. Especially in large systems, programming and debugging are complicated, making it difficult to achieve efficient and fast programming and manufacturing. Summary of the invention
[0004] The purpose of the present invention is to provide a programming method, device and storage medium for a programmable logic controller in order to solve the technical problems existing in the prior art.
[0005] To achieve the above purpose, the technical solution adopted in the embodiment of the present application is as follows: In a first aspect, an embodiment of the present application provides a programming method for a programmable logic controller, the method comprising: Obtaining a first control logic code pre-written for business needs, wherein the first control logic code is written according to a grammatical rule of a first programming language, and the first control logic code is written based on a pre-built programming interface, wherein the programming interface includes a plurality of functions, each function being used to implement one or more functions in the programmable logic controller; The first control logic code is converted into a second control logic code according to a pre-constructed syntax mapping table, wherein the syntax mapping table is used to characterize the syntax conversion relationship between the first programming language and the second programming language; the second control logic code is a machine code executable by the programmable logic controller; Deploying the second control logic code to an operating system run by the programmable logic controller, and running the second control logic code; Obtaining a control instruction input by a user through a graphical user interface, and determining a target execution result according to the control instruction and the second control logic code, wherein the control instruction includes: device parameters and control instructions of a target device in the industrial control system, and the device parameters include: protocol type, port number, and IP address; The target execution result is displayed on the graphical user interface.
[0006] Optionally, the programming interface is called in the first control logic code, and at least one function in the programming interface is used.
[0007] Optionally, converting the first control logic code into a second control logic code according to a pre-built syntax mapping table includes: Parsing the first control logic code to obtain multiple statement parameters in the first control logic code; According to the type of each statement parameter and the syntax mapping table, each statement parameter is converted respectively to obtain a converted result corresponding to each statement parameter; The second control logic code is obtained according to the converted results corresponding to each of the statement parameters.
[0008] Optionally, the parsing the first control logic code to obtain multiple statement parameters in the first control logic code includes: Using a lexical analyzer provided by the first programming language, parsing the first control logic code into a plurality of lexical units; The syntax analyzer provided by the first programming language is used to perform syntax analysis on the multiple lexical units to obtain an abstract syntax tree, wherein the abstract syntax tree includes: multiple statement parameters.
[0009] Optionally, converting each of the statement parameters according to the type of each of the statement parameters and the syntax mapping table to obtain a converted result corresponding to each of the statement parameters includes: Determine the node type of each statement parameter in the abstract syntax tree; According to the node type of each statement parameter and the syntax mapping table, each statement parameter is converted respectively to obtain the converted result corresponding to each statement parameter; wherein the node type of the statement parameter includes: program node, declaration node, statement node, expression node, function call node or control structure node.
[0010] Optionally, determining a target execution result according to the control instruction and the second control logic code includes: inputting the control instruction as an input into the second control logic code, and running the second control logic code; When the second control logic code is running, it calls a pre-built protocol interface to convert the device parameters of the target device in the control instruction into a protocol format supported by the programmable logic controller, and determines the target execution result based on the converted device parameters and control instructions.
[0011] Optionally, calling a pre-built protocol interface to convert device parameters of a target device in the control instruction into a protocol format supported by the programmable logic controller includes: According to the protocol type in the device parameters of the target device, the protocol interface class corresponding to the protocol type in the device parameters of the target device is called to convert the device parameters of the target device into a protocol format supported by the programmable logic controller.
[0012] Optionally, obtaining a target execution result according to the converted device parameters and control instructions includes: When the second control logic code is executed, the target device is controlled to perform the target operation indicated by the control instruction according to the converted device parameters and the control instruction; Acquire current monitoring data collected by a detection unit associated with the target device, and determine the target execution result based on the current monitoring data.
[0013] In a second aspect, an embodiment of the present application further provides a programming device for a programmable logic controller, the device comprising: an acquisition module, used to acquire a first control logic code pre-written according to a business requirement, wherein the first control logic code is written according to a grammatical rule of a first programming language, and the first control logic code is written based on a pre-built programming interface, wherein the programming interface includes a plurality of functions, each function being used to implement one or more functions in the programmable logic controller; a conversion module, configured to convert the first control logic code into a second control logic code according to a pre-constructed syntax mapping table, wherein the syntax mapping table is used to characterize a syntax conversion relationship between the first programming language and the second programming language; the second control logic code is a machine code executable by the programmable logic controller; An operating module, used for deploying the second control logic code to an operating system run by the programmable logic controller, and running the second control logic code; The acquisition module is further used to acquire control instructions input by the user through the graphical user interface; A determination module, configured to determine a target execution result according to the control instruction and the second control logic code, wherein the control instruction includes: device parameters and control instructions of a target device in the industrial control system, and the device parameters include: protocol type, port number, and IP address; A display module is used to display the target execution result on the graphical user interface.
[0014] Optionally, the programming interface is called in the first control logic code, and at least one function in the programming interface is used.
[0015] Optionally, the conversion module is specifically used to: Parsing the first control logic code to obtain multiple statement parameters in the first control logic code; According to the type of each statement parameter and the syntax mapping table, each statement parameter is converted respectively to obtain a converted result corresponding to each statement parameter; The second control logic code is obtained according to the converted results corresponding to each of the statement parameters.
[0016] Optionally, the conversion module is specifically used to: Using a lexical analyzer provided by the first programming language, parsing the first control logic code into a plurality of lexical units; The syntax analyzer provided by the first programming language is used to perform syntax analysis on the multiple lexical units to obtain an abstract syntax tree, wherein the abstract syntax tree includes: multiple statement parameters.
[0017] Optionally, the conversion module is specifically used to: Determine the node type of each statement parameter in the abstract syntax tree; According to the node type of each statement parameter and the syntax mapping table, each statement parameter is converted respectively to obtain the converted result corresponding to each statement parameter; wherein the node type of the statement parameter includes: program node, declaration node, statement node, expression node, function call node or control structure node.
[0018] Optionally, the determining module is specifically configured to: inputting the control instruction as an input into the second control logic code, and running the second control logic code; When the second control logic code is running, it calls a pre-built protocol interface to convert the device parameters of the target device in the control instruction into a protocol format supported by the programmable logic controller, and determines the target execution result based on the converted device parameters and control instructions.
[0019] Optionally, the determining module is specifically configured to: According to the protocol type in the device parameters of the target device, the protocol interface class corresponding to the protocol type in the device parameters of the target device is called to convert the device parameters of the target device into a protocol format supported by the programmable logic controller.
[0020] Optionally, the determining module is specifically configured to: When the second control logic code is executed, the target device is controlled to perform the target operation indicated by the control instruction according to the converted device parameters and the control instruction; Acquire current monitoring data collected by a detection unit associated with the target device, and determine the target execution result based on the current monitoring data.
[0021] In a third aspect, an embodiment of the present application further provides an electronic device, comprising: a processor, a storage medium and a bus, wherein the storage medium stores machine-readable instructions executable by the processor, and when the electronic device is running, the processor communicates with the storage medium through the bus, and the processor executes the machine-readable instructions to perform the steps of the programming method of the programmable logic controller provided in the first aspect.
[0022] In a fourth aspect, an embodiment of the present application further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the programming method of the programmable logic controller provided in the first aspect is executed.
[0023] The beneficial effects of this application are: The present application provides a programming method, device and storage medium for a programmable logic controller, which can write a first control logic code based on a pre-built programming interface and the grammatical rules of a first programming language, wherein the programming interface includes: multiple functions or methods, and each function or method is used to implement one or more specific functions in the PLC; then, based on a pre-built syntax mapping table, the first control logic code is converted into a machine code executable by the PLC, and the PLC executable machine code is deployed to a real-time operating system run by the PLC, and the PLC executable machine code is run; and the device parameters and control instructions input by the user are obtained, wherein the device parameters include: the protocol type, port number, IP address and control instructions of the target device, and the target execution result is determined according to the device parameters and control instructions. Therefore, this solution proposes to use an easy-to-learn first programming language to program a first control logic code, and then convert the first control logic code into a machine code executable by the PLC, so as to quickly implement complex control logic, improve programming efficiency, and reduce dependence on professional PLC programming knowledge; at the same time, by deploying the PLC executable machine code to the real-time execution environment of the PLC, it is ensured that the first control logic code written in the first programming language can run in real time and stably on the PLC, meeting the real-time requirements of the industrial automation field; then, the user inputs the device parameters, and the PLC executable machine code performs protocol conversion on the device parameters, ensuring that the PLC executable machine code written in the first programming language is compatible with the PLC hardware and network communication protocol, solving the compatibility problems that may exist in the implementation process of different existing programming languages, and making the debugging and maintenance of various hardware devices in the industrial control system simpler and more convenient. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings required for use in the embodiments are briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present invention and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without creative work.
[0025] Figure 1 A flowchart of a programming method for a programmable logic controller provided in an embodiment of the present application; Figure 2 A flowchart of another method for programming a programmable logic controller provided in an embodiment of the present application; Figure 3 A flowchart of another method for programming a programmable logic controller provided in an embodiment of the present application; Figure 4A flowchart of another method for programming a programmable logic controller provided in an embodiment of the present application; Figure 5 A flowchart of another method for programming a programmable logic controller provided in an embodiment of the present application; Figure 6 A flowchart of another method for programming a programmable logic controller provided in an embodiment of the present application; Figure 7 A module diagram of a programming method for a programmable logic controller provided in an embodiment of the present application; Figure 8 A schematic diagram of the structure of a programming device for a programmable logic controller provided in an embodiment of the present application. DETAILED DESCRIPTION
[0026] To make the purpose, technical scheme and advantages of the embodiments of the present application clearer, the technical scheme in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. It should be understood that the drawings in the present application only serve the purpose of explanation and description and are not used to limit the scope of protection of the present application. In addition, it should be understood that the schematic drawings are not drawn in real proportion. The flowchart used in this application shows the operations implemented according to some embodiments of the present application. It should be understood that the operations of the flowchart can be implemented out of sequence, and the steps without logical context can be reversed in order or implemented simultaneously. In addition, those skilled in the art can add one or more other operations to the flowchart under the guidance of the content of the present application, or remove one or more operations from the flowchart.
[0027] In addition, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments. The components of the embodiments of the present application described and shown in the drawings here can be arranged and designed in various configurations. Therefore, the following detailed description of the embodiments of the present application provided in the drawings is not intended to limit the scope of the application claimed for protection, but merely represents the selected embodiments of the present application. Based on the embodiments of the present application, all other embodiments obtained by those skilled in the art without making creative work belong to the scope of protection of the present application.
[0028] It should be noted that the term "comprising" will be used in the embodiments of the present application to indicate the existence of the features declared thereafter, but does not exclude the addition of other features.
[0029] First, the background technology involved in this application is explained.
[0030] In the related technology, traditional PLC controllers usually use dedicated programming languages, such as Ladder Diagram (LD), Function Block Diagram (FBD) and Instruction List (IL). These programming languages are intuitive, easy to understand, and widely used in industrial control. However, there are still some problems, as follows: (1) High cost: The cost of PLC control system is high, including the price of hardware and software, as well as the cost of installation and debugging; (2) Complex programming: Programming of PLC control systems requires a certain level of technical skills and experience. For users without relevant experience, programming may be difficult; (3) Not suitable for large-scale control: PLC control systems are suitable for small and medium-scale control tasks, but for large-scale control tasks, the processing capacity of the PLC control system may not meet the requirements; (4) Limited functions: The functions of the PLC control system are limited by the design of its hardware and software. In some specific control tasks, the PLC control system may not be able to meet the requirements; (5) High hardware cost: PLC has high hardware cost, which limits its widespread deployment in cost-sensitive applications; (6) Slow programming speed: PLC software programming is relatively complex and the programming speed is slow, which affects the development efficiency; (7) Limited programmability: PLC has limited programmability and is difficult to quickly adapt to new programming requirements and logic changes.
[0031] Therefore, these languages have problems such as steep learning curves, low programming efficiency, and limited functional scalability. Especially in large systems, programming and debugging are complex, making it difficult to achieve efficient and fast programming and manufacturing.
[0032] The embodiments of the present application are based on the above-mentioned problems. The present application provides a programming method for PLC, which can write a first control logic code based on a pre-built programming interface and the grammatical rules of a first programming language, wherein the programming interface includes: multiple functions or methods, and each function or method is used to implement one or more specific functions in the PLC; then, based on a pre-built syntax mapping table, the first control logic code is converted into a machine code executable by the PLC, and the PLC executable machine code is deployed to a real-time operating system run by the PLC, and the PLC executable machine code is run; and the device parameters and control instructions input by the user are obtained, wherein the device parameters include: the protocol type, port number, IP address and control instructions of the target device, and the target execution result is determined according to the device parameters and control instructions. Therefore, this solution proposes to use an easy-to-learn first programming language to program a first control logic code, and then convert the first control logic code into a machine code executable by the PLC, so as to quickly implement complex control logic, improve programming efficiency, and reduce dependence on professional PLC programming knowledge; at the same time, by deploying the PLC executable machine code to the real-time execution environment of the PLC, it is ensured that the first control logic code written in the first programming language can run in real time and stably on the PLC, meeting the real-time requirements of the industrial automation field; then, the user inputs the device parameters, and the PLC executable machine code performs protocol conversion on the device parameters, ensuring that the PLC executable machine code written in the first programming language is compatible with the PLC hardware and network communication protocol, solving the compatibility problems that may exist in the implementation process of different existing programming languages, and making the debugging and maintenance of various hardware devices in the industrial control system simpler and more convenient.
[0033] The implementation principles and corresponding beneficial effects of the programming method steps of the programmable logic controller provided in the present application will be explained through a number of specific embodiments as follows.
[0034] In one embodiment, reference Figure 1 As shown, a programming method for a programmable logic controller is provided. Optionally, the execution subject of the method can be a programmable logic controller in an industrial control system, which has a data processing function.
[0035] It should be understood that in other embodiments, the order of some steps in the programming method of the programmable logic controller can be interchanged according to actual needs, or some steps can be omitted or deleted. Figure 1 As shown, the method includes: S101. Obtain a first control logic code pre-written according to business requirements.
[0036] The first control logic code is written according to the grammatical rules of the first programming language, and the first control logic code is written based on a pre-built programming interface, the programming interface includes multiple functions, and each function is used to implement one or more functions in the programmable logic controller.
[0037] The first programming language may be a programming language that is easy to learn and understand, such as Python, Matlab, LabVIEW, etc.
[0038] Among them, business requirements refer to the control logic that needs to be implemented. For example, in a chemical production line, the mixing ratio of two liquids (A and B) needs to be accurately controlled. Therefore, the control logic code written takes the current flow rate data of each pump collected by the flow sensor as input, and automatically adjusts the running speed of the two pumps through the control logic code so that the current mixing ratio meets the preset mixing ratio, ensuring the accuracy of the mixing ratio.
[0039] In this example, for example, the control logic code can be written according to the grammatical rules of the Python language, and during the writing process, one or more functions in the predefined programming interface can be called, such as the function set_pump_speed(self, pump_id, speed) corresponding to setting the pump speed, to generate the first control logic code.
[0040] Optionally, the written multiple first control logic codes are submitted to the code integration module, and the code integration module processes the multiple first control logic codes. Exemplarily, the code integration module can be Cython, PyPy, or Numba. They can replace the Python integration module to varying degrees, especially in scenarios with high performance requirements. Cython can convert Python code into C code, PyPy improves Python running speed through JIT technology, and Numba can compile Python code into local machine instructions at runtime.
[0041] S102: Convert the first control logic code into a second control logic code according to a pre-built syntax mapping table.
[0042] The syntax mapping table is used to characterize the syntax conversion relationship between the first programming language and the second programming language; the second control logic code is a machine code executable by the PLC. Exemplarily, the syntax mapping table is used to characterize the syntax conversion relationship between the Python language and the PLC language. Taking the conditional statement in the Python language as an example, the conditional statement in the Python language is if, elif, else; and the corresponding conditional statement in the PLC language is IF, ELSIF, ELSE.
[0043] Therefore, based on the syntax mapping table, each programming statement in the first control logic code can be converted into machine code executable by the PLC, so that the converted programming code can be deployed in the real-time execution environment of the PLC and ensure that the first control logic code can run stably under strict real-time constraints.
[0044] S103: deploy the second control logic code to the operating system run by the programmable logic controller, and run the second control logic code.
[0045] Exemplarily, the operating system run by the programmable logic controller is a real-time operating system (RTOS for short), wherein the RTOS operating system is a real-time execution environment, which can ensure that the first control logic code can run under strict real-time constraints.
[0046] Therefore, the PLC executable machine code can be deployed to the RTOS operating system and run.
[0047] S104: Obtain a control instruction input by a user through a graphical user interface, and determine a target execution result according to the control instruction and a second control logic code.
[0048] The control instruction includes: device parameters and control instructions of the target device in the industrial control system, and the device parameters include: protocol type, port number, and IP address. For example, the target device is pump 1, the protocol type of pump 1 is Modbus, the port number is 502, the IP address is 192.168.0.10, and the control instruction is to control the opening of pump 1 to decrease.
[0049] S105: Display the target execution result on a graphical user interface.
[0050] In this embodiment, for example, if the user needs to control the opening of pump 1 to be reduced, the protocol type, port number, IP address and control instructions of pump 1 can be input through the graphical user interface, and this series of control instructions can be input into the second control logic code. The second control logic code performs protocol conversion on the device parameters of the target device to ensure that the PLC executable machine code written in the first programming language is compatible with the communication protocol of pump 1, and controls the opening of pump 1 to be reduced based on the converted protocol format and control instructions. After execution, the target execution result is determined and displayed on the graphical user interface, so that the user can timely understand the current operation status of the hardware device, realize the debugging and maintenance of each hardware device in the industrial control system, and ensure that the system operates as expected.
[0051] In another feasible method, it is also possible to only receive query instructions from user input, such as obtaining the mixing ratio at the current moment, and querying the mixing ratio at the current moment from the mixing ratios at different moments generated when the second control logic code is running, so that the user can understand the operating status of the current system in a timely manner.
[0052] In summary, the present application provides a programming method for PLC, which can write a first control logic code based on a pre-built programming interface and the grammatical rules of a first programming language, wherein the programming interface includes: multiple functions or methods, and each function or method is used to implement one or more specific functions in the PLC; then, based on a pre-built syntax mapping table, the first control logic code is converted into a machine code executable by the PLC, and the PLC executable machine code is deployed to a real-time operating system run by the PLC, and the PLC executable machine code is run; and the device parameters and control instructions input by the user are obtained, wherein the device parameters include: the protocol type, port number, IP address and control instructions of the target device, and the target execution result is determined according to the device parameters and control instructions. Therefore, this solution proposes to use an easy-to-learn first programming language to program a first control logic code, and then convert the first control logic code into a machine code executable by the PLC, so as to quickly implement complex control logic, improve programming efficiency, and reduce dependence on professional PLC programming knowledge; at the same time, by deploying the PLC executable machine code to the real-time execution environment of the PLC, it is ensured that the first control logic code written in the first programming language can run in real time and stably on the PLC, meeting the real-time requirements of the industrial automation field; then, the user inputs the device parameters, and the PLC executable machine code performs protocol conversion on the device parameters, ensuring that the PLC executable machine code written in the first programming language is compatible with the PLC hardware and network communication protocol, solving the compatibility problems that may exist in the implementation process of different existing programming languages, and making the debugging and maintenance of various hardware devices in the industrial control system simpler and more convenient.
[0053] Optionally, a programming interface is called in the first control logic code, and at least one function in the programming interface is used.
[0054] In one achievable manner, if the first programming language is Python. In order to provide a simple and easy-to-use interface function to PLC programmers, the functions and classes required for PLC hardware communication are encapsulated into a programming interface. That is, the programming interface encapsulates PLC-specific functional functions, such as timers, counters, data processing, etc., so that Python can directly interact with the PLC. In this way, when programmers use the first programming language for programming, when they need to implement timing functions and timing functions, they can directly call the timer function and counter function in the programming interface, so that developers can use Python code to set and control the timer and counter of the PLC.
[0055] Optionally, a pre-written data exchange library is called in the first control logic code to provide data processing functions such as data conversion, filtering, aggregation, etc. to the PLC programmer, that is, it can support data reading and writing operations between PLC and Python, including digital input / output, analog input / output, etc., thereby facilitating data analysis and processing in Python.
[0056] The following embodiments will be used to explain in detail how to convert the first control logic code into a machine code executable by the PLC.
[0057] Optionally, refer to Figure 2 As shown, the above S102 includes: S201. Parse a first control logic code to obtain multiple statement parameters in the first control logic code.
[0058] In one implementable manner, the process of parsing the first control logic code may be: first, selecting a parser that matches the first control logic code; then, using the selected parser to parse the first control logic code to obtain multiple statement parameters in the first control logic code, for example, multiple keywords, multiple function names, and the number of parameters in each function name.
[0059] S202: Convert each statement parameter according to the type of each statement parameter and the syntax mapping table to obtain a converted result corresponding to each statement parameter.
[0060] The types of statement parameters include at least: keyword type, expression type, function type, etc.
[0061] The syntax mapping table records mapping information of different types of statement parameters, for example, if, elif, else in Python, and IF, ELSIF, ELSE in PLC.
[0062] S203. Obtain a second control logic code according to the converted results corresponding to each statement parameter.
[0063] In one implementable method, when the statement parameter to be converted is "=", that is, the type of the statement parameter to be converted is an operator type, the target operator corresponding to the operator can be directly queried from the operator type in the syntax mapping table as ":", that is, the target operator is the converted result corresponding to the operator; after converting each language parameter in the first control logic code, the second control logic code that conforms to the grammatical rules of the second programming language can be directly obtained from the converted results corresponding to each statement parameter, thereby realizing the conversion of control logic between different programming languages.
[0064] Optionally, refer to Figure 3 As shown, the above S201 includes: S301 . Use a lexical analyzer provided by a first programming language to parse a first control logic code into a plurality of lexical units.
[0065] Exemplarily, taking the first programming language as Python as an example, the lexical analyzer may be Lex or LexicalAnalyzer, which is used to parse the first control logic code into lexical units (Tokens).
[0066] For example, a logic code in the first control logic code is: set_point = 50. After parsing the logic code using Lex or Lexical Analyzer, the lexical units obtained include: set_point, =, 50.
[0067] S302: Use a syntax analyzer provided by the first programming language to perform syntax analysis on multiple lexical units to obtain an abstract syntax tree.
[0068] The abstract syntax tree includes: multiple statement parameters.
[0069] The syntax analyzer is Yacc. Therefore, the syntax analyzer can perform syntax analysis on multiple lexical units in each parsed logic code, such as obtaining the syntax analysis results of the lexical units set_point, =, 50 as expression types, and assigning 50 to set_point, and combining the obtained syntax analysis results into an abstract syntax tree.
[0070] Optionally, refer to Figure 4 As shown, the above S202 includes: S401: Determine the node type of each statement parameter in the abstract syntax tree.
[0071] The node types of statement parameters include: program node, declaration node, statement node, expression node, function call node or control structure node.
[0072] For example, taking a statement parameter in the abstract syntax tree as expression type, 50 assigned to set_point as an example, the node type of the statement parameter is expression type.
[0073] S402: Convert each statement parameter according to the node type and syntax mapping table of each statement parameter to obtain a converted result corresponding to each statement parameter.
[0074] Based on the above embodiment, the node type of the above statement parameter is an expression type. According to the node type of the statement parameter, from the expression type in the syntax mapping table, the conversion result corresponding to set_point is SET_POINT, the conversion result corresponding to the assignment "=" is ";", and the conversion result corresponding to 50 is 50. Therefore, it can be determined that after the statement parameter is converted, the converted result obtained is: SET_POINT: 50.
[0075] The following embodiments will explain in detail how to implement information interaction with various hardware devices based on the query parameters input by the user and the second control logic code when the second control logic code is running.
[0076] Optionally, refer to Figure 5 As shown, the above step S104 includes: S501. Input a control instruction as an input into a second control logic code, and run the second control logic code.
[0077] S502. When the second control logic code is running, it calls a pre-built protocol interface to convert the device parameters of the target device in the control instruction into a protocol format supported by the programmable logic controller, and determines the target execution result according to the converted device parameters and the control instruction.
[0078] Among them, the protocol interface (Protocol Adapter Interface) includes: multiple functions, such as connect(): establish connection, disconnect(): disconnect, send(data): send data, receive(): receive data, configure(settings): configure adapter parameters.
[0079] Among them, different hardware in industrial control systems support different protocols, and protocol format conversion can be achieved through protocol interfaces. For example, the protocols supported by all hardware in industrial control systems include: Modbus and Profinet. Therefore, two classes ModbusAdapter and ProfinetAdapter can be created, and both classes inherit the protocol interface and implement all functions in the protocol interface. In this way, only one protocol interface needs to be defined, and the configuration parameters of different protocols are managed through the protocol interface. The protocol interface can also create and initialize specific protocol adapter instances based on the incoming device parameters (such as protocol type, port number, IP address, etc.), ensuring that the PLC program written in Python is compatible with the PLC hardware and network communication protocol.
[0080] Secondly, the solution also supports dynamic protocol processing, that is, it allows different communication protocols to be loaded and unloaded as needed when the second control logic code is running.
[0081] In one achievable manner, the target device is pump 1, and the control instruction includes: the protocol type of pump 1 is Modbus, the port number is 502, the IP address is 192.168.0.10, and the opening of pump 1 is controlled to be reduced; therefore, the protocol type of pump 1 is Modbus, the port number is 502, and the IP address is 192.168.0.10 as input, which is input into the second control logic code to realize the input in the connection function with the PLC, such as: plc = PLCInterface(connection_params={"ip": "192.168.0.10", "port":502}); Then, run the second control logic code, and when the second control logic code is running, call the protocol interface to convert the device parameters of the target device in the control instruction into the protocol format supported by the programmable logic controller, that is, to achieve compatibility with the PLC hardware and network communication protocol; then, determine the target execution result based on the converted device parameters and control instructions.
[0082] Optionally, the above step S502 includes: According to the protocol type in the device parameters of the target device, the protocol interface class corresponding to the protocol type in the device parameters of the target device is called to convert the device parameters of the target device into a protocol format supported by the programmable logic controller.
[0083] For example, the protocol type in the device parameters of the target device is Modbus, so the protocol interface class corresponding to the identifier of the target device can be directly called. Since the protocol interface class inherits the protocol interface (Protocol AdapterInterface), that is, each function of the protocol interface can be used, the protocol interface class can be used to convert the device parameters of the target device into the protocol format supported by the programmable logic controller, ensuring that the PLC program written in Python is compatible with the PLC hardware and network communication protocol, solving the communication barriers between different devices, ensuring that devices with different protocols can be efficiently integrated, and improving the interoperability between devices.
[0084] Optionally, refer to Figure 6 As shown, the above step S502 includes: S601. When the second control logic code is running, according to the converted device parameters and the control instruction, the target device is controlled to execute the target operation indicated by the control instruction.
[0085] For example, the target operation indicated by the control instruction is to control the opening of the pump 1 to decrease.
[0086] In this embodiment, when the second control logic code is running, the converted device parameters and control instructions are input into the second control logic code, and the opening of the pump 1 is controlled to be reduced.
[0087] S602: Acquire current monitoring data collected by a detection unit associated with the target device, and determine a target execution result based on the current monitoring data.
[0088] Exemplarily, the detection unit associated with the target device is a flow sensor 1. After controlling the pump 1 to execute the opening reduction operation indicated by the control instruction, the flow rate of the pump 1 at the current moment collected by the flow sensor 1 can be obtained, and based on the flow rate of the pump 1 at the current moment, it is determined whether the opening of the pump 1 is successfully controlled to be reduced, that is, the target execution result is obtained, and the target execution result is returned and displayed to the graphical user interface, so that the user can timely understand the operation status of the pump 1, thereby realizing the monitoring of the operating status of different hardware in the industrial control system.
[0089] Optionally, Figure 7 A module diagram of a programming method for a programmable logic controller provided in this application, the method comprising: Among them, Python is taken as an example as the first programming language.
[0090] The first step is to write Python code; The second step is to submit the written Python code to the Python integration module and code converter in turn to generate PLC executable machine code.
[0091] The third step is to deploy the PLC executable machine code to the real-time execution environment where the PLC runs, and run the PLC executable machine code.
[0092] Step 4: receiving the control command input by the user and inputting it into the input and output module; The control instruction includes: device parameters of the target device and control instructions. For example, the target device is pump 1, and the control instruction is to reduce the opening of pump 1.
[0093] Step 5. When the PLC executable machine code is running, the pre-built protocol interface is called to convert the device parameters of the target device in the control instruction into the protocol format supported by the programmable logic controller, and according to the converted device parameters and control instructions, the target device is controlled to perform the target operation (such as controlling the opening of pump 1 to decrease), and the current monitoring data collected from the external sensor is read, and the target execution result is determined based on the current monitoring data.
[0094] The target execution result includes: success or failure. For example, the current flow rate of the pump 1 can be collected by a flow sensor, and based on the current flow rate of the pump 1, it is determined whether the opening of the pump 1 is successfully controlled to be reduced.
[0095] The pump 1 and the PLC are connected to each other through a network communication interface to achieve data interaction. For example, the network communication interface includes TCP or a network card.
[0096] Step 6: Display the target execution results on the graphical user interface.
[0097] In another achievable manner, when the PLC executable machine code is running, it is also possible to receive query instructions input by the user, such as querying the mixed concentration at a target time, and querying the mixed concentration at the target time from a database, wherein the database stores the mixed solubility and other data at multiple times generated when the PLC executable machine code is running, and returns the mixed concentration at the target time to the graphical user interface for display.
[0098] In summary, the solution provided by this application has the following advantages: (1) Improve programming efficiency: This application supports easy-to-learn and understand programming languages, such as Python, which can quickly implement complex control logic by using its concise syntax and powerful library, greatly improving programming efficiency compared to traditional methods such as ladder diagrams and logic gates.
[0099] (2) Enhance system flexibility and scalability: The flexibility of Python makes the PLC system more flexible and scalable. Compared with ladder diagrams and logic gates, Python provides more programming options and better adaptability, and can easily integrate new functions and protocols.
[0100] (3) Simplify programming and debugging of large systems: For large systems, Python's advanced programming features can simplify the programming and debugging process, and can reduce programming complexity and error rate compared to ladder diagrams.
[0101] (4) Improve code readability and maintainability: Python code is generally easier to read and maintain than ladder diagram and logic gate code, which is crucial for the maintenance and upgrade of long-term projects.
[0102] (5) Improve system compatibility: This application provides a protocol interface, which can realize the conversion of the protocol format of device parameters by calling one or more functions of the protocol interface, ensuring that the PLC program written in Python is compatible with the PLC hardware and network communication protocol, thus solving the compatibility problems that may exist in the existing Python PLC programming implementation.
[0103] (6) Enhanced real-time performance and stability: This application deploys the PLC executable machine code to the real-time execution environment to ensure that the Python program can run in real time and stably on the PLC, meeting the real-time requirements in the field of industrial automation.
[0104] (7) Reduced reliance on professional PLC programming knowledge: The use of a programming language that is easy to learn and understand reduces reliance on professional PLC programming knowledge, allowing more technicians to participate in PLC programming.
[0105] (8) Integration of modern programming practices: This application supports modern programming practices such as database interaction, network communication, and advanced data processing, which are difficult or inefficient to implement in traditional ladder diagram and logic gate programming.
[0106] (9) Providing a unified development environment: The interface functions provided in this application include: all the functions and methods required by PLC, providing a unified development environment for programming, allowing Python developers to use familiar tools and methods to develop PLC applications.
[0107] Based on the same inventive concept, the embodiment of the present application also provides a programming device for a programmable logic controller corresponding to the programming method of the programmable logic controller. Since the principle of solving the problem by the device in the embodiment of the present application is similar to the programming method of the programmable logic controller in the embodiment of the present application, the implementation of the device can refer to the implementation of the method, and the repeated parts will not be repeated.
[0108] Optionally, refer to Figure 8 As shown, a schematic diagram of the structure of a programming device for a programmable logic controller is also provided in an embodiment of the present application, and the device includes: An acquisition module 801 is used to acquire a first control logic code pre-written according to a business requirement, wherein the first control logic code is written according to a grammatical rule of a first programming language, and the first control logic code is written based on a pre-built programming interface, wherein the programming interface includes a plurality of functions, each function being used to implement one or more functions in the programmable logic controller; A conversion module 802 is used to convert the first control logic code into a second control logic code according to a pre-built syntax mapping table, wherein the syntax mapping table is used to characterize the syntax conversion relationship between the first programming language and the second programming language; the second control logic code is a machine code executable by the programmable logic controller; An operating module 803 is used to deploy the second control logic code to the operating system run by the programmable logic controller and run the second control logic code; The acquisition module 801 is also used to acquire the control instruction input by the user through the graphical user interface; The determination module 804 is used to determine the target execution result according to the control instruction and the second control logic code, wherein the control instruction includes: device parameters and control instructions of the target device in the industrial control system, and the device parameters include: protocol type, port number, IP address; The display module 805 is used to display the target execution result on the graphical user interface.
[0109] Optionally, the programming interface is called in the first control logic code, and at least one function in the programming interface is used.
[0110] Optionally, the conversion module 802 is specifically configured to: Parsing the first control logic code to obtain multiple statement parameters in the first control logic code; According to the type of each statement parameter and the syntax mapping table, each statement parameter is converted respectively to obtain a converted result corresponding to each statement parameter; The second control logic code is obtained according to the converted results corresponding to each of the statement parameters.
[0111] Optionally, the conversion module 802 is specifically configured to: Using a lexical analyzer provided by the first programming language, parsing the first control logic code into a plurality of lexical units; The syntax analyzer provided by the first programming language is used to perform syntax analysis on the multiple lexical units to obtain an abstract syntax tree, wherein the abstract syntax tree includes: multiple statement parameters.
[0112] Optionally, the conversion module 802 is specifically configured to: Determine the node type of each statement parameter in the abstract syntax tree; According to the node type of each statement parameter and the syntax mapping table, each statement parameter is converted respectively to obtain the converted result corresponding to each statement parameter; wherein the node type of the statement parameter includes: program node, declaration node, statement node, expression node, function call node or control structure node.
[0113] Optionally, the determining module 804 is specifically configured to: inputting the control instruction as an input into the second control logic code, and running the second control logic code; When the second control logic code is running, it calls a pre-built protocol interface to convert the device parameters of the target device in the control instruction into a protocol format supported by the programmable logic controller, and determines the target execution result based on the converted device parameters and control instructions.
[0114] Optionally, the determining module 804 is specifically configured to: According to the protocol type in the device parameters of the target device, the protocol interface class corresponding to the protocol type in the device parameters of the target device is called to convert the device parameters of the target device into a protocol format supported by the programmable logic controller.
[0115] Optionally, the determining module 804 is specifically configured to: When the second control logic code is executed, the target device is controlled to perform the target operation indicated by the control instruction according to the converted device parameters and the control instruction; Acquire current monitoring data collected by a detection unit associated with the target device, and determine the target execution result based on the current monitoring data.
[0116] The above-mentioned device is used to execute the method provided by the aforementioned embodiment, and its implementation principle and technical effect are similar, which will not be repeated here.
[0117] The above modules may be one or more integrated circuits configured to implement the above methods, such as one or more application specific integrated circuits (ASIC), or one or more digital singnal processors (DSP), or one or more field programmable gate arrays (FPGA). For another example, when a module is implemented in the form of a processing element scheduling program code, the processing element may be a general-purpose processor, such as a central processing unit (CPU) or other processor that can call program code. For another example, these modules may be integrated together and implemented in the form of a system-on-a-chip (SOC).
[0118] Optionally, the present invention also provides a program product, such as a computer-readable storage medium, comprising a program, which is used to execute the above method embodiment when executed by a processor.
[0119] In the several embodiments provided by the present invention, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0120] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0121] In addition, each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of hardware plus software functional units.
[0122] The above-mentioned integrated unit implemented in the form of a software functional unit can be stored in a computer-readable storage medium. The above-mentioned software functional unit is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) or a processor (English: processor) to perform some steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (English: Read-Only Memory, referred to as: ROM), random access memory (English: Random Access Memory, referred to as: RAM), disk or optical disk and other media that can store program codes.
Claims
1. A method for programming a programmable logic controller, characterized in that: The method comprises: Obtaining a first control logic code pre-written for business needs, wherein the first control logic code is written according to a grammatical rule of a first programming language, and the first control logic code is written based on a pre-built programming interface, wherein the programming interface includes a plurality of functions, each function being used to implement one or more functions in the programmable logic controller; The first control logic code is converted into a second control logic code according to a pre-constructed syntax mapping table, wherein the syntax mapping table is used to characterize the syntax conversion relationship between the first programming language and the second programming language; the second control logic code is a machine code executable by the programmable logic controller; Deploying the second control logic code to an operating system run by the programmable logic controller, and running the second control logic code; Obtaining a control instruction input by a user through a graphical user interface, and determining a target execution result according to the control instruction and the second control logic code, wherein the control instruction includes: device parameters and control instructions of a target device in the industrial control system, and the device parameters include: protocol type, port number, and IP address; The target execution result is displayed on the graphical user interface.
2. The method according to claim 1, characterized in that The programming interface is called in the first control logic code, and at least one function in the programming interface is used.
3. The method according to claim 1, characterized in that The converting the first control logic code into a second control logic code according to a pre-built syntax mapping table includes: Parsing the first control logic code to obtain multiple statement parameters in the first control logic code; According to the type of each statement parameter and the syntax mapping table, each statement parameter is converted respectively to obtain a converted result corresponding to each statement parameter; The second control logic code is obtained according to the converted results corresponding to each of the statement parameters.
4. The method according to claim 3, characterized in that The parsing of the first control logic code to obtain multiple statement parameters in the first control logic code includes: Using a lexical analyzer provided by the first programming language, parsing the first control logic code into a plurality of lexical units; The syntax analyzer provided by the first programming language is used to perform syntax analysis on the multiple lexical units to obtain an abstract syntax tree, wherein the abstract syntax tree includes: multiple statement parameters.
5. The method according to claim 4, characterized in that The converting of each statement parameter according to the type of each statement parameter and the syntax mapping table to obtain a converted result corresponding to each statement parameter includes: Determine the node type of each statement parameter in the abstract syntax tree; According to the node type of each statement parameter and the syntax mapping table, each statement parameter is converted respectively to obtain the converted result corresponding to each statement parameter; wherein the node type of the statement parameter includes: program node, declaration node, statement node, expression node, function call node or control structure node.
6. The method according to claim 1, characterized in that The step of determining a target execution result according to the control instruction and the second control logic code includes: inputting the control instruction as an input into the second control logic code, and running the second control logic code; When the second control logic code is running, it calls a pre-built protocol interface to convert the device parameters of the target device in the control instruction into a protocol format supported by the programmable logic controller, and determines the target execution result based on the converted device parameters and control instructions.
7. The method according to claim 6, characterized in that The calling of the pre-built protocol interface to convert the device parameters of the target device in the control instruction into a protocol format supported by the programmable logic controller includes: According to the protocol type in the device parameters of the target device, the protocol interface class corresponding to the protocol type in the device parameters of the target device is called to convert the device parameters of the target device into a protocol format supported by the programmable logic controller.
8. The method according to claim 6, characterized in that Determining the target execution result according to the converted device parameters and control instructions includes: When the second control logic code is executed, the target device is controlled to perform the target operation indicated by the control instruction according to the converted device parameters and the control instruction; Acquire current monitoring data collected by a detection unit associated with the target device, and determine the target execution result based on the current monitoring data.
9. A programming device for a programmable logic controller, characterized in that: The device comprises: an acquisition module, used to acquire a first control logic code pre-written according to a business requirement, wherein the first control logic code is written according to a grammatical rule of a first programming language, and the first control logic code is written based on a pre-built programming interface, wherein the programming interface includes a plurality of functions, each function being used to implement one or more functions in the programmable logic controller; a conversion module, configured to convert the first control logic code into a second control logic code according to a pre-constructed syntax mapping table, wherein the syntax mapping table is used to characterize a syntax conversion relationship between the first programming language and the second programming language; the second control logic code is a machine code executable by the programmable logic controller; An operating module, used for deploying the second control logic code to an operating system run by the programmable logic controller, and running the second control logic code; The acquisition module is further used to acquire control instructions input by the user through the graphical user interface; A determination module, configured to determine a target execution result according to the control instruction and the second control logic code, wherein the control instruction includes: device parameters and control instructions of a target device in the industrial control system, and the device parameters include: protocol type, port number, and IP address; A display module is used to display the target execution result on the graphical user interface.
10. A computer-readable storage medium, characterized in that: The storage medium stores a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1 to 8 is executed.
Citation Information
Patent Citations
Transformation method for programmable logic controller programming language
CN109032056A
Programmable logic control language compiling method and device
CN112835323A
PLC supporting Chinese programming, programmable logic control system, implementation method of Chinese programming language and electronic device
CN113778396A
System for developing industrial control programming, method for programming industrial system and medium
CN115840421A
Compilation method for fully-distributed intelligent building system application program
CN116974573A
Cited By
PLC (Programmable Logic Controller) control method and device integrated with Python algorithm and related product
CN121300225A