Serial port communication method and system based on weighted average algorithm adaptive transceiving mechanism
By using a weighted average algorithm to adjust the read and write cycles adaptively, combined with the retransmission mechanism and CRC verification, the problem of inefficient multi-frame data processing and period adjustment in serial communication is solved, and more efficient and stable Communication.
Patent Information
- Application Number
- CN202411469809.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-21
- Publication Date
- 2025-02-11
- Estimated Expiration
- 2044-10-21
AI Technical Summary
Serial communication has problems of inefficiency and real-time impact when processing multi-frame data and dynamically adjusting read and write cycles.
Adaptive transmission and reception mechanism based on weighted average algorithm is adopted to dynamically calculate the serial port read and write cycle time, improve the real-time communication, and ensure the stability and accuracy of data transmission through retransmission mechanism and CRC verification.
It improves the real-time and stability of serial communication, avoids resource waste and communication delay caused by fixed cycles, and enhances the guarantee of successful peer-side processing.
Smart Images

Figure CN119415452A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of serial communication, and in particular to a serial communication method and system based on a weighted average algorithm adaptive transceiver mechanism. Background Art
[0002] Serial communication is widely used in many fields, including industrial control, communication equipment, embedded systems, etc. Due to its weak anti-interference ability, the two parties in general will formulate corresponding serial port protocols when sending valid data. The protocol content includes the start identifier pkgheader, the length identifier pkglen, the type identifier pkgtype, and the function code identifier pkgfun. The start identifier pkgheader is usually used to indicate the header of this data packet, and the length identifier pkglen is used to ensure the accuracy and integrity of the serial port data frame. Most serial port protocols are specific and not universal.
[0003] Since the serial port transmission method is based on a single byte, usually, the serial port data received and processed at one time is usually a combination of multiple frames of data, and a method is needed to extract valid serial port data. In addition, the fixed serial port read and write cycle is relatively rigid. If the cycle is too short, it will cause loss of system CPU, and if the cycle is too long, it will affect the real-time performance of communication and insufficient receiving buffer. Summary of the invention
[0004] The purpose of the present invention is to solve at least one technical problem in the background technology and to provide a serial communication method and system based on a weighted average algorithm adaptive transceiver mechanism.
[0005] To achieve the above object, the present invention provides a serial communication method based on a weighted average algorithm adaptive transceiver mechanism, comprising:
[0006] S1. The local and remote ends agree that the serial port protocol at least includes the start flag pkgheader, the length flag pkglen, the type flag pkgtype, and the function code flag pkgfun; the frame message type is divided into local message, remote message, and response message; the local message and the remote message need to fill in the expected response message function code, the local message type is sent by the local end, and the remote message type is sent by the remote end;
[0007] S2. Initialize the serial port send queue SendBuff1, retransmit queue SendBuff2, receive data buffer RecvBuff, expect response message function code ExcpectFun, and set the serial port data processing cycle initial value InitTime;
[0008] S3. The local end actively sends the class message or the response message and pushes it to the sending queue SendBuff1; when the serial port data processing cycle is up, if the SendBuff1 queue is not empty, the head message is popped from the SendBuff1 queue. When the local serial port module writes data, it is determined that the message is a local message and has the expected response function code ExcpectFun, and the message is pushed to the retransmission queue SendBuff2. This operation is not required for other types of messages;
[0009] S4. Read data to the receive data buffer RecvBuff through the serial port module. The serial port reading method returns the total length of the read message msglen. The read data includes the send and reply messages of the other end.
[0010] S5. When processing received data, first read the start identification bit pkgheader, and then read the length identification bit pkglen; if the length identification bit pkglen is greater than the total message length msglen, the message illegally discards this data; if the length identification bit pkglen is equal to the total message length msglen, the message read this time is a single-frame message; if the length identification bit pkglen is less than the total message length msglen, the message read this time is multi-frame data; by moving the start identification bit pkgheader, obtain the length identification bit pkglen, obtain the entire frame of data according to the start identification bit pkgheader and the length identification bit pkglen, move the start identification bit pkgheader according to the length identification bit pkglen, obtain each frame of data, and then execute the next step;
[0011] S6. After reading the single-frame message, check the type identification bit pkgtype. If it is a message from the other end, there is no need to check the function code identification bit pkgfun; if it is a response message, proceed to the next step;
[0012] S7. If the expected response message function code ExcpectFun is empty, there is no need to check the function code; if the expected response message function code ExcpectFun is not empty, then check the function code flag pkgfun, and then execute the next step;
[0013] S8. If the expected response message function code ExcpectFun, the corresponding message in the retransmission queue SendBuff2 is deleted; if the expected response message function code ExcpectFun is not expected, the corresponding message in the retransmission queue SendBuff2 is popped, and the retransmission will delete this message;
[0014] S9. Record each serial port read / write cycle time time and the number of received frame messages pkgnum, perform a weighted average algorithm based on the serial port read / write cycle time time and the number of frame messages pkgnum, calculate the next serial port read / write cycle time time, and when the next serial port read / write cycle time arrives, repeat steps S1-S9.
[0015] According to one aspect of the present invention, a weighted average algorithm is performed based on the serial port read / write cycle time time and the frame message number pkgnum to calculate the next serial port read / write cycle time time as:
[0016]
[0017] Wherein, t1 and t2 are the upper limit time and lower limit time of the time interval between the two serial ports' read and write cycles, respectively, and pkgnum 1 and pkgnum 2 are the frame message numbers corresponding to the upper limit time and the lower limit time, respectively.
[0018] According to one aspect of the present invention, in steps S1 to S8, 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.
[0019] According to one aspect 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.
[0020] According to one aspect of the present invention, in the frame message structure, the frame message header is fixed to a field length, and the frame message length is the second byte;
[0021] The third byte in the frame message structure is fixed as the frame message type, and the two fields at the end are CRC check codes used to check the validity of the frame message data. The sender calculates the CRC check code before sending and fills it into the last two bytes of the frame message; after the receiving end receives the message, it needs to recalculate the CRC 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 also provides a serial communication system based on a weighted average algorithm adaptive transceiver mechanism, comprising:
[0023] Serial port protocol agreement module, the serial port protocol agreed by the local end and the opposite end at least includes the start identifier pkgheader, the length identifier pkglen, the type identifier pkgtype, and the function code identifier pkgfun; the frame message type is divided into local message, opposite end message, and response message; the local message and the opposite end message need to fill in the expected response message function code, the local message type is sent by the local end, and the opposite end message type is sent by the opposite end;
[0024] Initialization module, initializes the serial port sending queue SendBuff1, retransmission queue SendBuff2, receiving data buffer RecvBuff, expects the response message function code ExcpectFun, and sets the initial value InitTime of the serial port data processing cycle;
[0025] Message sending module: The local end actively sends messages or response messages and pushes them into the sending queue SendBuff1; when the serial port data processing cycle is up, if the SendBuff1 queue is not empty, the head message is popped from the SendBuff1 queue. When the local serial port module writes data, it is determined that the message is a local message and has the expected response function code ExcpectFun, and the message is pushed into the retransmission queue SendBuff2. This operation is not required for other types of messages.
[0026] The data reading module reads data to the receiving data buffer RecvBuff through the serial port module. The serial port reading method returns the total length of the read message msglen. The read data includes the send and reply messages of the other end.
[0027] The receiving data processing module first reads the start identification bit pkgheader and then reads the length identification bit pkglen when processing the received data; if the length identification bit pkglen is greater than the total message length msglen, the message illegally discards this data; if the length identification bit pkglen is equal to the total message length msglen, the message read this time is a single-frame message; if the length identification bit pkglen is less than the total message length msglen, the message read this time is multi-frame data; by moving the start identification bit pkgheader, the length identification bit pkglen is obtained, and the whole frame data is obtained according to the start identification bit pkgheader and the length identification bit pkglen, the start identification bit pkgheader is moved according to the length identification bit pkglen to obtain each frame of data, and then the next step is executed;
[0028] The data verification module, after reading a single-frame message, verifies the type identification bit pkgtype. If it is a peer message, there is no need to verify the function code identification bit pkgfun; if it is a response message, proceed to the next step;
[0029] Function code verification module: if the expected response message function code ExcpectFun is empty, there is no need to verify the function code; if the expected response message function code ExcpectFun is not empty, then verify the function code identification bit pkgfun, and then execute the next step;
[0030] The message processing module deletes the corresponding message in the retransmission queue SendBuff2 if the function code of the expected response message is ExcpectFun; if the function code of the expected response message is not ExcpectFun, the corresponding message in the retransmission queue SendBuff2 is popped out, and the message will be deleted when it is retransmitted;
[0031] The serial port read / write cycle calculation module records each serial port read / write cycle time time and the number of received frame messages pkgnum, performs a weighted average algorithm based on the serial port read / write cycle time time and the number of frame messages pkgnum, calculates the next serial port read / write cycle time time, and when the next serial port read / write cycle time arrives, repeats steps S1 to S9.
[0032] To achieve the above-mentioned purpose, the present invention also provides an electronic device, including 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 port 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, on which a computer program is stored. When the computer program is executed by a processor, the serial port communication method based on the weighted average algorithm adaptive transceiver mechanism as described above is implemented.
[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 the problem of 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, add the expected function code verification logic, and the retransmission mechanism to ensure 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 to enable those skilled in the art to better understand and thus 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 schematically shows a serial communication method based on a weighted average algorithm adaptive transceiver mechanism according to an embodiment of the present invention. 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 at least includes the start flag pkgheader, the length flag pkglen, the type flag pkgtype, and the function code flag pkgfun; the frame message type is divided into local message, remote message, and response message; the local message and the remote message need to fill in the expected response message function code, the local message type is sent by the local end, and the remote message type is sent by the remote end;
[0042] S2. Initialize the serial port send queue SendBuff1, retransmit queue SendBuff2, receive data buffer RecvBuff, expect response message function code ExcpectFun, and set the serial port data processing cycle initial value InitTime;
[0043] S3. The local end actively sends the class message or the response message and pushes it to the sending queue SendBuff1; when the serial port data processing cycle is up, if the SendBuff1 queue is not empty, the head message is popped from the SendBuff1 queue. When the local serial port module writes data, it is determined that the message is a local message and has the expected response function code ExcpectFun, and the message is pushed to the retransmission queue SendBuff2. This operation is not required for other types of messages;
[0044] S4. Read data to the receive data buffer RecvBuff through the serial port module. The serial port reading method returns the total length of the read message msglen. The read data includes the send and reply messages of the other end.
[0045] S5. When processing received data, first read the start identification bit pkgheader, and then read the length identification bit pkglen; if the length identification bit pkglen is greater than the total message length msglen, the message illegally discards this data; if the length identification bit pkglen is equal to the total message length msglen, the message read this time is a single-frame message; if the length identification bit pkglen is less than the total message length msglen, the message read this time is multi-frame data; by moving the start identification bit pkgheader, obtain the length identification bit pkglen, obtain the entire frame of data according to the start identification bit pkgheader and the length identification bit pkglen, move the start identification bit pkgheader according to the length identification bit pkglen, obtain each frame of data, and then execute the next step;
[0046] S6. After reading the single-frame message, check the type identification bit pkgtype. If it is a message from the other end, there is no need to check the function code identification bit pkgfun; if it is a response message, proceed to the next step;
[0047] S7. If the expected response message function code ExcpectFun is empty, there is no need to check the function code; if the expected response message function code ExcpectFun is not empty, then check the function code flag pkgfun, and then execute the next step;
[0048] S8. If the expected response message function code ExcpectFun, the corresponding message in the retransmission queue SendBuff2 is deleted; if the expected response message function code ExcpectFun is not expected, the corresponding message in the retransmission queue SendBuff2 is popped, and the retransmission will delete this message;
[0049] S9. Record each serial port read / write cycle time time and the number of received frame messages pkgnum, perform a weighted average algorithm based on the serial port read / write cycle time time and the number of frame messages pkgnum, calculate the next serial port read / write cycle time time, and when the next serial port read / write cycle time arrives, repeat steps S1-S9.
[0050] Further, according to an embodiment of the present invention, a weighted average algorithm is performed based on the serial port read / write cycle time time and the frame message number pkgnum to calculate the next serial port read / write cycle time time as:
[0051] time=(time1*pkgnum1+time2*pkgnum2+...) / (pkgnum1+pkgnum2+...)
[0052] time∈[t1,t2];
[0053] Wherein, t1 and t2 are the upper limit time and lower limit time of the time interval between the two serial ports' read and write cycles, respectively, and pkgnum 1 and pkgnum 2 are the frame message numbers corresponding to the upper limit time and the lower limit time, respectively.
[0054] Further, according to an embodiment of the present invention, in step S1 to step S8, when the serial port module reads and parses the 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 the data.
[0055] Further, 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.
[0056] Further, according to an embodiment of the present invention, the frame message header in the frame message structure is fixed to a field length, and the frame message length is the second byte;
[0057] The third byte in the frame message structure is fixed as the frame message type, and the last two fields are CRC check codes used to check the validity of the frame message data. The sender calculates the CRC check code before sending and fills it into the last two bytes of the frame message; after the receiver receives the message, it needs to recalculate the CRC check code. If the two are equal, the message is correct, otherwise the data is invalid.
[0058] In this implementation, the serial port module specifies the protocol message structure and message interaction process according to the protocol, the frame message header is fixed to a field length, fixed to a certain field such as 0x68, and the frame message length is the second byte, which can determine the entire frame length;
[0059] According to the current frame header position and frame message length, the entire data frame can be obtained;
[0060] The third byte is fixed as the frame message type, strictly following the agreed message type, and other types are not processed;
[0061] The two fields at the end are CRC check codes used to check the validity of the frame message data. The sender calculates the check code before sending and fills it into the last two bytes of the frame message. After the receiver receives the message, it needs to recalculate the CRC check code. If the two are equal, the message is correct, otherwise the data is invalid.
[0062] In this embodiment, the serial port sends data. For some very important data, it is necessary to ensure that the receiver receives the message and processes it successfully. It is necessary to verify whether the response message is successful according to the serial port message agreement process, and add a retransmission mechanism to ensure that the message can be sent correctly and can be processed by the other end.
[0063] According to the above scheme 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 the problem of 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.
[0064] When the serial port sends key frame data, it is necessary to confirm that the other end has processed it successfully, add the expected function code verification logic, and the retransmission mechanism to ensure the stability of message sending.
[0065] 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.
[0066] Furthermore, to achieve the above object, the present invention also provides a serial communication system based on a weighted average algorithm adaptive transceiver mechanism, comprising:
[0067] Serial port protocol agreement module, the serial port protocol agreed by the local end and the opposite end at least includes the start identifier pkgheader, the length identifier pkglen, the type identifier pkgtype, and the function code identifier pkgfun; the frame message type is divided into local message, opposite end message, and response message; the local message and the opposite end message need to fill in the expected response message function code, the local message type is sent by the local end, and the opposite end message type is sent by the opposite end;
[0068] Initialization module, initializes the serial port sending queue SendBuff1, retransmission queue SendBuff2, receiving data buffer RecvBuff, expects the response message function code ExcpectFun, and sets the initial value InitTime of the serial port data processing cycle;
[0069] Message sending module: The local end actively sends messages or response messages and pushes them into the sending queue SendBuff1; when the serial port data processing cycle is up, if the SendBuff1 queue is not empty, the head message is popped from the SendBuff1 queue. When the local serial port module writes data, it is determined that the message is a local message and has the expected response function code ExcpectFun, and the message is pushed into the retransmission queue SendBuff2. This operation is not required for other types of messages.
[0070] The data reading module reads data to the receiving data buffer RecvBuff through the serial port module. The serial port reading method returns the total length of the read message msglen. The read data includes the send and reply messages of the other end.
[0071] The receiving data processing module first reads the start identification bit pkgheader and then reads the length identification bit pkglen when processing the received data; if the length identification bit pkglen is greater than the total message length msglen, the message illegally discards this data; if the length identification bit pkglen is equal to the total message length msglen, the message read this time is a single-frame message; if the length identification bit pkglen is less than the total message length msglen, the message read this time is multi-frame data; by moving the start identification bit pkgheader, the length identification bit pkglen is obtained, and the whole frame data is obtained according to the start identification bit pkgheader and the length identification bit pkglen, the start identification bit pkgheader is moved according to the length identification bit pkglen to obtain each frame of data, and then the next step is executed;
[0072] The data verification module, after reading a single-frame message, verifies the type identification bit pkgtype. If it is a peer message, there is no need to verify the function code identification bit pkgfun; if it is a response message, proceed to the next step;
[0073] Function code verification module: if the expected response message function code ExcpectFun is empty, there is no need to verify the function code; if the expected response message function code ExcpectFun is not empty, then verify the function code identification bit pkgfun, and then execute the next step;
[0074] The message processing module deletes the corresponding message in the retransmission queue SendBuff2 if the function code of the expected response message is ExcpectFun; if the function code of the expected response message is not ExcpectFun, the corresponding message in the retransmission queue SendBuff2 is popped out, and the message will be deleted when it is retransmitted;
[0075] The serial port read / write cycle calculation module records each serial port read / write cycle time time and the number of received frame messages pkgnum, performs a weighted average algorithm based on the serial port read / write cycle time time and the number of frame messages pkgnum, calculates the next serial port read / write cycle time time, and when the next serial port read / write cycle time arrives, repeats steps S1 to S9.
[0076] Further, according to an embodiment of the present invention, a weighted average algorithm is performed based on the serial port read / write cycle time time and the frame message number pkgnum to calculate the next serial port read / write cycle time time as:
[0077] time=(time1*pkgnum1+time2*pkgnum2+...) / (pkgnum1+pkgnum2+...)
[0078] time∈[t1,t2];
[0079] Wherein, t1 and t2 are the upper limit time and lower limit time of the time interval between the two serial ports' read and write cycles, respectively, and pkgnum 1 and pkgnum 2 are the frame message numbers corresponding to the upper limit time and the lower limit time, respectively.
[0080] Further, 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.
[0081] Further, 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.
[0082] Further, according to an embodiment of the present invention, the frame message header in the frame message structure is fixed to a field length, and the frame message length is the second byte;
[0083] The third byte in the frame message structure is fixed as the frame message type, and the last two fields are CRC check codes used to check the validity of the frame message data. The sender calculates the CRC check code before sending and fills it into the last two bytes of the frame message; after the receiver receives the message, it needs to recalculate the CRC check code. If the two are equal, the message is correct, otherwise the data is invalid.
[0084] In this implementation, the serial port module specifies the protocol message structure and message interaction process according to the protocol, the frame message header is fixed to a field length, fixed to a certain field such as 0x68, and the frame message length is the second byte, which can determine the entire frame length;
[0085] According to the current frame header position and frame message length, the entire data frame can be obtained;
[0086] The third byte is fixed as the frame message type, strictly following the agreed message type, and other types are not processed;
[0087] The two fields at the end are CRC check codes used to check the validity of the frame message data. The sender calculates the check code before sending and fills it into the last two bytes of the frame message. After the receiver receives the message, it needs to recalculate the CRC check code. If the two are equal, the message is correct, otherwise the data is invalid.
[0088] In this embodiment, the serial port sends data. For some very important data, it is necessary to ensure that the receiver receives the message and processes it successfully. It is necessary to verify whether the response message is successful according to the serial port message agreement process, and add a retransmission mechanism to ensure that the message can be sent correctly and can be processed by the other end.
[0089] According to the above scheme 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 the problem of 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.
[0090] When the serial port sends key frame data, it is necessary to confirm that the other end has processed it successfully, add the expected function code verification logic, and the retransmission mechanism to ensure the stability of message sending.
[0091] 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.
[0092] Furthermore, to achieve the above-mentioned purpose, the present invention also provides an electronic device, including 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.
[0093] Furthermore, to achieve the above-mentioned purpose, the present invention also provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the serial port communication method based on the weighted average algorithm adaptive transceiver mechanism as described above is implemented.
[0094] Those skilled in the art will appreciate that the modules and algorithm steps described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present invention.
[0095] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the above-described devices and equipment can refer to the corresponding processes in the aforementioned method implementation methods, and will not be repeated here.
[0096] 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. There may be other division methods in actual implementation, such as multiple modules or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or modules, which can be electrical, mechanical or other forms.
[0097] The modules described as separate components may or may not be physically separated, and the components shown as modules may or may not be physical modules, that is, they may be located in one place or distributed on multiple network modules. Some or all of the modules may be selected according to actual needs to achieve the purpose of the embodiments of the present invention.
[0098] In addition, each functional module in the embodiment of the present invention may be integrated into one processing module, or each module may exist physically separately, or two or more modules may be integrated into one module.
[0099] 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 solution of the present invention can be essentially or partly embodied in the form of a software product that contributes to the prior art. The computer software product is stored in a storage medium and includes several instructions for enabling a computer device (which can be a personal computer, server, or network device, etc.) to perform all or part of the steps of the energy-saving signal sending / receiving method of each embodiment of the present invention. The aforementioned storage medium includes: various media that can store program codes, such as USB flash drives, mobile hard disks, ROM, RAM, magnetic disks, or optical disks.
[0100] The above description is only a preferred embodiment of the present application and an explanation of the technical principles used. Those skilled in the art should understand that the scope of the invention involved in the present application is not limited to the technical solution formed by a specific combination of the above technical features, but 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 are replaced with the technical features with similar functions disclosed in this application (but not limited to) by each other.
[0101] It should be understood that the size of the serial numbers of each step in the content of the invention and the implementation methods of the present invention does not absolutely mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the implementation methods of the present invention.
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 at least includes the start flag pkgheader, the length flag pkglen, the type flag pkgtype, and the function code flag pkgfun; the frame message type is divided into local message, remote message, and response message; the local message and the remote message need to fill in the expected response message function code, the local message type is sent by the local end, and the remote message type is sent by the remote end; S2. Initialize the serial port send queue SendBuff1, retransmit queue SendBuff2, receive data buffer RecvBuff, expect response message function code ExcpectFun, and set the serial port data processing cycle initial value InitTime; S3. The local end actively sends the class message or the response message and pushes it to the sending queue SendBuff1; when the serial port data processing cycle is up, if the SendBuff1 queue is not empty, the head message is popped from the SendBuff1 queue. When the local serial port module writes data, it is determined that the message is a local message and has the expected response function code ExcpectFun, and the message is pushed to the retransmission queue SendBuff2. This operation is not required for other types of messages; S4. Read data to the receive data buffer RecvBuff through the serial port module. The serial port reading method returns the total length of the read message msglen. The read data includes the send and reply messages of the other end. S5. When processing received data, first read the start identifier pkgheader, then read the length identifier pkglen; if the length identifier pkglen is greater than the total message length msglen, the message is illegal and the data is discarded; If the length flag pkglen is equal to the total message length msglen, the message read this time is a single-frame message; If the length flag pkglen is less than the total message length msglen, the message read this time is multi-frame data; by moving the start flag pkgheader, obtain the length flag pkglen, obtain the entire frame data according to the start flag pkgheader and the length flag pkglen, move the start flag pkgheader according to the length flag pkglen, obtain each frame data, and then execute the next step; S6. After reading the single-frame message, check the type identification bit pkgtype. If it is a message from the other end, there is no need to check the function code identification bit pkgfun; if it is a response message, proceed to the next step; S7. If the expected response message function code ExcpectFun is empty, there is no need to check the function code; if the expected response message function code ExcpectFun is not empty, then check the function code flag pkgfun, and then execute the next step; S8. If the expected response message function code ExcpectFun, the corresponding message in the retransmission queue SendBuff2 is deleted; if the expected response message function code ExcpectFun is not expected, the corresponding message in the retransmission queue SendBuff2 is popped, and the retransmission will delete this message; S9. Record each serial port read / write cycle time time and the number of received frame messages pkgnum, perform a weighted average algorithm based on the serial port read / write cycle time time and the number of frame messages pkgnum, calculate the next serial port read / write cycle time time, and when the next serial port read / write cycle time arrives, repeat steps S1-S9.
2. The serial communication method based on the weighted average algorithm adaptive transceiver mechanism according to claim 1 is characterized in that: According to the serial port read and write cycle time time and the frame message number pkgnum, a weighted average algorithm is used to calculate the next serial port read and write cycle time time as follows: time=(time1*pkgnum1+time2*pkgnum2+...) / (pkgnum1+pkgnum2+...) time∈[t1,t2]; Wherein, t1 and t2 are the upper limit time and lower limit time of the time interval between the two serial ports' read and write cycles, respectively, and pkgnum 1 and pkgnum 2 are the frame message numbers corresponding to the upper limit time and the lower limit time, 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 parses 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 the 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 receiving 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 is 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 two fields at the end are CRC check codes used to check the validity of the frame message data. The sender calculates the CRC check code before sending and fills it into the last two bytes of the frame message; after the receiving end receives the message, it needs to recalculate the CRC 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 serial port protocol agreed by the local end and the opposite end at least includes the start identifier pkgheader, the length identifier pkglen, the type identifier pkgtype, and the function code identifier pkgfun; the frame message type is divided into local message, opposite end message, and response message; the local message and the opposite end message need to fill in the expected response message function code, the local message type is sent by the local end, and the opposite end message type is sent by the opposite end; Initialization module, initializes the serial port sending queue SendBuff1, retransmission queue SendBuff2, receiving data buffer RecvBuff, expects the response message function code ExcpectFun, and sets the initial value InitTime of the serial port data processing cycle; Message sending module: The local end actively sends messages or response messages and pushes them into the sending queue SendBuff1; when the serial port data processing cycle is up, if the SendBuff1 queue is not empty, the head message is popped from the SendBuff1 queue. When the local serial port module writes data, it is determined that the message is a local message and has the expected response function code ExcpectFun, and the message is pushed into the retransmission queue SendBuff2. This operation is not required for other types of messages. The data reading module reads data to the receiving data buffer RecvBuff through the serial port module. The serial port reading method returns the total length of the read message msglen. The read data includes the send and reply messages of the other end. The receiving data processing module first reads the start identifier pkgheader and then reads the length identifier pkglen when processing the received data; If the length flag pkglen is greater than the total message length msglen, the message is illegal and the data is discarded; If the length flag pkglen is equal to the total message length msglen, the message read this time is a single-frame message; If the length flag pkglen is less than the total message length msglen, the message read this time is multi-frame data; by moving the start flag pkgheader, obtain the length flag pkglen, obtain the entire frame data according to the start flag pkgheader and the length flag pkglen, move the start flag pkgheader according to the length flag pkglen, obtain each frame data, and then execute the next step; The data verification module, after reading a single-frame message, verifies the type identification bit pkgtype. If it is a peer message, there is no need to verify the function code identification bit pkgfun; if it is a response message, proceed to the next step; Function code verification module: if the expected response message function code ExcpectFun is empty, there is no need to verify the function code; if the expected response message function code ExcpectFun is not empty, then verify the function code identification bit pkgfun, and then execute the next step; The message processing module deletes the corresponding message in the retransmission queue SendBuff2 if the function code of the expected response message is ExcpectFun; if the function code of the expected response message is not ExcpectFun, the corresponding message in the retransmission queue SendBuff2 is popped out, and the message will be deleted when it is retransmitted; The serial port read / write cycle calculation module records each serial port read / write cycle time time and the number of received frame messages pkgnum, performs a weighted average algorithm based on the serial port read / write cycle time time and the number of frame messages pkgnum, calculates the next serial port read / write cycle time time, and when the next serial port read / write cycle time arrives, repeats steps S1 to S9.
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 serial communication method based on the adaptive transceiver mechanism of the weighted average algorithm as described in 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 port 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
SVG (Static Var Generator) interactive acquisition method and system capable of configuring multiple protocols, and SVG equipment
CN115484321A
RS422 universal serial port communication method based on FPGA
CN116226015A
MCU dynamic control method and system based on application program
CN118034751A
Serial port data processing method and device, equipment and storage medium
CN118282570A