Reliable Transmission Method for Serial Port Data Packets in Linux System Based on Loongson 2K Processor
By developing program components in the user-state of Linux system, using data packaging and fixed-length packet sending and receiving modules, the problem of interruption of serial process data transmission on the Loongson 2K processor platform is solved, reliable transmission of serial data packets is achieved, development and maintenance costs are reduced, and serial communication performance is maintained.
Patent Information
- Application Number
- CN202211324693.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-10-27
- Publication Date
- 2025-06-03
- Estimated Expiration
- 2042-10-27
AI Technical Summary
In the Linux system running on the existing Loongson 2K processor platform, the serial port process causes data transmission interruption due to the scheduling of the task scheduler, resulting in incomplete data, and thus causing errors, increasing the time and labor costs of development and maintenance.
A program component is developed in the user mode of Linux system, with a structure divided into user layer, kernel layer and hardware layer to realize the reliable transmission of serial port data packets. This method uses the data packaging and fixed-length packet sending and receiving module to ensure the integrity of data transmission using the serial port sending and receiving interface of the Glibc library.
It realizes that without modifying the Linux system kernel serial driver and serial subsystem, ensures reliable transmission of serial data packets, avoids data interruption and incompleteness, reduces the development and maintenance costs of user programs, and maintains the performance of serial communication.
Smart Images

Figure CN115794236B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of serial communication, and particularly relates to a method for reliable transmission of serial port data packets in a Linux system based on the Loongson 2K processor. Background Art
[0002] At present, 12 UART controllers are integrated on the Loongson 2K processor platform and communicate with the bus bridge through the APB bus. The UART controller provides the function of serial communication with other external devices. For example, it communicates with another computer using the UART serial line in accordance with the RS232 standard. Among them, UART0, UART3, UART4, and UART5 share the UART0 interface; UART1, UART6, UART7, and UART8 share the UART1 interface; UART2, UART9, UART10, and UART11 share the UART2 interface, as Figure 1 shown in the hardware layer. In addition, as Figure 1 shown, the software layer runs the Linux system. The Linux system includes the user space and the kernel space. The serial port process in the user space realizes the function of serial communication with other external devices through the UART serial port character device interface of the Glibc software library. The serial port process copies the serial port data frame to be sent into the UART serial port circular buffer space in the core layer of the UART serial port driver. The core layer of the UART serial port driver routes the serial port data frame in the UART serial port circular buffer space to the specified UART device through the UART interface controller driver according to the device descriptor. The serial port data frame to be received by the serial port process is obtained from the UART serial port circular buffer space in the core layer of the UART serial port driver through the UART device defined in the UART interface controller driver. The serial port process then reads the serial port data frame into the process space of the serial port process through the UART serial port character device interface of the Glibc software library. In addition, because the Linux system is a non-real-time, batch processing system based on the macro kernel design. The serial port process, like other processes in the user space of the Linux system, is scheduled by the macro kernel task scheduler in the kernel space of the Linux system.
[0003] The task scheduler used by the Linux system running on the existing Loongson 2K processor platform is a scheduler implemented based on a non-preemptive policy scheduling algorithm and belongs to a non-real-time scheduler. This causes the serial port process to be scheduled out by the Linux system task scheduler due to task scheduling needs when sending and receiving data. As a result, the serial port process does not run, leading to the interruption of data sending and receiving by the serial port process. The interruption of data sending and receiving by the serial port process causes the serial port process to mistakenly think that the data sending and receiving are completed. When other user-mode programs then use the data sent and received by the serial port process, the data is incomplete, resulting in errors. In addition, the UART driver subsystem in the Linux system is relatively complete and robust general-purpose code. Making rash changes will lead to unpredictable errors, and in severe cases, it may cause the entire Linux system to crash. For user-developed serial port-based application programs, especially relatively complete application programs, rashly changing the data sending and receiving subroutines for the serial port will also reduce the stability of the user application program. This greatly increases the time cost and labor cost for the development and maintenance of user application programs. Summary of the Invention
[0004] (1) Technical problems to be solved
[0005] The technical problem to be solved by the present invention is: how to design a reliable transmission method for serial port data packets under the Linux system to ensure that the serial port data sent and received by the serial port process in the user state is the data expected by the user and there will be no defect that the data expected by the user is incomplete due to the scheduling of the Linux system task scheduler.
[0006] (2) Technical solutions
[0007] To solve the above technical problems, the present invention provides a reliable transmission method for serial port data packets in a Linux system based on a Loongson 2K processor. This method is implemented based on a program component developed in the user space of the Linux system. The structure of the program component is divided into three layers, namely the user layer, the kernel layer, and the hardware layer. The user layer runs the serial port process to implement the functions of sending and receiving serial port data. The kernel layer is used to implement the UART serial port driver program. The hardware layer includes the UART serial port communication link. Let A and B be two device ends running the program component respectively. Then, the Linux system at end A communicates with the Linux system in the virtual box at end B through the UART serial port communication line. The programs running at ends A and B are the same. The user layers at both ends A and B run two modules, namely the data sending work module and the data receiving work module. Among them, the data sending work module includes a data encapsulation module program, a fixed-length packet sending module program, and a Glibc library serial port data sending interface. The data receiving work module includes a data parsing module program, a fixed-length packet receiving module program, and a Glibc library serial port data receiving interface. The data sending work module at end A sends data to the data receiving work module at end B, while the data sending work module at end B sends data to the data receiving work module at end A.
[0008] Preferably, this method includes two working processes, namely the serial port data sending working process and the serial port data receiving working process.
[0009] Preferably, the serial port data sending working process is as follows:
[0010] Step A1: The application program developed by the user prepares to start sending serial port data according to requirements.
[0011] Step A2: The data encapsulation module program calculates the number of fixed-length serial port data packets that can be encapsulated according to the length of the serial port data to be sent in Step A1.
[0012] Step A3: The data encapsulation module program encapsulates a header data packet. This header data packet records the number of packets of the data to be sent. And the data encapsulation module program judges whether the data volume calculated in Step A2 that can be encapsulated into fixed-length serial port data packets is greater than 1. If it is greater than 1, then enter Step A4, otherwise enter Step A5.
[0013] Step A4: The data encapsulation module program encapsulates the serial port data to be sent in Step A1 into multiple data packets. After completion, enter Step A6.
[0014] Step A5: The data encapsulation module program encapsulates the serial port data to be sent in Step A1 into one data packet. After completion, enter Step A6.
[0015] Step A6: The fixed-length packet sending module program sends a packet header data packet encapsulated by the data encapsulation module program in Step A3;
[0016] Step A7: Send the serial port data frame in the packet header data packet through the serial port data sending interface of the Glibc library;
[0017] Step A8: The fixed-length packet sending module program determines whether the sending of the data packet header data packet is completed. If the sending is completed, go to Step A9; otherwise, return to Step A7 to send the uncompleted serial port data frame in the packet header data packet;
[0018] Step A9: The fixed-length packet sending module program sequentially selects an unsent data packet encapsulated in Step A4 or Step A5 and enters Step A10;
[0019] Step A10: The serial port data sending interface used to send the data packet is the same as the serial port data sending interface of the Glibc library used in Step A7. Send the serial port data frame in the data packet through the serial port data sending interface of the Glibc library;
[0020] Step A11: The fixed-length packet sending module program determines whether the sending of the data packet is completed. If the sending is completed, go to Step A12; otherwise, return to Step A10 to send the uncompleted serial port data frame in the data packet;
[0021] Step A12: The fixed-length packet sending module program determines whether the sending of all data packets is completed. If completed, enter Step A13; otherwise, jump back to Step A9 to continue sending the next unsent data packet;
[0022] Step A13: The application program developed by the user completes the sending of the serial port data.
[0023] Preferably, the serial port data receiving work process is as follows:
[0024] Step B1: The application program developed by the user starts to receive serial port data through the serial port interrupt;
[0025] Step B2: The fixed-length packet receiving module program receives the serial port data frame through the serial port data receiving interface of the Glibc library;
[0026] Step B3: The fixed-length packet receiving module program calculates whether the number of received serial port data frames is equal to the length of one packet. If so, enter Step B4; otherwise, return to Step B2 to continue receiving the serial port data frame;
[0027] Step B4: The data parsing module program parses the data packet header received in Step B3 to obtain the number of data packets to be received;
[0028] Step B5: The fixed-length packet receiving module program receives the serial port data frame;
[0029] Step B6: The program of the fixed-length packet receiving module calculates whether the number of received serial port data frames is equal to the length of one packet. If so, go to Step B7; otherwise, return to Step B5 to continue receiving serial port data frames.
[0030] Step B7: The program of the fixed-length packet receiving module calculates whether the number of received data packets is equal to the number of data packets in the data packet header parsed in Step B4. If so, go to Step B8; otherwise, return to Step B5 to continue receiving serial port data frames.
[0031] Step B8: The program of the data parsing module parses all received data packets, extracts the data in all data packets, and transfers the data to the application program developed by the user to complete the reception of serial port data.
[0032] Preferably, the hardware layer further includes a UART serial port controller.
[0033] The present invention also provides a serial port data packet reliable transmission system for implementing the above method.
[0034] The present invention also provides a serial port communication method implemented based on the above method.
[0035] The present invention also provides a serial port communication method implemented based on the above system.
[0036] The present invention also provides an application of the above method in the field of serial port communication technology.
[0037] The present invention also provides an application of the above system in the field of serial port communication technology.
[0038] (III) Beneficial Effects
[0039] The present invention can achieve reliable transmission of serial port data packets in a non-real-time, batch-processing Linux system without changing the serial port driver and the serial port subsystem of the Linux system kernel. Moreover, the present invention can ensure that the serial port data packets received by the user program are uninterrupted and the user program can correctly judge the integrity of the data by only changing the serial port interface function. In addition, the present invention makes full use of the serial port sending and receiving data interface functions in the Glibc library. Therefore, the method of the present invention has good code portability. Compared with the prior art solutions, the method of the present invention has a minimal impact on the performance of serial port communication. BRIEF DESCRIPTION OF THE DRAWINGS
[0040] Figure 1 Schematic diagram of the prior art solution for serial port data packet transmission in a Linux system based on Loongson processors;
[0041] Figure 2Schematic diagram of the program component structure for implementing the method of the present invention;
[0042] Figure 3 Schematic diagram of the reliable transmission workflow of the serial port data packet of the present invention. Detailed implementation manners
[0043] To make the objectives, contents and advantages of the present invention clearer, the following further describes in detail the specific implementation manners of the present invention with reference to the accompanying drawings and embodiments.
[0044] A method for reliable transmission of serial port data packets in a Linux system based on the Loongson 2K processor provided by the present invention is implemented based on the Linux system running on the Loongson 2K processor platform. Mainly, a set of program components is developed in the user space of the Linux system to achieve reliable transmission of serial port data packets under the Linux system. The present invention can ensure that the serial port data sent and received by the serial port process in the user space is the data expected by the user, and there will be no defect that the data expected by the user is incomplete due to the scheduling of the Linux system task scheduler.
[0045] For the application programs already developed by users, without changing the original program logic, only by changing the serial port program interface functions called by the application programs already developed by users, the reliable transmission of serial port data packets under the Linux system can be ensured. Moreover, the time overhead of the program components developed by the present invention is very small, and the impact on the serial port transceiver performance of the Linux system is very small.
[0046] Refer to Figure 2 As shown, a method for reliable transmission of serial port data packets in a Linux system based on the Loongson 2K processor provided by the present invention is implemented based on a program component developed in the user space of the Linux system. The structure of the program component is divided into three layers, namely the user layer, the kernel layer and the hardware layer. The user layer runs the serial port process to implement the functions of sending serial port data and receiving serial port data; the kernel layer mainly implements the UART serial port driver program; the hardware layer includes the UART serial port controller and the UART serial port communication link. Figure 2 Both the virtual box A end and the virtual box B end shown are Linux systems based on the Loongson 2K processor platform. The Linux system at the virtual box A end communicates with the Linux system at the virtual box B end through the UART serial port communication line. The programs running at the virtual box A end and the virtual box B end are the same. There are two serial port working modules at both the virtual box A end and the virtual box B end, namely the data sending working module and the data receiving module. The data sending working module includes the data encapsulation module program, the fixed-length packet sending module program and the Glibc library serial port data sending interface. The data receiving working module includes the data parsing module program, the fixed-length packet receiving module program and the Glibc library serial port data receiving interface.
[0047] In the above program components, a serial port data sending program component and a serial port data receiving program component are formed. Refer to Figure 3 , the reliable transmission method of serial port data packets in the Linux system based on Loongson processor of the present invention includes two working processes, namely the serial port data sending working process of the serial port data sending program component and the serial port data receiving working process of the serial port data receiving program component, which are as follows:
[0048] The serial port data sending working process of the serial port data sending program component is as follows:
[0049] Step A1: The application program developed by the user prepares to start sending serial port data according to the requirements;
[0050] Step A2: Such as the data encapsulation module program shown in Figure 2 . The data encapsulation module program of the serial port data sending program component calculates the number of fixed-length serial port data packets that can be encapsulated according to the length of the serial port data to be sent in Step A1;
[0051] Step A3: The data encapsulation module program encapsulates a header data packet, and this header data packet records the number of data packets to be sent. And the data encapsulation module program judges whether the data volume that can be encapsulated into fixed-length serial port data packets calculated in Step A2 is greater than 1. If it is greater than 1, it enters Step A4, otherwise it enters Step A5;
[0052] Step A4: The data encapsulation module program encapsulates the serial port data to be sent in Step A1 into multiple data packets, and after completion, enters Step A6;
[0053] Step A5: The data encapsulation module program encapsulates the serial port data to be sent in Step A1 into one data packet, and after completion, enters Step A6;
[0054] Step A6: Such as the fixed-length packet sending module program shown in Figure 2 . This fixed-length packet sending module program sends one header data packet encapsulated by the data encapsulation module program in Step A3;
[0055] Step A7: Such as the Glibc library serial port data sending interface shown in Figure 2 . The serial port data sending program component sends the serial port data frame in the header data packet through the Glibc library serial port data sending interface;
[0056] Step A8: The fixed-length packet sending module program of the serial port data sending program component judges whether the sending of the data header data packet is completed. If the sending is completed, it enters Step A9, otherwise it returns to Step A7 to send the serial port data frame that has not been completed in the header data packet;
[0057] Step A9: The fixed-length packet sending module program of the serial port data sending program component sequentially selects an unsent data packet encapsulated in Step A4 or Step A5 and enters Step A10;
[0058] Step A10: The Glibc library serial port data sending interface used by the serial port data sending program component to send data packets is the same as the Glibc library serial port data sending interface used in Step A7. The serial port data sending program component sends the serial port data frame in the data packet through the Glibc library serial port data sending interface;
[0059] Step A11: The fixed-length packet sending module program of the serial port data sending program component determines whether the data packet sending is completed. If the sending is completed, it enters Step A12; otherwise, it returns to Step A10 to send the unsent serial port data frame in the data packet;
[0060] Step A12: The fixed-length packet sending module program of the serial port data sending program component determines whether all data packets have been sent. If so, it enters Step A13; otherwise, it jumps back to Step A9 to continue sending the next unsent data packet;
[0061] Step A13: The user-developed application program finishes sending the serial port data.
[0062] The serial port data receiving workflow of the serial port data receiving program component is as follows:
[0063] Step B1: The user-developed application program starts receiving serial port data through the serial port interrupt;
[0064] Step B2: As shown in Figure 2 the Glibc library serial port data receiving interface and the fixed-length packet receiving module program. The fixed-length packet receiving module program of the serial port data receiving program component receives the serial port data frame through the Glibc library serial port data receiving interface;
[0065] Step B3: The fixed-length packet receiving module program of the serial port data receiving program component calculates whether the number of received serial port data frames is equal to the length of one packet. If so, it enters Step B4; otherwise, it returns to Step B2 to continue receiving serial port data frames;
[0066] Step B4: As shown in Figure 2 the data parsing module program. The data parsing module program of the serial port data receiving program component parses the data packet header received in Step B3 to obtain the number of data packets that the serial port data receiving program component needs to receive;
[0067] Step B5: Similar to Step B2, the fixed-length packet receiving module program of the serial port data receiving program component receives the serial port data frame;
[0068] Step B6: The fixed-length packet receiving module program of the serial port data receiving program component calculates whether the number of received serial port data frames is equal to the length of one packet. If so, proceed to Step B7; otherwise, return to Step B5 to continue receiving serial port data frames.
[0069] Step B7: The fixed-length packet receiving module program of the serial port data receiving program component calculates whether the number of received data packets is equal to the number of data packets in the data packet header parsed in Step B4. If so, proceed to Step B8; otherwise, return to Step B5 to continue receiving serial port data frames.
[0070] Step B8: The data parsing module program of the serial port data receiving program component parses all received data packets, extracts the data from all data packets, and passes the data to the application program developed by the user to complete the reception of serial port data.
[0071] Compared with the prior art of serial port data packet sending in the existing Linux system, the method proposed by the present invention realizes the reliable transmission of serial port data packets in the Linux system. The method proposed by the present invention does not require modifying the hardware motherboard and firmware, nor does it require modifying the serial port driver and serial port subsystem of the Linux system kernel. Compared with the prior art, there is no obvious difference in the performance of serial port sending and receiving data. Both the present invention and the background technology send a 1024-byte-length string 100,000 times through the serial port of the Linux system. The present invention can ensure that 100% of the serial port data packets sent and received by the user program are uninterrupted. While for the user program under the prior art in the same hardware and software environment, 30.113% of the sent serial port data packets are interrupted, and 94.477% of the sent serial port data packets are interrupted.
[0072] The performance comparison is shown in Table 1 below.
[0073] Table 1 Performance Comparison between the Present Invention and the Prior Art
[0074]
[0075] The above are only the preferred embodiments of the present invention. It should be noted that for those of ordinary skill in the art, without departing from the technical principle of the present invention, several improvements and deformations can be made, and these improvements and deformations should also be regarded as the protection scope of the present invention.
Claims
1. A reliable transmission method for serial port data packets in a Linux system based on the Loongson 2K processor, characterized in that, this method is implemented based on a program component developed in the user space of the Linux system. The structure of the program component is divided into three layers, namely the user layer, the kernel layer, and the hardware layer. The user layer runs the serial port process to implement the functions of sending and receiving serial port data. The kernel layer is used to implement the UART serial port driver program. The hardware layer includes the UART serial port communication link. Let A and B be two device ends running the program component respectively. Then, the Linux system at end A communicates with the Linux system at the virtual box end B through the UART serial port communication line. The programs running at ends A and B are the same. The user layers at both ends A and B run two modules, namely the data sending work module and the data receiving work module. Among them, the data sending work module includes the data encapsulation module program, the fixed-length packet sending module program, and the Glibc library serial port data sending interface. The data receiving work module includes the data parsing module program, the fixed-length packet receiving module program, and the Glibc library serial port data receiving interface. The data sending work module at end A sends data to the data receiving work module at end B, while the data sending work module at end B sends data to the data receiving work module at end A.
2. The method according to claim 1, characterized in that, this method includes two working processes, namely the serial port data sending working process and the serial port data receiving working process.
3. The method according to claim 2, characterized in that, the serial port data sending working process is as follows: Step A1: The application program developed by the user prepares to start sending serial port data according to requirements; Step A2: The data encapsulation module program calculates the number of fixed-length serial port data packets that can be encapsulated according to the length of the serial port data to be sent in Step A1; Step A3: The data encapsulation module program encapsulates a packet header data packet, and this packet header data packet records the number of packets of the data to be sent. And the data encapsulation module program judges whether the data volume calculated in Step A2 that can be encapsulated into fixed-length serial port data packets is greater than 1. If it is greater than 1, then enter Step A4, otherwise enter Step A5; Step A4: The data encapsulation module program encapsulates the serial port data to be sent in Step A1 into multiple data packets, and after completion, enter Step A6; Step A5: The data encapsulation module program encapsulates the serial port data to be sent in Step A1 into one data packet, and after completion, enter Step A6; Step A6: The fixed-length packet sending module program sends a packet header data packet encapsulated by the data encapsulation module program in Step A3; Step A7: Send the serial port data frame in the packet header data packet through the Glibc library serial port data sending interface; Step A8: The fixed-length packet sending module program judges whether the sending of the data packet header data packet is completed. If the sending is completed, then enter Step A9, otherwise return to Step A7 to send the uncompleted serial port data frame in the packet header data packet; Step A9: The fixed-length packet sending module program sequentially selects an un-sent data packet encapsulated in Step A4 or Step A5 and enters Step A10; Step A10: The serial port data sending interface of the Glibc library used to send data packets is the same as that used in Step A7. The serial port data frame in the data packet is sent through the serial port data sending interface of the Glibc library. Step A11: The fixed-length packet sending module program determines whether the data packet sending is completed. If the sending is completed, it proceeds to Step A12; otherwise, it returns to Step A10 to send the serial port data frame in the data packet that has not been completely sent. Step A12: The fixed-length packet sending module program determines whether all data packets have been sent. If so, it proceeds to Step A13; otherwise, it jumps back to Step A9 to continue sending the next data packet that has not been sent yet. Step A13: The application program developed by the user completes the sending of serial port data.
4. The method according to claim 2, wherein, the serial port data receiving workflow is as follows: Step B1: The application program developed by the user starts receiving serial port data through the serial port interrupt. Step B2: The fixed-length packet receiving module program receives the serial port data frame through the serial port data receiving interface of the Glibc library. Step B3: The fixed-length packet receiving module program calculates whether the number of received serial port data frames is equal to the length of one packet. If so, it proceeds to Step B4; otherwise, it returns to Step B2 to continue receiving serial port data frames. Step B4: The data parsing module program parses the data packet header received in Step B3 to obtain the number of data packets to be received. Step B5: The fixed-length packet receiving module program receives the serial port data frame. Step B6: The fixed-length packet receiving module program calculates whether the number of received serial port data frames is equal to the length of one packet. If so, it proceeds to Step B7; otherwise, it returns to Step B5 to continue receiving serial port data frames. Step B7: The fixed-length packet receiving module program calculates whether the number of received data packets is equal to the number of data packets in the data packet header parsed in Step B4. If so, it proceeds to Step B8; otherwise, it returns to Step B5 to continue receiving serial port data frames. Step B8: The data parsing module program parses all received data packets, extracts the data in all data packets, and passes the data to the application program developed by the user to complete the reception of serial port data.
5. The method according to any one of claims 1 to 4, wherein, the hardware layer further includes a UART serial port controller.
6. A reliable serial port data packet transmission system for implementing the method according to any one of claims 1 to 5.
7. A serial port communication method implemented based on the method according to any one of claims 1 to 5.
8. A serial port communication method implemented based on the system according to claim 6.
9. An application of the method according to any one of claims 1 to 5 in the field of serial port communication technology.
10. An application of the system according to claim 6 in the field of serial port communication technology.
Citation Information
Patent Citations
Ethernet serial port driving unit and method of transmitting data flow when performing narrowband dialing
CN101183948A
Method and device for serial communication
CN102629240A