Communication code generation method based on XML configuration file and storage medium

By using an XML configuration file-based communication code generation method, the code is automatically converted into a DOM tree and an abstract syntax tree is generated, solving the problem of low development efficiency in traditional Modbus communication code and achieving fast and reliable communication code generation.

CN120994174AActive Publication Date: 2025-11-21BEIJING ACCUENERGY TECH CO LTD

Patent Information

Application Number
CN202511112739.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-08-09
Publication Date
2025-11-21
Estimated Expiration
2045-08-09

AI Technical Summary

Technical Problem

Traditional Modbus communication code development is inefficient, requires manual coding, and cannot quickly adapt to the access of different devices.

Method used

A communication code generation method based on XML configuration files is adopted. The configuration file is converted into a DOM tree by an XML parser, an abstract syntax tree is generated, static analysis and code generation are performed to ensure the rigor of the code logic.

Benefits of technology

It significantly reduces manual coding workload, improves development efficiency, and enables developers unfamiliar with the details of the Modbus protocol to quickly implement reliable communication functions. The generated code strictly follows the syntax rules of the target language, ensuring readability and maintainability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120994174A_ABST
    Figure CN120994174A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of code generation, in particular to a communication code generation method based on an XML configuration file and a storage medium, and aims to improve the code development efficiency. The communication code generation method based on the XML configuration file comprises the steps that the XML configuration file is converted into a DOM tree with a hierarchical relation through an XML parser; traversing nodes of the DOM tree, and converting the DOM tree into an abstract syntax tree; based on the abstract syntax tree, statically checking register address conflicts, reference validity, data type compatibility and protocol constraints; traversing the abstract syntax tree to generate a Modbus communication code; and outputting the Modbus communication code as a structured source code file according to the grammar rule of the target language. According to the method, the manual coding workload is greatly reduced, and the development efficiency is improved.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of code generation, in particular to a communication code generation method based on an XML configuration file and a storage medium. BACKGROUND

[0002] In modern industrial automation systems, Modbus protocol, as a widely used communication standard, is widely used for data acquisition and control between devices.

[0003] Traditional Modbus communication code development usually relies on manual writing. Developers need to define register addresses, data types, communication parameters, etc. according to specific device configurations and communication requirements, and implement corresponding data parsing and processing logic. Each time a new device (electricity meter, water meter, etc.) is connected, the corresponding communication code (register mapping, data parsing, etc.) needs to be manually written, so the efficiency is extremely low. SUMMARY

[0004] In order to solve the above problems in the prior art, the present application provides a communication code generation method based on an XML (eXtensible Markup Language) configuration file and a storage medium, which improves the code development efficiency.

[0005] In a first aspect of the present application, a communication code generation method based on an XML configuration file is provided, which comprises:

[0006] Converting the XML configuration file into a DOM (Document Object Model) tree with hierarchical relationship by using an XML parser;

[0007] Traversing the nodes of the DOM tree to convert into an AST (Abstract Syntax Tree);

[0008] Based on the AST, statically checking register address conflicts, reference validity, data type compatibility and protocol constraints;

[0009] Traversing the AST to generate Modbus communication code;

[0010] Outputting the Modbus communication code as a structured source code file according to the syntax rules of the target language.

[0011] Preferably, before "extracting device description information in the XML configuration file", the method further comprises:

[0012] Obtaining the XML configuration file;

[0013] The XML configuration file comprises:

[0014] a channel configuration for specifying an ID (Identifier) of a communication channel, a protocol type and communication parameters;

[0015] a device type configuration for specifying a type of a device and register addresses, data types, byte orders and scaling factors supported by the device;

[0016] a device instance configuration for configuring an instance of a specific device, including a device type, a device address, a bound communication channel and a list of acquisition parameters;

[0017] wherein,

[0018] when the protocol type is Modbus RTU (Remote Terminal Unit), the communication parameters comprise a baud rate, data bits, check bits and stop bits;

[0019] when the protocol type is Modbus TCP / IP (Modbus Transmission Control Protocol / Internet Protocol), the communication parameters comprise an IP address and a port number.

[0020] Preferably, the root node of the abstract syntax tree comprises a channel list, a device type list and a device instance list;

[0021] the channel list comprises a plurality of channel nodes;

[0022] the device type list comprises a plurality of device type nodes;

[0023] the device instance list comprises a plurality of device instance nodes;

[0024] each channel node comprises a channel identifier, a protocol type and a plurality of parameter fields;

[0025] the channel identifier is used for storing the ID of the communication channel;

[0026] the protocol type is used for storing the ID of the protocol type;

[0027] the parameter fields are used for storing the communication parameters;

[0028] each device type node comprises a device type identifier, a description text and a register list;

[0029] the device type identifier is used for storing an ID representing the device type;

[0030] the description text is used for recording a detailed description of the device type, including a manufacturer, a model and a version;

[0031] The register list comprises a plurality of register nodes;

[0032] Each of the register nodes comprises the register address, the data type, the byte order and the scaling factor;

[0033] Each of the device instance nodes comprises an instance identifier, an instance type, a channel number, a slave address and a parameter list;

[0034] The instance identifier is used to store a serial number of a device instance;

[0035] The instance type refers to the corresponding device type identifier;

[0036] The channel number refers to the corresponding channel identifier;

[0037] The slave address is used to store a device address;

[0038] The parameter list comprises a plurality of parameter nodes;

[0039] Each of the parameter nodes comprises a parameter name, a reference to the corresponding register node and the acquisition parameter;

[0040] Preferably, the step of "traversing the abstract syntax tree to generate Modbus communication code" comprises:

[0041] Parsing the abstract syntax tree root node to obtain the channel list, the device type list and the device instance list;

[0042] Traversing each of the channel nodes in the channel list to generate a corresponding communication channel object;

[0043] Traversing each of the device type nodes in the device type list to generate a corresponding device type definition object;

[0044] Traversing each of the device instance nodes in the device instance list to generate a corresponding device instance object;

[0045] Generating a main program framework code for channel initialization, creating device instances and periodic data acquisition.

[0046] Preferably, the step of "traversing each of the channel nodes in the channel list to generate a corresponding communication channel object" comprises:

[0047] Generating a unique channel object instance name according to the channel identifier;

[0048] Selecting a corresponding Modbus protocol stack initialization method according to the protocol type to generate a channel connection object;

[0049] mapping the parameter field as a configuration attribute of the channel object.

[0050] Preferably, the step of "traversing each device type node in the device type list, generating a corresponding device type definition object" comprises:

[0051] generating a device type enumeration object according to the device type identifier;

[0052] extracting the description text to generate device description information, and storing as an attribute of the device type definition object;

[0053] traversing the register list, generating a corresponding register definition object for each register node, and binding to the device type definition object.

[0054] Preferably, the step of "traversing each device instance node in the device instance list, generating a corresponding device instance object" comprises:

[0055] generating a unique device instance object name according to the instance identifier;

[0056] binding a corresponding device type definition object through the instance type attribute;

[0057] binding a corresponding communication channel object through the channel number attribute;

[0058] setting the slave address as an addressing attribute of the device instance object;

[0059] traversing the parameter list, generating a data acquisition method for each parameter node, and binding to the device instance object.

[0060] Preferably, the step of "traversing the register list, generating a corresponding register definition object for each register node, and binding to the device type definition object" comprises:

[0061] mapping the register address as a Modbus protocol address attribute of the register definition object;

[0062] generating a data conversion method according to the data type and the byte order, and binding to the register definition object;

[0063] generating a data scaling method according to the scaling factor, and binding to the register definition object;

[0064] binding the register definition object to a corresponding device type definition object;

[0065] The step of "generating data acquisition methods for each parameter node by traversing the parameter list, and binding to the device instance object" comprises:

[0066] generating a data storage attribute of the device instance object according to the parameter name;

[0067] determining a Modbus read-write operation method through the referenced register definition object;

[0068] generating the data acquisition method according to the read-write operation method, and binding to the corresponding device instance object;

[0069] generating a timing acquisition scheduling logic according to the acquisition parameter;

[0070] The main program framework code comprises:

[0071] an initialization module, configured to call an initialization method of all the communication channel objects;

[0072] a device instance management module, configured to create and manage a set of all the device instance objects;

[0073] a main loop module, configured to call a data acquisition method of each device instance object in a preset polling order, and process the acquired data according to the data conversion method and the data scaling method.

[0074] Preferably, before "outputting the Modbus communication code as a structured source code file according to the syntax rules of the target language", the method further comprises:

[0075] performing static checking on the generated code to ensure the syntax correctness and functional integrity of the code.

[0076] In a second aspect, the application provides a computer readable storage medium, which stores a computer program capable of being loaded and executed by a processor to perform the method described above.

[0077] The application has the following beneficial effects:

[0078] The communication code generation method based on the XML configuration file disclosed by the present application converts the XML configuration file into a DOM tree automatically, converts the DOM tree into an abstract syntax tree, and finally generates complete and usable Modbus communication code, greatly reducing the workload of manual coding and improving the development efficiency. The conversion process from the XML configuration file to the DOM tree and then to the abstract syntax tree realizes the structured processing and verification of the configuration information, and ensures that the generated code is logically rigorous. Through the configuration file driven mode, the developers who are not familiar with the details of the Modbus protocol can also quickly realize reliable communication functions. The generated code strictly follows the syntax rules of the target language, and the output is a structured source code file, which ensures the readability and maintainability of the code. BRIEF DESCRIPTION OF DRAWINGS

[0079] Figure 1 is a main step schematic diagram of the communication code generation method based on the XML configuration file of the present application. DETAILED DESCRIPTION

[0080] The preferred embodiments of the present application will be described below with reference to the accompanying drawings. Those skilled in the art should understand that these embodiments are only used to explain the technical principles of the present application, and are not intended to limit the protection scope of the present application.

[0081] To make the purpose, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be described clearly and completely below with reference to the accompanying drawings of the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments of the present application. Based on the embodiments of the present application, all other embodiments obtained by those skilled in the art without creative labor fall within the protection scope of the present application.

[0082] It should be noted that, in the description of the present application, the terms "first", "second" are only for convenience of description, and do not indicate or imply relative importance of the devices, elements or parameters, and therefore cannot be understood as a limitation of the present application. In addition, the term "and / or" in the present application is only a description of the association relationship of the associated objects, which means that there can be three relationships, for example, A and / or B can represent the three cases of A alone, A and B together, and B alone. In addition, the character " / " in this paper, if not specially stated, generally represents an "or" relationship between the associated objects before and after it.

[0083] Figure 1 is a main step schematic diagram of the communication code generation method based on the XML configuration file of the present application. As shown in Figure 1 the code generation method of the present embodiment includes steps S10-S50:

[0084] Step S10, converting the XML configuration file into a DOM tree with hierarchical relationship by using an XML parser.

[0085] In this embodiment, the XML configuration file includes: channel configuration, device type configuration and device instance configuration.

[0086] The channel configuration is used to specify the ID, protocol type and communication parameters of the communication channel; the device type configuration is used to specify the type of the device and the register address, data type, byte order and scaling factor supported by the device; and the device instance configuration is used to configure the instance of a specific device, including the device type, device address, bound communication channel and acquisition parameter list.

[0087] When the protocol type is Modbus RTU, the communication parameters include: baud rate, data bits, check bits and stop bits; and when the protocol type is Modbus TCP / IP, the communication parameters include: IP address and port number.

[0088] The following is an example of an XML configuration file:

[0089]

[0090]

[0091] Step S20, traversing the nodes of the DOM tree and converting into an abstract syntax tree.

[0092] In this embodiment, the root node of the abstract syntax tree includes: channel list, device type list and device instance list. The channel list includes multiple channel nodes; the device type list includes multiple device type nodes; and the device instance list includes multiple device instance nodes.

[0093] Each channel node includes: channel identification, protocol type and multiple parameter fields. The channel identification is used to store the ID of the communication channel; the protocol type is used to store the ID of the protocol type; and the parameter fields are used to store the communication parameters.

[0094] Each device type node includes: device type identification, description text and register list. The device type identification is used to store the ID representing the device type; the description text is used to record the detailed description of the device type, including manufacturer, model and version; and the register list includes multiple register nodes. Each register node includes: register address, data type, byte order and scaling factor.

[0095] Each device instance node comprises: an instance identifier, an instance type, a channel number, a slave address and a parameter list. The instance identifier is used to store a serial number of the device instance; the instance type refers to a corresponding device type identifier; the channel number refers to a corresponding channel identifier; the slave address is used to store a device address; the parameter list comprises a plurality of parameter nodes; each parameter node comprises: a parameter name, a reference to a corresponding register node, and the collection parameter.

[0096] Step S30, based on the abstract syntax tree, statically checking register address conflicts, reference validity, data type compatibility and protocol constraints.

[0097] Step S40, traversing the abstract syntax tree to generate Modbus communication code.

[0098] Specifically, step S40 comprises steps S41-S45:

[0099] Step S41, parsing the root node of the abstract syntax tree to obtain a channel list, a device type list and a device instance list.

[0100] Step S42, traversing each channel node in the channel list to generate a corresponding communication channel object. This step can specifically comprise steps S421-S423:

[0101] Step S421, generating a unique channel object instance name according to the channel identifier.

[0102] Step S422, selecting a corresponding Modbus protocol stack initialization method according to the protocol type to generate a channel connection object.

[0103] Step S423, mapping the parameter field to a configuration attribute of the channel object.

[0104] Step S43, traversing each device type node in the device type list to generate a corresponding device type definition object. This step can specifically comprise steps S431-S433:

[0105] Step S431, generating a device type enumeration object according to the device type identifier.

[0106] Step S432, extracting the description text to generate device description information, which is stored as an attribute of the device type definition object.

[0107] Step S433, traversing the register list to generate a corresponding register definition object for each register node and bind it to the device type definition object.

[0108] Specifically, for each register node, the following operations are performed: mapping the register address to the Modbus protocol address attribute of the register definition object; generating a data conversion method according to the data type and byte order, and binding the data conversion method to the register definition object; generating a data scaling method according to the scaling factor, and binding the data scaling method to the register definition object; and binding the register definition object to the corresponding device type definition object.

[0109] Step S44, each device instance node in the device instance list is traversed to generate a corresponding device instance object. This step can specifically include steps S441-S445:

[0110] Step S441, a unique device instance object name is generated according to the instance identifier.

[0111] Step S442, the corresponding device type definition object is bound through the instance type attribute.

[0112] Step S443, the corresponding communication channel object is bound through the channel number attribute.

[0113] Step S444, the slave address is set as the addressing attribute of the device instance object.

[0114] Step S445, the parameter list is traversed to generate a data acquisition method for each parameter node and bind the data acquisition method to the device instance object.

[0115] Specifically, for each parameter node, the following operations are performed: a data storage attribute of the device instance object is generated according to the parameter name; a Modbus read-write operation method is determined through the referenced register definition object; a data acquisition method is generated according to the read-write operation method and is bound to the corresponding device instance object; and a timing acquisition scheduling logic is generated according to the acquisition parameter.

[0116] Step S45, a main program framework code is generated for channel initialization, device instance creation, and periodic data acquisition.

[0117] In this embodiment, the main program framework code includes an initialization module, a device instance management module, and a main loop module.

[0118] The initialization module is configured to call the initialization methods of all communication channel objects; the device instance management module is configured to create and manage a collection of all device instance objects; and the main loop module is configured to call the data acquisition methods of each device instance object in a preset polling order, and process the acquired data according to the data conversion method and the data scaling method.

[0119] Step S50, the Modbus communication code is output as a structured source code file according to the syntax rules of the target language.

[0120] In an alternative embodiment, step S10 is preceded by step S5:

[0121] Step S5, obtaining the XML configuration file.

[0122] The XML configuration file can be read from a previous storage, or received from a user via a network.

[0123] In another alternative embodiment, step S50 is preceded by step S46:

[0124] Step S46, performing static checking on the generated code to ensure the syntax correctness and functional integrity of the code.

[0125] Although the steps are described in the above-mentioned order in the above embodiments, it should be understood by those skilled in the art that, in order to achieve the effects of the embodiments, the steps do not have to be executed in such an order, and they can be executed simultaneously (in parallel) or in a reversed order, and these simple changes are within the protection scope of the present application.

[0126] Based on the above method embodiments, the present application further provides a computer readable storage medium embodiment, and the storage medium of the embodiment stores a computer program capable of being loaded and executed by a processor to perform the above method.

[0127] The computer readable storage medium can include a U disk, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, and various storage medium capable of storing program codes.

[0128] Those skilled in the art should be aware that the method steps of the examples described in combination with the embodiments disclosed herein can be realized by electronic hardware, computer software or a combination of both. In order to clearly illustrate the interchangeability of electronic hardware and software, the components and steps of the examples have been described in general terms in the above description. Whether the functions are realized by electronic hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to realize the described functions for each specific application, but such implementation should not be considered beyond the scope of the present application.

[0129] The technical solutions of the present application have been described in combination with the preferred embodiments shown in the drawings. However, it is easy for those skilled in the art to understand that the protection scope of the present application is obviously not limited to these specific embodiments. Those skilled in the art can make equivalent changes or replacements to the related technical features without departing from the principles of the present application, and the technical solutions after the changes or replacements will all fall within the protection scope of the present application.

Claims

1. A method for generating communication code based on XML configuration files, characterized in that, The method includes: Use an XML parser to convert XML configuration files into a hierarchical DOM tree; Traverse the nodes of the DOM tree and convert them into an abstract syntax tree; Based on the abstract syntax tree, static checks are performed to detect register address conflicts, reference validity, data type compatibility, and protocol constraints. Traverse the abstract syntax tree to generate Modbus communication code; The Modbus communication code is output as a structured source code file according to the syntax rules of the target language.

2. The communication code generation method based on XML configuration files according to claim 1, characterized in that, Before "extracting device description information from the XML configuration file", the method further includes: Obtain the XML configuration file; The XML configuration file includes: Channel configuration, used to specify the communication channel ID, protocol type, and communication parameters; Device type configuration, used to specify the type of device as well as the register addresses, data types, byte order and scaling factors supported by the device; Device instance configuration is used to configure a specific device instance, including device type, device address, bound communication channel, and collection parameter list; in, When the protocol type is Modbus RTU, the communication parameters include: baud rate, data bits, parity bits, and stop bits; When the protocol type is Modbus TCP / IP, the communication parameters include: IP address and port number.

3. The communication code generation method based on XML configuration files according to claim 2, characterized in that, The root node of the abstract syntax tree includes: a channel list, a device type list, and a device instance list; The channel list includes multiple channel nodes; The device type list includes multiple device type nodes; The device instance list includes multiple device instance nodes; Each of the channel nodes includes: a channel identifier, a protocol type, and multiple parameter fields; The channel identifier is used to store the ID of the communication channel; The protocol type is used to store the ID of the protocol type; The parameter field is used to store the communication parameters; Each device type node includes: a device type identifier, a description text, and a register list; The device type identifier is used to store an ID representing the device type; The descriptive text is used to record a detailed description of the device type, including the manufacturer, model, and version; The register list includes multiple register nodes; Each of the register nodes includes: the register address, the data type, the byte order, and the scaling factor; Each device instance node includes: instance identifier, instance type, channel number, slave address, and parameter list; The instance identifier is the serial number used for the storage device instance; The instance type references the corresponding device type identifier; The channel number refers to the corresponding channel identifier; The slave address is used to store the device address; The parameter list includes multiple parameter nodes; Each parameter node includes: a parameter name, a reference to the corresponding register node, and the acquired parameter.

4. The communication code generation method based on XML configuration files according to claim 3, characterized in that, The steps of "traversing the abstract syntax tree to generate Modbus communication code" include: Parse the root node of the abstract syntax tree to obtain the channel list, the device type list, and the device instance list; Iterate through each channel node in the channel list to generate a corresponding communication channel object; Iterate through each device type node in the device type list to generate a corresponding device type definition object; Iterate through each device instance node in the device instance list and generate a corresponding device instance object; Generate the main program framework code, which is used for channel initialization, device instance creation, and periodic data collection.

5. The communication code generation method based on XML configuration files according to claim 4, characterized in that, The step of "traversing each channel node in the channel list and generating a corresponding communication channel object" includes: A unique channel object instance name is generated based on the channel identifier; Select the corresponding Modbus protocol stack initialization method according to the protocol type, and generate a channel connection object; Map the parameter fields to the configuration properties of the channel object.

6. The communication code generation method based on XML configuration files according to claim 5, characterized in that, The step of "traversing each device type node in the device type list and generating a corresponding device type definition object" includes: Generate a device type enumeration object based on the device type identifier; Extract the description text to generate device description information, and store it as an attribute of the device type definition object; Traverse the register list, generate a corresponding register definition object for each register node, and bind it to the device type definition object.

7. The communication code generation method based on XML configuration files according to claim 6, characterized in that, The step of "traversing each device instance node in the device instance list and generating a corresponding device instance object" includes: A unique device instance object name is generated based on the instance identifier; The instance type attribute is used to bind the corresponding device type definition object; The corresponding communication channel object is bound through the channel number attribute; Set the slave address as the addressing attribute of the device instance object; The parameter list is traversed, a data acquisition method is generated for each parameter node, and then bound to the device instance object.

8. The communication code generation method based on XML configuration files according to claim 7, characterized in that, The steps of "traversing the register list, generating a corresponding register definition object for each register node, and binding it to the device type definition object" include: Map the register address to the Modbus protocol address attribute of the register definition object; A data conversion method is generated based on the data type and the byte order, and then bound to the register definition object; A data scaling method is generated based on the scaling factor and bound to the register definition object; Bind the register definition object to the corresponding device type definition object; The steps of "traversing the parameter list, generating a data acquisition method for each parameter node, and binding it to the device instance object" include: Generate the data storage attributes of the device instance object based on the parameter name; The Modbus read / write operation method is determined by referencing the aforementioned register definition object; The data acquisition method is generated according to the read / write operation method and bound to the corresponding device instance object; Generate timed data acquisition scheduling logic based on the acquisition parameters; The main program framework code includes: The initialization module is used to call the initialization methods of all the aforementioned communication channel objects; The device instance management module is used to create and manage a collection of all the device instance objects mentioned above. The main loop module is used to call the data acquisition method of each device instance object in a preset polling order, and process the acquired data according to the data conversion method and the data scaling method.

9. The communication code generation method based on XML configuration files according to claim 1, characterized in that, Before "outputting the Modbus communication code as a structured source code file according to the syntax rules of the target language", the method further includes: Perform static analysis on the generated code to ensure its syntactic correctness and functional integrity.

10. A computer-readable storage medium, characterized in that, The computer program is stored that can be loaded by a processor and execute the method as described in any one of claims 1-9.

Citation Information

Patent Citations

  • Universal MODBUS protocol data analysis method

    CN111431784A

  • Webpage adaptation method and device based on AST

    CN112463152A

  • Data acquisition method and system, storage medium and electronic equipment

    CN114185585A

  • Code generation method and system based on abstract syntax tree

    CN115080011A

  • Robot program detection method, device and equipment and storage medium

    CN115587024A

Cited By

  • SOA communication model integration method and device and electronic equipment

    CN122316917A