A method to efficiently provide real-time data for Python programming in controllers

Through the combination of daemons, protocol handlers and shared variable dictionaries, it solves the complexity of peripheral protocol parsing and communication in Python programming, provides a unified data interface and callback mechanism, improves programming efficiency and real-time performance, and simplifies program logic. .

CN114020271BActive Publication Date: 2024-10-11BEIJING BIAOZHI ZHUOXIN SCIENSE&TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202110656805.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-06-11
Publication Date
2024-10-11
Estimated Expiration
2041-06-11

AI Technical Summary

Technical Problem

In Python programming, peripheral protocol parsing is complex, communication time is consumed, and different brands of equipment have different asynchronous methods, which increases program logic complexity and real-time issues.

Method used

Using daemons, protocol handlers and shared variable dictionaries, the shared variable dictionary provides a standard library, implements data caching and callback mechanisms, unifies peripheral interfaces, reduces programmers' burden on protocol parsing and communication processing, and improves real-time performance.

Benefits of technology

It significantly reduces the difficulty of programming, simplifies application software design, improves work efficiency, achieves high real-time performance, avoids repeated development and asynchronous operation complexity, and makes full use of multi-CPU or multi-core computing capabilities.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114020271B_ABST
    Figure CN114020271B_ABST
Patent Text Reader

Abstract

The present invention relates to the field of programming technology, and in particular to a method for efficiently providing real-time data for Python programming in a controller. First, the real-time value Value of the peripheral data parsed is updated to a shared variable dictionary through a unique variable name Key as an index through a protocol processing program; then the Python application references import and calls the API of the standard library, and searches the current real-time value in the shared variable dictionary at any time through the name Key of the variable; when the protocol processing program detects that the variable is modified by Python, the change is immediately encoded with corresponding data and sent to the corresponding device; and a specific Python subroutine is called to notify the Python program that the sending has been completed; finally, all peripherals establish a variable dictionary in a memory in this way, and all heterogeneous devices are unified on a standard dictionary interface. The present invention greatly reduces the workload of learning peripheral communication protocols, writing communication programs, and data encoding and decoding when accessing various heterogeneous peripherals in a Python program.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of programming technology, and in particular to a method for efficiently providing real-time data for Python programming in a controller. Background Art

[0002] With the development of computer hardware and software technology, dedicated programmable controllers (PLCs) are increasingly being replaced by various new intelligent controllers or edge devices based on PC operating systems (such as Linux and Windows). These new controllers or edge devices generally have powerful computing capabilities and can be programmed using various common high-level languages, replacing traditional programming methods such as ladder diagrams in PLCs. Among these high-level languages, Python is quite popular among people who are not educated in computer science because of its ease of use, platform independence, and support for a large number of function libraries. This makes many intelligent controllers and edge devices prefer Python as a built-in control program. In controllers or edge devices, many measurement and execution devices (collectively referred to as peripherals) are generally built-in or external. For example, the measurement of on-site temperature and pressure, various calculations are performed to obtain the calculation results, and then the actuator is driven to open or close the valve according to the calculation results.

[0003] In this process, the Python program needs to understand the communication protocol of each peripheral, and then communicate with the peripheral according to the communication protocol. After obtaining the data, it performs various decoding conversions into data types that Python can calculate. For example, the data read back from the temperature sensor may be a string type, and Python needs floating-point data for calculation. After the calculation is completed, based on different results, the Python program may choose to open / close the valve of a certain actuator. At this time, the Python program needs to perform relevant data processing, coroutine and thread processing according to the encoding and communication protocol of the actuator before sending the command to the actuator to achieve the control purpose.

[0004] When Python programs parse device protocols and communicate with peripherals, there are three issues that cannot be ignored.

[0005] 1. Analysis of peripheral protocols: You need to carefully read the peripheral manual and have a deep understanding of it in order to write a program that complies with the device protocol. For some complex devices, a longer learning cycle is required, and some professional knowledge of computer algorithms may also be required. These contents may be a challenge for on-site front-line engineers.

[0006] 2. Time consumption for communication with peripherals: Data communication with peripherals consumes computer time, and the Python program needs to wait to send a request or wait for the result. This situation greatly reduces the real-time performance of the controller, because in the process of waiting for a peripheral, the status of an important device may change, but the program does not handle it in time, which may cause losses to on-site production or facilities. Some equipment manufacturers or third parties will provide some equipment parsing libraries for engineers to use, which can reduce the difficulty of programming, but they still run in the current Python process environment and also take up the computer time of the Python protocol handler.

[0007] 3. Different brands of peripherals have different asynchronous methods: Some device manufacturers or third-party libraries design the library to run asynchronously in order to save the Python protocol handler communication waiting time. This can save the running time of the Python process, but the library processing methods of different devices are different. Often, several asynchronous methods must be used in a Python program for processing, which greatly increases the complexity of the program logic. Summary of the invention

[0008] In view of this, the purpose of the present invention is to provide a method for efficiently providing real-time data for Python programming in a controller to solve the problems mentioned in the background technology. The present invention further provides a method for efficiently providing real-time data for Python programming in a controller, which involves 5 main components, specifically:

[0009] 1. Daemon (can be integrated into the protocol handler). The daemon is responsible for loading and managing different protocol handlers, shared variable dictionaries and Python applications according to the configuration of the peripherals used by the user. When a program has a problem, the daemon can start it up and run again. The daemon, dictionary and protocol handler can be integrated together in the scene.

[0010] 2. Protocol handler: The protocol handler is responsible for peripheral protocol parsing, communication and data encoding and decoding.

[0011] 3. Shared variable dictionary (can be integrated into the protocol handler). The shared variable dictionary is responsible for caching real-time data. It uses key-value pairs to quickly retrieve data. It also provides access interfaces for protocol handlers and Python applications. It also supports protocol handlers and Python applications to register callback functions to achieve timely processing of important messages, events, and data changes.

[0012] 4. Shared variable dictionary standard Python library, a standard library provided for Python application development. Programmers can directly reference the API in the library to retrieve variables in the dictionary, read / write variable values, and register callback functions.

[0013] 5. Python application, which implements the application logic on the user's site, can completely ignore the process of communicating with peripherals and data encoding and decoding by referencing the standard library provided by the shared variable dictionary.

[0014] Furthermore, the method first updates the parsed real-time value Value of the peripheral data into the shared variable dictionary through the protocol processing program by using the unique variable name Key as the index;

[0015] S2: The Python application references import and calls the API of the standard library. It can search the current real-time value in the shared variable dictionary at any time through the variable name Key. It can also choose to update the value of the variable in the dictionary through the variable name Key.

[0016] S3: When the protocol handler detects that the variable is modified by Python, it immediately encodes the change into corresponding data and sends it to the corresponding device. It can also update the value of the variable in the shared variable dictionary after the callback is triggered after the processing is completed, and call the Python subroutine to notify the Python program that the sending has been completed.

[0017] The shared variable dictionary also provides a way to obtain real-time data through a callback mechanism. The Python application references and calls the API of the standard library, registers with the shared variable dictionary, and calls a Python subroutine when a variable changes; this allows the Python program to respond to certain important messages, events, or data changes in a timely manner. This eliminates the need for programmers to perform complex asynchronous operations based on the protocols of various device manufacturers.

[0018] S4: All peripherals use this method to build variable dictionaries in memory, unifying all kinds of heterogeneous devices into a standard dictionary interface. This method completely avoids the Python program from performing protocol parsing, data communication, and data encoding and decoding, greatly reducing the workload of writing Python programs, and Python programs do not have to waste time waiting for responses from some slow devices.

[0019] Furthermore, the protocol handler of the peripheral can be written in various languages. In scenarios such as low-performance CPU systems or low-memory systems, the daemon, protocol handler, and shared variable dictionary can be integrated into one program to reduce the demand on system resources.

[0020] Furthermore, the daemon, protocol handler, shared variable dictionary program and Python application program run in parallel, and according to the characteristics of the CPU and the operating system, on a multi-CPU system or a multi-core system, these programs and Python application program can be set to run on different CPUs or cores to fully utilize the computing power of each CPU or core.

[0021] A method for efficiently providing real-time data for Python programming in a controller of the present invention, the present invention:

[0022] 1. It greatly reduces the learning difficulty of various peripherals for on-site programmers and shortens the learning cycle.

[0023] 2. For various peripherals, the differences are shielded, providing a unified data interface, simplifying application software design and significantly improving work efficiency.

[0024] 3. Protocol handlers for the same peripheral can be reused in source code or executable binary form, avoiding duplicate development.

[0025] 4. Provide a unified callback method for messages, events and important data changes. Programmers only need to register callbacks for messages, events or data in Python to process them in a timely manner, providing two benefits. The unified callback mechanism avoids different asynchronous mechanisms for different manufacturers and simplifies program logic. It avoids Python applications waiting for responses from peripherals and achieves high real-time performance.

[0026] 5. On multi-CPU or multi-core systems, the processor cores of protocol handlers and Python programs can be dynamically specified to fully utilize the computing power of multiple CPUs or multiple cores. BRIEF DESCRIPTION OF THE DRAWINGS

[0027] Figure 1 is a flow chart of the present invention;

[0028] Figure 2 is a system block diagram of the present invention;

[0029] Figure 3 It is a process flow chart of the method of the present invention. DETAILED DESCRIPTION

[0030] The present invention will be described in detail below in conjunction with the accompanying drawings and specific embodiments. Obviously, the described embodiments are only part of the embodiments of the present application, rather than all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present application.

[0031] In this embodiment, Figure 1 As shown, the daemon, protocol processing program, and shared variable dictionary program of the present invention can be implemented by any language with communication function. In order to improve efficiency, this embodiment selects C, C++, and Python to develop different programs respectively.

[0032] The three programs are logically independent of each other, but in practice, they can be unified into one program, or any two or three can be combined. In this embodiment, three separate independent programs are selected to achieve selective loading of protocol processing programs for different peripherals.

[0033] In this embodiment, the daemon: provides an HTML-based user configuration interface, and the user can select the protocol handler according to the brand, model and quantity of the peripherals connected to this controller, and can define various variables of this peripheral according to the characteristics of the peripheral. For example, this controller peripheral is connected to a frequency converter with a Modbus RTU interface of "Shenzhen Huichuan" company, model "MD290". In this embodiment, select "Shenzhen Huichuan" under the RS485 interface, and then find the corresponding frequency converter according to the model. In this embodiment, find MD290, and then click Add, and the device is added. According to the manual of Huichuan Company's MD290, the company's MD290 model frequency converter, Modbus communication defaults to 9600 baud rate, 8 data bits, 1 stop bit, no check, and the device address is 1. Based on this information, this embodiment sets the corresponding parameters on the interface, sets the Modbus address to 1, the baud rate to 9600, the data bit selection to 8, the stop bit selection to 1, and the parity selection to None. This completes the addition of the device.

[0034] In this embodiment, after the peripherals are added, custom variables are added under this inverter device, and these variables correspond to the values ​​in the corresponding register addresses in the inverter. For example, in this embodiment, you want to read the running status of the MD290 inverter in real time, whether it is "forward operation", "reverse operation" or "stop" state. Through the user manual of MD290, you know that you need to obtain the value at address 0x3000 in the Modbus holding register address space. At this time, add a variable called "RunningStatus" on the interface, set this variable to correspond to the 0x3000 address of the holding register in the MD290 inverter, and the data type is unsigned integer. If you need to read the real-time operating frequency of MD290, add another variable "Freq" in this embodiment, corresponding to the address 0x1001, and the data type is unsigned integer. According to the user manual, this embodiment can arbitrarily add variables that you want to read or write. This completes the addition of variables in the device. When the user completes all variable definitions, click the Save button, and the daemon saves these user configurations as a configuration file for the protocol handler and shared variable dictionary. Users can also manually edit this configuration file to add devices or variables in batches. When the user clicks "Start Run" on the HTML user interface, the daemon loads the pre-defined peripheral protocol handler and shared variable dictionary program and starts these programs into operation.

[0035] In this embodiment, when the daemon program detects that a certain protocol processing program or dictionary program exits abnormally, the program is automatically restarted.

[0036] Protocol handler: You can integrate the parsing protocols and communication processing of several peripherals into one program, or you can provide an independent protocol handler for each independent peripheral. After the daemon starts and runs the protocol handler, the protocol handler communicates with the corresponding peripheral according to the content of the configuration file, obtains data and parses it into a format acceptable to Python, and then refreshes the data into the dictionary program with the variable name as the index.

[0037] In this embodiment, the shared variable dictionary: In memory, the Python native data structure "Dictionary" is used to maintain the names and values ​​of the variables defined in the "daemon". The key of the dictionary is the variable name, and the value of the dictionary is the current value read from the device in real time, or the value written to the dictionary by the Python program. Because the Python native dictionary uses a hash algorithm, the dictionary can theoretically be expanded without limit and can be quickly indexed.

[0038] In this embodiment, a standard Python library for sharing dictionary variables: implements a standard Python library to facilitate Python programs to call dictionary programs. The library provides at least two basic methods, GetValue and SetValue. GetValue reads the value of a variable in the dictionary, and SetValue writes the value of a variable in the dictionary. When calling, the variable name is used as the parameter GetValue (variable name), for example: status = GetValue (MD290.RunningStatus). The Python program can get the value of the variable in the dictionary or write the value to the dictionary without waiting.

[0039] The above embodiments are only used to illustrate the technical solutions of the present invention and are not intended to limit the present invention. Although the present invention is described in detail with reference to the preferred embodiments, it should be understood by those skilled in the art that the technical solutions of the present invention may be modified or replaced by equivalents without departing from the purpose and scope of the technical solutions of the present invention, which should be included in the scope of the claims of the present invention. The techniques, shapes, and structural parts not described in detail in the present invention are all known technologies.

Claims

1. A method for efficiently providing real-time data for Python programming in controllers, Features: It includes daemon, protocol handler, shared variable dictionary, shared variable dictionary standard Python library and Python application. Please follow the steps below: S 1 :First, the protocol handler updates the real-time value of the parsed peripheral data Value to the shared variable dictionary using the unique variable name Key as the index; S 2 :The Python application references import and calls the API of the standard library. It can search the current real-time value in the shared variable dictionary at any time through the variable name Key. It can also choose to update the value of the variable in the dictionary through the variable name Key; S 3 :When the protocol handler detects that the variable is modified by Python, it will immediately encode the change into corresponding data and send it to the corresponding device; it can also trigger the callback after the processing is completed, update the value of the variable in the shared variable dictionary, and call the Python subroutine to notify the Python program that the sending has been completed; it can also choose to provide a callback mechanism for the shared variable dictionary, and the Python application references and calls the API of the standard library to register with the shared variable dictionary to call a Python subroutine when a variable changes; S 4 :All peripherals establish variable dictionaries in memory in this way, unifying all kinds of heterogeneous devices into a standard dictionary interface.

2. A method for efficiently providing real-time data for Python programming in a controller according to claim 1, Features: The protocol processing program of the method can be written in various languages. In certain scenarios, the daemon, the protocol processing program and the shared variable dictionary can be integrated into one program.

3. A method for efficiently providing real-time data for Python programming in a controller according to claim 1, Features: When the protocol processing program, the shared variable dictionary program and the Python application program are run in parallel, these programs and the Python application program can be set to run on different CPUs or cores on a multi-CPU system or a multi-core system according to the characteristics of the CPU and the operating system.