Method for translating CIR message structure
By introducing a common message analysis tool module and Java reflection mechanism into CIR devices, CIR messages are automatically analyzed and parsed, and the problem of inefficient message resolution in the existing technology is solved, efficient and flexible message processing is achieved, and the rapid development of railway driving services is supported.
Patent Information
- Application Number
- CN202411986991.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-31
- Publication Date
- 2025-05-16
AI Technical Summary
When existing CIR devices process multiple complex messages, developers need to perform cumbersome byte cutting and data mapping, resulting in inefficient development and difficulty in responding to new business needs quickly.
Through a general message analysis tool module, the caller can automatically analyze the message type and generate corresponding parsing results based on fields such as business type and command, and use the Java reflection mechanism to dynamically parse the message data fields to generate a complete Java message receiving class object.
It greatly improves the work efficiency of developers, reduces the repetitive work in message parsing, simplifies the impact of protocol complexity on the development process, and enhances the flexibility and scalability of message processing.
Smart Images

Figure CN120017214A_ABST
Abstract
Description
Technical Field
[0001] The invention relates to the field of railway communications, and in particular to a method for translating a CIR message structure. Background Art
[0002] As a wireless communication device dedicated to railway trains, the locomotive integrated wireless communication equipment (CIR) is an upgraded product of the traditional wireless train radio station and an important device to ensure the safety of GSM-R section traffic. Since the CIR equipment was put into use, it has been used for more than ten years. The railway traffic business it carries has been increasing, and it has become an indispensable key equipment for railway traffic. However, since the CIR equipment was finalized at an early stage, with the expansion of railway business needs, the types and complexity of the messages it needs to process have also increased. Faced with a wide variety of CIR messages with complex protocols, developers often need to spend a lot of energy on tedious byte cutting and data mapping when parsing and translating messages, which affects development efficiency.
[0003] At present, in response to various types of CIR equipment failures, local areas transmit real-time generated CIR messages to ground servers via Ethernet. The server deconstructs and translates the messages, stores them in the database, and builds a CIR online monitoring system based on them to achieve real-time monitoring of CIR equipment on trains. However, the existing CIR message parsing methods have many limitations, such as high complexity of message protocols, a wide variety of messages, and poor scalability of translation methods. These problems not only increase the workload of developers, but also affect the system's ability to respond quickly to new business needs.
[0004] In order to deal with the wide variety of CIR messages and the boring and repetitive message cutting work, and to free the hands of developers, an efficient, flexible and easily extensible CIR message structure translation method is urgently needed. Through a universal message parsing tool module, the message type can be automatically analyzed and the corresponding parsing results can be generated according to the business type and command fields, so as to greatly improve the development efficiency, reduce the developers' repeated work in message parsing, and better support the rapid development of railway driving business. Summary of the invention
[0005] In order to solve the above problems, the present invention provides a method for structured translation of CIR messages. The caller automatically analyzes the message type according to relevant fields such as business type and command by calling the message parsing tool class module, and returns the corresponding deconstructed entity class to the caller, which greatly improves the developer's work efficiency.
[0006] To achieve the above-mentioned purpose, the technical solution adopted by the present invention is: a method for translating a CIR message structure, comprising: a receiving module, a cyclic field assignment module and a message receiving class module; the receiving module is used to receive various parameters passed in by the caller, including a message Byte array, a field title set, a field size set and a message receiving class, and to perform a legality check on these parameters to provide basic data for subsequent parsing work; the cyclic field assignment module is used to dynamically parse the message data field by traversing the field title set and the field size set in combination with a reflection mechanism, extract the corresponding byte data from the message and complete the field assignment according to the field type and offset, and finally generate a corresponding receiving class object; the message receiving class module is used to define the basic structure of all fields in the message, including field name, field type, field value in the form of Byte array, field title set and field size set, and to ensure the correctness of the field content through reflection dynamic assignment.
[0007] Furthermore, the implementation steps of the method are: a. Start executing the receiving module and receive the parameters required by the method, including the following parameters: Parameter 1: Message Byte array: mainly the Byte array of the CIR message; Parameter 2: Message receiving class: mainly used to facilitate the loop field assignment module, find the corresponding field title and size through reflection, store the split CIR message fields, and return them to the caller; Parameter 3: Offset: Mainly used to calculate the position of the cut message, which can achieve multi-CIR message parsing; Parameter 4: Field title set: mainly stores the field names defined by the CIR protocol; Parameter 5: Field size set: mainly stores the field name size defined by the CIR protocol; b. According to the receiving class, use the Java reflection mechanism principle to obtain all the field sets of the receiving class and enter the loop field assignment module step c; c. Loop through all fields; c1. Define a loop variable to record the current loop position; c2. Get the annotation of the field according to the loop variable; c3. Determine whether the annotation is empty. If it is empty, that is, there is no annotation, the loop variable +1, jump to c1, and continue the loop. Otherwise, execute the next step c3; c3. Get the title of the field; c4. Get the index of the field through the title; c5. Get the initial position of the field through a loop; c5-1. Define loop variable i=0, define the initial position equal to the offset, the loop variable is used to mark the current loop position, and the initial position is used to mark the starting position of the byte array; c5-2. Get the size of the current loop variable in the field size set; c5-3. Add the size of the current loop variable field and the initial value; c5-4, loop variable i++, check whether it is less than the current index position, if so, jump to c5-2 and continue the loop, otherwise, terminate the loop and execute the next step c5-5; c5-5, finally get the initial position of the field; c6. Find the size of the current field by obtaining the index of the field and the field size set; c7. Segment the original Byte array according to the initial position and field size of the field, get the Byte array value of the field, and then get the actual value of the field; c8. Assign a value to the Byte array of the current field through the Java reflection mechanism; c9. Determine whether there is a Java type in the annotation. If yes, execute the next step c10. If not, jump to the following logical step c11. c10. Convert the Byte array of the field to the given Java type and assign it to the corresponding Java class field. c11. Determine whether the loop variable has reached the maximum value. If so, the loop ends and continues to step d below; otherwise, the loop variable is increased by 1 and the loop continues to c1; d. Return the assembled Java message receiving class to the caller, ending the process.
[0008] The beneficial effects of the present invention are: The present invention provides an efficient CIR message structure translation method, which shows significant advantages in message parsing and development efficiency. Through this method, developers can reduce the energy investment in complex message parsing work and focus more time on the development of business logic. Compared with traditional methods, the present invention has obvious improvements in scalability and flexibility. For new types of messages, it is only necessary to create a corresponding message receiving class and define a field title set, a field size set and a field annotation, and then the parsing can be achieved through a general message cutting method, avoiding the tedious operation of manually cutting message bytes.
[0009] This method is based on the Java reflection mechanism. It dynamically traverses the field set of the message receiving class, parses and assigns values according to the field size, and finally generates a complete Java message receiving class object and returns it to the caller. Developers only need to define the field information according to the protocol to quickly complete the parsing and processing of the message, which simplifies the impact of protocol complexity on the development process and effectively optimizes the development process.
[0010] In summary, the method of the present invention realizes efficient parsing and dynamic assembly of CIR messages through the collaborative work of the receiving module, the cyclic field assignment module and the message receiving class module. The method unifies the parsing process of multiple message formats, enhances the flexibility and scalability of message processing, and provides technical support for the function optimization and technological innovation of railway communication systems. BRIEF DESCRIPTION OF THE DRAWINGS
[0011] Figure 1 It is a module association diagram of the present invention; Figure 2 It is a flow chart of the implementation steps of the present invention. DETAILED DESCRIPTION
[0012] In order to more clearly demonstrate the technical solutions and implementation examples of the present invention, the present invention is further described in detail below with reference to the accompanying drawings. Figure 1 As shown, a method for translating a CIR message structure mainly includes three modules: a receiving module, a cyclic field assignment module and a message receiving class module.
[0013] The function of the receiving module is to receive various parameters passed in by the caller, including the message Byte array, field title set, field size set, and message receiving class, and to verify the legitimacy of these parameters to provide basic data for subsequent parsing work. The loop field assignment module dynamically parses the message data field by traversing the field title set and field size set, combining the reflection mechanism, extracting the corresponding byte data from the message and completing the field assignment according to the field type and offset, and finally generating the corresponding receiving class object. The message receiving class module defines the basic structure of all fields in the message, including field name, field type, field value in Byte array form, field title set, and field size set, and ensures the correctness of the field content through dynamic reflection assignment.
[0014] The present invention mainly uses the Java programming language, and realizes dynamic parsing of message structure and generation of receiving class objects through Java's reflection mechanism. The advantage of Java reflection mechanism is that it can dynamically obtain fields and annotation information in a class, and assign values to these fields, thereby flexibly returning different Java receiving class objects to the caller.
[0015] Java reflection is a powerful runtime feature that allows programs to dynamically obtain information about classes, interfaces, methods, and fields, and to operate on these members. Through reflection, developers can check the structure of a class, call object methods, access or modify object fields, and even instantiate class objects. This dynamism provides a high degree of flexibility for framework development, tool implementation, and modular design, but it also requires certain performance overhead and security risks.
[0016] The implementation of Java reflection mechanism depends on the core classes in java.lang.Class and java.lang.reflect packages, including Class, Field, Method, and Constructor. Class class is the entry point of reflection, which can load classes and obtain their basic information, such as methods, fields, and constructors; Field class is used to operate the fields of a class, including obtaining and modifying field values and their types; Method class is used to represent and call methods in a class, and can obtain method parameters, return type, and other information; Constructor class is used to represent the constructor, through which class instances can be dynamically created.
[0017] Java reflection mechanism is widely used in practical applications, for example, it is often used for dependency injection and dynamic proxy in framework development, for serialization and class information extraction in tool libraries, and for dynamic module loading in plug-in mechanisms. However, since reflection bypasses the type checking and access control mechanism at compile time, it may cause performance problems and security risks. Therefore, the reflection mechanism should be used with caution under the premise of ensuring necessity and rationality.
[0018] like Figure 2 As shown, the specific implementation process of the present invention is as follows: a. Start executing the receiving module and receive the parameters required by the method, including the following parameters: Parameter 1: Message Byte array: mainly the Byte array of the CIR message; Parameter 2: Message receiving class: mainly used to facilitate the loop field assignment module, find the corresponding field title and size through reflection, store the split CIR message fields, and return them to the caller; Parameter 3: Offset: Mainly used to calculate the position of the cut message, which can achieve multi-CIR message parsing; Parameter 4: Field title set: mainly stores the field names defined by the CIR protocol; Parameter 5: Field size set: mainly stores the field name size defined by the CIR protocol; b. According to the receiving class, use the Java reflection mechanism principle to obtain all the field sets of the receiving class and enter the loop field assignment module step c; c. Loop through all fields; c1. Define a loop variable to record the current loop position; c2. Get the annotation of the field according to the loop variable; c3. Determine whether the annotation is empty. If it is empty, that is, there is no annotation, the loop variable +1, jump to c1, and continue the loop. Otherwise, execute the next step c3; c3. Get the title of the field; c4. Get the index of the field through the title; c5. Get the initial position of the field through a loop; c5-1. Define loop variable i=0, define the initial position equal to the offset, the loop variable is used to mark the current loop position, and the initial position is used to mark the starting position of the byte array; c5-2. Get the size of the current loop variable in the field size set; c5-3. Add the size of the current loop variable field and the initial value; c5-4, loop variable i++, check whether it is less than the current index position, if so, jump to c5-2 and continue the loop, otherwise, terminate the loop and execute the next step c5-5; c5-5, finally get the initial position of the field; c6. Find the size of the current field by obtaining the index of the field and the field size set; c7. Segment the original Byte array according to the initial position and field size of the field, get the Byte array value of the field, and then get the actual value of the field; c8. Assign a value to the Byte array of the current field through the Java reflection mechanism; c9. Determine whether there is a Java type in the annotation. If yes, execute the next step c10. If not, jump to the following logical step c11. c10. Convert the Byte array of the field to the given Java type and assign it to the corresponding Java class field. c11. Determine whether the loop variable has reached the maximum value. If so, the loop ends and continues to step d below; otherwise, the loop variable is increased by 1 and the loop continues to c1; d. Return the assembled Java message receiving class to the caller, ending the process.
[0019] Embodiment 1: The practical application of the above-mentioned general message parsing method is described in detail below through specific examples.
[0020] Embodiment background: Assume that there is a CIR protocol message, which is defined as follows: Protocol version: occupies 1 byte, indicating the current protocol version, which needs to be assigned an int type after parsing; Device ID: occupies 4 bytes, indicating the unique identifier of the device. After parsing, it needs to be assigned a String type; Status code: occupies 2 bytes, indicating the current status of the device. After parsing, it needs to be assigned to the int type; The corresponding field title set is ["protocol version", "device ID", "status code"], the field size set is [1, 4, 2], and the initial offset is 0.
[0021] Example of message content: [0x01, 0x31, 0x32, 0x33, 0x34, 0x00, 0x01], its meaning is as follows: Protocol version: 0x01 (1 byte, corresponding to integer 1); Device ID: 0x31, 0x32, 0x33, 0x34 (4 bytes, corresponding to the string "1234"); Status code: 0x00, 0x01 (2 bytes, corresponding to the integer 1).
[0022] Step by step guide Step 1: Receive parameters.
[0023] The caller passes the following parameters to the method: 1. Message Byte array: [0x01, 0x31, 0x32, 0x33, 0x34, 0x00, 0x01]; 2. Message receiving class: a Java class containing fields protocol version, device ID and status code; 3. Offset: initial value is 0; 4. Field title collection: ["protocol version", "device ID", "status code"]; 5. Field size set: [1, 4, 2]; The receiving module verifies the parameters to ensure their integrity and legality.
[0024] Step 2: Initialize the field collection.
[0025] The field set of the message receiving class is obtained through the Java reflection mechanism, including: Field name: protocol version, device ID, status code; Field type: int, String, int respectively; Field Notes: Provides matching information to the field title.
[0026] Step 3: Enter the field parsing loop.
[0027] Field I: Protocol version.
[0028] 1. According to the field title set, the index of the protocol version matched is 0.
[0029] 2. According to the offset (initial value is 0) and the field size set, determine that the starting position of the field is 0 and the length is 1 byte.
[0030] 3. Extract the 1 byte at position 0 from the message Byte array: [0x01].
[0031] 4. According to the int type specified in the annotation, the byte value 0x01 is parsed into the integer 1.
[0032] 5. Through the Java reflection mechanism, the parsed value 1 is assigned to the protocol version field of the message receiving class.
[0033] 6. Update offset: offset = 0 + 1 = 1.
[0034] Field II: Device ID.
[0035] 1. The index of the matched device ID is 1.
[0036] 2. Based on the offset (current value is 1) and the field size set, determine that the starting position of the field is 1 and the length is 4 bytes.
[0037] 3. Extract the bytes at positions 1 to 4 from the message Byte array: [0x31, 0x32, 0x33, 0x34].
[0038] 4. According to the String type specified in the annotation, parse the Byte array into the string "1234".
[0039] 5. Through the Java reflection mechanism, the parsed value "1234" is assigned to the device ID field of the message receiving class.
[0040] 6. Update offset: offset = 1 + 4 = 5.
[0041] Field III: Status code.
[0042] 1. The index of the matching status code is 2.
[0043] 2. Based on the offset (currently 5) and the field size set, determine that the starting position of the field is 5 and the length is 2 bytes.
[0044] 3. Extract the bytes at positions 5 to 6 from the message Byte array: [0x00, 0x01].
[0045] 4. According to the int type specified in the annotation, parse the Byte array into the integer 1.
[0046] 5. Through the Java reflection mechanism, the parsed value 1 is assigned to the status code field of the message receiving class.
[0047] 6. Update offset: offset = 5 + 2 = 7.
[0048] Step 4: The field loop ends.
[0049] After all fields are parsed, the loop logic ends.
[0050] Step 5: Assemble and return the message receiving class.
[0051] Through the Java reflection mechanism, all assigned fields are assembled into a complete message receiving class object, which contains the following values: protocol version: 1; device ID: "1234"; status code: 1; Finally, the assembled message receiving class object is returned to the caller.
[0052] The extensibility and practical application of the present invention: First, adapting to various message structures, this method can parse messages of various different structures by dynamically providing field title sets and field size sets. For example, if a new field timestamp (8 bytes, long type) is added, only the corresponding definition needs to be added to the field title set and field size set without modifying the parsing logic.
[0053] Secondly, it supports multiple protocol versions. In the protocol upgrade scenario, if the order or size of the fields changes, you only need to adjust the collection parameters without changing the method code.
[0054] Furthermore, this method has diversified application scenarios and is applicable to any message parsing needs based on a fixed byte structure, including industrial communications, IoT data transmission, etc.
[0055] This example demonstrates the practical application of the general message parsing method in the CIR protocol. Through the collaborative work of the receiving module, the cyclic field assignment module and the message receiving module, the efficient parsing and dynamic assembly of the CIR message are realized. This method is efficient, universal and flexible, and provides an efficient and reliable solution for the parsing of complex protocol messages.
Claims
1. A method for translating a CIR message structure, characterized in that: The method comprises: a receiving module, a circular field assignment module and a message receiving class module; the receiving module is used to receive various parameters passed in by the caller, including a message Byte array, a field title set, a field size set and a message receiving class, and to perform a legality check on these parameters to provide basic data for subsequent parsing work; the circular field assignment module is used to dynamically parse the message data field by traversing the field title set and the field size set in combination with a reflection mechanism, extract corresponding byte data from the message and complete field assignment according to the type and offset of the field, and finally generate a corresponding receiving class object; the message receiving class module is used to define the basic structure of all fields in the message, including field name, field type, field value in the form of Byte array, field title set and field size set, and ensure the correctness of the field content through reflection dynamic assignment.
2. The method for translating CIR message structure according to claim 1, characterized in that: The implementation steps of the method are: a. Start executing the receiving module and receive the parameters required by the method, including the following parameters: Parameter 1: Message Byte array: mainly the Byte array of the CIR message; Parameter 2: Message receiving class: mainly used to facilitate the loop field assignment module, find the corresponding field title and size through reflection, store the split CIR message fields, and return them to the caller; Parameter 3: Offset: Mainly used to calculate the position of the cut message, which can achieve multi-CIR message parsing; Parameter 4: Field title set: mainly stores the field names defined by the CIR protocol; Parameter 5: Field size set: mainly stores the field name size defined by the CIR protocol; b. According to the receiving class, use the Java reflection mechanism principle to obtain all the field sets of the receiving class and enter the loop field assignment module step c; c. Loop through all fields; c1. Define a loop variable to record the current loop position; c2. Get the annotation of the field according to the loop variable; c3. Determine whether the annotation is empty. If it is empty, that is, there is no annotation, the loop variable +1, jump to c1, and continue the loop. Otherwise, execute the next step c3; c3. Get the title of the field; c4. Get the index of the field through the title; c5. Get the initial position of the field through a loop; c5-1. Define loop variable i=0, define the initial position equal to the offset, the loop variable is used to mark the current loop position, and the initial position is used to mark the starting position of the byte array; c5-2. Get the size of the current loop variable in the field size set; c5-3. Add the size of the current loop variable field and the initial value; c5-4, loop variable i++, check whether it is less than the current index position, if so, jump to c5-2 and continue the loop, otherwise, terminate the loop and execute the next step c5-5; c5-5, finally get the initial position of the field; c6. Find the size of the current field by obtaining the index of the field and the field size set; c7. Segment the original Byte array according to the initial position and field size of the field, get the Byte array value of the field, and then get the actual value of the field; c8. Assign a value to the Byte array of the current field through the Java reflection mechanism; c9. Determine whether there is a Java type in the annotation. If yes, execute the next step c10. If not, jump to the following logical step c11. c10. Convert the Byte array of the field to the given Java type and assign it to the corresponding Java class field. c11. Determine whether the loop variable has reached the maximum value. If so, the loop ends and continues to step d below; otherwise, the loop variable is increased by 1 and the loop continues to c1; d. Return the assembled Java message receiving class to the caller, ending the process.