Serial communication method and system based on adaptive transceiver mechanism of weighted average algorithm
Through the weighted average algorithm adaptive transceiver mechanism, the serial port read and write cycle is dynamically adjusted, which solves the problems caused by the lack of universality and fixed cycle of the serial communication protocol, and achieves more efficient data transmission and stability.
Patent Information
- Application Number
- CN202411469809.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-21
- Publication Date
- 2025-10-17
- Estimated Expiration
- 2044-10-21
AI Technical Summary
Existing serial communication protocols lack universality, and fixed serial port read/write cycles lead to resource waste or real-time communication issues, as well as insufficient receive buffers.
Adopt weighted average algorithm to adaptively transmit and receive mechanism, dynamically calculate serial port read and write cycle, read data through serial port module and parse data according to frame message structure, add expected function code check and retransmission mechanism.
It improves the real-time performance and stability of serial communication, ensures data integrity and the effectiveness of the receive buffer, and reduces the waste of system resources.
Smart Images

Figure CN119415452B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of serial communication, and particularly relates to a serial communication method and system based on a weighted average algorithm adaptive transceiving mechanism. BACKGROUND
[0002] Serial communication is widely used in many fields, including industrial control, communication equipment, embedded systems, etc. Because of its weak anti-interference ability, the two communication parties generally formulate corresponding serial protocols when sending valid data. The protocol content includes start identification bit , length identification bit , type identification bit , function code identification bit , wherein the start identification bit is usually used to indicate the data packet header, and the length identification bit is used to ensure the accuracy and integrity of the serial data frame. Most serial protocols are specific and not universal.
[0003] Because the serial transmission mode is in units of single bytes, under normal circumstances, the serial data received and processed at a time is usually a combination of multiple frames of data, and the valid serial data needs to be extracted by a method. In addition, the fixed serial read-write cycle is relatively rigid, and a too short cycle will cause the system CPU to be wasted, and a too long cycle will affect the real-time communication and cause the receiving buffer to not be sufficient. SUMMARY
[0004] The present application aims to solve at least one of the technical problems in the background art, and provides a serial communication method and system based on a weighted average algorithm adaptive transceiving mechanism.
[0005] To achieve the above-mentioned purpose, the present application provides a serial communication method based on a weighted average algorithm adaptive transceiving mechanism, comprising:
[0006] S1. The local end and the opposite end agree that the serial protocol at least contains start identification bit , length identification bit , type identification bit , function code identification bit ; the frame message type is divided into local end message, opposite end message, and response message; the local end message and the opposite end message need to fill in the expected response message function code, the local end message type is local end upload, and the opposite end message type is opposite end upload;
[0007] S2. Initialize the serial sending queue , the retransmission queue , the receiving data buffer area , the expected response message function code , and set the initial value of the serial data processing period ;
[0008] S3. The local end actively sends the message or pushes the response message into the sending queue ;Serial port data processing cycle is up, if The queue is not empty, from When the queue pops the header message and the local serial port module writes data, it is determined that the message is a local message and has an expected response function code. , push this message into the retransmission queue , other types of messages do not require this operation;
[0009] S4. Read data to the receiving data buffer through the serial port module , the serial port reading method returns the total length of the read message , the data read includes the messages sent and responded by the other end;
[0010] S5. When processing received data, first read the start flag , then read the length identifier ; If the length indicator Greater than the total message length , the message illegally discards this data; if the length identifier Equal to the total length of the message , the message read this time is a single-frame message; if the length flag Less than the total message length , the message read this time is multi-frame data; by moving the start mark , get the length indicator , according to the start marker and length indicator Get the entire frame of data, according to the length indicator Move start flag , obtain each frame data, then execute the next step;
[0011] S6. After reading the single-frame message, check the type identification bit If it is a message from the other end, there is no need to check the function code identification bit ; If it is a response message, proceed to the next step;
[0012] S7. If the function code of the response message is expected If it is empty, there is no need to check the function code; if the function code of the response message is expected If it is not empty, check the function code identification bit again. , then proceed to the next step;
[0013] S8. If it is the expected response message function code , then delete the resend queue corresponding message; if not the expected response message function code retransmission queue corresponding message out of stack, retransmission will delete the message;
[0014] S9. Record each serial port read-write cycle time and the number of received frame messages According to the serial port read-write cycle time and the number of frame messages weighted average algorithm, the next serial port read-write cycle time The next serial port read-write cycle time is to repeat steps S1-S9.
[0015] According to one aspect of the present application, according to the serial port read-write cycle time and the number of frame messages weighted average algorithm, the next serial port read-write cycle time is:
[0016] ;
[0017] wherein, are the upper and lower time limits of the two serial port read-write cycle time intervals, and are the number of frame messages corresponding to the upper and lower time limits.
[0018] According to one aspect of the present application, in steps S1-S8, when the serial port module reads and parses data, the data is single frame data or multi-frame data, and the sending data is not received in response to the active retransmission mechanism.
[0019] According to one aspect of the present application, when the serial port module receives multi-frame data, it moves the frame message header frame pointer according to the frame message structure and frame message length, extracts each frame data and parses it.
[0020] According to one aspect of the present application, in the frame message structure, the frame message header is fixed as a field length, and the frame message length is the second byte;
[0021] In the frame message structure, the third byte is fixed as the frame message type, and the last two fields are check code for checking the validity of frame message data, the sending end calculates check code filled into the last two bytes of the frame message; after receiving the message, the receiving end needs to recalculate check code, if the two are equal, the message is correct, otherwise the data is invalid.
[0022] To achieve the above object, the present invention further provides a serial communication system based on a weighted average algorithm adaptive transceiver mechanism, comprising:
[0023] Serial port protocol agreement module, the local and remote end agree that the serial port protocol at least contains the start flag bit , length indicator , type identification bit , Function code identification bit ; Frame message types are divided into local message, peer message, and response message; local message and peer message need to fill in the expected response message function code, the local message type is sent by the local end, and the peer message type is sent by the peer end;
[0024] Initialization module, initialize the serial port sending queue , retransmission queue , receive data buffer , expected response message function code , set the initial value of the serial port data processing cycle ;
[0025] Message sending module, the local end actively sends messages or pushes response messages into the sending queue ;Serial port data processing cycle is up, if The queue is not empty, from When the queue pops the header message and the local serial port module writes data, it is determined that the message is a local message and has an expected response function code. , push this message into the retransmission queue , other types of messages do not require this operation;
[0026] Data reading module, reads data to the receiving data buffer area through the serial port module , the serial port reading method returns the total length of the read message , the data read includes the messages sent and responded by the other end;
[0027] The receiving data processing module first reads the start flag when processing the received data , then read the length identifier ; If the length indicator Greater than the total message length , the message illegally discards this data; if the length identifier Equal to the total length of the message , the message read this time is a single-frame message; if the length flag Less than the total message length , the message read this time is multi-frame data; by moving the start mark , get the length indicator , according to the start marker and length indicator Get the entire frame of data, according to the length indicator Move start flag , obtain each frame data, then execute the next step;
[0028] The data verification module reads a single frame message and checks the type identification bit If it is a message from the other end, there is no need to check the function code identification bit ; If it is a response message, proceed to the next step;
[0029] Function code verification module, if the expected response message function code If it is empty, there is no need to check the function code; if the function code of the response message is expected If it is not empty, check the function code identification bit again. , then proceed to the next step;
[0030] Message processing module, if it is the expected response message function code , then delete the resend queue Corresponding message; if it is not the expected response message function code , retransmission queue The corresponding message is popped out of the stack, and retransmission will delete this message;
[0031] Serial port read and write cycle calculation module, records each serial port read and write cycle time and the number of received frame messages , according to the serial port read and write cycle time and frame message count Perform weighted average algorithm to calculate the next serial port read and write cycle time , the next serial port read / write cycle time is up, and steps S1 to S9 are repeated.
[0032] To achieve the above-mentioned objectives, the present invention also provides an electronic device, comprising a processor, a memory, and a computer program stored in the memory and executable on the processor, wherein when the computer program is executed by the processor, the serial communication method based on the weighted average algorithm adaptive transceiver mechanism as described above is implemented.
[0033] To achieve the above objectives, the present invention also provides a computer-readable storage medium, which stores a computer program. When the computer program is executed by a processor, it implements the serial port communication method based on the weighted average algorithm adaptive transceiver mechanism as described above.
[0034] According to the solution of the present invention, the fixed serial port read and write cycle is relatively rigid. If the cycle is too short, it will lead to excessive loss of system resources or empty running. If the cycle is too long, it will affect the real-time performance of communication and insufficient receiving buffer. The present invention uses a weighted average algorithm to dynamically calculate the next cycle time interval to improve the real-time performance of communication.
[0035] When the serial port sends key frame data, it is necessary to confirm that the other end has processed it successfully. The expected function code verification logic is added, and the retransmission mechanism ensures the stability of message sending.
[0036] The serial port receives multiple frames of data, moves the message frame header pointer according to the frame length, accurately extracts and analyzes the data of each frame, and improves the stability and versatility of message reception. BRIEF DESCRIPTION OF THE DRAWINGS
[0037] Figure 1 The flowchart schematically shows a serial communication method based on a weighted average algorithm adaptive transceiver mechanism according to an embodiment of the present invention. DETAILED DESCRIPTION
[0038] The present invention will now be discussed with reference to exemplary embodiments. It should be understood that the embodiments discussed are only for enabling those skilled in the art to better understand and implement the present invention, rather than implying any limitation on the scope of the present invention.
[0039] As used herein, the term "including" and variations thereof are to be interpreted as open-ended terms meaning "including, but not limited to." The term "based on" is to be interpreted as "based, at least in part, on." The terms "one embodiment" and "an embodiment" are to be interpreted as "at least one embodiment."
[0040] Figure 1 The flowchart of the serial communication method based on the weighted average algorithm adaptive transceiver mechanism according to one embodiment of the present invention is schematically shown. Figure 1 As shown, in this embodiment, the serial communication method based on the weighted average algorithm adaptive transceiver mechanism includes:
[0041] S1. The local and remote ends agree that the serial port protocol contains at least a start flag. , length indicator , type identification bit , Function code identification bit ; Frame message types are divided into local message, peer message, and response message; local message and peer message need to fill in the expected response message function code, the local message type is sent by the local end, and the peer message type is sent by the peer end;
[0042] S2. Initialize the serial port sending queue , retransmission queue , receive data buffer , expected response message function code , set serial port data processing period initial value ;
[0043] S3. The local end initiatively issues a class message or a response message stack to the sending queue ; the serial port data processing period is over, and if the queue is not empty, the head message is stacked out of the queue, the local serial port module writes data, and it is judged that the message is a local message and has an expected response function code , the message is stacked to the retransmission queue , and other types of messages do not need this operation ;
[0044] S4. Read data through the serial port module to the receive data buffer , the serial port reading method returns the total length of the read message , and the read data contains the opposite end sending and response message
[0045] S5. When processing the received data, first read the start identification bit , and then read the length identification bit ; if the length identification bit is greater than the total length of the message , the message is illegal and the data is discarded; if the length identification bit is equal to the total length of the message , the read message this time is a single frame message; if the length identification bit is less than the total length of the message , the read message this time is multi-frame data; by moving the start identification bit , the length identification bit is obtained, the whole frame data is obtained according to the start identification bit and the length identification bit , the start identification bit is moved according to the length identification bit , and each frame data is obtained, then the next step is executed
[0046] S6. After reading a single frame message, the type identification bit is checked , if it is a peer message, the function code identification bit does not need to be checked ; if it is a response message, the next step is executed
[0047] S7. If the expected response message function code is empty, the function code does not need to be checked; if the expected response message function code is not empty, the function code identification bit is checked , and the next step is executed
[0048] S8. If the packet is an expected response packet , then delete the retransmission queue ; if the packet is not an expected response packet , then the retransmission queue corresponding to the packet is popped, and the retransmission will delete the packet;
[0049] S9. Record the time of each serial port read-write cycle and the number of received frame packets , and perform a weighted average algorithm according to the time of the serial port read-write cycle and the number of frame packets to calculate the time of the next serial port read-write cycle ; when the time of the next serial port read-write cycle arrives, repeat steps S1-S9.
[0050] Further, according to an embodiment of the present application, the time of the next serial port read-write cycle is calculated by performing a weighted average algorithm according to the time of the serial port read-write cycle and the number of frame packets as follows:
[0051] ;
[0052] wherein are the upper limit time and the lower limit time of the two serial port read-write cycle time intervals, respectively, and are the number of frame packets corresponding to the upper limit time and the lower limit time, respectively.
[0053] Further, according to an embodiment of the present application, in steps S1-S8, when the serial port module reads and parses data, the data is single-frame data or multi-frame data, and the sending data is responded to by an active retransmission mechanism when no response is received.
[0054] Further, according to an embodiment of the present application, when the serial port module receives multi-frame data, the frame packet structure and the frame packet length are used to move the received packet header frame pointer, and each frame of data is extracted and parsed.
[0055] Further, according to an embodiment of the present application, in the frame packet structure, the frame packet header is fixed as a field length, and the frame packet length is a second byte;
[0056] In the frame packet structure, the third byte is fixed as the frame packet type, and the last two fields are a check code used to check the validity of the frame packet data, which is calculated by the sending end before sending and filled into the last two bytes of the frame packet; after receiving the packet by the receiving end, the check code needs to be recalculated The check code is used for checking the validity of the frame message data, and the sending end calculates the check code and fills the last two bytes of the frame message before sending; the receiving end needs to recalculate the check code after receiving the message.
[0057] In the embodiment, the serial port module determines the protocol message structure and the message interaction process according to the protocol, the frame message head is fixed as a field length, and is fixed as a certain field such as , and the frame message length is the second byte, and the entire frame length can be determined;
[0058] According to the current frame head position and the frame message length, the entire complete data frame can be obtained;
[0059] The third byte is fixed as the frame message type, and other types are not processed according to the agreed message type;
[0060] The last two fields are The check code is used for checking the validity of the frame message data, and the sending end calculates the check code and fills the last two bytes of the frame message before sending; the receiving end needs to recalculate the check code after receiving the message. The check code is used for checking the validity of the frame message data, and the sending end calculates the check code and fills the last two bytes of the frame message before sending; the receiving end needs to recalculate the check code after receiving the message.
[0061] In the embodiment, the serial port sends data, and for some very important sending data, it is necessary to ensure that the receiving end receives the message and successfully processes it, and it is necessary to check whether the response message is successful according to the serial port message agreement process, to increase the retransmission mechanism to ensure that the message can be correctly sent and processed by the opposite end.
[0062] According to the above scheme of the application, the fixed serial port read-write cycle is relatively rigid, and a too short cycle will cause excessive system resource consumption or empty running, and a too long cycle will affect the real-time communication, and the receiving buffer is not enough, and the application dynamically calculates the next cycle time interval through the weighted average algorithm, and improves the real-time communication.
[0063] The serial port sends key frame data, and needs to determine that the opposite end successfully processes, increases the expected function code checking logic, and the retransmission mechanism guarantees the stability of the message sending.
[0064] The serial port receives multiple frame data, moves the message frame head pointer according to the frame length, accurately extracts each frame data and parses, and improves the stability and universality of the message receiving.
[0065] Further, in order to achieve the above object, the application also provides a serial port communication system based on a weighted average algorithm adaptive transceiver mechanism, comprising:
[0066] A serial port protocol agreement module, the local end and the opposite end agree that the serial port protocol at least includes start identification bits , length identification bits , type identification bits , and function code identification bits ; Frame message types are divided into local message, peer message, and response message; local message and peer message need to fill in the expected response message function code, the local message type is sent by the local end, and the peer message type is sent by the peer end;
[0067] Initialization module, initialize the serial port sending queue , retransmission queue , receive data buffer , expected response message function code , set the initial value of the serial port data processing cycle ;
[0068] Message sending module, the local end actively sends messages or pushes response messages into the sending queue ;Serial port data processing cycle is up, if The queue is not empty, from When the queue pops the header message and the local serial port module writes data, it is determined that the message is a local message and has an expected response function code. , push this message into the retransmission queue , other types of messages do not require this operation;
[0069] Data reading module, reads data to the receiving data buffer area through the serial port module , the serial port reading method returns the total length of the read message , the data read includes the messages sent and responded by the other end;
[0070] The receiving data processing module first reads the start flag when processing the received data , then read the length identifier ; If the length indicator Greater than the total message length , the message illegally discards this data; if the length identifier Equal to the total length of the message , the message read this time is a single-frame message; if the length flag Less than the total message length , the message read this time is multi-frame data; by moving the start mark , get the length indicator , according to the start marker and length indicator Get the entire frame of data, according to the length indicator Move start flag , obtain each frame data, then execute the next step;
[0071] The data verification module reads a single frame message and checks the type identification bit If it is a message from the other end, there is no need to check the function code identification bit ; If it is a response message, proceed to the next step;
[0072] Function code verification module, if the expected response message function code If it is empty, there is no need to check the function code; if the function code of the response message is expected If it is not empty, check the function code identification bit again. , then proceed to the next step;
[0073] Message processing module, if it is the expected response message function code , then delete the resend queue Corresponding message; if it is not the expected response message function code , retransmission queue The corresponding message is popped out of the stack, and retransmission will delete this message;
[0074] Serial port read and write cycle calculation module, records each serial port read and write cycle time and the number of received frame messages , according to the serial port read and write cycle time and frame message count Perform weighted average algorithm to calculate the next serial port read and write cycle time , the next serial port read / write cycle time is up, and steps S1 to S9 are repeated.
[0075] Further, according to an embodiment of the present invention, according to the serial port read and write cycle time and frame message count Perform weighted average algorithm to calculate the next serial port read and write cycle time for:
[0076] ;
[0077] in, They are the upper limit and lower limit of the time interval between the two serial port read and write cycles, and The numbers of frame messages corresponding to the upper and lower time limits respectively.
[0078] Furthermore, according to an embodiment of the present invention, when the serial port module reads and parses data, the data is single-frame data or multi-frame data, and responds to an active retransmission mechanism when no response is received when sending data.
[0079] Furthermore, according to an embodiment of the present invention, when the serial port module receives multiple frames of data, it moves the frame pointer of the received message header according to the frame message structure and the frame message length, extracts each frame of data, and parses it.
[0080] Further, according to an embodiment of the present application, the frame message header in the frame message structure is fixed as a field length, and the frame message length is a second byte;
[0081] The third byte in the frame message structure is fixed as a frame message type, and the last two fields are The check code is used for checking the validity of the frame message data, and is calculated by the sending end before sending The check code is filled in the last two bytes of the frame message; after receiving the message, the receiving end needs to recalculate the check code, and if the two check codes are equal, the message is correct, otherwise the data is invalid.
[0082] In the embodiment, the serial port module is clear about the protocol message structure and message interaction process according to the protocol, the frame message header is fixed as a field length, and is fixed as a certain field such as The frame message length is a second byte, and the entire frame length can be determined;
[0083] According to the current frame header position and the frame message length, the entire complete data frame can be obtained;
[0084] The third byte is fixed as a frame message type, and other types are not processed according to the agreed message type;
[0085] The last two fields are The check code is used for checking the validity of the frame message data, and is calculated by the sending end before sending The check code is filled in the last two bytes of the frame message; after receiving the message, the receiving end needs to recalculate the check code, and if the two check codes are equal, the message is correct, otherwise the data is invalid.
[0086] In the embodiment, the serial port sends data, and for some very important sending data, it is necessary to ensure that the receiving end receives the message and successfully processes it, and it is necessary to check whether the response message is successful according to the serial port message agreement process, to increase the retransmission mechanism to ensure that the message can be correctly sent and processed by the opposite end.
[0087] According to the above scheme of the present application, the fixed serial port read-write cycle is relatively rigid, and a too short cycle will cause excessive system resource consumption or empty running, and a too long cycle will affect the real-time communication, and the receiving buffer is not enough, etc. The present application dynamically calculates the next cycle time interval by using the weighted average algorithm, to improve the real-time communication.
[0088] The serial port sends key frame data, and needs to determine that the opposite end successfully processes it, to increase the expected function code check logic, and the retransmission mechanism ensures the stability of the message sending.
[0089] The serial port receives multiple frame data, moves the frame header pointer according to the frame length, accurately extracts each frame data and parses it, to improve the stability and universality of the message receiving.
[0090] Further, to achieve the above object, the present application provides an electronic device, comprising a processor, a memory, and a computer program stored in the memory and executable on the processor, and the computer program implements the serial communication method based on the adaptive transceiver mechanism of the weighted average algorithm when executed by the processor.
[0091] Further, to achieve the above object, the present application provides a computer readable storage medium, and the computer readable storage medium stores a computer program, and the computer program implements the serial communication method based on the adaptive transceiver mechanism of the weighted average algorithm when executed by a processor.
[0092] Those skilled in the art can understand that the modules and algorithm steps described in combination with the embodiments disclosed herein can be realized by electronic hardware or a combination of computer software and electronic hardware. Whether the functions are realized in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of the present application.
[0093] Those skilled in the art can clearly understand that, for the convenience and brevity of the description, the specific working process of the above-described device and equipment can refer to the corresponding process in the foregoing method embodiments, which will not be repeated here.
[0094] In the embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the modules is only a logical function division. In actual implementation, another division mode can be used. For example, a plurality of modules or components can be combined or integrated into another system, or some features can be ignored or not executed. In addition, the coupling or direct coupling or communication connection between the displayed or discussed mutual ones can be indirect coupling or communication connection through some interfaces, devices or modules, and can be electrical, mechanical or other forms.
[0095] The modules described as separate components can or can not be physically separated, and the components shown as modules can or can not be physical modules, i.e. they can be located in one place or distributed on a plurality of network modules. Some or all of the modules can be selected according to actual needs to achieve the purpose of the embodiments of the present application.
[0096] In addition, each functional module in the embodiments of the present application can be integrated into a processing module, or each module can exist physically independently, or two or more modules can be integrated into one module.
[0097] If the functions are implemented in the form of software function modules and sold or used as independent products, they can be stored in a computer readable storage medium. Based on this understanding, the technical solutions of the present application or the parts of the present application that essentially contribute to the prior art can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes a number of instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the energy-saving signal transmission / reception method of the embodiments of the present application. The aforementioned storage medium includes: a U disk, a mobile hard disk, a ROM, a RAM, a magnetic disk or an optical disk, and various program code storage media.
[0098] The above description is merely preferred embodiments of the present application and a description of the principles of the technology used. Those skilled in the art should understand that the scope of the application involved in the present application is not limited to the technical solutions formed by the specific combinations of the above technical features, and should also cover other technical solutions formed by any combination of the above technical features or their equivalent features without departing from the inventive concept. For example, the above features can be replaced with the technical features disclosed in the present application (but not limited to) having similar functions to form technical solutions.
[0099] It should be understood that the size of the serial number of each step in the summary and embodiments of the present application does not absolutely mean the order of execution, and the execution order of each process should be determined according to its function and inherent logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.
Claims
1. A serial communication method based on a weighted average algorithm adaptive transceiver mechanism, characterized in that: include: S1. The local and remote ends agree that the serial port protocol contains at least a start flag. , length indicator , type identification bit , Function code identification bit ; Frame message types are divided into local message, peer message, and response message; local message and peer message need to fill in the expected response message function code, the local message type is sent by the local end, and the peer message type is sent by the peer end; S2. Initialize the serial port sending queue , retransmission queue , receive data buffer , expected response message function code , set the initial value of the serial port data processing cycle ; S3. The local end actively sends the message or pushes the response message into the sending queue ;Serial port data processing cycle is up, if The queue is not empty, from When the queue pops the header message and the local serial port module writes data, it is determined that the message is a local message and has an expected response function code. , push this message into the retransmission queue , other types of messages do not require this operation; S4. Read data to the receiving data buffer through the serial port module , the serial port reading method returns the total length of the read message , the data read includes the messages sent and responded by the other end; S5. When processing received data, first read the start flag , then read the length identifier ; If the length indicator Greater than the total message length , the message illegally discards this data; if the length identifier Equal to the total length of the message , the message read this time is a single-frame message; if the length flag Less than the total message length , the message read this time is multi-frame data; by moving the start mark , get the length indicator , according to the start marker and length indicator Get the entire frame of data, according to the length indicator Move start flag , obtain each frame data, then execute the next step; S6. After reading the single-frame message, check the type identification bit If it is a peer message, there is no need to check the function code identification bit ; If it is a response message, proceed to the next step; S7. If the function code of the response message is expected If it is empty, there is no need to check the function code; if the function code of the response message is expected If it is not empty, check the function code identification bit again. , then proceed to the next step; S8. If it is the expected response message function code , then delete the resend queue Corresponding message; if it is not the expected response message function code , retransmission queue The corresponding message is popped out of the stack, and retransmission will delete this message; S9. Record the read and write cycle time of each serial port and the number of received frame messages , according to the serial port read and write cycle time and frame message count Perform weighted average algorithm to calculate the next serial port read and write cycle time , the next serial port read / write cycle time is up, and steps S1 to S9 are repeated.
2. The serial communication method based on the weighted average algorithm adaptive transceiver mechanism according to claim 1, characterized in that: According to the serial port read and write cycle time and frame message count Perform weighted average algorithm to calculate the next serial port read and write cycle time for: ; in, They are the upper limit and lower limit of the time interval between the two serial port read and write cycles, and The numbers of frame messages corresponding to the upper and lower time limits respectively.
3. The serial communication method based on the weighted average algorithm adaptive transceiver mechanism according to claim 1, characterized in that: In step S1 to step S8, when the serial port module reads and analyzes the data, the data is single-frame data or multi-frame data, and responds to the active retransmission mechanism when no response is received when sending data.
4. The serial communication method based on the weighted average algorithm adaptive transceiver mechanism according to claim 1, characterized in that: When the serial port module receives multiple frames of data, it moves the frame pointer of the received message header according to the frame message structure and the frame message length, extracts each frame of data and parses it.
5. The serial communication method based on the weighted average algorithm adaptive transceiver mechanism according to claim 4, characterized in that: In the frame message structure, the frame message header is fixed to a field length, and the frame message length is the second byte; The third byte in the frame message structure is fixed as the frame message type, and the last two fields are The check code is used to check the validity of the frame message data. The sender calculates it before sending. The check code is filled in the last two bytes of the frame message; after the receiving end receives the message, it needs to be recalculated Check code, if the two are equal, the message is correct, otherwise the data is invalid.
6. A serial communication system based on a weighted average algorithm adaptive transceiver mechanism, characterized in that: include: Serial port protocol agreement module, the local and remote end agree that the serial port protocol at least contains the start flag bit , length indicator , type identification bit , Function code identification bit ; Frame message types are divided into local message, peer message, and response message; local message and peer message need to fill in the expected response message function code, the local message type is sent by the local end, and the peer message type is sent by the peer end; Initialization module, initialize the serial port sending queue , retransmission queue , receive data buffer , expected response message function code , set the initial value of the serial port data processing cycle ; Message sending module, the local end actively sends messages or pushes response messages into the sending queue ;Serial port data processing cycle is up, if The queue is not empty, from When the queue pops the header message and the local serial port module writes data, it is determined that the message is a local message and has an expected response function code. , push this message into the retransmission queue , other types of messages do not require this operation; Data reading module, reads data to the receiving data buffer area through the serial port module , the serial port reading method returns the total length of the read message , the data read includes the messages sent and responded by the other end; The receiving data processing module first reads the start flag when processing the received data , then read the length identifier ; If the length indicator Greater than the total message length , the message illegally discards this data; if the length identifier Equal to the total length of the message , the message read this time is a single-frame message; if the length flag Less than the total message length , the message read this time is multi-frame data; by moving the start mark , get the length indicator , according to the start marker and length indicator Get the entire frame of data, according to the length indicator Move start flag , obtain each frame data, then execute the next step; The data verification module reads a single frame message and checks the type identification bit If it is a message from the other end, there is no need to check the function code identification bit ; If it is a response message, proceed to the next step; Function code verification module, if the expected response message function code If it is empty, there is no need to check the function code; if the function code of the response message is expected If it is not empty, check the function code identification bit again. , then proceed to the next step; Message processing module, if it is the expected response message function code , then delete the resend queue Corresponding message; if it is not the expected response message function code , retransmission queue The corresponding message is popped out of the stack, and retransmission will delete this message; Serial port read and write cycle calculation module, records each serial port read and write cycle time and the number of received frame messages , according to the serial port read and write cycle time and frame message count Perform weighted average algorithm to calculate the next serial port read and write cycle time , the next serial port read / write cycle time is up, and steps S1 to S9 are repeated.
7. An electronic device, characterized in that The invention comprises a processor, a memory and a computer program stored in the memory and executable on the processor, wherein when the computer program is executed by the processor, the method for serial communication based on the adaptive transceiver mechanism of the weighted average algorithm according to any one of claims 1 to 5 is implemented.
8. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a computer program, and when the computer program is executed by the processor, the serial communication method based on the weighted average algorithm adaptive transceiver mechanism according to any one of claims 1 to 5 is implemented.
Citation Information
Patent Citations
Synchronous serial communication interface and communication method thereof
CN103427945A
RS422 universal serial port communication method based on FPGA
CN116226015A