A serial port receiving and sending management method, device, equipment and medium
By building independent serial port sending and receiving management tasks in a single chip computer and using interrupt-driven message transmission methods, the problems of low execution efficiency and insufficient real-time performance of serial port sending and receiving management in the existing technology are solved, and efficient and real-time serial port data processing is achieved, which is suitable for master-slave mode and complex protocols.
Patent Information
- Application Number
- CN202411827108.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-12
- Publication Date
- 2025-04-04
AI Technical Summary
The prior art has problems such as low execution efficiency, insufficient real-time, fixed data reception length and only applicable to host mode in the serial port transmission and reception management of microcontrollers, making it difficult to meet the application scenarios of high real-time and complex protocols.
By building independent serial port sending and receiving management tasks, program decoupling is achieved and interrupt-driven message transmission is adopted to improve real-time and execution efficiency. At the same time, the frame break timer is used to determine that the data reception is completed, which is suitable for master-slave mode and any protocol.
It realizes program decoupling, improves the execution efficiency and real-time performance of serial port transmission and reception, is suitable for master-slave mode and complex protocols, and reduces maintenance difficulty.
Smart Images

Figure CN119759826A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of single-chip embedded systems, and in particular to a serial port receiving and sending management method, device, equipment and medium. Background Art
[0002] In the programming environment of the microcontroller, the driver and application are usually coupled together. When using the serial port to send and receive data, the application logic will be bound to the driver program, that is, the application task of the serial port directly calls the driver interface to send and receive data. At this time, whether the sending and receiving is completed depends on the query result of the microcontroller, which takes up more CPU running time. At the same time, it is more difficult to implement multiple channels to multiplex a serial port peripheral software.
[0003] Chinese invention CN105487992A, published on April 13, 2016, proposed a multi-channel software and hardware parallel serial port communication processing method, which realizes multi-channel software and hardware parallel serial port communication by multiplexing a serial port interrupt and peripherals through multiple channels, saving computer hardware resources and reducing the CPU time occupied by multi-channel serial port communication. However, there are still the following shortcomings:
[0004] (1) Low execution efficiency: Since the main task process reads the data in the serial port buffer in turn, and needs to poll the serial port status in real time to obtain whether the serial port has completed sending or receiving, the execution efficiency is low.
[0005] (2) Insufficient real-time performance: Since the main task process is used to poll the receiving and sending status in real time, after receiving the data, it is necessary to wait for the main task process to poll the data reception completion status next time before processing the received data. That is, there may be a delay of up to one polling cycle, and it is impossible to process the data immediately after receiving it. The real-time performance is insufficient and cannot meet the application scenarios with high real-time requirements. If the real-time performance is to be improved, the polling cycle time needs to be reduced, but this will increase the polling frequency and occupy more CPU usage time. In addition, when there are multiple tasks, the high execution frequency of the main task process will also lead to a long execution time for other tasks, because it is necessary to frequently switch to the main task execution and the tasks are frequently interrupted.
[0006] (3) Fixed data reception length: Only one timeout timer is used to handle data reception timeout, and the expected length of received data is combined to determine whether reception is complete. For complex protocols, the length of request and response data is not fixed, so it is impossible to fully determine whether reception is complete. This is only applicable to fixed-length protocols.
[0007] (4) Only applicable to host mode: The method of specifying the received data length is only applicable to the host. If the slave receives data passively, the received data cannot be correctly parsed because the length of the data sent by the host is not fixed. Summary of the invention
[0008] The technical problem to be solved by the present invention is to provide a serial port receiving and sending management method, device, equipment and medium, which can realize program decoupling and improve the real-time performance of execution efficiency. It is applicable to both master and slave machines, reduces maintenance difficulty, and thus overcomes the shortcomings of the prior art.
[0009] In a first aspect, the present invention provides a serial port transceiver management method, which is characterized by comprising:
[0010] S1. Implement the receiving and sending functions of the serial port driver and interrupt service function to meet the following requirements:
[0011] During the data transmission process, the serial port transceiver management task will fill the transmission data and the length to be sent into the corresponding serial port transmission buffer, and trigger the serial port transmission idle interrupt, enter the serial port transmission idle interrupt to send the first byte of data; then turn on the transmission completion interrupt in the serial port transmission idle interrupt, and send the data in the buffer by byte transmission task. Each time a byte is sent, the transmission completion interrupt will be triggered until all the transmission is completed, turn off the transmission completion interrupt and turn on the serial port receiving interrupt;
[0012] During the data receiving process, the serial port receiving interruption is used to receive data byte by byte, and after receiving the data, the data is saved in the receiving buffer. At the same time, the frame break timer is started, the byte timeout time is set, and the serial port receiving interruption is exited;
[0013] The judgment of receiving completion is that the byte timeout time is reached, and the frame break timer triggers the frame break timeout interrupt to realize the judgment of data receiving completion, and sends a receiving completion message to the serial port transceiver management task, and exits the frame break timeout interrupt;
[0014] S2. Create an independent serial port transceiver management task and initialize it. During initialization, input parameters according to the device configuration; the parameters include: configuration parameter definition, serial port name, master-slave mode, serial port, serial port peripheral, baud rate, parity check, data bit and stop bit;
[0015] If the initialization device is configured as host mode, the receiving and sending functions and interrupts of the serial port are closed by default, and the receiving and sending state is entered by receiving a sending request message. If there is no sending request message, it blocks and waits until the sending request message is received, executes the sending process of the host, opens the corresponding serial port interrupt, and the sending request message contains a receiving completion message of the serial port frame break timer that blocks and waits within the frame timeout period; if the receiving completion message sent by the frame break timer is not received within the frame timeout period, it is judged as a receiving timeout, and a receiving timeout message is sent to the device task, otherwise it means that the communication is normal, and the received data is sent to the device task in the form of a message, and the data logic processing and analysis of the application layer are performed;
[0016] If the initialization device is configured as a slave mode, the serial port receiving function is enabled by default, the sending interrupt is disabled, and the slave mode blocks and waits for the receiving interrupt to complete the receiving message until the receiving completion message sent by the frame-breaking timer is received. The received data is forwarded to the device task of the application layer through a message to process and parse the received data. After the application layer completes the processing, the device task replies the reply data to the serial port transceiver management task through a message, and the serial port transceiver management task starts the sending interrupt to send the reply data out, completing a round of data reception and reply;
[0017] S3. Create a device task and bind the device task to the receiving message channel of the serial port transceiver management task. Send the data to be received and sent to the serial port transceiver management task in the form of messages to realize data reception and transmission. If multiple device tasks need to be bound to one serial port, all the messages of the device tasks will be bound to the receiving message channel of the same serial port transceiver management task.
[0018] In a second aspect, the present invention provides a serial port transceiver management device, characterized in that it includes:
[0019] The transceiver function realization module is used to realize the transceiver function of the serial port driver and interrupt service function, thereby meeting:
[0020] During the data transmission process, the serial port transceiver management task will fill the transmission data and the length to be sent into the transmission buffer of the corresponding serial port, and trigger the serial port transmission idle interrupt, enter the serial port transmission idle interrupt to send the first byte of data; then turn on the transmission completion interrupt in the serial port transmission idle interrupt sending function, and send the data in the buffer by byte transmission task. Each time a byte is sent, the transmission completion interrupt will be triggered until all the transmission is completed, turn off the transmission completion interrupt and turn on the serial port receiving interrupt;
[0021] During the data receiving process: the serial port receiving interruption is used to receive data byte by byte and save the data into the receiving buffer. At the same time, the frame break timer is started, the byte timeout time is set, and the serial port receiving interruption is exited;
[0022] Judgment of reception completion: when the byte timeout time is reached, the frame break timer triggers the frame break timeout interrupt to realize the judgment of data reception completion, and sends a reception completion message to the serial port transceiver management task, and exits the frame break timeout interrupt;
[0023] The serial port transceiver management task creation module is used to create an independent serial port transceiver management task and initialize it. During initialization, parameters are passed in according to the device configuration; the parameters include: configuration parameter definition, serial port name, master-slave mode, serial port number, serial port peripherals, baud rate, parity check, data bits, and stop bits;
[0024] If the initialization device is configured as host mode, the receiving and sending functions and interrupts of the serial port are closed by default, and the receiving and sending state is entered by receiving a sending request message. If there is no sending request message, it blocks and waits until the sending request message is received, executes the sending process of the host, opens the corresponding serial port interrupt, and the sending request message contains a receiving completion message of the serial port frame break timer that blocks and waits within the frame timeout period; if the receiving completion message sent by the frame break timer is not received within the frame timeout period, it is judged as a receiving timeout, and a receiving timeout message is sent to the device task, otherwise it means that the communication is normal, and the received data is sent to the device task in the form of a message, and the data logic processing and analysis of the application layer are performed;
[0025] If the initialization device is configured as a slave mode, the serial port receiving function is enabled by default, the sending interrupt is disabled, and the slave mode blocks and waits for the receiving interrupt to complete the receiving message until the receiving completion message sent by the frame-breaking timer is received. The received data is forwarded to the device task of the application layer through a message to process and parse the received data. After the application layer completes the processing, the device task replies the reply data to the serial port transceiver management task through a message, and the serial port transceiver management task starts the sending interrupt to send the reply data out, completing a round of data reception and reply;
[0026] The device task creation module is used to create a device task and bind the device task to the receiving message channel of the serial port transceiver management task. The data to be received and sent is sent to the serial port transceiver management task in the form of a message to realize data reception and transmission. If multiple device tasks are to be bound to one serial port, all the messages of the device tasks are bound to the receiving message channel of the same serial port transceiver management task.
[0027] In a third aspect, the present invention provides an electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the method described in the first aspect when executing the program.
[0028] In a fourth aspect, the present invention provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the method described in the first aspect.
[0029] One or more technical solutions provided by the present invention have at least the following technical effects or advantages:
[0030] (1) Program decoupling: By building an independent serial port transceiver management task, the serial port driver / interrupt program is only related to the serial port transceiver management and has nothing to do with the device's application logic. When the device needs to change the application logic, only the device task needs to be modified, which has nothing to do with the serial port transceiver logic. At the same time, if the serial port transceiver logic needs to be changed, or the serial port number needs to be changed, only the corresponding serial port task needs to be modified.
[0031] (2) Reduce maintenance difficulty: Since the serial port transceiver management task and the device task use message communication, multiple device tasks can share one serial port. If you need to add a device, you only need to add the device task and send a message to the corresponding serial port. If you want to delete a device, you only need to delete the device task directly without modifying the serial port logic. The serial port transceiver task is not affected.
[0032] (3) High execution efficiency: Using the message transmission method, the serial port receiving and sending task can release the CPU usage rights while waiting for data to be received, allowing other tasks to use more CPU, which is more suitable for multi-tasking systems.
[0033] (4) Higher real-time performance: The present invention proposes using interrupts to send messages and activate blocked serial port transceiver tasks, thereby improving the real-time performance of received data processing and being more suitable for systems with effectiveness requirements.
[0034] (5) Applicable to both master and slave machines: The present invention uses a timeout frame breaking method to determine whether data reception is complete. There is no need to limit the receiving length, and the code programming is more concise. It is applicable to any protocol, and different timeout times can be configured according to the properties of different devices to meet different application scenarios. It is applicable to both master and slave machine tasks.
[0035] The above description is only an overview of the technical solution of the present invention. In order to more clearly understand the technical means of the present invention, it can be implemented according to the contents of the specification. In order to make the above and other purposes, features and advantages of the present invention more obvious and easy to understand, the specific implementation methods of the present invention are listed below. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] The present invention will be further described below in conjunction with embodiments with reference to the accompanying drawings.
[0037] Figure 1 Flow chart of the serial port receiving and sending management solution in an embodiment of the present invention;
[0038] Figure 2 This is a serial port receiving and sending timing diagram in an embodiment of the present invention;
[0039] Figure 3 This is a flow chart of sending data by interrupting a serial port in an embodiment of the present invention;
[0040] Figure 4 A flowchart of receiving data by serial port receiving interruption in an embodiment of the present invention;
[0041] Figure 5 This is a flow chart of determining the completion of data reception by interrupting a serial port receiving timer in an embodiment of the present invention;
[0042] Figure 6 A flowchart of the creation and initialization of a serial port transceiver management task in an embodiment of the present invention;
[0043] Figure 7 A flowchart of creating and initializing a device task in an embodiment of the present invention;
[0044] Figure 8 It is a complete timing diagram in the host mode in an embodiment of the present invention;
[0045] Fig. 9 This is a complete timing diagram in slave mode in an embodiment of the present invention;
[0046] Fig.10 A schematic diagram of the contents of a message structure according to an embodiment of the present invention;
[0047] Fig.11 It is a structural schematic diagram of the device in the second embodiment of the present invention;
[0048] Fig.12 This is a schematic diagram of the structure of an electronic device in Embodiment 3 of the present invention;
[0049] Fig.13 Schematic diagram of the structure of the medium in the fourth embodiment of the present invention. DETAILED DESCRIPTION
[0050] The embodiments of the present application provide a serial port receiving and sending management method, device, equipment and medium, which decouples programs and improves the real-time execution efficiency. The method is applicable to both master and slave machines, reduces the difficulty of maintenance, and thus overcomes the shortcomings of the prior art.
[0051] The technical solution in the embodiment of the present application has the following general idea: program decoupling is achieved by constructing an independent serial port transceiver management task, so that the serial port driver / interrupt program is only related to the serial port transceiver management and has nothing to do with the application logic of the device. The serial port transceiver management task and the device task use message communication, which can realize the situation where multiple device tasks share one serial port, and the device addition and deletion is more free and simple, without modifying the serial port logic, and the serial port transceiver task is not affected. Using the message transmission method, the serial port transceiver task can release the CPU usage right when waiting for data reception, and the execution efficiency is high. The present invention proposes to use interrupts to send messages to activate the blocked serial port transceiver task, improve the real-time processing of received data, and is more suitable for systems with effectiveness requirements. The timer timeout (i.e. byte timeout) frame breaking method is used to determine whether the data is received. There is no need to limit the receiving length, the code programming is more concise, and it is applicable to any protocol. Different timeout times can be configured according to the properties of different devices to meet different application scenarios, so that both master and slave tasks can be applicable. By using the message reception timeout (i.e. frame timeout) to determine whether the slave reply has timed out, the occupation of timer interrupt resources can be reduced when the slave device is abnormal, invalid interrupts can be avoided, and program execution efficiency can be improved.
[0052] Embodiment 1
[0053] like Figure 1 As shown, this embodiment provides a serial port transmission and reception management method, including:
[0054] S1, realize the receiving and sending functions of the serial port driver and interrupt service function, the receiving and sending timing diagram is as follows Figure 2 As shown, it satisfies:
[0055] (a) During the data transmission process, the serial port transceiver management task will fill the transmission data and the length to be sent into the corresponding serial port transmission buffer, and trigger the serial port transmission idle interrupt, enter the serial port transmission idle interrupt to send the first byte of data; then turn on the transmission completion interrupt in the serial port transmission idle interrupt, and send the data in the buffer by byte transmission task. Each time a byte is sent, the transmission completion interrupt will be triggered until all the transmissions are completed, turn off the transmission completion interrupt and turn on the serial port receiving interrupt; after the transmission completion interrupt is turned on, it can ensure that each byte is sent after it is completed, and then the next byte is sent. Figure 3 As shown, the execution process of the interrupt service function is:
[0056] S11, first the serial port sends an idle interrupt to trigger an interrupt, and determines whether it is the first byte in the cache;
[0057] If yes, disable the serial port send idle interrupt, send the next byte in the buffer, and enable the send completion interrupt;
[0058] If not, determine whether all the data in the buffer has been sent. If so, end the sending, turn off the serial port send idle interrupt, enable the receiving and turn on the serial port receive interrupt; if not, send the next byte of the buffer and turn on the sending completion interrupt;
[0059] S12, exiting the serial port sending idle interrupt.
[0060] (b) During data reception, if Figure 4 As shown, the serial port receiving interruption is used to receive data byte by byte, and the data is saved in the receiving buffer. At the same time, the frame break timer is started, the byte timeout time is set, and the serial port receiving interruption is exited.
[0061] The frame break time can be freely set according to the byte interval requirements of the protocol. For example, the Modbus protocol requires that the byte interval is no more than 3.5 byte times. For example, at a baud rate of 9600, each byte time is approximately 1ms, and 3.5 bytes is 3.5ms.
[0062] (c) Receiving the judgment of completion, such as Figure 5 As shown, when the byte timeout time is reached, the frame break timer triggers the frame break timeout interrupt to determine whether data reception is complete, and sends a reception completion message to the serial port transceiver management task to exit the frame break timeout interrupt;
[0063] The frame break timer is used to implement the frame break processing of byte timeout, which can match the protocol receiving unknown length, especially when acting as a slave, the length of the data sent by the host cannot be predicted by the slave.
[0064] S2. Create and initialize an independent serial port transceiver management task. The serial port transceiver management task is independent of the main application process and uses message blocking to receive and send. It has higher execution efficiency and is more suitable for multi-tasking mode.
[0065] like Figure 6 As shown, during initialization, parameters are passed in according to the device configuration; the parameters include: configuration parameter definition, serial port name, master-slave mode, serial port number, serial port peripherals, baud rate, parity check, data bits, and stop bits;
[0066] (a) If the initialization device is configured as a host mode, the receiving and sending functions and interrupts of the serial port are closed by default, and the receiving and sending state is entered by receiving a sending request message. If there is no sending request message, it blocks and waits until the sending request message is received, and then the host's sending process is executed, the corresponding serial port interrupt is turned on, and the sending request message contains a receiving completion message of the serial port frame break timer that blocks and waits within the frame timeout period; if the receiving completion message sent by the frame break timer is not received within the frame timeout period, it is judged as a receiving timeout, and a receiving timeout message is sent to the device task, otherwise it means that the communication is normal, and the received data is sent to the device task in the form of a message, and the data logic processing and analysis of the application layer are performed. The specific sending process of the host is:
[0067] S211, blocking and waiting for the receiving host to send a message;
[0068] S212, enable sending and enable the serial port sending idle interrupt;
[0069] S213, determine whether it is a non-broadcast frame, if so, return to S21, if not, proceed to the next step;
[0070] S214, waiting for the serial port data reception completion message after the blocking frame timeout period;
[0071] S215, judging whether the reception has timed out, that is, judging whether the reception completion message has not been received when the frame timeout period has expired, if so, it means that the reception has timed out, if not, it means that the reception has not timed out;
[0072] If yes, determine whether the number of retransmissions exceeds the maximum number of retransmissions. If yes, send a receive timeout message. If no, return to S21.
[0073] If not, send a data reply success message and return to S21.
[0074] (b) If the initialization device is configured as a slave mode, the serial port receiving function is enabled by default, the sending interrupt is disabled, and the slave mode blocks and waits for the receiving interrupt to complete the receiving message until the receiving completion message sent by the frame-breaking timer is received. The received data is forwarded to the device task of the application layer through a message to process and parse the received data. After the application layer completes the processing, the device task replies the reply data to the serial port transceiver management task through a message, and the serial port transceiver management task starts the sending interrupt to send the reply data out, completing a round of data reception and reply. The specific sending process of the slave is:
[0075] S221, open the serial port receiving interrupt and wait for the host computer to receive data;
[0076] S222, blocking and waiting for the message that serial port data reception is completed;
[0077] S223, when receiving the serial port data reception success message, block and wait for 50ms, and accept the data parsing reply frame message;
[0078] S224, determine whether it has timed out, if so, continue to block and wait for the message that the serial port data reception is completed; if not, enable sending and turn on the serial port send idle interrupt.
[0079] S3. Create device tasks, such as Figure 7 As shown, the device communication task parameters are passed in and then initialized (the device task is also called the device general task, which is an application layer device framework, and the specific functions implemented are passed in through function pointers); and the device task is bound to the receiving message channel of the serial port transceiver management task, and the data to be received and sent is sent to the serial port transceiver management task in the form of a message to realize data reception and transmission. If multiple device tasks are to be bound to one serial port, all the messages of the device tasks are bound to the receiving message channel of the same serial port transceiver management task.
[0080] like Figure 8 As shown, in the host mode, the data transmission and reception process between the device task and the serial port transmission and reception management task is as follows:
[0081] S311, after the device task parses the data frame to be sent according to the application logic, it sends the serial port data frame to the serial port transceiver management task;
[0082] S312, the serial port transceiver management task starts the serial port sending idle interrupt of the bound serial port to the interrupt function;
[0083] S313, the interrupt function sends data to the serial port peripheral, which is then sent to the slave (pv inv) by the serial port peripheral, and after receiving the data replied by the slave, the interrupt function returns the filling data to the serial port transceiver management task; wherein the filling data includes the sending buffer buf and length information;
[0084] S314, the serial port transceiver management task sends a return frame to the device task, and the device task parses the reply data;
[0085] like Fig. 9 As shown, in slave mode, the data transmission and reception process between the device task and the serial port transceiver management task is as follows:
[0086] S321, the interrupt function receives the power-on command sent by the host computer, and sends a data reception success message to the serial port transceiver management task;
[0087] S322, the serial port transceiver management task sends a data reception success message to the device task, wherein the data reception success message includes the reception buffer buf and length information;
[0088] S323, the device task sends the call data pointer parsing data to the application layer task (modbus rtumaster task), and the application layer task sends a power-on command to the slave (pv inv), receives a reply power-on command, fills in the query content and returns the filled data to the device task; wherein the filled data includes the send buffer buf and length information;
[0089] S324, the device task returns a data reply message to the serial port transceiver management task;
[0090] S325, the serial port transceiver management task starts the slave-bound serial port and sends an idle interrupt to the interrupt function; the interrupt function sends reply data to the host computer.
[0091] like Fig.10 As shown, it is an example of a message structure, including a structure of a message sent by a serial port management task to a device task, a structure of a message sent by a device task to a serial port management task, and a structure of a serial port data reception completion message.
[0092] Embodiment 2
[0093] Based on the same inventive concept, the present application also provides a device corresponding to the method in Example 1, see Example 2 for details.
[0094] like Fig.11 As shown, in this embodiment, a serial port transceiver management device is provided, including a transceiver function implementation module, a serial port transceiver management task creation module and a device task creation module.
[0095] The transceiver function implementation module is used to implement the transceiver functions of the serial port driver and the interrupt service function, thereby satisfying:
[0096] (a) During the data transmission process, the serial port transceiver management task will fill the transmission data and the length to be sent into the corresponding serial port transmission buffer, and trigger the serial port transmission idle interrupt, enter the serial port transmission idle interrupt to send the first byte of data; then turn on the transmission completion interrupt in the serial port transmission idle interrupt, and send the data in the buffer by byte transmission task. Each time a byte is sent, the transmission completion interrupt will be triggered until all the transmissions are completed, turn off the transmission completion interrupt and turn on the serial port receiving interrupt; after the transmission completion interrupt is turned on, it can ensure that each byte is sent after it is completed, and then the next byte is sent. Figure 3 As shown, the execution process of the interrupt service function is:
[0097] S11, first the serial port sends an idle interrupt to trigger an interrupt, and determines whether it is the first byte in the cache;
[0098] If yes, disable the serial port send idle interrupt, send the next byte in the buffer, and enable the send completion interrupt;
[0099] If not, determine whether all the data in the buffer has been sent. If so, end the sending, turn off the serial port send idle interrupt, enable the receiving and turn on the serial port receive interrupt; if not, send the next byte of the buffer and turn on the sending completion interrupt;
[0100] S12, exiting the serial port sending idle interrupt.
[0101] (b) During data reception, if Figure 4 As shown, the serial port receiving interruption is used to receive data byte by byte, and the data is saved in the receiving buffer. At the same time, the frame break timer is started, the byte timeout time is set, and the serial port receiving interruption is exited.
[0102] The frame break time can be freely set according to the byte interval requirements of the protocol. For example, the Modbus protocol requires that the byte interval is no more than 3.5 byte times. For example, at a baud rate of 9600, each byte time is approximately 1ms, and 3.5 bytes is 3.5ms.
[0103] (c) Receiving the judgment of completion, such as Figure 5 As shown, when the byte timeout time is reached, the frame break timer triggers the frame break timeout interrupt to determine whether data reception is complete, and sends a reception completion message to the serial port transceiver management task to exit the frame break timeout interrupt;
[0104] The frame break timer is used to implement the frame break processing of byte timeout, which can match the protocol receiving unknown length, especially when acting as a slave, the length of the data sent by the host cannot be predicted by the slave.
[0105] The serial port transceiver management task creation module is used to create an independent serial port transceiver management task and initialize it. During initialization, parameters are passed in according to the device configuration; the parameters include: configuration parameter definition, serial port name, master-slave mode, serial port number, serial port peripherals, baud rate, parity check, data bit and stop bit;
[0106] like Figure 6 As shown, during initialization, parameters are passed in according to the device configuration; the parameters include: configuration parameter definition, serial port name, master-slave mode, serial port number, serial port peripherals, baud rate, parity check, data bits, and stop bits;
[0107] (a) If the initialization device is configured as a host mode, the receiving and sending functions and interrupts of the serial port are closed by default, and the receiving and sending state is entered by receiving a sending request message. If there is no sending request message, it blocks and waits until the sending request message is received, and then the host's sending process is executed, the corresponding serial port interrupt is turned on, and the sending request message contains a receiving completion message of the serial port frame break timer that blocks and waits within the frame timeout period; if the receiving completion message sent by the frame break timer is not received within the frame timeout period, it is judged as a receiving timeout, and a receiving timeout message is sent to the device task, otherwise it means that the communication is normal, and the received data is sent to the device task in the form of a message, and the data logic processing and analysis of the application layer are performed. The specific sending process of the host is:
[0108] S211, blocking and waiting for the receiving host to send a message;
[0109] S212, enable sending and enable the serial port sending idle interrupt;
[0110] S213, determine whether it is a non-broadcast frame, if so, return to S21, if not, proceed to the next step;
[0111] S214, waiting for the serial port data reception completion message after the blocking frame timeout period;
[0112] S215, judging whether the reception has timed out, that is, judging whether the reception completion message has not been received when the frame timeout period has expired, if so, it means that the reception has timed out, if not, it means that the reception has not timed out;
[0113] If yes, determine whether the number of retransmissions exceeds the maximum number of retransmissions. If yes, send a receive timeout message. If no, return to S21.
[0114] If not, send a data reply success message and return to S21.
[0115] (b) If the initialization device is configured as a slave mode, the serial port receiving function is enabled by default, the sending interrupt is disabled, and the slave mode blocks and waits for the receiving interrupt to complete the receiving message until the receiving completion message sent by the frame-breaking timer is received. The received data is forwarded to the device task of the application layer through a message to process and parse the received data. After the application layer completes the processing, the device task replies the reply data to the serial port transceiver management task through a message, and the serial port transceiver management task starts the sending interrupt to send the reply data out, completing a round of data reception and reply. The specific sending process of the slave is:
[0116] S221, open the serial port receiving interrupt and wait for the host computer to receive data;
[0117] S222, blocking and waiting for the message that serial port data reception is completed;
[0118] S223, when receiving the serial port data reception success message, block and wait for 50ms, and accept the data parsing reply frame message;
[0119] S224, determine whether it has timed out, if so, continue to block and wait for the message that the serial port data reception is completed; if not, enable sending and turn on the serial port send idle interrupt.
[0120] The device task creation module is used to create a device task and bind the device task to the receiving message channel of the serial port transceiver management task. The data to be received and sent is sent to the serial port transceiver management task in the form of a message to realize data reception and transmission. If multiple device tasks are to be bound to one serial port, all the messages of the device tasks are bound to the receiving message channel of the same serial port transceiver management task.
[0121] like Figure 8 As shown, in the host mode, the data transmission and reception process between the device task and the serial port transmission and reception management task is as follows:
[0122] S311, after the device task parses the data frame to be sent according to the application logic, it sends the serial port data frame to the serial port transceiver management task;
[0123] S312, the serial port transceiver management task starts the serial port sending idle interrupt of the bound serial port to the interrupt function;
[0124] S313, the interrupt function sends data to the serial port peripheral, which is then sent to the slave (pv inv) by the serial port peripheral, and after receiving the data replied by the slave, the interrupt function returns the filling data to the serial port transceiver management task; wherein the filling data includes the sending buffer buf and length information;
[0125] S314, the serial port transceiver management task sends a return frame to the device task, and the device task parses the reply data;
[0126] like Fig. 9 As shown, in slave mode, the data transmission and reception process between the device task and the serial port transceiver management task is as follows:
[0127] S321, the interrupt function receives the power-on command sent by the host computer, and sends a data reception success message to the serial port transceiver management task;
[0128] S322, the serial port transceiver management task sends a data reception success message to the device task, wherein the data reception success message includes the reception buffer buf and length information;
[0129] S323, the device task sends the call data pointer parsing data to the application layer task (modbus rtumaster task), and the application layer task sends a power-on command to the slave (pv inv), receives a reply power-on command, fills in the query content and returns the filled data to the device task; wherein the filled data includes the send buffer buf and length information;
[0130] S324, the device task returns a data reply message to the serial port transceiver management task;
[0131] S325, the serial port transceiver management task starts the slave-bound serial port and sends an idle interrupt to the interrupt function; the interrupt function sends reply data to the host computer.
[0132] Since the device introduced in the second embodiment of the present invention is a device used to implement the method of the first embodiment of the present invention, those skilled in the art can understand the specific structure and deformation of the device based on the method introduced in the first embodiment of the present invention, so it is not described here in detail. All devices used in the method of the first embodiment of the present invention belong to the scope of protection of the present invention.
[0133] Embodiment 3
[0134] Based on the same inventive concept, the present application provides an electronic device embodiment corresponding to the first embodiment, see the third embodiment for details. This embodiment provides an electronic device, such as Fig.12 As shown, it includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, any implementation method in the first embodiment can be implemented.
[0135] Since the electronic device introduced in this embodiment is a device used to implement the method in the first embodiment of the present application, based on the method introduced in the first embodiment of the present application, a person skilled in the art can understand the specific implementation of the electronic device of the present embodiment and its various variations, so how the electronic device implements the method in the embodiment of the present application is not described in detail here. As long as a person skilled in the art implements the device used by the method in the embodiment of the present application, it belongs to the scope of protection of the present application.
[0136] Embodiment 4
[0137] Based on the same inventive concept, this application provides a storage medium corresponding to Embodiment 1, see Embodiment 4 for details. This embodiment provides a computer-readable storage medium, such as Fig.13 As shown, a computer program is stored thereon, and when the computer program is executed by a processor, any implementation method in Example 1 can be implemented.
[0138] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0139] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0140] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0141] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process in the computer or other programmable device. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0142] Although the specific implementation modes of the present invention are described above, those skilled in the art should understand that the specific implementation modes described are only illustrative and are not intended to limit the scope of the present invention. Equivalent modifications and changes made by those skilled in the art in accordance with the spirit of the present invention should be included in the scope of protection of the claims of the present invention.
Claims
1. A serial port receiving and sending management method, characterized in that: include: S1. Implement the receiving and sending functions of the serial port driver and interrupt service function to meet the following requirements: During the data transmission process, the serial port transceiver management task will fill the transmission data and the length to be sent into the corresponding serial port transmission buffer, and trigger the serial port transmission idle interrupt, enter the serial port transmission idle interrupt to send the first byte of data; then turn on the transmission completion interrupt in the serial port transmission idle interrupt, and send the data in the buffer by byte transmission task. Each time a byte is sent, the transmission completion interrupt will be triggered until all the transmission is completed, turn off the transmission completion interrupt and turn on the serial port receiving interrupt; During the data receiving process, the serial port receiving interruption is used to receive data byte by byte, and after receiving the data, the data is saved in the receiving buffer. At the same time, the frame break timer is started, the byte timeout time is set, and the serial port receiving interruption is exited; The judgment of receiving completion is that the byte timeout time is reached, and the frame break timer triggers the frame break timeout interrupt to realize the judgment of data receiving completion, and sends a receiving completion message to the serial port transceiver management task, and exits the frame break timeout interrupt; S2. Create an independent serial port transceiver management task and initialize it. During initialization, input parameters according to the device configuration; the parameters include: configuration parameter definition, serial port name, master-slave mode, serial port, serial port peripheral, baud rate, parity check, data bit and stop bit; If the initialization device is configured as host mode, the receiving and sending functions and interrupts of the serial port are closed by default, and the receiving and sending state is entered by receiving a sending request message. If there is no sending request message, it blocks and waits until the sending request message is received, executes the sending process of the host, opens the corresponding serial port interrupt, and the sending request message contains a receiving completion message of the serial port frame break timer that blocks and waits within the frame timeout period; if the receiving completion message sent by the frame break timer is not received within the frame timeout period, it is judged as a receiving timeout, and a receiving timeout message is sent to the device task, otherwise it means that the communication is normal, and the received data is sent to the device task in the form of a message, and the data logic processing and analysis of the application layer are performed; If the initialization device is configured as a slave mode, the serial port receiving function is enabled by default, the sending interrupt is disabled, and the slave mode blocks and waits for the receiving interrupt to complete the receiving message until the receiving completion message sent by the frame-breaking timer is received. The received data is forwarded to the device task of the application layer through a message to process and parse the received data. After the application layer completes the processing, the device task replies the reply data to the serial port transceiver management task through a message, and the serial port transceiver management task starts the sending interrupt to send the reply data out, completing a round of data reception and reply; S3. Create a device task and bind the device task to the receiving message channel of the serial port transceiver management task. Send the data to be received and sent to the serial port transceiver management task in the form of messages to realize data reception and transmission. If multiple device tasks need to be bound to one serial port, all the messages of the device tasks will be bound to the receiving message channel of the same serial port transceiver management task.
2. A serial port receiving and sending management method according to claim 1, characterized in that: In S1, the execution process of the interrupt service function is: S11, first the serial port sends an idle interrupt to trigger an interrupt, and determines whether it is the first byte in the cache; If yes, disable the serial port send idle interrupt, send the next byte in the buffer, and enable the send completion interrupt; If not, determine whether all the data in the buffer has been sent. If so, end the sending, turn off the serial port send idle interrupt, enable the receiving and turn on the serial port receive interrupt; if not, send the next byte of the buffer and turn on the sending completion interrupt; S12, exiting the serial port sending idle interrupt.
3. A serial port transceiver management method according to claim 1, characterized in that: In S2, the sending process of the host is specifically: S21, blocking and waiting for the receiving host to send a message; S22, enable sending and open the serial port sending idle interrupt; S23, determine whether it is a non-broadcast frame, if so, return to S21, if not, proceed to the next step; S24, blocking frame timeout time to wait for serial port data reception completion message; S25. Determine whether the reception has timed out. If yes, determine whether the number of retransmissions exceeds the maximum number of retransmissions. If yes, send a receive timeout message. If no, return to S21. If not, send a data reply success message and return to S21.
4. A serial port transceiver management method according to claim 1, characterized in that: In S3, In the host mode, the data transmission and reception process between the device task and the serial port transceiver management task is as follows: S311, after the device task parses the data frame to be sent according to the application logic, it sends the serial port data frame to the serial port transceiver management task; S312, the serial port transceiver management task starts the serial port sending idle interrupt of the bound serial port to the interrupt function; S313, the interrupt function sends data to the serial port peripheral, which is then sent to the slave by the serial port peripheral, and after receiving the data replied by the slave, the interrupt function returns the filling data to the serial port transceiver management task; wherein the filling data includes the sending buffer buf and length information; S314, the serial port transceiver management task sends a return frame to the device task, and the device task parses the reply data; In slave mode, the data transmission and reception process between the device task and the serial port transceiver management task is as follows: S321, the interrupt function receives the power-on command sent by the host computer, and sends a data reception success message to the serial port transceiver management task; S322, the serial port transceiver management task sends a data reception success message to the device task, wherein the data reception success message includes the reception buffer buf and length information; S323, the device task sends the call data pointer parsing data to the application layer task, and the application layer task sends a power-on command to the slave, receives a reply power-on command, fills in the query content and returns the filling data to the device task; wherein the filling data includes the sending buffer buf and length information; S324, the device task returns a data reply message to the serial port transceiver management task; S325, the serial port transceiver management task starts the slave-bound serial port and sends an idle interrupt to the interrupt function; the interrupt function sends reply data to the host computer.
5. A serial port transceiver management device, characterized in that: include: The transceiver function realization module is used to realize the transceiver function of the serial port driver and interrupt service function, thereby meeting: During the data transmission process, the serial port transceiver management task will fill the transmission data and the length to be sent into the transmission buffer of the corresponding serial port, and trigger the serial port transmission idle interrupt, enter the serial port transmission idle interrupt to send the first byte of data; then turn on the transmission completion interrupt in the serial port transmission idle interrupt sending function, and send the data in the buffer by byte transmission task. Each time a byte is sent, the transmission completion interrupt will be triggered until all the transmission is completed, turn off the transmission completion interrupt and turn on the serial port receiving interrupt; During the data receiving process: the serial port receiving interruption is used to receive data byte by byte and save the data into the receiving buffer. At the same time, the frame break timer is started, the byte timeout time is set, and the serial port receiving interruption is exited; Judgment of reception completion: when the byte timeout time is reached, the frame break timer triggers the frame break timeout interrupt to realize the judgment of data reception completion, and sends a reception completion message to the serial port transceiver management task, and exits the frame break timeout interrupt; The serial port transceiver management task creation module is used to create an independent serial port transceiver management task and initialize it. During initialization, parameters are passed in according to the device configuration; the parameters include: configuration parameter definition, serial port name, master-slave mode, serial port number, serial port peripherals, baud rate, parity check, data bits, and stop bits; If the initialization device is configured as host mode, the receiving and sending functions and interrupts of the serial port are closed by default, and the receiving and sending state is entered by receiving a sending request message. If there is no sending request message, it blocks and waits until the sending request message is received, executes the sending process of the host, opens the corresponding serial port interrupt, and the sending request message contains a receiving completion message of the serial port frame break timer that blocks and waits within the frame timeout period; if the receiving completion message sent by the frame break timer is not received within the frame timeout period, it is judged as a receiving timeout, and a receiving timeout message is sent to the device task, otherwise it means that the communication is normal, and the received data is sent to the device task in the form of a message, and the data logic processing and analysis of the application layer are performed; If the initialization device is configured as a slave mode, the serial port receiving function is enabled by default, the sending interrupt is disabled, and the slave mode blocks and waits for the receiving interrupt to complete the receiving message until the receiving completion message sent by the frame-breaking timer is received. The received data is forwarded to the device task of the application layer through a message to process and parse the received data. After the application layer completes the processing, the device task replies the reply data to the serial port transceiver management task through a message, and the serial port transceiver management task starts the sending interrupt to send the reply data out, completing a round of data reception and reply; The device task creation module is used to create a device task and bind the device task to the receiving message channel of the serial port transceiver management task. The data to be received and sent is sent to the serial port transceiver management task in the form of a message to realize data reception and transmission. If multiple device tasks are to be bound to one serial port, all the messages of the device tasks are bound to the receiving message channel of the same serial port transceiver management task.
6. A serial port transceiver management device according to claim 5, characterized in that: During the data transmission process, the execution process of the interrupt service function is: S11, first the serial port sends an idle interrupt to trigger an interrupt, and determines whether it is the first byte in the cache; If yes, disable the serial port send idle interrupt, send the next byte in the buffer, and enable the send completion interrupt; If not, determine whether all the data in the buffer has been sent. If so, end the sending, turn off the serial port send idle interrupt, enable the receiving and turn on the serial port receive interrupt; if not, send the next byte of the buffer and turn on the sending completion interrupt; S12, exiting the serial port sending idle interrupt.
7. A serial port transceiver management device according to claim 5, characterized in that: The sending process of the host is specifically: S21, blocking and waiting for the receiving host to send a message; S22, enable sending and open the serial port sending idle interrupt; S23, determine whether it is a non-broadcast frame, if so, return to S21, if not, proceed to the next step; S24, blocking frame timeout time to wait for serial port data reception completion message; S25, determining whether reception has timed out; If yes, determine whether the number of retransmissions exceeds the maximum number of retransmissions. If yes, send a receive timeout message. If no, return to S21. If not, send a data reply success message and return to S21.
8. A serial port transceiver management device according to claim 5, characterized in that: In the host mode, the data transmission and reception process between the device task and the serial port transceiver management task is as follows: S311, after the device task parses the data frame to be sent according to the application logic, it sends the serial port data frame to the serial port transceiver management task; S312, the serial port transceiver management task starts the serial port sending idle interrupt of the bound serial port to the interrupt function; S313, the interrupt function sends data to the serial port peripheral, which is then sent to the slave by the serial port peripheral, and after receiving the data replied by the slave, the interrupt function returns the filling data to the serial port transceiver management task; wherein the filling data includes the sending buffer buf and length information; S314, the serial port transceiver management task sends a return frame to the device task, and the device task parses the reply data; In slave mode, the data transmission and reception process between the device task and the serial port transceiver management task is as follows: S321, the interrupt function receives the power-on command sent by the host computer, and sends a data reception success message to the serial port transceiver management task; S322, the serial port transceiver management task sends a data reception success message to the device task, wherein the data reception success message includes the reception buffer buf and length information; S323, the device task sends the call data pointer parsing data to the application layer task, and the application layer task sends a power-on command to the slave, receives a reply power-on command, fills in the query content and returns the filling data to the device task; wherein the filling data includes the sending buffer buf and length information; S324, the device task returns a data reply message to the serial port transceiver management task; S325, the serial port transceiver management task starts the slave-bound serial port and sends an idle interrupt to the interrupt function; the interrupt function sends reply data to the host computer.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the program, the method according to any one of claims 1 to 4 is implemented.
10. 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 4 is implemented.
Citation Information
Patent Citations
Multi-channel software-hardware paralleled serial communication processing method
CN105487992A