Communication method and communication system of embedded system
By designing a layered communication protocol and generalizing the transport layer, the problem of lack of universality and security in communication protocols in embedded systems is solved, realizing a simple and efficient communication method that is adaptable to various application scenarios and reduces development and maintenance costs.
Patent Information
- Application Number
- CN202510891593.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-30
- Publication Date
- 2025-11-04
AI Technical Summary
The lack of universality in the design of communication protocols in embedded systems leads to significant differences between protocols, complex development and maintenance, and insufficient security.
The design adopts a layered communication protocol. The transport layer defines the frame format, including frame header, length checksum, length, data checksum, and data information. The length checksum ensures the correctness of the length field. Sequence number and status fields are added to distinguish frame content. C/R identifiers are set to achieve equality between the two communicating parties. F/W identifiers support long message packet splitting. Error codes are handled independently for frame transmission issues.
It achieves modularity, commonality, and universality of communication protocols, reduces development and maintenance costs, improves security and stability, and supports compatibility in various scenarios.
Smart Images

Figure CN120896993A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the field of embedded system communication technology, and in particular to a communication method and system for embedded system. BACKGROUND
[0002] In embedded system, communication function is often used, especially single-point to single-point communication. For example, between product and peripheral, between product and host, between modules in product, etc. Since communication requirements of embedded products are different, and in most cases, simple function is mainly used. Therefore, in practice, a similar one-off negotiation method is generally used. That is, a communication protocol is designed for a communication requirement, and each communication protocol is matched with a software code. Thus, in the field of embedded system, various customized communication protocols can be seen, and sometimes the difference between protocols is also great.
[0003] In the design of communication protocol, there is a concept of encapsulation framing, i.e. framing and deframing. There are two common methods: 1. Character counting method The frame header records the length, and the structure is as follows:
[0004] That is, the length of the frame is determined by the length field of the frame header. The disadvantage of this method is obvious. Once the frame header is wrong, the following frame will also be wrong, causing a chain reaction.
[0005] 2. Head and tail definition method The frame header and the frame tail use special characters to represent the start and end of the frame.
[0006]
[0007] The frame header and the frame tail can use the same character or different characters. In order to avoid the appearance of the same character in the data field as the flag, an escape character needs to be introduced. Although this method is safe, it is too complex. Development, debugging and maintenance are very troublesome.
[0008] In the actual application scene of embedded system, there are mainly two design methods of communication protocol: 1. Directly use application message and abandon frame.
[0009] Generally used in simple function occasions. Directly use application message, such as "instruction + parameter".
[0010] 2. On the basis of application message, increase frame header and frame tail, but do not do escape.
[0011] Generally used in occasions that need framing and deframing, but the security requirement is not high.
[0012] In summary, based on the characteristics of embedded development, various communication protocol designs for different scenarios are filled with rough designs, which are not universal and have great risks. SUMMARY
[0013] The technical problem solved by the present application is to provide a communication method and a communication system for an embedded system, which are safe and simple, and have strong universality.
[0014] To solve the above technical problems, the technical scheme adopted by the present application is as follows: a communication method for an embedded system, comprising: The sender generates a data frame according to a preset communication protocol, and sends the data frame to the receiver. The receiver receives the data frame and parses the data frame according to the communication protocol. The communication protocol comprises a transport layer and an application layer; the transport layer defines the frame format of the data frame, and the frame format comprises the following fields: frame header, length checksum, length, data checksum and data information; the frame header indicates the start of the frame; the length checksum indicates the checksum of the data length; the length indicates the data length of the data information; the data checksum indicates the checksum of the data information; and the data information comprises data to be communicated.
[0015] The present application also provides a communication system for an embedded system, comprising a first communication end and a second communication end. The first communication end is used to generate a data frame according to a preset communication protocol, and send the data frame to the second communication end. The second communication end is used to receive the data frame and parse the data frame according to the communication protocol. The communication protocol comprises a transport layer and an application layer; the transport layer defines the frame format of the data frame, and the frame format comprises the following fields: frame header, length checksum, length, data checksum and data information; the frame header indicates the start of the frame; the length checksum indicates the checksum of the data length; the length indicates the data length of the data information; the data checksum indicates the checksum of the data information; and the data information comprises data to be communicated.
[0016] The application has the beneficial effects that: by layering the communication protocol and designing the transmission layer in a universal manner, the transmission layer can be adapted to most application scenarios, and can be developed and maintained in a modular manner, realizing the modularization, commonality and universalization of the communication protocol; by designing the frame header and length, the legality of the frame header information is double-protected; by adding the length check sum, the correctness of the length field is ensured. Compared with the custom communication protocol commonly used in embedded systems, the application is more secure and robust, and compared with the strict "start-end definition method", the application is more concise. While meeting the communication stability, the development and maintenance costs are reduced. In the most efficient case, compatibility with various scenarios is realized. BRIEF DESCRIPTION OF DRAWINGS
[0017] Figure 1 A flowchart of a communication method of an embedded system according to an embodiment of the application; Figure 2 A schematic diagram of a layered structure of a communication protocol according to an embodiment of the application; Figure 3 A transmission schematic diagram when a check error occurs according to an embodiment of the application; Figure 4 A transmission schematic diagram when a receiving timeout occurs according to an embodiment of the application; Figure 5 A schematic diagram of communication between two parties with equal status according to an embodiment of the application. DETAILED DESCRIPTION
[0018] To explain the technical content, purposes and effects of the application in detail, the following will be explained in combination with the embodiments and the drawings.
[0019] Please refer to Figure 1 A communication method of an embedded system, comprising: A sender generates a data frame according to a preset communication protocol, and sends the data frame to a receiver; The receiver receives the data frame, and parses the data frame according to the communication protocol; The communication protocol comprises a transmission layer and an application layer; the transmission layer defines a frame format of the data frame, and the frame format comprises the following fields: a frame header, a length check sum, a length, a data check sum and data information; the frame header indicates a flag of the start of the frame; the length check sum indicates a check sum of the data length; the length indicates the data length of the data information; the data check sum indicates a check sum of the data information; and the data information comprises data to be communicated.
[0020] From the above description, the beneficial effects of the present application are that the design of the frame format takes into account safety and simplicity; while meeting the stability of communication, the cost of development and maintenance is reduced; in the case of as high efficiency as possible, compatibility with various scenes is realized.
[0021] Further, the data information includes a serial number, a state and a parameter; The serial number is used to represent the frame serial number of the data frame; The state includes a frame identifier and an error code, and the frame identifier includes a type identifier and an end identifier; The parameter includes data to be communicated; Wherein, when the type identifier is 1, it indicates that the data frame is an instruction frame, and when the type identifier is 0, it indicates that the data frame is a response frame; when the end identifier is 1, it indicates that the data frame is an end frame, and when the end identifier is 0, it indicates that the data frame is a waiting frame; when the error code is 0, it indicates that the data frame is a normal frame, and when the error code is not 0, it indicates that the data frame is an abnormal frame.
[0022] In some embodiments, the data to be communicated can include application layer message data.
[0023] From the above description, by setting the serial number field, the contents of different frames can be distinguished, the lost frame or retransmitted frame can be identified, the correctness of retransmission and response can be ensured, and replay attacks can be prevented. By setting the type identifier (C / R identifier), it is identified whether the data frame is an instruction frame or a response frame, so that the received data frame can be automatically routed to the application layer for processing, and the status of the communication parties is equal. By setting the end identifier (F / W identifier), it is identified whether the data frame is an end frame or a waiting frame, so that long messages can be automatically divided into multiple short frames for transmission, and compatibility with low memory resource products is realized. By setting the error code, the transmission layer can independently process the frame transmission problem according to the agreement, so as to not interfere with the application layer.
[0024] Further, the sender generates a data frame according to a preset communication protocol, and sends the data frame to the receiver, including: The sender splits the data to be communicated according to a preset buffer size to obtain at least one data segment; According to the preset communication protocol, data frames corresponding to each data segment are sequentially generated and sent to the receiver.
[0025] From the above description, automatic packeting can be supported, and long messages can be automatically divided into multiple short frames for transmission.
[0026] Further, the frame header occupies 1 byte; The receiver receives the data frame and parses the data frame according to the communication protocol, including: The receiver receives the data sent by the sender byte by byte; If the received single-byte character is a preset frame header character, X byte characters after the single-byte character are obtained, and x1 first byte characters in the X byte characters are used to check x1+1 to x1+x2 byte characters in the X byte characters, wherein X=x1+x2, X=x1+x2+x3, or X=x1+x2+x3+x4+x5, x1 represents the number of bytes of the length check sum, x2 represents the number of bytes of the length, x3 represents the number of bytes of the data check sum, x4 represents the number of bytes of the serial number, and x5 represents the number of bytes of the state; If the check passes, x3 byte characters after the X byte characters are used to receive a data check sum in a current data frame, x1+1 to x1+x2 byte characters are used to receive data information in the current data frame, and the reception of the current data frame is completed, or x1+1 to x1+x2 byte characters are used to receive data information in the current data frame, and the reception of the current data frame is completed, or x1+1 to x1+x2 byte characters in the X byte characters and x4 and x5 are used to receive parameters in the current data frame, and the reception of the current data frame is completed.
[0027] Further, the check of x1+1 to x1+x2 byte characters in the X byte characters based on x1 first byte characters in the X byte characters includes: A check sum of x1+1 to x1+x2 byte characters in the X byte characters is calculated, and it is determined whether the check sum is consistent with x1 first byte characters in the X byte characters; If the check sum is consistent with x1 first byte characters in the X byte characters, it is determined that the check passes.
[0028] As described above, the single-byte reception is performed first until the frame header character is received, then the fixed length is received, and after the length domain is checked to be legal, the subsequent variable-length part data is received according to the length, so that the buffer is dynamically allocated for the data area according to the value of the length domain, and the purpose of optimal memory utilization is achieved.
[0029] Further, after the reception of the current data frame is completed, the method further includes: The data information in the current data frame is checked according to the data check sum in the current data frame; If the check does not pass, a first response frame is returned to the sender, the type identifier in the first response frame is 0, and the error code is a preset error code corresponding to the check error, so that the sender retransmits the current data frame.
[0030] From the above description, if the data check fails, it indicates that there may be partial data errors in the frame transmission process, resulting in inconsistency between the data sent by the sender and the data received by the receiver. The error reason is informed to the sender by returning the response frame, so that the sender re-sends the data frame.
[0031] Further, after the data information in the current data frame is checked according to the data checksum in the current data frame, the method further comprises: If the check passes, the state field value and the parameter field value in the current data frame are obtained; If the receiver is currently in a state of waiting to receive an instruction frame and the type identifier in the state field value is 0, or the receiver is currently in a state of waiting to receive a response frame and the type identifier in the state field value is 1, the parameter field value in the current data frame is provided to a callback function for processing; If the error code in the state field value is not 0, corresponding frame operations are performed according to the error reason corresponding to the error code, and the frame operations include retransmission and discarding.
[0032] From the above description, the effect of automatic routing can be achieved. By performing corresponding processing according to the error code, errors that may occur in the frame transmission process can be solved.
[0033] Further, after the receiver receives one data frame, the receiver is paused, and the receiving is enabled after the data information in the one data frame is parsed.
[0034] From the above description, multi-frame receiving can be achieved by using flow control functions.
[0035] Further, the length checksum and the data checksum are calculated by using LRC algorithm or CRC algorithm.
[0036] The application further provides a communication system of an embedded system, comprising a first communication end and a second communication end. The first communication end is configured to generate a data frame according to a preset communication protocol, and send the data frame to the second communication end. The second communication end is configured to receive the data frame, and parse the data frame according to the communication protocol. The communication protocol comprises a transport layer and an application layer; the transport layer defines a frame format of the data frame, and the frame format comprises the following fields: a frame header, a length checksum, a length, a data checksum and data information; the frame header indicates a flag of the beginning of the frame; the length checksum indicates a checksum of the data length; the length indicates a data length of the data information; the data checksum indicates a checksum of the data information; and the data information comprises data to be communicated.
[0037] Embodiment one Please refer to Figures 1-5 Embodiment 1 of the present invention is: a communication method for an embedded system, such as... Figure 1 As shown, it includes the following steps: S1: The sender generates a data frame according to a preset communication protocol and sends the data frame to the receiver.
[0038] S2: The receiver receives the data frame and parses the data frame according to the communication protocol.
[0039] like Figure 2 As shown, this embodiment divides the communication protocol into two layers: the transport layer and the application layer, with the application layer built on top of the transport layer. The transport layer handles frame transmission requirements, while the application layer handles user functional requirements; that is, the transport layer processes frame transmission-related functions, and the application layer processes application-related functions. Transmission-related requirements in the communication protocol are aggregated into the transport layer and designed with commonality to adapt to communication needs in various scenarios. When migrating and adapting the communication protocol to different scenarios, only the data structure of the application layer needs to be modified.
[0040] The relationship between the transport layer and the application layer is as follows:
[0041] Transport layer frame delimiting, data structure as follows:
[0042] Frame header: A single character is designated as the start of a frame. Subsequent characters in the frame are allowed to repeat the header without escaping.
[0043] Length: Use length to define a frame.
[0044] This method combines "character counting" and "start and end delimitation". Compared to "start and end delimitation", it only retains the frame header, its main function being to quickly locate the start of the frame. Compared to "character counting", it adds a length field check. This check ensures the correctness of the length field. Through the design of the frame header and length, it effectively provides double protection for the legitimacy of the frame start information.
[0045] The data structure of the transport layer frame identifier is as follows:
[0046] Wherein, "C / R" (type identifier) is used to indicate whether the current frame is a command frame or a response frame. The command frame represents the local as an active party, sending request or notification information, etc. The response frame represents the local as a passive party, responding to the request. This identifier is mainly used to indicate the direction, and the frame management module can automatically route to the corresponding software processing module according to the indication. The design of this identifier is to make the status of both parties equal, and both parties can send command frames and response frames at any time.
[0047] "F / W" (end identifier) is used to indicate whether to wait after the current frame. Finish, that is, a complete application message is sent and does not need to continue to wait for the subsequent frame; wait, that is, a complete application message is split into multiple frames for transmission, and the subsequent frame needs to be continued until the frame identifier finish. In this way, one application message is transmitted through multiple frames, and a time commitment is generally set between frames.
[0048] In this embodiment, the transport layer frame format is defined as follows:
[0049] Wherein, the format of "state" is defined as follows:
[0050] Wherein, the domain name means the field, and the domain length means the field length. B means Byte (byte), 1B=8b, and b means bit (bit).
[0051] "Frame header" is the mark of the beginning of the frame. In this embodiment, the character 0x7e is agreed as the mark of the beginning of the frame.
[0052] "Length checksum" indicates the checksum of the "length" field. In this embodiment, the LRC algorithm (Longitudinal Redundancy Check) is used to check the "length" field. In other embodiments, the CRC (Cyclic Redundancy Check) algorithm can also be used for checking.
[0053] "Length" indicates the interval length from "sequence number" to "parameter".
[0054] "Data checksum" indicates the checksum of the interval data from "sequence number" to "parameter". In this embodiment, the LRC algorithm is used to check the interval data from "sequence number" to "parameter". In other embodiments, the CRC algorithm can also be used for checking.
[0055] "Sequence number" represents frame sequence number, used to distinguish the content of different frames, identify lost frames or retransmitted frames, ensure the correctness of retransmission and response, prevent replay attacks, etc.
[0056] "State" is used to record frame identification and error code.
[0057] "Parameter" is used to carry the content of the application layer.
[0058] In this embodiment, for the "C / R" bit, taking 1 is a command frame, and taking 0 is a response frame.
[0059] For the "F / W" bit, taking 1 indicates a finish frame, and no further waiting is needed; taking 0 is a wait frame, and further waiting for subsequent frames is needed.
[0060] For the "error code", taking 0 is a normal frame, and a non-zero value is an abnormal frame. Each non-zero value corresponds to an error number.
[0061] During transmission, some errors may occur, such as check error, sequence number error, and timeout error, which can be recorded in the error code field. After receiving the error code, the frame processing module will process it according to the protocol agreement of the transmission layer.
[0062] For example, as shown in Figure 3 , A sends a command frame to B, and B receives the command frame and finds a check error. Then B returns a response frame corresponding to the check error to A, requesting A to resend the command frame. In this embodiment, the frame sequence number is identified only after the command frame is correctly received. Therefore, for the case of resending the command frame due to a check error, there will be no case of receiving a duplicate frame.
[0063] For another example, as shown in Figure 4 , if A sends a command frame to B and does not receive a response frame returned by B within a timeout, A resends the command frame. If B receives the command frame and does not return a response frame to A in time, B will receive the command frame sent by A again after returning the response frame, and the frame sequence number is the same as the previous command frame. At this time, B will discard the duplicate frame to avoid the same command being executed twice.
[0064] In some embodiments, in the case where a complete command frame is not received, the receiver does not respond, and the sender does not receive the response frame of the receiver within a specified time, it is determined as a "timeout error", triggering the processing mechanism of resending the command frame. That is, in these embodiments, the "timeout error" is an error identified by the sender.
[0065] Error codes are independent, and the transport layer can handle frame transmission issues independently according to the agreement, so as not to interfere with the application layer. That is, the application layer does not need to care about the error handling of the transport layer.
[0066] Embedded system communication applications are generally not very complex, and for stability reasons, a master-slave approach is typically used. That is, the communicating parties are a master and a slave, with all communication initiated by the master and responded to by the slave. This embodiment adds a C / R identifier at the transport layer to automatically route received data frames to the application layer for processing, thus achieving equality between the communicating parties. That is, if the application layer is waiting to receive a response frame, it will only receive the response frame. If a request frame is received during this period, it will be automatically routed by the transport layer to the request branch of the application layer for processing. At the application layer, request and response processing are completely separated, ensuring stability while simplifying application development and processing logic. If one party closes its request processing branch and the other closes its response processing branch, the protocol reverts to master-slave mode. In other words, the design of equality also supports master-slave mode.
[0067] Specifically, either party in the communication can send a command frame at any time. Therefore, while waiting for an acknowledgment frame, a command frame may be received simultaneously; that is, both command frames and acknowledgment frames may be received at the same time. To avoid interference from command frames while waiting for an acknowledgment frame, the transport layer management module can provide a callback function for command frame processing. If a command frame is received while waiting for an acknowledgment frame, the transport layer automatically parses the command frame and provides the parameter field values from the command frame to the callback function for processing, thereby achieving automatic routing.
[0068] For example, such as Figure 5 As shown, if B receives a command frame from A while waiting for a command frame, it can route the command frame to the application layer for processing. If B receives a response frame from A while waiting for a command frame, it considers this an abnormal situation and routes the response frame to the error handling module (implemented through a callback function). When A sends command frame 3 to B and waits for B's response frame 3, if it receives command frame 1 from B, it routes command frame 1 to the command processing module (implemented through a callback function) and continues to wait for response frame 3; if it receives response frame 3 from B, it directly routes response frame 3 to the application layer for processing.
[0069] Depending on the functional requirements, the message length at the application layer can vary significantly. This embodiment supports automatic packet splitting by setting the F / W flag, meaning it can automatically divide long messages into multiple short frames for transmission.
[0070] Specifically, step S1 includes: S101: The sender splits the communication data according to the preset buffer size to obtain at least one data segment.
[0071] In an actual application scenario, the two parties of the transceiving can agree on a suitable cache size according to resource conditions, and split the message according to the cache size. The data segments can be equal in size or not equal in size, as long as they do not exceed the preset cache size.
[0072] S102: According to a preset communication protocol, data frames corresponding to the data segments are sequentially generated and sent to the receiving party. That is, the application layer message is split and placed in the "parameter" field of multiple data frames, multiple data frames are generated, and the frames are sent one by one.
[0073] When sending a message, it needs to be encapsulated into a complete frame first and then sent out through a physical channel; when receiving a message, it is generally necessary to first receive a complete frame and then verify its validity before parsing and processing. Therefore, it is generally necessary to pre-allocate transceiving cache according to the maximum message length. In the embodiment, by adding F / W identification in the transmission layer, long messages can be automatically divided into multiple short frames for transmission. The W identification also indicates that there is a subsequent frame (i.e., the F / W identification is set to 0), i.e., the current message transmission has not ended; the F identification indicates the end of the frame (i.e., the F / W identification is set to 1), i.e., it is the last frame of the current message. Based on this, the two parties of the transceiving can agree on a suitable cache size according to resource conditions, rather than having to allocate frame cache according to the maximum length requirement.
[0074] In specific implementation, the processing of the sending party is relatively simple, and it only needs to automatically split the application layer message into multiple data frames. The receiving party is relatively complex and needs to be adjusted according to the actual situation. For example, if the physical interface is a serial port, multiple frame reception can be achieved by using flow control. That is, flow control is triggered after receiving a frame of data, and reception is enabled again after the data is parsed and processed. In the field of lightweight embedded systems, it is common for the two parties of the communication to have different resource sizes, i.e., one party has more resources and the other party has fewer resources. By supporting the automatic packeting function, the application layer can ignore the differences in hardware resources and smoothly migrate between different platforms.
[0075] Further, in the frame format of the transmission layer of the embodiment, the fixed-length part is placed in the front and the variable-length part is placed in the back, which can simplify the programming logic. For example, a single byte is received first until the frame header character is received; then a fixed length is received, and after verifying that the length field is legal, the subsequent variable-length part data is received according to the length. In this way, the value of the length field can be used to dynamically allocate cache for the data area, achieving the purpose of optimizing memory utilization.
[0076] Specifically, step S2 includes: S201: The receiving party receives the data sent by the sending party byte by byte, and determines whether the current single-byte character received is a preset frame header character. If yes, step S202 is performed; if no, the receiving party continues to receive the data sent by the sending party byte by byte.
[0077] S202: Obtain X byte characters after the current single byte character, and perform a check on x1+1th to x1+x2th byte characters in the X byte characters according to the first x1 byte characters in the X byte characters, to determine whether the check is passed, if yes, execute step S203, if not, continue to receive the data sent by the sending party byte by byte.
[0078] In some embodiments, X=x1+x2, x1 represents the number of bytes of the length check sum, and x2 represents the number of bytes of the length. Taking the above frame format as an example, the frame header occupies 1 byte, the length check sum occupies 1 byte, and the length occupies 2 bytes, i.e., x1=1 and x2=2, and thus X=3. Therefore, when the preset frame header character is received, three byte characters following the frame header character are obtained, and a check is performed on the three byte characters, i.e., a check sum of the last two byte characters in the three byte characters is calculated, and it is determined whether the calculated check sum is consistent with the first byte character in the three byte characters, if yes, it is considered that the check is passed, and the frame header information is legal, otherwise, it is considered that the check is not passed.
[0079] In other embodiments, X=x1+x2+x3, x3 represents the number of bytes of the data check sum. Taking the above frame format as an example, the length check sum occupies 1 byte, the length occupies 2 bytes, and the data check sum occupies 1 byte, i.e., x1=1, x2=2, and x3=1, and thus X=4. Therefore, when the preset frame header character is received, four byte characters following the frame header character are obtained, and a check is performed on the first three byte characters in the four byte characters, i.e., a check sum of the 2nd-3rd byte characters in the four byte characters is calculated, and it is determined whether the calculated check sum is consistent with the first byte character in the four byte characters, if yes, it is considered that the check is passed, otherwise, it is considered that the check is not passed.
[0080] In other embodiments, X=x1+x2+x3+x4+x5, x4 represents the number of bytes of the sequence number, and x5 represents the number of bytes of the state, i.e., the fixed-length part data in the frame format is obtained first. Taking the above frame format as an example, the sequence number occupies 1 byte, and the state occupies 1 byte, i.e., x4=1 and x5=1, and thus X=6. Therefore, when the preset frame header character is received, six byte characters following the frame header character are obtained, and a check is performed on the first three byte characters in the six byte characters, and the check process is the same as above.
[0081] S203: Receive subsequent part data of the data frame, and complete the reception of one data frame.
[0082] When the frame header information check is passed, the x1+1th to x1+x2th byte characters in the X byte characters are the length field value, i.e., the data length of the "data information".
[0083] Taking the above frame format as an example, the data check sum occupies 1 byte, the serial number occupies 1 byte, and the state occupies 1 byte. Therefore, when X = x1 + x2, the 1 byte character after the X byte character is received first, and the "data check sum" in the current data frame is obtained. Then, according to the data length, the subsequent data is received, and the "data information" in the current data frame is obtained, so that the current data frame is received.
[0084] When X = x1 + x2 + x3, the "data information" in the current data frame is obtained directly according to the data length, and the current data frame is received.
[0085] When X = x1 + x2 + x3 + x4 + x5, only the "parameter" part in the current data frame has not been obtained at this time, the number of bytes of the "parameter" is the data length of the "data information" - x4 - x5, that is, the data length - 2, and according to the number of bytes of the "parameter", the subsequent data is received, and the "parameter" in the current data frame is obtained, so that the current data frame is received.
[0086] Further, after receiving the current data frame, the receiving party can check the data information according to the data check sum. In this embodiment, the check sum of the data information is calculated by LRC algorithm, and if the calculated check sum is consistent with the data check sum, it is considered that the check is passed, otherwise it is considered that the check is not passed.
[0087] During the transmission of the data frame, one byte or multiple bytes may be wrong due to interference and other factors, resulting in inconsistency between the data sent by the sender A and the data received by the receiver B, that is, the sender A sends a normal frame (error code 0), but the receiver B receives data with check error. At this time, the receiver B will set the error code in the response frame to the error code corresponding to the "check error". After receiving the response frame, the sender A will resend the current data frame.
[0088] If the check is passed, the receiving party can parse the received data information. That is, according to the above frame format, the serial number field value, the state field value and the parameter field value can be parsed.
[0089] When the sender transmits a long message by dividing it into multiple short frames, the parameter field value parsed at this time is a data segment in the message. According to the serial number field value, the position of the current data segment in the whole message can be determined. When the F / W identifier in the state field value is 0, it indicates that the current received data frame is an intermediate data frame of the message; when the F / W identifier in the state field value is 1, it indicates that the current received data frame is the last data frame of the message. By splicing the data segments in each data frame of the message, the complete message can be obtained.
[0090] The embodiment divides the communication protocol into layers, and designs the transmission layer to be universal, so that it can adapt to most application scenarios. In this way, the transmission layer can be developed and maintained modularly. In actual use, the transmission layer can be used as a public module, and the user only needs to focus on the application layer function. The design of the communication protocol in the embodiment is simple and secure. In the most efficient case, compatibility with various scenarios is realized.
[0091] In summary, the communication method and the communication system of the embedded system provided by the application divide the communication protocol into layers, and design the transmission layer to be universal, so that it can adapt to most application scenarios, and can be developed and maintained modularly, realizing the modularization, commonality and universality of the communication protocol. Through the design of the frame header and the length, the legality of the frame header information is double-protected, and security and simplicity are taken into account. Through the addition of the length check sum, the correctness of the length field is ensured. Through the setting of the sequence number field, the content of different frames can be distinguished, lost frames or retransmitted frames can be identified, the correctness of retransmission and response is ensured, and replay attacks are prevented. Through the setting of the C / R identifier, it is identified whether the data frame is an instruction frame or a response frame, so that the received data frame can be automatically routed to the application layer for processing, realizing the equal status of the two communication parties, and the master-slave mode can also be compatible. Through the setting of the F / W identifier, it is identified whether the data frame is an end frame or a waiting frame, so that long messages can be automatically divided into multiple short frames for transmission, realizing the compatibility with low memory resource products. Through the setting of the error code, the transmission layer can independently process the frame transmission problem according to the agreement, so as to not interfere with the application layer. The application reduces the development and maintenance cost while meeting the communication stability, and realizes the compatibility with various scenarios in the most efficient case.
[0092] The above description is only an embodiment of the application, and does not limit the patent scope of the application. Any equivalent transformation or direct or indirect application in related technical fields based on the content of the specification and drawings is also included in the patent protection scope of the application.
Claims
1. A communication method for an embedded system, characterized in that, include: The sender generates a data frame according to a preset communication protocol and sends the data frame to the receiver; The receiver receives the data frame and parses the data frame according to the communication protocol; The communication protocol includes a transport layer and an application layer; The transport layer defines the frame format of the data frame, which includes the following fields: frame header, length checksum, length, data checksum, and data information.
2. The communication method for an embedded system according to claim 1, characterized in that, The data information includes serial number, status, and parameters; The sequence number is used to represent the frame sequence number of the data frame; The status includes a frame identifier and an error code, and the frame identifier includes a type identifier and an end identifier; The parameters include the data to be communicated; Wherein, when the type identifier is 1, it indicates that the data frame is a command frame, and when the type identifier is 0, it indicates that the data frame is a response frame; when the end identifier is 1, it indicates that the data frame is an end frame, and when the end identifier is 0, it indicates that the data frame is a waiting frame; when the error code is 0, it indicates that the data frame is a normal frame, and when the error code is not 0, it indicates that the data frame is an abnormal frame.
3. The communication method for an embedded system according to claim 2, characterized in that, The sender generates a data frame according to a preset communication protocol and sends the data frame to the receiver, including: The sender splits the data to be communicated according to the preset buffer size, and obtains at least one data segment; According to the preset communication protocol, data frames corresponding to each data segment are generated sequentially and sent to the receiver.
4. The communication method for an embedded system according to claim 2, characterized in that, The frame header occupies 1 byte; The receiver receives the data frame and parses the data frame according to the communication protocol, including: The receiver receives the data sent by the sender byte by byte; If the received single-byte character is a preset frame header character, then obtain the X bytes following the single-byte character, and based on the first x1 bytes of the X bytes, verify the (x1+1)th to (x1+x2)th bytes of the X bytes; where X = x1+x2, X = x1+x2+x3 or X = x1+x2+x3+x4+x5, x1 represents the number of bytes for the length checksum, x2 represents the number of bytes for the length, x3 represents the number of bytes for the data checksum, x4 represents the number of bytes for the sequence number, and x5 represents the number of bytes for the status. If the verification passes, the data checksum in the current data frame is received based on the x3 bytes following the X bytes, and the data information in the current data frame is received based on the (x1+1)th to (x1+x2)th bytes, thus completing the reception of the current data frame. Alternatively, based on the x1+1 to x1+x2th bytes, the data information in the current data frame can be received to complete the reception of the current data frame. Alternatively, based on the x1+1 to x1+x2 bytes of the X bytes, as well as x4 and x5, the parameters in the current data frame can be received to complete the reception of the current data frame.
5. The communication method for an embedded system according to claim 4, characterized in that, The step of verifying the (x1+1)th to (x1+x2)th bytes of the X bytes based on the first x1 bytes of the X bytes includes: Calculate the checksum of the (x1+1)th to (x1+x2)th bytes of the X bytes, and determine whether the checksum is consistent with the first x1 bytes of the X bytes. If they match, the verification is considered successful.
6. The communication method for an embedded system according to claim 4, characterized in that, After receiving the current data frame, the process also includes: Verify the data information in the current data frame based on the data checksum in the current data frame; If the verification fails, a first response frame is returned to the sender. The type identifier in the first response frame is 0, and the error code is the error code corresponding to the preset verification error, so that the sender can retransmit the current data frame.
7. The communication method for an embedded system according to claim 6, characterized in that, After verifying the data information in the current data frame based on the data checksum in the current data frame, the process further includes: If the verification passes, the values of the status field and parameter field in the current data frame are retrieved; If the receiver is currently in a state of waiting to receive a command frame and the type identifier in the state field value is 0, or the receiver is currently in a state of waiting to receive a response frame and the type identifier in the state field value is 1, then the parameter field value in the current data frame is provided to the callback function for processing. If the error code in the status field is not 0, then according to the error reason corresponding to the error code, the corresponding frame operation is performed, including retransmission and discard.
8. The communication method for an embedded system according to claim 4, characterized in that, After receiving a data frame, the receiver pauses receiving and resumes receiving only after parsing the data information in the data frame.
9. The communication method of the embedded system according to claim 1, characterized in that, The length checksum and data checksum are calculated using the LRC algorithm or the CRC algorithm.
10. A communication system for an embedded system, characterized in that, Includes a first communication terminal and a second communication terminal; The first communication terminal is used to generate a data frame according to a preset communication protocol and send the data frame to the second communication terminal; The second communication terminal is used to receive data frames and parse the data frames according to the communication protocol; The communication protocol includes a transport layer and an application layer; the transport layer defines the frame format of the data frame, and the frame format includes the following fields: frame header, length checksum, length, data checksum, and data information.
Citation Information
Patent Citations
Bluetooth communication method of electric power metering on-site test data
CN106100703A
Data communication framing method based on character escape
CN110912931A
Communication method, apparatus and device, and readable storage medium
CN115314155A
Vehicle-mounted transmission system, data transmission method and vehicle
CN116112523A
User data protocol for internet data communications
US6233248B1
Cited By
Master-slave internal bus communication design method for aerospace embedded system
CN121664582A