Serial port communication method and system for simulating watchdog
Through the serial communication method of simulated watchdog, the received data count and timeout time are detected by timing timing to solve the problem of complex logic and insufficient universality of the existing serial communication interrupt processing, and simplified interrupt processing and high universality are realized. Serial port communication.
Patent Information
- Application Number
- CN202411572872.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-06
- Publication Date
- 2025-02-18
AI Technical Summary
The existing serial communication interrupt processing logic is complex and lacks versatility, making it difficult to switch between different communication protocols.
By simulating the watchdog, the value of the received data count iRecCount is detected using timing timing, the size of the iWdt value and the REC_OUTTIME is compared, and the data received is determined according to the communication protocol after the data reception is completed, and the buffer area is cleared.
The logic of the interrupt processing function is simplified, the portability and universality of serial communication is improved, and it is suitable for various communication protocols, and the practicality of batch processing of serial communication data is realized.
Smart Images

Figure CN119473979A_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the technical field of serial communication, and in particular relates to a serial communication method and system for simulating a watchdog. Background Art
[0002] UART serial communication is one of the most commonly used data communication methods in engineering applications. There are many methods for serial data reception and processing, most of which use interruption, single byte processing, or fixed-format communication protocol judgment processing. The advantage of single-byte processing is that there is no restriction on the communication protocol, but the disadvantage is that the judgment logic in the interrupt function is complex and prone to errors; the advantage of using fixed-format processing is that the logic judgment is simple, but the disadvantage is poor versatility. Summary of the invention
[0003] The invention provides a serial communication method and system for simulating a watchdog, which are used to solve the technical problem that the complexity and universality of the existing serial communication interruption processing logic cannot coexist.
[0004] In a first aspect, the present invention provides a serial communication method for simulating a watchdog, comprising:
[0005] According to the serial communication timing, the value of the received data count iRecCount is detected regularly. When the value of iRecCount is greater than zero, it means that the serial port data is received, and the "dog feeding" flag iWdt is increased by one, which means that data communication is in progress by default.
[0006] Compare the iWdt value with REC_OUTTIME;
[0007] When the iWdt value is greater than REC_OUTTIME, it indicates that the serial port has not received the batch data for a period of time. The data in the receiving buffer area rxBuf is processed according to the communication protocol.
[0008] After the processing is completed, the receiving count iRecCount is cleared and the receiving data buffer rxBuf data is cleared, that is, the next data reception judgment is performed.
[0009] In a second aspect, the present invention provides a serial communication system for simulating a watchdog, comprising:
[0010] The superposition module is configured to detect the value of the received data count iRecCount according to the timing of the serial communication. When the value of iRecCount is greater than zero, it indicates that the serial port data is received, and the "dog feeding" flag iWdt is increased by one, which means that data communication is in progress by default;
[0011] The judgment module is configured to compare the iWdt value with the REC_OUTTIME value;
[0012] The receiving module is configured so that when the iWdt value is greater than REC_OUTTIME, it indicates that the batch data that has not been received from the serial port for a period of time has been received, and the receiving buffer area rxBuf data is processed according to the communication protocol;
[0013] The clearing module is configured to clear the receiving count iRecCount to zero after the processing is completed, and clear the receiving data buffer area rxBuf data, that is, to make the next data reception judgment.
[0014] According to a third aspect, an electronic device is provided, comprising: at least one processor, and a memory communicatively connected to the at least one processor, wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can perform the steps of the serial communication method for simulating a watchdog according to any embodiment of the present invention.
[0015] In a fourth aspect, the present invention further provides a computer-readable storage medium having a computer program stored thereon, wherein when the program instructions are executed by a processor, the processor executes the steps of the serial communication method for simulating a watchdog in any embodiment of the present invention.
[0016] The serial communication method and system for simulating a watchdog of the present application have the following beneficial effects:
[0017] The logic of the interrupt handling function becomes simpler: it only needs to receive serial port data and perform "feeding the dog" operations;
[0018] Whether the data reception is completed is judged by the timeout parameter instead of the communication protocol. The serial communication has good portability and versatility and is applicable to various communication protocols.
[0019] For engineering applications, batch processing of serial communication data is the norm, and communication protocols are often different, which makes the advantages of batch processing of data and good versatility of the present invention more prominent and highly practical. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0021] Figure 1 A flowchart of a serial communication method for simulating a watchdog provided by an embodiment of the present invention;
[0022] Figure 2A flowchart of a serial communication method according to a specific embodiment of the present invention is provided;
[0023] Figure 3 A structural block diagram of a serial communication system for simulating a watchdog provided by an embodiment of the present invention;
[0024] Figure 4 It is a schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0025] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0026] See also Figure 1 , which shows a flow chart of a serial communication method for simulating a watchdog in the present application.
[0027] like Figure 1 As shown, the serial communication method for simulating a watchdog specifically includes the following steps:
[0028] Step S101, according to the serial communication timing, the value of the received data count iRecCount is detected. When the value of iRecCount is greater than zero, it indicates that serial port data is received, and the "dog feeding" flag iWdt is increased by one, which indicates that data communication is in progress.
[0029] In this step, initialize and set system parameters: set the serial port reception to interrupt mode; define a buffer area rxBuf to store the serial port reception data; define a value iRecCount as the count of received data; define a parameter iWdt as the "feed the dog" flag; define a timeout parameter REC_OUTTIME and set the threshold.
[0030] Step S102, comparing the iWdt value with REC_OUTTIME.
[0031] In a specific embodiment, when the iWdt value is not greater than REC_OUTTIME, the "feed the dog" flag iWdt is cleared based on the serial port interrupt service function, that is, when there is continuous data reception, the iWdt value is always equal to zero; if the data stops and the interrupt service function cannot be entered, iWdt is not cleared; and in the timing detection of the main process, the iWdt value is continuously accumulated.
[0032] Step S103, when the iWdt value is greater than REC_OUTTIME, it indicates that the batch data that has not been received by the serial port within a period of time has been received, and the receiving buffer area rxBuf data is processed according to the communication protocol;
[0033] Step S104, after the processing is completed, the receiving count iRecCount is cleared to zero, and the receiving data buffer area rxBuf data is cleared, that is, the next data reception judgment is performed.
[0034] In summary, the method of the present application adopts an interrupt mode to receive serial port data. When a serial port interrupt occurs, the data is received and saved, and a "dog feeding" operation is performed; an appropriate timeout parameter is set. When the received data times out, that is, "stop feeding the dog", it indicates that the batch data reception is completed, and the data packet can be processed according to the communication protocol at this time; the present application is a software implementation method, which is mostly used in embedded systems and can realize batch serial port data reception and processing of any protocol.
[0035] like Figure 2 As shown, initialize and set the system related parameters: the serial port receives in interrupt mode; define a buffer area rxBuf to store the serial port received data; define a value iRecCount as the count of received data; define a parameter iWdt as the "feed the dog" mark; define a timeout parameter REC_OUTTIME and set an appropriate value.
[0036] When a byte of serial port data is received, that is, a serial port interrupt is generated, the processing flow of the interrupt service function is: the received data is saved to the buffer area rxBuf, and the receiving count iRecCount is increased by one; at the same time, the "feed the dog" operation is performed, that is, the "feed the dog" flag iWdt is cleared.
[0037] In the main process of the system, the value of the received data count iRecCount can be detected according to the timing of the serial communication. When the value of iRecCount is greater than zero, it means that the serial port data has been received, and the "feed the dog" flag iWdt is increased by one, and the data communication is performed by default. Then the received data timeout is judged, that is, the size of iWdt and REC_OUTTIME is compared. The principle of data comparison at this time is based on the fact that in the serial port interrupt service function, the "feed the dog" flag iWdt is cleared to zero, that is, when there is continuous data reception, the value of iWdt is always equal to zero; if the data stops and the interrupt service function cannot be entered, iWdt is not cleared to zero; and in the timing detection of the main process, the iWdt value is constantly accumulated. Therefore, when the iWdt value is greater than the timing reception timeout parameter, it means that no serial port data has been received for a period of time, and it is considered that the batch data at this time has been received, and the receiving buffer area rxBuf can be processed according to the communication protocol. After the processing is completed, the receiving count iRecCount is cleared and the receiving data buffer rxBuf is cleared, and the next data reception judgment can be performed. At this point, a data reception cycle is completed.
[0038] See also Figure 3 , which shows a structural block diagram of a serial communication system for simulating a watchdog in the present application.
[0039] like Figure 3 As shown, the serial communication system 200 includes a superposition module 210 , a determination module 220 , a receiving module 230 and a clearing module 240 .
[0040] Among them, the superposition module 210 is configured to detect the value of the received data count iRecCount according to the timing of the serial communication. When the value of iRecCount is greater than zero, it indicates that the serial port data is received, the "dog feeding" flag iWdt is increased by one, and the data communication is performed by default; the judgment module 220 is configured to compare the iWdt value with the size of REC_OUTTIME; the receiving module 230 is configured to, when the iWdt value is greater than REC_OUTTIME, it indicates that the batch data of the serial port has not been received within a period of time, and the receiving buffer area rxBuf data is processed according to the communication protocol; the clearing module 240 is configured to clear the receiving count iRecCount to zero after the processing is completed, and clear the receiving data buffer area rxBuf data, that is, to make the next data reception judgment.
[0041] It should be understood that Figure 3 Modules and references documented in Figure 1 Therefore, the operations and features described above for the method and the corresponding technical effects are also applicable to Figure 3 The modules in it will not be described in detail here.
[0042] In some other embodiments, the embodiments of the present invention further provide a computer-readable storage medium having a computer program stored thereon, wherein when the program instructions are executed by a processor, the processor executes the serial communication method for simulating a watchdog in any of the above method embodiments;
[0043] As an implementation mode, the computer-readable storage medium of the present invention stores computer-executable instructions, and the computer-executable instructions are configured as follows:
[0044] According to the serial communication timing, the value of the received data count iRecCount is detected regularly. When the value of iRecCount is greater than zero, it means that the serial port data is received, and the "dog feeding" flag iWdt is increased by one, which means that data communication is in progress by default.
[0045] Compare the iWdt value with REC_OUTTIME;
[0046] When the iWdt value is greater than REC_OUTTIME, it indicates that the serial port has not received the batch data for a period of time. The data in the receiving buffer area rxBuf is processed according to the communication protocol.
[0047] After the processing is completed, the receiving count iRecCount is cleared and the receiving data buffer rxBuf data is cleared, that is, the next data reception judgment is performed.
[0048] The computer-readable storage medium may include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function; the data storage area may store data created according to the use of the serial communication system simulating the watchdog, etc. In addition, the computer-readable storage medium may include a high-speed random access memory, and may also include a memory, such as at least one disk storage device, a flash memory device, or other non-volatile solid-state storage device. In some embodiments, the computer-readable storage medium may optionally include a memory remotely arranged relative to the processor, and these remote memories may be connected to the serial communication system simulating the watchdog via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0049] Figure 3 is a schematic diagram of the structure of an electronic device provided by an embodiment of the present invention, such as Figure 3 As shown, the device includes: a processor 310 and a memory 320. The electronic device may also include: an input device 330 and an output device 340. The processor 310, the memory 320, the input device 330 and the output device 340 may be connected via a bus or other means. Figure 3In the example, the bus connection is used. The memory 320 is the above-mentioned computer-readable storage medium. The processor 310 executes various functional applications and data processing of the server by running the non-volatile software programs, instructions and modules stored in the memory 320, that is, the serial communication method for simulating the watchdog in the above-mentioned method embodiment is implemented. The input device 330 can receive input digital or character information, and generate key signal input related to user settings and function control of the serial communication system for simulating the watchdog. The output device 340 may include display devices such as display screens.
[0050] The electronic device can execute the method provided by the embodiment of the present invention, and has the functional modules and beneficial effects corresponding to the execution method. For technical details not described in detail in this embodiment, please refer to the method provided by the embodiment of the present invention.
[0051] As an implementation mode, the electronic device is applied to a serial communication system simulating a watchdog, and is used for a client, and includes: at least one processor; and a memory connected to the at least one processor in communication; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can:
[0052] According to the serial communication timing, the value of the received data count iRecCount is detected regularly. When the value of iRecCount is greater than zero, it means that the serial port data is received, and the "dog feeding" flag iWdt is increased by one, which means that data communication is in progress by default.
[0053] Compare the iWdt value with REC_OUTTIME;
[0054] When the iWdt value is greater than REC_OUTTIME, it indicates that the serial port has not received the batch data for a period of time. The data in the receiving buffer area rxBuf is processed according to the communication protocol.
[0055] After the processing is completed, the receiving count iRecCount is cleared and the receiving data buffer rxBuf data is cleared, that is, the next data reception judgment is performed.
[0056] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods of each embodiment or some parts of the embodiment.
[0057] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A serial communication method for simulating a watchdog, characterized in that: include: According to the serial communication timing, the value of the received data count iRecCount is detected regularly. When the value of iRecCount is greater than zero, it means that the serial port data is received, and the "feeding dog" flag iWdt is increased by one, which means that data communication is in progress by default. Compare the iWdt value with REC_OUTTIME; When the iWdt value is greater than REC_OUTTIME, it indicates that the serial port has not received the batch data for a period of time. The data in the receiving buffer area rxBuf is processed according to the communication protocol. After the processing is completed, the receiving count iRecCount is cleared and the receiving data buffer rxBuf data is cleared, that is, the next data reception judgment is performed.
2. A serial communication method for simulating a watchdog according to claim 1, characterized in that: After comparing the iWdt value with the REC_OUTTIME value, the method further includes: When the iWdt value is not greater than REC_OUTTIME, the "dog feeding" flag iWdt is cleared in the serial port interrupt service function, that is, when there is continuous data reception, the iWdt value is always equal to zero; If the data stops and cannot enter the interrupt service function, iWdt will not be cleared; while in the timing detection of the main process, the iWdt value is continuously accumulated.
3. A serial communication method for simulating a watchdog according to claim 1, characterized in that: Before detecting the value of the received data count iRecCount according to the timing of the serial communication, the method further includes: Initialize and set system parameters: Set the serial port to receive in interrupt mode; define a buffer area rxBuf to store serial port received data; define a value iRecCount as the count of received data; define a parameter iWdt as the "feed the dog" flag; define a timeout parameter REC_OUTTIME and set the threshold.
4. A serial communication system simulating a watchdog, characterized in that: include: The superposition module is configured to detect the value of the received data count iRecCount according to the timing of the serial communication. When the value of iRecCount is greater than zero, it means that the serial port data is received, and the "feeding dog" flag iWdt is increased by one, which means that data communication is in progress by default; The judgment module is configured to compare the iWdt value with the REC_OUTTIME value; The receiving module is configured so that when the iWdt value is greater than REC_OUTTIME, it indicates that the batch data that has not been received from the serial port for a period of time has been received, and the receiving buffer area rxBuf data is processed according to the communication protocol; The clearing module is configured to clear the receiving count iRecCount to zero after the processing is completed, and clear the receiving data buffer area rxBuf data, that is, to make the next data reception judgment.
5. An electronic device, characterized in that: include: At least one processor, and a memory communicatively connected to the at least one processor, wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to execute the method described in any one of claims 1 to 3.
6. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 3 is implemented.
Citation Information
Patent Citations
Embedded system serial reliable communicating control method
CN101241451A
Serial communication redundancy self-checking design method
CN105512074A
Microprocessor, electronic device, data transmission method and storage medium
CN117009263A
Watchdog control method
CN1815449A