Code automatic generation method and device
By generating an automatic code generation method for industrial equipment communication, and utilizing the message definition table and parsing function of the preset communication protocol, communication messages are automatically parsed, solving the problem of low efficiency in writing message parsing code in the existing technology, and realizing efficient and accurate automatic code generation.
Patent Information
- Application Number
- CN202510904471.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Priority Date
- 2024-09-26
- Filing Date
- 2025-07-01
- Publication Date
- 2025-10-31
AI Technical Summary
In industrial equipment communication, the existing technology for writing message parsing code is inefficient and error-prone, and the same code cannot be shared. It is necessary to manually write tedious parsing code for each device.
By obtaining the message definition table of preset instructions based on preset communication protocols from engineering equipment and terminals, the corresponding code content is generated using the first and second parsing functions, the communication messages are automatically parsed, and the target command line is generated to execute the preset instructions.
It greatly saves coding time, reduces errors caused by manual operation, and improves code generation efficiency and accuracy.
Smart Images

Figure CN120872304A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of communication protocol technology, and specifically to a method and apparatus for automatic code generation. Background Technology
[0002] In industrial equipment communication, each device follows a pre-defined protocol format. These protocols contain various communication commands. To parse the corresponding communication content, the messages must be parsed according to the protocol format. Therefore, developers need to be familiar with the communication protocol manual and write the corresponding message parsing code. Since the content and format of each communication command are different, the same message parsing code cannot be used interchangeably. Therefore, developers need to write dedicated parsing code for each message. However, the protocols contain numerous communication commands, each with a different message format. Manually writing message parsing code is prone to errors. Furthermore, the sheer number of communication commands makes the process tedious, time-consuming, and inefficient. Summary of the Invention
[0003] The purpose of this application is to provide a method and apparatus for automatic code generation, so as to solve the technical problems of low efficiency and error-proneness in writing message parsing code in the prior art.
[0004] To achieve the above objectives, the first aspect of this application provides a method for automatic code generation, the method comprising: Obtain the message definition table of preset instructions from engineering equipment and terminals based on preset communication protocols. The message definition table includes multiple instruction parameters. For each instruction parameter, the instruction parameter is parsed based on the first parsing function to generate the first code content corresponding to the instruction parameter according to the parameter name, parameter identifier and the byte position of the parameter identifier included in the instruction parameter; For each instruction parameter, the instruction parameter is parsed based on the second parsing function to generate the second code content corresponding to the instruction parameter according to the parameter name, parameter content and the byte position of the parameter content. Generate a target command line corresponding to the preset instruction based on all the first and second code contents, so as to execute the operation of the preset instruction through the target command line.
[0005] A second aspect of this application provides an automatic code generation apparatus, comprising: The memory is configured to store instructions; The processor is configured to retrieve instructions from memory and, when executing instructions, to implement the code generation method described above.
[0006] A third aspect of this application provides a machine-readable storage medium storing instructions that cause a machine to execute the code automatically generated method described above.
[0007] The above technical solution involves acquiring protocol messages of preset instructions transmitted by engineering equipment and terminals based on a preset communication protocol. These protocol messages include multiple instruction parameters. For each instruction parameter, a first parsing function is used to parse the parameter, generating first code content corresponding to the parameter based on its name, identifier, and byte position. For each instruction parameter, a second parsing function is used to parse it, generating second code content corresponding to the parameter based on its name, content, and byte position. A target command line corresponding to the preset instruction is generated based on all the first and second code contents, allowing the preset instruction to be executed via the target command line. This solution significantly reduces coding time, minimizes repetitive work, and reduces errors that may result from manual operation.
[0008] Other features and advantages of the embodiments of this application will be described in detail in the following detailed description section. Attached Figure Description
[0009] The accompanying drawings are provided to further illustrate the embodiments of this application and form part of the specification. They are used together with the following detailed description to explain the embodiments of this application, but do not constitute a limitation on the embodiments of this application. In the drawings: Figure 1 The illustration shows a flowchart of an automatic code generation method according to an embodiment of this application; Figure 2 A schematic diagram illustrating a message definition table for protocol messages according to an embodiment of this application is provided. Figure 3 The illustration shows a flowchart of an automatic code generation method according to a specific embodiment of this application; Figure 4 This schematic diagram illustrates a structural block diagram of an automatic code generation apparatus according to an embodiment of the present application; Figure 5 The illustration shows a schematic diagram of the structure of a computer device according to an embodiment of the present application. Detailed Implementation
[0010] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. It should be understood that the specific embodiments described herein are only for illustration and explanation of the embodiments of this application and are not intended to limit the embodiments of this application. All other embodiments obtained by those skilled in the art based on the embodiments of this application without creative effort are within the scope of protection of this application.
[0011] It should be noted that if the embodiments of this application involve directional indicators (such as up, down, left, right, front, back, etc.), the directional indicators are only used to explain the relative positional relationship and movement of each component in a certain specific posture (as shown in the figure). If the specific posture changes, the directional indicators will also change accordingly.
[0012] Furthermore, if the embodiments of this application involve descriptions such as "first" or "second," these descriptions are for descriptive purposes only and should not be construed as indicating or implying their relative importance or implicitly specifying the number of technical features indicated. Therefore, features defined with "first" or "second" may explicitly or implicitly include at least one of those features. Additionally, the technical solutions of various embodiments can be combined with each other, but this must be based on the ability of those skilled in the art to implement them. If the combination of technical solutions is contradictory or impossible to implement, it should be considered that such a combination of technical solutions does not exist and is not within the scope of protection claimed in this application.
[0013] Figure 1 The illustration shows a flowchart of an automatic code generation method according to an embodiment of this application. Figure 1 As shown in the figure, this application provides a method for automatic code generation, which may include the following steps: S102, acquire the protocol message of the preset instruction transmitted by the engineering equipment and terminal based on the preset communication protocol. The protocol message includes multiple instruction parameters.
[0014] Specifically, engineering equipment can refer to industrial equipment such as tightening machines. A terminal can be a data acquisition terminal. Preset commands refer to commands transmitted between engineering equipment and terminals based on a preset communication protocol. A preset command is a type of message in a communication protocol; a communication protocol may define multiple types of messages, each type being called a preset command. Command parameters refer to parameters for specific functions included in the protocol message. The preset communication protocol can be an Open Protocol. Open Protocols are commonly used for data exchange and control information transmission between industrial equipment such as tightening machines. The protocol defines the data format, command structure, and communication specifications for communication between devices, allowing tightening machines from different manufacturers to communicate according to the same standard. Open Protocols typically define some common commands, each with specific data fields and formats. Tightening machines can achieve functions such as data acquisition and control operations by sending and receiving these commands.
[0015] In the embodiments of this application, the protocol message is a message definition table generated according to a preset format. The first column of the message definition table includes the parameter name of the instruction parameter, the second column includes the byte position of the instruction parameter, and the third column includes the parameter identifier and parameter content of the instruction parameter. Alternatively, the third column may also include the parameter identifier of the instruction parameter or the valid range of the parameter.
[0016] It's understandable that the protocol manual for the pre-defined communication protocol provides detailed definitions of protocol messages for each communication command. Specifically, this includes the parameter name, byte position, parameter content, and corresponding byte length for each command parameter in the protocol message. These protocol message definitions can be understood as key-value pairs, where the key represents the meaning of the protocol message field (its byte length is fixed), and the value is the data corresponding to that field (its parameter content and byte length are variable). Therefore, by using the rules defined in the protocol manual for the pre-defined communication protocol, a message definition table with a pre-defined format can be generated. (Reference) Figure 2The first column of the message definition table includes the parameter name, which can be either Error code, Controller ready status, Tool ready status, or Time. The second column includes the byte position of the parameter. In Open Protocol, the header length of a protocol message is fixed at 20 bytes. The position of the command parameter in the message definition starts from the 21st byte, including byte positions 21-22, 23-26, 27-28, 29, 30-31, 32, 33-34, and 35-53. The third column includes the parameter content (Value) and parameter identifier, with identifiers including 01, 02, 03, and 04. The parameter content includes strings (character data) and numbers, etc. By constructing the message definition table, various command parameters and their contents are integrated, reducing potential errors from manual operation. This application implements a method that reads the message definition table of any preset instruction and automatically generates a parsing function (target command line) for parsing the communication message corresponding to that preset instruction. This method is more efficient than the traditional method of manually writing communication message parsing functions.
[0017] S104, for each instruction parameter, the instruction parameter is parsed based on the first parsing function to generate the first code content corresponding to the instruction parameter according to the parameter name, parameter identifier and the byte position of the parameter identifier included in the instruction parameter.
[0018] As can be understood, the first parsing function is the function that parses the parameter identifiers in the instruction parameters and generates code. The first code content refers to the code corresponding to the parameter identifiers in the instruction parameters. Each piece of first code content contains information such as the parameter name, parameter identifier, and the byte position of the parameter identifier for the corresponding instruction parameter.
[0019] In the embodiments of this application, for each instruction parameter, the first code content includes multiple first parsing function parameters. The multiple first parsing function parameters include a first parameter, a second parameter, a third parameter, a fourth parameter, a fifth parameter, a sixth parameter, and a seventh parameter arranged in order. The first parameter refers to the first buffer where the protocol message parsed by the first parsing function is stored. The second parameter refers to the second buffer where the protocol message needs to be parsed by the first parsing function. The third parameter refers to the parameter name of the instruction parameter. The fourth parameter refers to the parameter identifier of the instruction parameter. The fifth parameter refers to the byte length of the parameter identifier determined according to the byte position of the parameter identifier. The sixth parameter refers to the offset of the protocol message parsed by the first parsing function. The seventh parameter refers to the callback function called when the first parsing function encounters a parsing error.
[0020] In the embodiments of this application, for each instruction parameter, the first code content includes multiple first parsing function parameters. These multiple first parsing function parameters include a first parameter, a second parameter, a third parameter, a fourth parameter, a fifth parameter, a sixth parameter, and a seventh parameter arranged in sequence. The first parameter refers to the first buffer where the parsed data obtained from parsing the communication message to be parsed based on the first parsing code content is stored. The second parameter refers to the second buffer where the communication message to be parsed needs to be parsed based on the first parsing code content is stored. The third parameter refers to the parameter name of the instruction parameter. The fourth parameter refers to the parameter identifier of the instruction parameter. The fifth parameter refers to the byte length of the instruction parameter. The sixth parameter refers to the offset of the communication message to be parsed based on the first parsing code content. The seventh parameter refers to the callback function called when the first parsing code content malfunctions. When called, the callback function is used to determine the parsed data as a preset error message.
[0021] In the embodiments of this application, for each instruction parameter, parsing the instruction parameter based on a first parsing function to generate first code content corresponding to the instruction parameter according to the parameter name, parameter identifier, and byte position of the parameter identifier included in the instruction parameter includes: for each instruction parameter belonging to the first parameter type, parsing the parameter name of the instruction parameter to determine the fields in the parameter name; parsing the parameter identifier of the instruction parameter to obtain the fixed value of the parameter identifier; parsing the byte position of the parameter identifier of the instruction parameter to calculate the byte length of the instruction parameter; constructing the corresponding initial first code content based on the form of the first parsing function, assigning the fields in the parameter name of the instruction parameter to the third parameter in the initial first code content, assigning the fixed value of the parameter identifier to the fourth parameter in the initial first code content, and assigning the byte length of the instruction parameter to the fifth parameter in the initial first code content, to assemble the first code content corresponding to the instruction parameter.
[0022] It can be understood that the first parameter type refers to the parameter type that defines the parameter identifier. The second parameter type refers to the parameter type that defines the parameter content. (Reference) Figure 2 The first column of the message definition table includes the parameter name (Parameter). The first row of parameters belongs to the first parameter type, and the second row belongs to the second parameter type. The first row records the parameter identifier of the error code and the byte position of the instruction parameter, while the second row records the parameter content of the error code and the byte position of the instruction parameter. It can be understood that the parameter content includes the enumerated values of the parameter. In the message definition table, technicians can fill different parameter types and their contents into the table according to a certain order or rule so that the parsing function can parse the data in the table. For example, odd-numbered rows are filled with the content of the first parameter type, and even-numbered rows are filled with the content of the second parameter type. It can be understood that the assembled first code content is part of the program for the preset instruction, which can automatically perform functions such as data acquisition, format conversion, data cleaning, validity verification, and data parsing for communication messages.
[0023] For example, regarding the Error code in the Parameter, the parameter name of the instruction parameter is parsed to determine the fields within that parameter name. Specifically, parsing the parameter name to determine the fields can be done by processing the character group corresponding to the parameter name according to the camelCase naming convention to obtain the processed target string as the field in the parameter name. Then, the parameter identifier of the instruction parameter is parsed to obtain the fixed value 01 of the parameter identifier. The byte position of the instruction parameter is parsed to calculate its byte length 22-21+1=2. Based on the form of the first parsing function, the corresponding initial first code content is constructed. The fields in the parameter name of the instruction parameter are assigned to the third parameter in the initial first code content, the fixed value of the parameter identifier is assigned to the fourth parameter in the initial first code content, and the byte length of the instruction parameter is assigned to the fifth parameter in the initial first code content to assemble the first code content corresponding to the instruction parameter. As shown in processKey(msg, buffer, "errorCode", 1, 2, position, cb).
[0024] In the embodiments of this application, for each instruction parameter, parsing the instruction parameter based on the first parsing function to generate first code content corresponding to the instruction parameter according to the parameter name, parameter identifier, and byte position of the parameter identifier included in the instruction parameter includes: for each instruction parameter, extracting a key value from the buffer corresponding to the first parameter based on the first parsing function; for each instruction parameter, if the key value does not match the preset key value corresponding to the parameter identifier in the fourth parameter, calling the callback function in the seventh parameter and returning a processing failure signal; for each instruction parameter, if the key value matches the preset key value corresponding to the parameter identifier in the fourth parameter, updating the sixth parameter and returning a processing success signal; for each instruction parameter, upon receiving the processing success signal, generating first code content corresponding to the instruction parameter according to the parameter name, parameter identifier, and byte position of the parameter identifier included in the instruction parameter.
[0025] In the embodiments of this application, the operation of executing a preset instruction through a target command line includes: executing the target command line to parse the communication message of the preset instruction of the preset communication protocol and obtain the parsed data of the communication message.
[0026] Specifically, in the embodiments of this application, when the target command line is executed, the steps for executing any first code content include: creating an attribute corresponding to the parameter name for the parsed data based on the parameter name corresponding to the third attribute; confirming the key value position information based on the fifth and sixth parameters, and extracting the key value from the communication message from the second buffer corresponding to the second parameter based on the key value position information; converting the key value into first character data; if the first character data and the preset key value corresponding to the parameter identifier in the fourth parameter do not match successfully, calling the corresponding callback function based on the seventh parameter, and storing the returned preset error information as the parsed data in the first buffer corresponding to the first parameter as the attribute corresponding to the parameter name; if the key value and the preset key value corresponding to the parameter identifier in the fourth parameter match successfully, storing the first character data as the parsed data in the first buffer corresponding to the first parameter as the attribute corresponding to the parameter name; obtaining the end position of the key value in the second buffer based on the addition of the fifth and sixth parameters, and updating the sixth parameter based on the end position.
[0027] Specifically, for each instruction parameter, the structure of the first code content generated based on the first parsing function is fixed. The first code content includes seven first parsing function parameters arranged in sequence. Specifically, the format of the code content generated by the first parsing function `processKey` is `processKey(parameter1, parameter2, parameter3, parameter4, parameter5, parameter6, parameter7)&&`. The first parameter is a fixed field `msg` (message structure), the second parameter is a fixed field `buffer`, the sixth parameter is a fixed field `position`, and the seventh parameter is a fixed field `cb` (callback function). The connector "&&" is used to join the preceding and following code content. (See reference) Figure 2 The third parameter is `parameter`, the fourth parameter is `byte`, and the fifth parameter is `Length`. The logic of the first parsing function `processKey` is as follows: it extracts a key-value pair from the buffer and matches it against the preset key-value pair corresponding to the parameter identifier in the fourth parameter. If the extracted key-value pair does not match the preset key-value pair, the first parsing function will call the callback function `cb` and return `false`, indicating an error occurred during processing. If the key-value pair matches, it updates the position of the key-value pair and returns `true`, indicating successful processing.
[0028] Specifically, the parameters of the first analytic function are as follows: Parameter 1 object: The object to be verified, usually the same as message.
[0029] Parameter 2 buffer: A second buffer containing the information to be verified.
[0030] Parameter 3: The name of the parameter to be verified.
[0031] Parameter 4 bytes: Preset key value.
[0032] Parameter 5 Length: The length of the key value.
[0033] Parameter 6 Position: The starting position of the key value in the buffer.
[0034] Parameter 7 cb: The callback function to be called when an error occurs.
[0035] The analytical process of the first analytic function is as follows: Step 1: Add Position and Length from the buffer to calculate the end position of the key value in the buffer.
[0036] Step 2: Extract key-value pairs: Extract a piece of binary data from the buffer and convert it into a string.
[0037] Step 3: Convert to numbers: Convert the extracted string to numbers and store it in the variable message.
[0038] Step 4, Key-Value Matching: Compare the converted numeric receiver with the preset key. If they are not equal, the extracted key is incorrect. At this point, call the callback function cb, passing a new Error object containing the error message, and the function returns false. If they are equal, the extracted key is correct.
[0039] S106, for each instruction parameter, the instruction parameter is parsed based on the second parsing function to generate the second code content corresponding to the instruction parameter according to the parameter name, parameter content and the byte position of the parameter content.
[0040] As can be understood, the second parsing function is the function that parses the parameter content in the instruction parameters and generates code. The second code content refers to the code corresponding to the parameter content in the instruction parameters. Each piece of first code content contains information such as the parameter name, parameter content, and the byte position of the parameter content for the corresponding instruction parameter.
[0041] In the embodiments of this application, for each instruction parameter, the second code content includes multiple second parsing function parameters. These multiple second parsing function parameters include an eighth parameter, a ninth parameter, a tenth parameter, an eleventh parameter, a twelfth parameter, a thirteenth parameter, and a fourteenth parameter arranged in sequence. The eighth parameter refers to the third buffer where the protocol message parsed by the second parsing function is stored. The ninth parameter refers to the fourth buffer where the protocol message needs to be parsed by the second parsing function. The tenth parameter refers to the parameter name of the instruction parameter. The eleventh parameter refers to the parameter content of the instruction parameter. The twelfth parameter refers to the byte length of the parameter content determined according to the byte position of the parameter content. The thirteenth parameter refers to the offset of the protocol message parsed by the second parsing function. The fourteenth parameter refers to the callback function called when the second parsing function encounters a parsing error.
[0042] In the embodiments of this application, for each instruction parameter, parsing the instruction parameter based on the second parsing function to generate second code content corresponding to the instruction parameter according to the parameter name, parameter content, and byte position of the parameter content includes the following steps: for each instruction parameter, extracting data of a preset byte length from the buffer corresponding to the eighth parameter based on the second parsing function; for each instruction parameter, converting the data of the preset byte length into a corresponding string or number according to the parameter content in the eleventh parameter; for each instruction parameter, updating the thirteenth parameter and returning a signal indicating successful processing if the length of the converted string matches the byte length of the parameter content in the eleventh parameter, or if the converted number is valid; for each instruction parameter, calling the callback function in the fourteenth parameter and returning a signal indicating failed processing if the length of the converted string does not match the byte length of the parameter content in the eleventh parameter, or if the converted number is invalid; and for each instruction parameter, generating second code content corresponding to the instruction parameter according to the parameter name, parameter content, and byte position of the parameter content when a signal indicating successful processing is received.
[0043] In the embodiments of this application, for each instruction parameter, the second code content includes multiple second parsing function parameters. These multiple second parsing function parameters include an eighth parameter, a ninth parameter, a tenth parameter, an eleventh parameter, a twelfth parameter, a thirteenth parameter, and a fourteenth parameter arranged in sequence. The eighth parameter refers to the first buffer where the parsed data obtained from parsing the communication message to be parsed based on the second parsing code content is stored. The ninth parameter refers to the second buffer where the communication message to be parsed needs to be parsed based on the second parsing code content is stored. The tenth parameter refers to the parameter name of the instruction parameter. The eleventh parameter refers to the legal range of the parameter content of the instruction parameter. The twelfth parameter refers to the byte length of the instruction parameter. The thirteenth parameter refers to the offset of the communication message to be parsed based on the second parsing code content. The fourteenth parameter refers to the callback function called when the second parsing function encounters a parsing error. When the callback function is called, it is used to determine the parsed data as a preset error message.
[0044] In the embodiments of this application, for each instruction parameter, parsing the instruction parameter based on the second parsing function to generate second code content corresponding to the instruction parameter according to the parameter name, parameter content, and byte position of the parameter content includes: for instruction parameters belonging to the second parameter type, parsing the parameter name of the instruction parameter to determine the fields in the parameter name; parsing the parameter content of the instruction parameter to obtain the data type and / or enumeration value information of the parameter content; parsing the byte position of the instruction parameter to calculate the byte length of the instruction parameter; constructing the corresponding initial second code content based on the form of the second parsing function, assigning the fields in the parameter name of the instruction parameter to the tenth parameter in the initial second code content, assigning the data type and / or enumeration value information of the parameter content to the eleventh parameter in the initial second code content, and assigning the byte length of the instruction parameter to the twelfth parameter in the initial second code content, to assemble the second code content corresponding to the instruction parameter.
[0045] It can be understood that the assembled second code content is a part of the target command line program, which can automatically perform functions such as data acquisition, format conversion, data cleaning, validity verification, and data parsing for communication messages. The first and second code contents are the complete code contents of their corresponding instruction parameters. The combination of all the first and second code contents constitutes the complete code content for parsing the preset instruction.
[0046] For example, regarding the Error code in the Parameter, the parameter name is parsed to determine the fields within that parameter name. Specifically, parsing the parameter name to determine the fields can be done by processing the character set corresponding to the parameter name using the camelCase naming convention, resulting in a processed target string that serves as the field in the parameter name. Then, the parameter content is parsed to obtain the data type of the parameter content. For instance, reading the first string "string" in the value indicates the data type of the command parameter.
[0047] Parse the byte position of the instruction parameter to calculate the byte length 26 - 23 + 1 = 4. Construct the corresponding initial second code content based on the form of the second parsing function. Assign the fields from the instruction parameter's parameter name to the tenth parameter of the initial second code content, assign the data type of the parameter content to the eleventh parameter of the initial second code content, and assign the byte length to the twelfth parameter of the initial second code content to assemble the second code content corresponding to the instruction parameter. As shown in `processParser(msg, buffer, "errorCode", "string", 4, position, cb)`.
[0048] In the embodiments of this application, the first parameter in each first code content of the target command line and the eighth parameter in each second code content of the command line are the same global variable; the second parameter in each first code content of the target command line and the ninth parameter in each second code content of the command line are the same global variable; the sixth parameter in each first code content of the target command line and the thirteenth parameter in each second code content of the command line are the same global variable; and the initial value of this global variable when the target command line is executed is the byte position of the indicator parameter corresponding to the first code content in the target command line. In this embodiment, by ensuring the consistency of global variables between the first and second code contents in the target command line, and through a carefully designed parameter structure, accurate matching and efficient execution of code contents can be achieved, thereby ensuring that communication messages with a preset instruction format in the second buffer can be correctly identified, parsed, and stored in the first buffer.
[0049] In the embodiments of this application, when the target command line is executed, the steps of executing any second code content include: creating an attribute corresponding to the parameter name corresponding to the tenth attribute for the parsed data; confirming the data position information based on the twelfth and thirteenth parameters, and extracting the data to be parsed from the communication message in the second buffer corresponding to the eighth parameter based on the data position information; determining whether the data to be parsed is valid according to the valid range of the parameter content in the eleventh parameter; if the data to be parsed is invalid, calling the corresponding callback function based on the fourteenth parameter, and storing the returned preset error information as the attribute of the parsed data corresponding to the parameter name in the first buffer corresponding to the ninth parameter; if the data to be parsed is valid, storing the processed data to be parsed as the attribute of the parsed data corresponding to the parameter name in the first buffer corresponding to the ninth parameter; obtaining the end position of the data to be parsed in the second buffer by adding the twelfth and thirteenth parameters, and updating the thirteenth parameter based on the end position.
[0050] In the embodiments of this application, determining whether the data to be parsed is valid based on the valid range of the parameter content in the eleventh parameter includes: if the data type in the eleventh parameter is a string, the data to be parsed is determined to be valid, and the string format data to be parsed after removing whitespace characters is used as the processed data to be parsed; if the data type in the eleventh parameter is a raw string, the data to be parsed is converted into a string format data to be verified while retaining whitespace characters, and if the length of the data to be verified is the same as the byte length corresponding to the twelfth parameter, the data to be parsed is confirmed to be valid, and the data to be verified is used as the processed data to be parsed; otherwise, the data to be parsed is determined to be invalid; if the data type in the eleventh parameter is a number, the data to be parsed is converted into the corresponding number format data, and if the number format data is a valid number, the data to be parsed is determined to be valid, and the number format data is used as the processed data to be parsed; otherwise, the data to be parsed is determined to be invalid; if the data type in the eleventh parameter is an invalid type, the data to be parsed is determined to be invalid.
[0051] Specifically, for each instruction parameter, the structure of the second code content generated by the second parsing function is fixed. The second code content includes seven second parsing function parameters arranged in sequence. Specifically, the format of the code content generated by the second parsing function `processParser` is `processParser(parameter8, parameter9, parameter10, parameter11, parameter12, parameter13, parameter14)&&`. The eighth parameter is a fixed field `msg`, the ninth parameter is a fixed field `buffer`, the thirteenth parameter is a fixed field `position`, the fourteenth parameter is a fixed field `cb`, and the connector "&&" is used to connect the preceding and following code content. (See reference) Figure 2 The eighth parameter is `parameter`, the ninth parameter is `parameterType` (parameter type), and the tenth parameter is `parameterLength` (parameter length). The second parsing function `processKey` extracts data from the buffer, converts the extracted data to the specified data type, and adds it to the message's corresponding payload. If an error occurs during processing, the first parsing function will call the callback function `cb` and return `false`, indicating an error occurred. If processing is successful, it returns `true`, indicating successful processing.
[0052] Specifically, the parameters of the second analytical function are as follows: Parameter 1 message: The object to be updated.
[0053] Parameter 2 buffer: The fourth buffer containing the data to be extracted.
[0054] Parameter 3: The name of the parameter to be extracted.
[0055] Parameter 4 parameterType: The data type to be extracted, which can be "string", "rawString" or "number".
[0056] Parameter 5 parameterLength: The length of the information to be extracted.
[0057] Parameter 6 Position: The object at the current processing position in the buffer, which includes the value property.
[0058] Parameter 7 cb: The callback function to be called when an error occurs.
[0059] The analytical process of the second analytic function is as follows: Step 1: Initialize the length: Assign parameterLength to length, and then calculate the end position of the extracted data, that is, extract the data of the preset byte length.
[0060] Step 2, Type determination: Based on the value of parameterType, use a switch statement to select different processing logic.
[0061] For parameters of type "string", the data in the fourth buffer is converted to a string, leading and trailing whitespace characters are removed, and then stored in message.payload[parameter].
[0062] For parameters of type "rawString", the data in the fourth buffer is converted to a string without removing whitespace, and the string length is checked against the length. If they do not match, cb is called and returns false.
[0063] If the parameter is of data type "rnumber", the data in the fourth buffer is converted to a number and checked for validity. If invalid, cb is called and returns false.
[0064] Step 3, Error Handling: If parameterType is not a valid type, call cb and return false.
[0065] Step 4: Update position: In any case, after processing is complete, the position will be updated to the new processing position.
[0066] Step 5: Return result: If the processing is successful, the function returns true.
[0067] In the embodiments of this application, for each third or tenth parameter, the character group corresponding to the parameter name is processed according to the camelCase naming rule to obtain the processed target string.
[0068] refer to Figure 2 After parsing the parameter names, the resulting fields are processed using camelCase naming conventions. The characters in the Parameter column are combined into a string, corresponding to the third parameter in the first code content, resulting in the tenth parameter in the code content. For example, the first parameter of Parameter, Error Code, is parsed as "errorCode".
[0069] S108, Generate a target command line corresponding to the preset instruction based on all the first code content and the second code content, so as to execute the operation of the preset instruction through the target command line.
[0070] The target command line refers to the code content generated after parsing and assembling preset instructions using a first parsing function and a second parsing function. Specifically, Open Protocol is a general communication protocol used for data exchange and control between industrial equipment such as tightening machines. Protocol communication messages strictly adhere to the protocol message definition table specifications, and parsing these communication messages is a crucial part of data acquisition. The target command line generated by the above scheme can be embedded into other code frameworks. This code can be the core data parsing part of the data acquisition program, primarily parsing the protocol message definition table of the instruction codes defined in the communication protocol to obtain the interface call code for field parsing. In practical use, embedding the generated code segment into other frameworks allows the system to take a received communication message data packet as an input parameter and, based on the target command line corresponding to the instruction code, parse the parameter fields in the communication message to obtain the data for the corresponding fields.
[0071] In the embodiments of this application, generating a target command line corresponding to a preset instruction based on all the first code content and the second code content includes: obtaining the byte position corresponding to each instruction parameter; and sequentially concatenating all the first code content and the second code content using a connector based on the byte position to assemble a target command line corresponding to the preset instruction.
[0072] It can be understood that each instruction parameter includes its byte position, which reflects the order of the various instruction parameters. Based on this order, all the first and second code contents can be sequentially connected using a connector to assemble a target command line corresponding to the preset instruction. When the target command line executes the preset instruction, any first code content and any second code content execute the preset instruction on the communication message in sequence, which can automatically perform functions such as data acquisition, format conversion, data cleaning, validity verification, and data parsing on the communication message.
[0073] Figure 3 The illustration shows a flowchart of an automatic code generation method according to an embodiment of this application. Figure 3 As shown in the embodiments of this application, for Figure 2 The message definition table of the protocol message shown, and the automatic code generation method include the following steps: Step 1: Read the message definition table of the protocol message.
[0074] Step 2: Confirm whether the message definition table file is normal. If the file is normal, proceed to Step 3; otherwise, report an error and exit.
[0075] Step 3: Read the first row of data in the message definition table to obtain the instruction code MID 0071 in the Open Protocol.
[0076] Step 4: Initialize i=0, read the instruction parameters in the i-th row of the message table, and obtain the Parameter, Byte, and Value of the i-th row.
[0077] Step 5: Parse the instruction parameters using the processKey function. Parse the parameter name in the Parameter column according to the processKey function; parse the Byte to calculate the byte length of the parameter identifier, such as 22-21+1=2 if it is 21-22; parse the Value to get the parameter content, such as converting the value 01 to the integer 1.
[0078] Step 6: Read the parameters in the (i+1)th row of the message definition table and obtain the Parameter, Byte, and Value of the i-th row.
[0079] Step 7: Parse the instruction parameters using the processParser function, and parse the parameter name in the Parameter column according to the processKey function; parse the Byte to calculate the byte length of the parameter content, such as 26-23+1=4 for 23-26; parse the Value to obtain the parameter content, and read the first string "string" in the value, which indicates the parameter type.
[0080] Step 8: Let i = i + 2, and check if there are instruction parameters in line i + 2. If not, it can be assumed that all instruction parameters in the message definition table have been read, and proceed to step 9. If they exist, it is necessary to obtain the Parameter, Byte, and Value of line i + 2, and then proceed to step 5 to parse the instruction parameters using the processKey function.
[0081] Step 9: Assemble the code content parsed by the processKey and processParser functions according to the format, and input it into the target command line.
[0082] Specifically, Figure 2 The target command line generated by the message table shown is as follows: processKey(msg, buffer, "errorCode", 1, 2, position, cb)&& processParser(msg, buffer, "errorCode", "string", 4, position, cb)&& processKey(msg, buffer, "controllerReadystatus", 2, 2, position, cb)&& processParser(msg, buffer, "controllerReadyStatus", "number", 1, position, cb)&& processKey(msg, buffer, "toolReadystatus", 3, 2, position, cb)&& processParser(msg, buffer, "toolReadyStatus", "number", 1, position, cb)&& processKey(msg, buffer, "timeStamp", 4, 2, position, cb)&& processParser(msg, buffer, "timeStamp", "string", 19, position, cb)&& The above technical solution involves acquiring protocol messages of preset instructions transmitted by engineering equipment and terminals based on a preset communication protocol. These protocol messages include multiple instruction parameters. For each instruction parameter, a first parsing function is used to parse the parameter, generating first code content corresponding to the parameter based on its name, identifier, and byte position. For each instruction parameter, a second parsing function is used to parse it, generating second code content corresponding to the parameter based on its name, content, and byte position. A target command line corresponding to the preset instruction is generated based on all the first and second code contents, allowing the preset instruction to be executed via the target command line. This solution significantly reduces coding time, minimizes repetitive work, and reduces errors that may result from manual operation.
[0083] Figure 1 This is a flowchart illustrating the code automatic generation method in one embodiment. It should be understood that, although... Figure 1 The steps in the flowchart are shown sequentially as indicated by the arrows, but these steps are not necessarily executed in the order indicated by the arrows. Unless otherwise specified herein, there is no strict order in which these steps are executed, and they can be performed in other orders. Figure 1 At least some of the steps in the process may include multiple sub-steps or multiple stages. These sub-steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these sub-steps or stages is not necessarily sequential, but can be executed in turn or alternately with other steps or at least some of the sub-steps or stages of other steps.
[0084] Figure 4 A schematic block diagram illustrating an automatic code generation apparatus according to an embodiment of this application is shown. Figure 4 As shown in the figure, this application provides a code automatic generation device, which may include: Memory 410 is configured to store instructions; Processor 420 is configured to retrieve instructions from memory 410 and to implement the aforementioned automatic code generation method when executing instructions.
[0085] Specifically, in this embodiment of the application, the processor 420 can be configured to: The system acquires protocol messages of preset instructions transmitted by engineering equipment and terminals based on a preset communication protocol. These protocol messages include multiple instruction parameters. For each instruction parameter, a first parsing function is used to parse the parameter, generating first code content corresponding to the parameter based on the parameter name, parameter identifier, and the byte position of the parameter identifier. For each instruction parameter, a second parsing function is used to parse the parameter, generating second code content corresponding to the parameter based on the parameter name, parameter content, and the byte position of the parameter content. A target command line corresponding to the preset instruction is generated based on all the first and second code contents, and the preset instruction is executed through the target command line.
[0086] In the embodiments of this application, for each instruction parameter, the first code content includes multiple first parsing function parameters. The multiple first parsing function parameters include a first parameter, a second parameter, a third parameter, a fourth parameter, a fifth parameter, a sixth parameter, and a seventh parameter arranged in order. The first parameter refers to the first buffer where the protocol message parsed by the first parsing function is stored. The second parameter refers to the second buffer where the protocol message needs to be parsed by the first parsing function. The third parameter refers to the parameter name of the instruction parameter. The fourth parameter refers to the parameter identifier of the instruction parameter. The fifth parameter refers to the byte length of the parameter identifier determined according to the byte position of the parameter identifier. The sixth parameter refers to the offset of the protocol message parsed by the first parsing function. The seventh parameter refers to the callback function called when the first parsing function encounters a parsing error.
[0087] In embodiments of this application, the processor 420 may also be configured to: For each instruction parameter, the first code content includes multiple first parsing function parameters, which are arranged in the following order: first parameter, second parameter, third parameter, fourth parameter, fifth parameter, sixth parameter, and seventh parameter. The first parameter refers to the first buffer where the parsed data obtained from parsing the communication message to be parsed based on the first parsing code content is stored. The second parameter refers to the second buffer where the communication message to be parsed needs to be parsed based on the first parsing code content is stored. The third parameter is the parameter name of the instruction parameter. The fourth parameter is the parameter identifier of the instruction parameter. The fifth parameter is the byte length of the instruction parameter. The sixth parameter is the offset of the communication message to be parsed based on the first parsing code content. The seventh parameter is the callback function called when the first parsing code content encounters a parsing error. When called, the callback function is used to determine the parsed data as a preset error message.
[0088] In embodiments of this application, the processor 420 may also be configured to: For each instruction parameter, the instruction parameter is parsed based on the first parsing function to generate the first code content corresponding to the instruction parameter according to the parameter name, parameter identifier, and byte position of the parameter identifier. This includes: for each instruction parameter belonging to the first parameter type, parsing the parameter name of the instruction parameter to determine the fields in the parameter name; parsing the parameter identifier of the instruction parameter to obtain the fixed value of the parameter identifier; parsing the byte position of the instruction parameter to calculate the byte length of the instruction parameter; constructing the corresponding initial first code content based on the form of the first parsing function, assigning the fields in the parameter name of the instruction parameter to the third parameter in the initial first code content, assigning the fixed value of the parameter identifier to the fourth parameter in the initial first code content, and assigning the byte length of the instruction parameter to the fifth parameter in the initial first code content, to assemble the first code content corresponding to the instruction parameter.
[0089] In embodiments of this application, the processor 420 may also be configured to: For each instruction parameter, there is a second code content. This second code content includes multiple second parsing function parameters, which are arranged in the following order: eighth parameter, ninth parameter, tenth parameter, eleventh parameter, twelfth parameter, thirteenth parameter, and fourteenth parameter. The eighth parameter refers to the first buffer where the parsed data obtained from parsing the communication message to be parsed based on the second parsing code content is stored. The ninth parameter refers to the second buffer where the communication message to be parsed based on the second parsing code content is stored. The tenth parameter refers to the parameter name of the instruction parameter. The eleventh parameter refers to the valid range of the instruction parameter content. The twelfth parameter refers to the byte length of the instruction parameter. The thirteenth parameter refers to the offset of the communication message to be parsed based on the second parsing code content. The fourteenth parameter refers to the callback function called when the second parsing function encounters a parsing error. When the callback function is called, it is used to determine the parsed data as a preset error message.
[0090] In embodiments of this application, the processor 420 may also be configured to: For each instruction parameter, the instruction parameter is parsed based on the second parsing function to generate second code content corresponding to the instruction parameter according to the parameter name, parameter content, and byte position of the parameter content. This includes: for instruction parameters of the second parameter type, parsing the parameter name of the instruction parameter to determine the fields in the parameter name; parsing the parameter content of the instruction parameter to obtain the data type and / or enumeration value information of the parameter content; parsing the byte position of the instruction parameter to calculate the byte length of the instruction parameter; constructing the corresponding initial second code content based on the form of the second parsing function, assigning the fields in the parameter name of the instruction parameter to the tenth parameter in the initial second code content, assigning the data type and / or enumeration value information of the parameter content to the eleventh parameter in the initial second code content, and assigning the byte length of the instruction parameter to the twelfth parameter in the initial second code content, to assemble the second code content corresponding to the instruction parameter.
[0091] In embodiments of this application, the processor 420 may also be configured to: The first parameter in each first code content of the target command line and the eighth parameter in each second code content of the command line are the same global variable. The second parameter in each first code content of the target command line and the ninth parameter in each second code content of the command line are the same global variable. The sixth parameter in each first code content of the target command line and the thirteenth parameter in each second code content of the command line are the same global variable. The initial value of this global variable when the target command line is executed is the byte position of the indicator parameter corresponding to the first code content in the target command line.
[0092] In embodiments of this application, the processor 420 may also be configured to: The operation of executing preset instructions through the target command line includes: executing the target command line to parse the communication message of the preset instruction of the preset communication protocol and obtain the parsed data of the communication message.
[0093] In embodiments of this application, the processor 420 may also be configured to: When the target command line is executed, the steps performed by any of the first code contents it contains include: creating an attribute corresponding to the parameter name for the parsed data based on the parameter name corresponding to the third attribute; confirming the key value position information based on the fifth and sixth parameters, and extracting the key value from the communication message in the second buffer corresponding to the second parameter based on the key value position information; converting the key value into first character data; if the first character data does not match the preset key value corresponding to the parameter identifier in the fourth parameter, calling the corresponding callback function based on the seventh parameter, and storing the returned preset error information as the attribute of the parsed data corresponding to the parameter name in the first buffer corresponding to the first parameter; if the key value matches the preset key value corresponding to the parameter identifier in the fourth parameter, storing the first character data as the attribute of the parsed data corresponding to the parameter name in the first buffer corresponding to the first parameter; obtaining the end position of the key value in the second buffer by adding the fifth and sixth parameters, and updating the sixth parameter based on the end position. The parameters; when the target command line is executed, the steps performed by any of the second code contents it contains include: creating an attribute corresponding to the parameter name for the parsed data based on the parameter name corresponding to the tenth attribute; confirming the data position information based on the twelfth and thirteenth parameters, and extracting the data to be parsed from the communication message in the second buffer corresponding to the eighth parameter based on the data position information; determining whether the data to be parsed is valid according to the valid range of the parameter content in the eleventh parameter; if the data to be parsed is invalid, calling the corresponding callback function based on the fourteenth parameter, and storing the returned preset error information as the attribute of the parsed data corresponding to the parameter name in the first buffer corresponding to the ninth parameter; if the data to be parsed is valid, storing the processed data to be parsed as the attribute of the parsed data corresponding to the parameter name in the first buffer corresponding to the ninth parameter; obtaining the end position of the data to be parsed in the second buffer by adding the twelfth and thirteenth parameters, and updating the thirteenth parameter based on the end position.
[0094] In embodiments of this application, the processor 420 may also be configured to: Based on the valid range of the parameters in parameter eleven, determine whether the data to be parsed is valid, including: if the data type of parameter eleven is string, the data to be parsed is valid, and the string formatted data after removing whitespace characters is used as the processed data to be parsed; if the data type of parameter eleven is raw string, convert the data to be parsed into a string format that retains whitespace characters, and if the length of the data to be parsed is the same as the byte length corresponding to parameter twelf, confirm that the data to be parsed is valid, and use the data to be parsed as the processed data to be parsed; otherwise, determine that the data to be parsed is invalid; if the data type of parameter eleven is number, convert the data to be parsed into the corresponding number format, and if the number format data is a valid number, determine that the data to be parsed is valid, and use the number format data as the processed data to be parsed; otherwise, determine that the data to be parsed is invalid; if the data type of parameter eleven is not a valid type, determine that the data to be parsed is invalid.
[0095] In embodiments of this application, the processor 420 may also be configured to: Parse the parameter name of the instruction to determine the fields in the parameter name, including: processing the character group corresponding to the parameter name according to the camelCase naming rule to obtain the processed target string as the field in the parameter name.
[0096] In embodiments of this application, the processor 420 may also be configured to: For each instruction parameter, a key value is extracted from the buffer corresponding to the first parameter based on the first parsing function; for each instruction parameter, if the key value does not match the preset key value corresponding to the parameter identifier in the fourth parameter, the callback function in the seventh parameter is called and a processing failure signal is returned; for each instruction parameter, if the key value matches the preset key value corresponding to the parameter identifier in the fourth parameter, the sixth parameter is updated and a processing success signal is returned; for each instruction parameter, upon receiving a processing success signal, the first code content corresponding to the instruction parameter is generated based on the parameter name, parameter identifier, and the byte position of the parameter identifier included in the instruction parameter.
[0097] In the embodiments of this application, for each instruction parameter, the second code content includes multiple second parsing function parameters. These multiple second parsing function parameters include an eighth parameter, a ninth parameter, a tenth parameter, an eleventh parameter, a twelfth parameter, a thirteenth parameter, and a fourteenth parameter arranged in sequence. The eighth parameter refers to the third buffer where the protocol message parsed by the second parsing function is stored. The ninth parameter refers to the fourth buffer where the protocol message needs to be parsed by the second parsing function. The tenth parameter refers to the parameter name of the instruction parameter. The eleventh parameter refers to the parameter content of the instruction parameter. The twelfth parameter refers to the byte length of the parameter content determined according to the byte position of the parameter content. The thirteenth parameter refers to the offset of the protocol message parsed by the second parsing function. The fourteenth parameter refers to the callback function called when the second parsing function encounters a parsing error.
[0098] In embodiments of this application, the processor 420 may also be configured to: For each instruction parameter, data of a preset byte length is extracted from the buffer corresponding to the eighth parameter based on the second parsing function; for each instruction parameter, the data of the preset byte length is converted into a corresponding string or number according to the parameter content in the eleventh parameter; for each instruction parameter, if the length of the converted string matches the byte length of the parameter content in the eleventh parameter, or the converted number is valid, the thirteenth parameter is updated, and a signal indicating successful processing is returned; for each instruction parameter, if the length of the converted string does not match the byte length of the parameter content in the eleventh parameter, or the converted number is invalid, the callback function in the fourteenth parameter is called, and a signal indicating processing failure is returned; for each instruction parameter, upon receiving a signal indicating successful processing, a second code content corresponding to the instruction parameter is generated based on the parameter name, parameter content, and byte position of the parameter content included in the instruction parameter.
[0099] In the embodiments of this application, for each third or tenth parameter, the character group corresponding to the parameter name is processed according to the camelCase naming rule to obtain the processed target string.
[0100] In embodiments of this application, the processor 420 may also be configured to: Obtain the byte position corresponding to each instruction parameter; based on the byte position, connect all the first code content and the second code content sequentially using a connector to assemble them into a target command line corresponding to the preset instruction.
[0101] In the embodiments of this application, the protocol message is a message definition table generated according to a preset format. The first column of the message definition table includes the parameter name of the instruction parameter, the second column of the message definition table includes the byte position of the instruction parameter, and the third column of the message definition table includes the parameter identifier and parameter content of the instruction parameter.
[0102] The above technical solution provides a method for automatically generating data acquisition program code based on the message definition table of the Open Protocol. The communication protocol manual provides detailed descriptions of the message definition for each communication command, including the name, location, and content length of each parameter in the message. These message definitions can be understood as key-value pairs, where the key represents the meaning of the message field and has a fixed length, while the value is the corresponding data, and its length is variable. By parsing the message definition table in the protocol manual, the corresponding code segment for message parsing can be generated, significantly saving developers' coding time, reducing repetitive work, and minimizing errors that may result from manual operation. Developers can then focus more on implementing business logic, thereby accelerating the development cycle.
[0103] The code obtained through parsing functions can be embedded into other code frameworks. This code is a core part of the data parsing process in the data acquisition program. Its main function is to parse the protocol message definition table of the instruction codes defined in the protocol to obtain the interface call code for field parsing. In practical use, embedding the generated code segment into other frameworks allows a received communication message data packet to be used as an input parameter. Based on the code segment corresponding to the instruction code, the parameter fields in the communication message are parsed to obtain the data of the corresponding fields, thereby acquiring the data information transmitted by industrial equipment such as tightening machines.
[0104] This application also provides a machine-readable storage medium storing instructions that cause a machine to execute the above-described automatic code generation method.
[0105] In one embodiment, a computer device is provided, which may be a server, and its internal structure diagram may be as follows: Figure 5As shown. The computer device includes a processor A01, a network interface A02, memory (not shown), and a database (not shown) connected via a system bus. The processor A01 provides computing and control capabilities. The memory includes internal memory A03 and a non-volatile storage medium A04. The non-volatile storage medium A04 stores an operating system B01, a computer program B02, and a database (not shown). The internal memory A03 provides an environment for the operation of the operating system B01 and the computer program B02 stored in the non-volatile storage medium A04. The database stores data for automatic code generation. The network interface A02 communicates with external terminals via a network connection. When the computer program B02 is executed by the processor A01, it implements an automatic code generation method.
[0106] Those skilled in the art will understand that Figure 5 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.
[0107] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0108] This application is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart... Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0109] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0110] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0111] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.
[0112] Memory may include non-persistent memory in computer-readable media, such as random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM. Memory is an example of computer-readable media.
[0113] Computer-readable media includes both permanent and non-permanent, removable and non-removable media that can store information using any method or technology. Information can be computer-readable instructions, data structures, modules of programs, or other data. Examples of computer storage media include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, CD-ROM, digital versatile optical disc (DVD) or other optical storage, magnetic tape, magnetic disk storage or other magnetic storage devices, or any other non-transferable medium that can be used to store information accessible by a computing device. As defined herein, computer-readable media does not include transient computer-readable media, such as modulated data signals and carrier waves.
[0114] It should also be noted that the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article, or apparatus. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes that element.
[0115] The above are merely embodiments of this application and are not intended to limit the scope of this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the scope of the claims of this application.
Claims
1. A method for automatic code generation, characterized in that, The method includes: Obtain a message definition table of preset instructions from engineering equipment and terminals based on a preset communication protocol, wherein the message definition table includes multiple instruction parameters; For each instruction parameter, the instruction parameter is parsed based on the first parsing function to generate first code content corresponding to the instruction parameter according to the parameter name, parameter identifier and the byte position of the parameter identifier included in the instruction parameter; For each instruction parameter, the instruction parameter is parsed based on the second parsing function to generate second code content corresponding to the instruction parameter according to the parameter name, parameter content and the byte position of the parameter content. Generate a target command line corresponding to the preset instruction based on all the first code content and the second code content, so as to execute the operation of the preset instruction through the target command line.
2. The code automatic generation method according to claim 1, characterized in that, For each instruction parameter, the first code content includes multiple first parsing function parameters, which are arranged in the following order: first parameter, second parameter, third parameter, fourth parameter, fifth parameter, sixth parameter, and seventh parameter. The first parameter refers to the first buffer storing the parsed data obtained from parsing the communication message to be parsed based on the first parsing code content. The second parameter refers to the second buffer storing the communication message to be parsed based on the first parsing code content. The third parameter is the parameter name of the instruction parameter. The fourth parameter is the parameter identifier of the instruction parameter. The fifth parameter is the byte length of the instruction parameter. The sixth parameter is the offset of the communication message to be parsed after parsing based on the first parsing code content. The seventh parameter is the callback function called when the first parsing code content malfunctions. When called, the callback function is used to determine the parsed data as a preset error message.
3. The code automatic generation method according to claim 2, characterized in that, The step of parsing each instruction parameter based on a first parsing function to generate first code content corresponding to the instruction parameter according to the parameter name, parameter identifier, and byte position of the parameter identifier includes: For each instruction parameter belonging to the first parameter type, parse the parameter name of the instruction parameter to determine the fields in the parameter name; Parse the parameter identifier of the instruction parameter to obtain the fixed value of the parameter identifier; Parse the byte position of the instruction parameter to calculate its byte length; The corresponding initial first code content is constructed based on the form of the first parsing function. The field in the parameter name of the instruction parameter is assigned to the third parameter in the initial first code content, the fixed value of the parameter identifier is assigned to the fourth parameter in the initial first code content, and the byte length of the instruction parameter is assigned to the fifth parameter in the initial first code content, so as to assemble the first code content corresponding to the instruction parameter.
4. The code automatic generation method according to claim 1, characterized in that, For each instruction parameter, the second code content includes multiple second parsing function parameters, which are arranged in the following order: eighth parameter, ninth parameter, tenth parameter, eleventh parameter, twelfth parameter, thirteenth parameter, and fourteenth parameter. The eighth parameter refers to the first buffer storing the parsed data obtained from parsing the communication message to be parsed based on the second parsing code content. The ninth parameter refers to the second buffer storing the communication message to be parsed based on the second parsing code content. The tenth parameter is the parameter name of the instruction parameter. The eleventh parameter is the valid range of the instruction parameter content. The twelfth parameter is the byte length of the instruction parameter. The thirteenth parameter is the offset of the communication message to be parsed after parsing based on the second parsing code content. The fourteenth parameter is the callback function called when the second parsing function encounters a parsing error. When called, the callback function is used to determine the parsed data as a preset error message.
5. The code automatic generation method according to claim 4, characterized in that, The step of parsing each instruction parameter using a second parsing function to generate second code content corresponding to the instruction parameter based on the parameter name, parameter content, and byte position of the parameter content includes: For instruction parameters belonging to the second parameter type, parse the parameter name of the instruction parameter to determine the fields in the parameter name; Parse the parameter content of the instruction to obtain the data type and / or enumeration value information of the parameter content; The byte position of the instruction parameter is parsed to calculate the byte length of the instruction parameter; The corresponding initial second code content is constructed based on the form of the second parsing function. The field in the parameter name of the instruction parameter is assigned to the tenth parameter in the initial second code content, the data type and / or enumeration value information of the parameter content is assigned to the eleventh parameter in the initial second code content, and the byte length of the instruction parameter is assigned to the twelfth parameter in the initial second code content, so as to assemble the second code content corresponding to the instruction parameter.
6. The code automatic generation method according to claim 2 or 4, characterized in that, The first parameter in each first code content of the target command line and the eighth parameter in each second code content of the command line are the same global variable. The second parameter in each first code content of the target command line and the ninth parameter in each second code content of the command line are the same global variable. The sixth parameter in each first code content of the target command line and the thirteenth parameter in each second code content of the command line are the same global variable. The initial value of the global variable when the target command line is executed is the byte position of the indicator parameter corresponding to the first code content in the target command line.
7. The code automatic generation method according to claim 1, characterized in that, The operation of executing the preset instruction through the target command line includes: Execute the target command line to parse the communication message of the preset instruction of the preset communication protocol and obtain the parsed data of the communication message.
8. The code automatic generation method according to claim 7, characterized in that, When the target command line is executed, the steps for executing any of the first code contents contained therein include: Based on the parameter name corresponding to the third attribute, create an attribute corresponding to the parameter name for the parsed data; Based on the fifth and sixth parameters, the key value location information is confirmed, and the key value in the communication message is extracted from the second buffer corresponding to the second parameter based on the key value location information. Convert the key value into first character data; If the first character data and the preset key value corresponding to the parameter identifier in the fourth parameter do not match successfully, the corresponding callback function is called based on the seventh parameter, and the returned preset error information is stored as the attribute of the parsed data corresponding to the parameter name in the first buffer corresponding to the first parameter. If the key value matches the preset key value corresponding to the parameter identifier in the fourth parameter, the first character data is stored as the attribute of the parsed data corresponding to the parameter name in the first buffer corresponding to the first parameter. The end position of the key value in the second buffer is obtained by adding the fifth parameter and the sixth parameter, and the sixth parameter is updated based on the end position. When the target command line is executed, the steps for executing any of the second code contents contained therein include: Based on the parameter name corresponding to the tenth attribute, create an attribute corresponding to that parameter name for the parsed data; Based on the twelfth and thirteenth parameters, the data location information is confirmed, and the data to be parsed in the communication message is extracted from the second buffer corresponding to the eighth parameter based on the data location information. Based on the valid range of the parameter content in the eleventh parameter, determine whether the data to be parsed is valid; If the data to be parsed is invalid, the corresponding callback function is called based on the fourteenth parameter, and the returned preset error information is stored as the attribute of the parsed data corresponding to the parameter name in the first buffer corresponding to the ninth parameter. If the data to be parsed is valid, the processed data to be parsed will be stored as the attribute of the parsed data corresponding to the parameter name in the first buffer corresponding to the ninth parameter. The end position of the data to be parsed in the second buffer is obtained by adding the twelfth parameter and the thirteenth parameter, and the thirteenth parameter is updated based on the end position.
9. The code automatic generation method according to claim 8, characterized in that, The step of determining whether the data to be parsed is valid based on the valid range of the parameter content in the eleventh parameter includes: If the data type in the eleventh parameter is a string, the data to be parsed is determined to be valid, and the data to be parsed in string format after removing whitespace characters is taken as the processed data to be parsed. If the data type in the eleventh parameter is a raw string, the data to be parsed is converted into a string format that retains whitespace characters. If the length of the data to be parsed is the same as the byte length corresponding to the twelfth parameter, the data to be parsed is confirmed to be valid, and the data to be parsed is used as the processed data to be parsed. Otherwise, the data to be parsed is determined to be invalid. If the data type in the eleventh parameter is a number, the data to be parsed is converted into the corresponding number format data. If the number format data is a valid number, the data to be parsed is determined to be valid, and the number format data is used as the processed data to be parsed. Otherwise, the data to be parsed is determined to be invalid. If the data type in the eleventh parameter is invalid, the data to be parsed is determined to be invalid.
10. The code automatic generation method according to claim 3 or 5, characterized in that, The process of parsing the parameter name of the instruction parameter to determine the fields in the parameter name includes: The character set corresponding to the parameter name is processed according to the camelCase naming convention to obtain the processed target string as a field in the parameter name.
11. The code automatic generation method according to claim 1, characterized in that, The step of generating the target command line corresponding to the preset instruction based on all the first code content and the second code content includes: Get the byte position corresponding to each instruction parameter; The order of each instruction parameter is determined based on the byte position; Based on the arrangement order, the first code content or the second code content corresponding to each instruction parameter is connected sequentially using a connector to assemble a target command line corresponding to the preset instruction.
12. The code automatic generation method according to claim 1, characterized in that, The first column of the message definition table includes the parameter name of the instruction parameter, the second column of the message definition table includes the byte position of the instruction parameter, and the third column of the message definition table includes the parameter identifier or the legal range of the parameter content of the instruction parameter.
13. A code automatic generation device, characterized in that, include: The memory is configured to store instructions; A processor configured to retrieve the instructions from the memory and, when executing the instructions, to implement the automatic code generation method according to any one of claims 1 to 12.
