AT instruction interaction algorithm based on timeout mechanism and circular queue
By using the AT instruction interaction algorithm based on timeout mechanism and cyclic queue in the communication between the MCU and the 4G module, the problems of low communication efficiency and logical confusion in the prior art are solved, and efficient and reliable communication is achieved.
Patent Information
- Application Number
- CN202411622808.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-13
- Publication Date
- 2025-03-11
AI Technical Summary
In the prior art, when the MCU communicates with the 4G module, there is a timeout waiting, resulting in low communication efficiency, logical confusion, and the continuity of communication cannot be guaranteed.
The AT instruction interaction algorithm based on the timeout mechanism and cyclic queue is adopted, and non-blocking reception and data matching are achieved through hardware timers and cyclic queue processing functions, and unfinished instructions are automatically resented to ensure the reliability and continuity of communication.
It improves the resource utilization efficiency of MCU, reduces logical chaos, and ensures the reliability and continuity of communication.
Smart Images

Figure CN119603644A_ABST
Abstract
Description
Technical Field
[0001] The invention relates to the technical field of AT command communication, in particular to an AT command interaction algorithm based on a timeout mechanism and a circular queue. Background Art
[0002] In embedded product design, 4G communication modules usually communicate with MCU through serial ports; MCU interacts with 4G modules by sending AT commands to implement functions such as dialing, data sending and receiving; each AT command sent to the 4G module needs to wait for a response, and the response time of different commands varies; some commands return a response within hundreds of milliseconds, while some commands may take several seconds.
[0003] In addition, the 4G module sometimes returns data suddenly, especially after the connection is established, the module may suddenly return data; when the MCU also needs to send data to the 4G module, this burst of data will make the communication logic complicated; the original processing method is to enter a timeout wait after the MCU sends data to the 4G module, and check whether there is data in the receiving buffer after the wait; if the received data matches the currently sent AT command, the subsequent command interaction continues; if no matching data is received, the MCU resends the AT command; during the timeout wait, the MCU is in an idle waiting state and no other operations are performed; this method not only has low communication efficiency, but may also cause logical confusion: the data received after the timeout may be irrelevant to the current command, but because the previous AT command has not been completed, the other response data received will make the software logic difficult to process.
[0004] The invention patent with patent number CN113438610A discloses "a method and communication system for controlling a POC communication module using AT commands", which records a method of controlling POC communication using AT commands to solve the complex problem of intercom services on the public network. However, after sending the AT command, there is a timeout waiting time, which reduces the communication efficiency; the invention patent with patent number CN104053252A discloses "an AT command processing method, device and system", which records an AT command processing method for realizing parallel processing of AT commands to improve efficiency, but there is logical confusion caused by burst data, and therefore the method will delete the corresponding AT command according to the processing result, and the continuity of communication cannot be guaranteed, so a new method is needed to solve the above problems. Summary of the invention
[0005] The purpose of the present invention is to provide an AT command interaction algorithm based on a timeout mechanism and a circular queue to solve the problems raised in the prior art.
[0006] To achieve the above object, the present invention provides the following technical solution: an AT command interaction algorithm based on a timeout mechanism and a circular queue, comprising the following steps:
[0007] S1, initialize the system and start the hardware timer;
[0008] S2, enter the 4G data receiving and sending task, and start the polling task in the while(1) loop;
[0009] After S3 and 4G modules are powered on, they establish a network by executing N AT commands in sequence;
[0010] S4, checking whether there is abnormal data in the circular queue through the program, and processing the abnormal data;
[0011] S5. After checking that the circular queue is empty, determine whether an AT command can be sent, and start the timeout count after sending the AT command;
[0012] S6. After the AT command is sent, the data in the circular queue is automatically monitored by the program;
[0013] S7, judging whether the data in the circular queue is the reply data of the currently sent AT command, and processing the data according to the judgment result;
[0014] S 8. The counter records the number of AT command timeouts. When an AT command times out M times, the program starts to reset the 4G module.
[0015] S9. The program executes each AT command according to the process until the dial-up and network connection are completed.
[0016] Further, in step S1: firstly, the hardware is initialized, and after the hardware initialization is completed, the serial port initialization, the hardware timer initialization and the receiving circular queue initialization are performed in sequence, and the hardware timer is started after the initialization is completed;
[0017] Hardware initialization is the beginning stage of system initialization, which includes memory controller initialization, CPU mode setting and device bus initialization. During the memory controller initialization process, the basic input and output system will ensure that the memory is ready for storing system information and loading the operating system. The CPU setting mode sets the CPU to the appropriate working mode through the input and output system. During the device bus initialization process, the basic input and output system initializes various device buses in the system to ensure that all connected external devices are available.
[0018] The universal synchronous or asynchronous serial receiver and transmitter is a highly flexible transceiver module. It can communicate with the host computer or other MCUs and realize mutual control between the host computer and other MCUs. If you want to initialize the synchronous or asynchronous serial port to synchronous mode, call the synchronous mode serial port initialization function, otherwise call the asynchronous mode serial port initialization function to initialize the serial port.
[0019] The timer counts the input clock and triggers an interrupt when the count value reaches the set value. Hardware timers include basic timers, general timers, advanced timers, and low-power timers. Their basic characteristics are almost the same, but their application scenarios are different. During the hardware timer initialization process, the hardware timer is initialized through the initialization function and the interrupt service function;
[0020] Sequential circular queues are prone to a false full state during use. To solve this problem, a method was developed that imaginary sequential queues as a ring-shaped space, namely a circular queue. The relationship between the pointer and the queue elements in the circular queue remains unchanged, and the circular movement of the pointer can be achieved by using only modular operations; the circular queue is initialized through a program, and the head and tail of the queue are equal and zero, then the circular queue has been initialized.
[0021] Further, in step S2: enter the 4G data receiving and sending task, and perform polling in the `while(1)` loop; the polling task is mainly responsible for processing the data returned by the 4G module and the data to be sent, and according to the current AT command, the data returned by the 4G module and the data to be sent are processed respectively through the circular queue processing function and the AT command sending function.
[0022] Furthermore, in step S3: after the 4G module is turned on, the network establishment requires the execution of N AT instructions in sequence; in the data processing and AT instruction sending functions of the circular queue, the switch-case statement is used to control the execution order of each AT instruction so as to complete the dial-up and network connection step by step.
[0023] Further, in step S4: after the polling task starts, the program enters the circular queue processing function to check whether there is data in the queue. At this time, no AT command has been sent, and the data in the queue cannot match the sent AT command. Under normal circumstances, there is no data in the circular queue at this time. If there is data, the program will take out all the data in the queue and discard it.
[0024] Further, in step S5: when the circular queue is empty, the program enters the AT command sending function to determine whether the AT command can be sent currently; when the AT command sending function does not send the AT command, the flag variable defaults to 0, and when an AT command is sent, the flag variable is set to 1; after sending the AT command, if the data returned by the module is received, the flag variable is set to 0, and if the data returned by the module is not received after the timeout, the flag variable is set to 0; this variable is used to control whether a new AT command can be sent currently, and the AT command can be sent to the module only when it is 0;
[0025] The AT command sending function determines whether the AT command can be sent. If it can be sent, it sends the AT command pointed to by the current switch case index and sets the maximum timeout period. After the AT command is sent, the program sets the AT command sent flag to 1 and starts the timeout count in the millisecond timer.
[0026] Further, in step S6: after the AT command is sent, the program will poll and check whether there is data in the circular queue. Since the AT command sent flag is set to 1, the AT command sending function will not be entered again.
[0027] Further, in step S7: when the program detects that there is data in the circular queue, it will automatically enter the queue processing function, extract a piece of data, and match it with the currently sent AT instruction through a switch-case statement to determine whether it is the reply data of the current instruction; if the data matches correctly, after processing the data, the switch-case index will point to the next AT instruction and clear the timeout counter; if the received data does not match the sent AT instruction, the program will process the received data and continue to process other data in the queue; if all the data in the queue have been processed and do not match the current AT instruction, the program will continue to wait for the correct reply of the module.
[0028] Furthermore, in step S8: if the counter has not received the return data from the 4G module after the timeout, the program will add 1 to the timeout count of the current AT command and resend the command; if the same AT command times out more than M times, the program will reset the 4G module to check whether a hardware failure causes the module to fail to respond normally.
[0029] Further, in step S9: according to the above process of executing AT commands, performing data matching and timeout counting, the program will execute each AT command in sequence until the dial-up and network connection are completed; if the device successfully establishes a TCP network connection through the 4G module, the program will enter the formal data sending and receiving stage.
[0030] Compared with the prior art, the present invention has the following beneficial effects:
[0031] 1. Non-blocking processing: Using circular queues and DMA to achieve non-blocking reception avoids wasting waiting time and improves the resource utilization efficiency of MCU;
[0032] 2. Accurate data matching: Immediately match the currently sent AT command response during the receiving process, reducing the logical confusion caused by burst data;
[0033] 3. Repeated transmission and timeout management: Automatically resend unfinished instructions after timeout to ensure communication reliability and continuity of data transmission. BRIEF DESCRIPTION OF THE DRAWINGS
[0034] Figure 1 The present invention is an overall block diagram of an AT command interaction algorithm based on a timeout mechanism and a circular queue;
[0035] Figure 2 A schematic diagram of an AT command execution flow of an AT command interaction algorithm based on a timeout mechanism and a circular queue of the present invention;
[0036] Figure 3 The present invention is a schematic diagram of a data parsing flow of a circular queue of an AT command interaction algorithm based on a timeout mechanism and a circular queue. DETAILED DESCRIPTION
[0037] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only 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.
[0038] like Figure 1-Figure 3 As shown, the present invention provides a technical solution, an AT command interaction algorithm based on a timeout mechanism and a circular queue, comprising the following steps:
[0039] S1, initialize the system and start the hardware timer;
[0040] S2, enter the 4G data receiving and sending task, and start the polling task in the while(1) loop;
[0041] After S3 and 4G modules are powered on, they establish a network by executing N AT commands in sequence;
[0042] S4, checking whether there is abnormal data in the circular queue through the program, and processing the abnormal data;
[0043] S5. After checking that the circular queue is empty, determine whether an AT command can be sent, and start the timeout count after sending the AT command;
[0044] S6. After the AT command is sent, the data in the circular queue is automatically monitored by the program;
[0045] S7, judging whether the data in the circular queue is the reply data of the currently sent AT command, and processing the data according to the judgment result;
[0046] S 8. The counter records the number of AT command timeouts. When an AT command times out M times, the program starts to reset the 4G module.
[0047] S9. The program executes each AT command according to the process until the dial-up and network connection are completed.
[0048] In step S1: first, perform hardware initialization. After the hardware initialization is completed, perform serial port initialization, hardware timer initialization and receiving circular queue initialization in sequence, and start the hardware timer after the initialization is completed;
[0049] Hardware initialization is the beginning stage of system initialization, which includes memory controller initialization, CPU mode setting and device bus initialization. During the memory controller initialization process, the basic input and output system will ensure that the memory is ready for storing system information and loading the operating system. The CPU setting mode sets the CPU to the appropriate working mode through the input and output system. During the device bus initialization process, the basic input and output system initializes various device buses in the system to ensure that all connected external devices are available.
[0050] The universal synchronous or asynchronous serial receiver and transmitter is a highly flexible transceiver module. It can communicate with the host computer or other MCUs and realize mutual control between the host computer and other MCUs. If you want to initialize the synchronous or asynchronous serial port to synchronous mode, call the synchronous mode serial port initialization function, otherwise call the asynchronous mode serial port initialization function to initialize the serial port.
[0051] The timer counts the input clock and triggers an interrupt when the count value reaches the set value. Hardware timers include basic timers, general timers, advanced timers, and low-power timers. Their basic characteristics are almost the same, but their application scenarios are different. During the hardware timer initialization process, the hardware timer is initialized through the initialization function and the interrupt service function;
[0052] Sequential circular queues are prone to a false full state during use. To solve this problem, a method was developed that imaginary sequential queues as a ring-shaped space, namely a circular queue. The relationship between the pointer and the queue elements in the circular queue remains unchanged, and the circular movement of the pointer can be achieved by using only modular operations; the circular queue is initialized through a program, and the head and tail of the queue are equal and zero, then the circular queue has been initialized.
[0053] In step S2: enter the 4G data receiving and sending task, and perform polling in the `while(1)` loop; the polling task is mainly responsible for processing the data returned by the 4G module and the data to be sent, and according to the current AT command, the data returned by the 4G module and the data to be sent are processed respectively through the circular queue processing function and the AT command sending function.
[0054] In step S3: after the 4G module is turned on, the network establishment requires the execution of N AT instructions in sequence; in the data processing and AT instruction sending functions of the circular queue, the switch-case statement is used to control the execution order of each AT instruction, so as to complete the dial-up and network connection step by step;
[0055] Switch means "switch" and is a "selection" statement. It is very simple to use. Switch is a multi-branch selection statement, and multi-branch is a combination of multiple if statements. Functionally, switch statements and if statements can completely replace each other, but from a programming perspective, they have their own characteristics. When there are fewer nested if statements, such as less than three, it is more concise to write a program with if, but when there are more branches to choose from, the number of nested if statements will be many, resulting in a lengthy program and reduced readability. The switch statement is used here because the number of AT instructions that need to be executed to establish a network is relatively large, generally greater than three, so it is more appropriate to choose the switch statement to handle it.
[0056] In step S4: after the polling task starts, the program enters the circular queue processing function to check whether there is data in the queue. At this time, no AT command has been sent, and the data in the queue cannot match the sent AT command. Under normal circumstances, there is no data in the circular queue at this time. If there is data, the program will take out all the data in the queue and discard it.
[0057] In step S5: when the circular queue is empty, the program enters the AT command sending function to determine whether the AT command can be sent currently; when the AT command sending function does not send the AT command, the flag variable defaults to 0, and when an AT command is sent, the flag variable is set to 1; after sending the AT command, if the data returned by the module is received, the flag variable is set to 0, if the data returned by the module is not received after the timeout, the flag variable is set to 0; this variable is used to control whether a new AT command can be sent currently, and the AT command can be sent to the module only when it is 0;
[0058] The AT command sending function determines whether the AT command can be sent. If it can be sent, it sends the AT command pointed to by the current switch case index and sets the maximum timeout period. After the AT command is sent, the program sets the AT command sent flag to 1 and starts the timeout count in the millisecond timer.
[0059] In step S6: after the AT command is sent, the program will poll and check whether there is data in the circular queue. Since the AT command sent flag is set to 1, the AT command sending function will not be entered again.
[0060] In step S7: when the program detects that there is data in the circular queue, it will automatically enter the queue processing function, extract a piece of data, and match it with the currently sent AT instruction through the switch-case statement to determine whether it is the reply data of the current instruction; if the data matches correctly, after processing the data, the switch-case index will point to the next AT instruction and clear the timeout counter; if the received data does not match the sent AT instruction, the program will process the received data and continue to process other data in the queue; if all the data in the queue have been processed and do not match the current AT instruction, the program will continue to wait for the correct reply of the module.
[0061] In step S8: if the counter times out and still does not receive the return data from the 4G module, the program will add 1 to the timeout count of the current AT command and resend the command; if the same AT command times out more than M times, the program will reset the 4G module to check whether a hardware failure causes the module to fail to respond normally.
[0062] In step S9: according to the above process of executing AT commands, performing data matching and timeout counting, the program will execute each AT command in sequence until the dial-up and network connection are completed; if the device successfully establishes a TCP network connection through the 4G module, the program will enter the formal data sending and receiving stage.
[0063] Embodiment 1:
[0064] S1, initialize the system and start the hardware timer;
[0065] S2, enter the 4G data receiving and sending task, and start the polling task in the while(1) loop;
[0066] After S3 and 4G modules are powered on, they establish a network by executing N=20 AT commands in sequence;
[0067] S4, checking whether there is abnormal data in the circular queue through the program, and processing the abnormal data;
[0068] S5. After checking that the circular queue is empty, determine whether an AT command can be sent, and start the timeout count after sending the AT command;
[0069] S6. After the AT command is sent, the data in the circular queue is automatically monitored by the program;
[0070] S7, judging whether the data in the circular queue is the reply data of the currently sent AT command, and processing the data according to the judgment result;
[0071] S 8. The counter records the number of AT command timeouts. When an AT command times out M = 10 times, the program starts to reset the 4G module.
[0072] S9. The program executes each AT command according to the process until the dial-up and network connection are completed.
[0073] In step S1: first, perform hardware initialization. After the hardware initialization is completed, perform serial port initialization, hardware timer initialization and receiving circular queue initialization in sequence, and start the hardware timer after the initialization is completed;
[0074] Hardware initialization is the beginning stage of system initialization, which includes memory controller initialization, CPU mode setting and device bus initialization. During the memory controller initialization process, the basic input and output system will ensure that the memory is ready for storing system information and loading the operating system. The CPU setting mode sets the CPU to the appropriate working mode through the input and output system. During the device bus initialization process, the basic input and output system initializes various device buses in the system to ensure that all connected external devices are available.
[0075] The universal synchronous or asynchronous serial receiver and transmitter is a highly flexible transceiver module. It can communicate with the host computer or other MCUs and realize mutual control between the host computer and other MCUs. If you want to initialize the synchronous or asynchronous serial port to synchronous mode, call the synchronous mode serial port initialization function, otherwise call the asynchronous mode serial port initialization function to initialize the serial port.
[0076] The timer counts the input clock and triggers an interrupt when the count value reaches the set value. Hardware timers include basic timers, general timers, advanced timers, and low-power timers. Their basic characteristics are almost the same, but their application scenarios are different. During the hardware timer initialization process, the hardware timer is initialized through the initialization function and the interrupt service function;
[0077] Sequential circular queues are prone to a false full state during use. To solve this problem, a method was developed that imaginary sequential queues as a ring-shaped space, namely a circular queue. The relationship between the pointer and the queue elements in the circular queue remains unchanged, and the circular movement of the pointer can be achieved by using only modular operations; the circular queue is initialized through a program, and the head and tail of the queue are equal and zero, then the circular queue has been initialized.
[0078] In step S2: enter the 4G data receiving and sending task, and perform polling in the `while(1)` loop; the polling task is mainly responsible for processing the data returned by the 4G module and the data to be sent, and according to the current AT command, the data returned by the 4G module and the data to be sent are processed respectively through the circular queue processing function and the AT command sending function.
[0079] In step S3: after the 4G module is turned on, the network establishment requires the execution of N=20 AT commands in sequence; in the data processing and AT command sending functions of the circular queue, the switch-case statement is used to control the execution order of each AT command, so as to complete the dial-up and network connection step by step;
[0080] Switch means a "selection" statement. It is very simple to use. Switch is a multi-branch selection statement. Multi-branch is a combination of multiple if statements. Functionally, switch statements and if statements can completely replace each other, but from a programming perspective, they have their own characteristics. When there are fewer nested if statements, such as less than three, it is simpler to write a program with if. However, when there are more branches to choose from, there will be many layers of nested if statements, resulting in a lengthy program and reduced readability. The switch statement is used here because a large number of AT instructions need to be executed to establish a network. Here, N=20, so it is more appropriate to choose the switch statement to handle it.
[0081] In step S4: after the polling task starts, the program enters the circular queue processing function to check whether there is data in the queue. At this time, no AT command has been sent, and the data in the queue cannot match the sent AT command. Under normal circumstances, there is no data in the circular queue at this time. If there is data, the program will take out all the data in the queue and discard it.
[0082] In step S5: when the circular queue is empty, the program enters the AT command sending function to determine whether the AT command can be sent currently; when the AT command sending function does not send the AT command, the flag variable defaults to 0, and when an AT command is sent, the flag variable is set to 1; after sending the AT command, if the data returned by the module is received, the flag variable is set to 0, if the data returned by the module is not received after the timeout, the flag variable is set to 0; this variable is used to control whether a new AT command can be sent currently, and the AT command can be sent to the module only when it is 0;
[0083] The AT command sending function determines whether the AT command can be sent. If it can be sent, it sends the AT command pointed to by the current switch case index and sets the maximum timeout period. After the AT command is sent, the program sets the AT command sent flag to 1 and starts the timeout count in the millisecond timer.
[0084] In step S6: after the AT command is sent, the program will poll and check whether there is data in the circular queue. Since the AT command sent flag is set to 1, the AT command sending function will not be entered again.
[0085] In step S7: when the program detects that there is data in the circular queue, it will automatically enter the queue processing function, extract a piece of data, and match it with the currently sent AT instruction through the switch-case statement to determine whether it is the reply data of the current instruction; if the data matches correctly, after processing the data, the switch-case index will point to the next AT instruction and clear the timeout counter; if the received data does not match the sent AT instruction, the program will process the received data and continue to process other data in the queue; if all the data in the queue have been processed and do not match the current AT instruction, the program will continue to wait for the correct reply of the module.
[0086] In step S8: if the counter times out and still does not receive the return data from the 4G module, the program will add 1 to the timeout count of the current AT command and resend the command; if the timeout count of the same AT command exceeds M=10 times, the program will reset the 4G module to check whether a hardware failure causes the module to fail to respond normally.
[0087] In step S9: according to the above process of executing AT commands, performing data matching and timeout counting, the program will execute each AT command in sequence until the dial-up and network connection are completed; if the device successfully establishes a TCP network connection through the 4G module, the program will enter the formal data sending and receiving stage.
[0088] It will be apparent to those skilled in the art that the invention is not limited to the details of the exemplary embodiments described above and that the invention can be implemented in other specific forms without departing from the spirit or essential features of the invention. Therefore, the embodiments should be considered exemplary and non-limiting in all respects, and the scope of the invention is defined by the appended claims rather than the foregoing description, and it is intended that all variations falling within the meaning and scope of the equivalent elements of the claims be included in the invention. Any reference numeral in a claim should not be considered as limiting the claim to which it relates.
Claims
1. An AT command interaction algorithm based on a timeout mechanism and a circular queue, characterized in that: The following steps are involved: S1, initialize the system and start the hardware timer; S2, enter the 4G data receiving and sending task, and start the polling task in the while(1) loop; After S3 and 4G modules are powered on, they establish a network by executing N AT commands in sequence; S4, checking whether there is abnormal data in the circular queue through the program, and processing the abnormal data; S5. After checking that the circular queue is empty, determine whether an AT command can be sent, and start the timeout count after sending the AT command; S6. After the AT command is sent, the data in the circular queue is automatically monitored by the program; S7, judging whether the data in the circular queue is the reply data of the currently sent AT command, and processing the data according to the judgment result; S 8. The counter records the number of AT command timeouts. When an AT command times out M times, the program starts to reset the 4G module. S9. The program executes each AT command according to the process until the dial-up and network connection are completed.
2. The AT command interaction algorithm based on timeout mechanism and circular queue according to claim 1, characterized in that: In step S1: first, the hardware is initialized. After the hardware initialization is completed, the serial port initialization, the hardware timer initialization and the receiving circular queue initialization are performed in sequence, and the hardware timer is started after the initialization is completed.
3. The AT command interaction algorithm based on timeout mechanism and circular queue according to claim 1, characterized in that: In step S2: enter the 4G data receiving and sending task, and perform polling in the `while(1)` loop; the polling task is mainly responsible for processing the data returned by the 4G module and the data to be sent, and according to the current AT command, the data returned by the 4G module and the data to be sent are processed respectively through the circular queue processing function and the AT command sending function.
4. The AT command interaction algorithm based on timeout mechanism and circular queue according to claim 1, characterized in that: In step S3: after the 4G module is powered on, the network establishment requires the execution of N AT instructions in sequence; in the data processing and AT instruction sending functions of the circular queue, the switch-case statement is used to control the execution order of each AT instruction.
5. The AT command interaction algorithm based on timeout mechanism and circular queue according to claim 1, characterized in that: In step S4: after the polling task starts, the program enters the circular queue processing function to check whether there is data in the queue. At this time, no AT command has been sent, and the data in the queue cannot match the sent AT command. Under normal circumstances, there is no data in the circular queue at this time. If there is data, the program will take out all the data in the queue and discard it.
6. The AT command interaction algorithm based on timeout mechanism and circular queue according to claim 1, characterized in that: In step S5: when the circular queue is empty, the program enters the AT command sending function to determine whether the AT command can be sent at present; when the AT command sending function does not send the AT command, the flag variable defaults to 0, and when an AT command is sent, the flag variable is set to 1; After sending the AT command, if the data returned by the module is received, the flag variable is set to 0. If no data returned by the module is received after the timeout, the flag variable is set to 0. If the AT command sending function determines that the AT command can be sent, it will send the AT command pointed to by the current switch case index and set the maximum timeout period; after the AT command is sent, the program will set the AT command sent flag to 1 and start the timeout count in the millisecond timer.
7. The AT command interaction algorithm based on timeout mechanism and circular queue according to claim 1, characterized in that: In step S6: after the AT command is sent, the program will poll and check whether there is data in the circular queue. Since the AT command sent flag is set to 1, the AT command sending function will not be entered again.
8. The AT command interaction algorithm based on timeout mechanism and circular queue according to claim 1, characterized in that: In step S7: when the program detects that there is data in the circular queue, it will automatically enter the queue processing function, extract a piece of data, and match it with the currently sent AT instruction through the switch-case statement to determine whether it is the reply data of the current instruction. If the data matches correctly, after processing the data, the switch-case index will point to the next AT instruction and clear the timeout counter; if the received data does not match the sent AT instruction, the program will process the received data and continue to process other data in the queue. If all the data in the queue has been processed and does not match the current AT instruction, the program continues to wait for the correct reply of the module.
9. The AT command interaction algorithm based on timeout mechanism and circular queue according to claim 1, characterized in that: In step S8: if the counter times out and still does not receive the return data from the 4G module, the program will add 1 to the timeout count of the current AT command and resend the command; if the same AT command times out more than M times, the program will reset the 4G module.
10. The AT command interaction algorithm based on timeout mechanism and circular queue according to claim 1, characterized in that: In step S9: according to the above process of executing AT commands, performing data matching and timeout counting, the program will execute each AT command in sequence until the dial-up and network connection are completed; if the device successfully establishes a TCP network connection through the 4G module, the program will enter the formal data sending and receiving stage.
Citation Information
Patent Citations
AT instruction processing method, device and system
CN104053252A
Method for controlling POC communication module by using AT instruction and communication system
CN113438610A