Multi-threaded serial communication between PC and embedded system
2026-04-06 04:30:34··#1
Abstract: This paper describes the implementation of multi-threaded serial communication between a PC and an embedded computer system. The necessity of multi-threaded design is introduced, and the program design flow is given. Keywords: Multi-threading; Serial communication 1. Introduction Currently, advanced embedded computers, with their excellent quality, high reliability, and modularity, are widely used in industrial control, aerospace, medical, intelligent instruments, communication, CNC, automated production equipment, data acquisition, and other fields. In practical applications, sometimes it is necessary to leverage the powerful data processing capabilities and rich software resources of microcomputers to make the assembled system more powerful. Therefore, to improve the overall performance of the system, communication between the PC and the embedded computer must be implemented. In navigation instrument production management, serial communication is adopted for data download due to its advantages such as simple connection, flexible and convenient use, and reliable data transmission. However, due to the shielding strategy adopted by Windows 95/98 for low-level system operations, users are not allowed to directly operate hardware I/O ports, and serial communication can only be accomplished by calling API functions; at the same time, Windows 9x uses message queues to drive the management program, making it difficult to implement DOS interrupt service routines, and real-time performance and reliability cannot be guaranteed; therefore, multi-threaded programming is used to solve this problem, which can improve data transmission throughput and application reliability. 2. System Function Introduction and Basic Structure This paper discusses the system using a navigation device production management system as an example. The embedded computer on site uploads stored information to the PC and downloads the latest version information from the PC. The data sent back by the embedded computer can be vividly and in real-time displayed to the user through the PC's human-machine interface. The basic system structure is shown in Figure 1. The PC and the embedded computer (slave) are connected according to the RS-485 protocol. [align=center]Figure 1[/align] 3. Multithreaded Serial Communication Implementation Process 3.1 Basic Process of Implementing Serial Communication Using API Functions [align=center]Figure 2[/align] As shown in Figure 2, firstly, the CreateFile() function opens the communication resource. Then, the configuration of the communication resource attributes is completed by the following API functions: SetupComm() sets the size of the input and output buffers of the serial communication port; the parameters of the serial port working state, such as baud rate, data bits, parity bits, etc., are modified and set through the device control block DCB; SetCommState() writes the contents of the DCB structure to the serial port settings; SetCommTimeouts() sets the overflow time for serial port read and write operations. After the setup is completed, serial communication can use ReadFile() to read the communication resource and WriteFile() to write it. When serial communication ends, the CloseHandle() function is called to close the serial port handle returned by the CreateFile() function. 3.2 PC Program Implementation Flow In multi-threaded serial I/O communication programming, reading and writing operations on the serial port are treated as two different tasks within the same process. Read threads and write threads are created to handle the read and write operations respectively. Due to the randomness and real-time nature of asynchronous serial communication events, the communication thread must be processed before the main thread. Therefore, the priority levels of each thread are set as follows: Read thread priority > Write thread priority > Main thread priority. On the PC side, an auxiliary thread is created to monitor the serial port communication status in real time. This monitoring thread sends corresponding messages to the main thread based on the communication status, which the main thread then analyzes and processes. The biggest advantage of multi-threaded serial communication is that the program has autonomous detection capabilities for received data. Once the auxiliary monitoring thread detects that data has been sent to the serial port, it automatically receives the data and sends a data reception message to the main thread. The application can then process the data transmitted from the serial port based on this message. Furthermore, the monitoring thread does not consume CPU time. The program implementation establishes an auxiliary thread: a serial port read thread, to complete the serial communication operation and monitor and manage the serial port communication input. The read thread waits indefinitely, reading data from the serial port and transmitting it to the main thread for processing. Besides opening, configuring, and closing the serial port communication resources, the main thread also creates and closes the read thread, coordinates multiple threads, handles intermediate data processing, and manages human-computer interaction with the front end. [align=center]Figure 3[/align] Figure 3 shows the serial communication flowchart for a PC. The left side represents the main thread, and the right side represents the sub-threads. Solid lines indicate view functions for the user interface, while dashed lines indicate communication functions. The user opens the serial port using controls on the interface designed in VC++ and sends data. Once the serial port parameters are set and the port is successfully opened, the sub-thread, i.e., the receiving thread, begins running. Simultaneously, the receiving thread's control function runs. When the control function receives a read port event, it calls the read port function to read the received data. The sub-thread runs continuously, constantly reading the received data and displaying it on the user's interface. When the user closes the serial port, the main thread terminates. At this time, the sub-thread also receives a close port event from the main thread, terminates its thread, and closes the serial port. In actual communication, data transmission errors may occur due to interference and other reasons. Therefore, real-time error handling is necessary to ensure normal data transmission. This will not be elaborated further here. 3.3 Embedded Slave Program Implementation Flow In the embedded slave part, communication is implemented using a single thread. After initializing the device, it waits for communication data. If data arrives, it calls the receive function to receive data from the PC, parses the data, returns the result, and sends the data back to the PC. If there is no data, it continues to wait. The program flowchart is shown in Figure 4. [align=center] Figure 4[/align] 4. Conclusion This paper analyzes the role of multi-threading technology in implementing serial communication between a PC and a microcontroller. By executing two communication threads concurrently and managing them synchronously, the application can receive data while sending data, exhibiting strong real-time responsiveness, high efficiency, and reliability, effectively avoiding problems such as data loss and program deadlock. The results in practical applications are satisfactory. References: [1] Mark Nelson. Serial Communication Development Guide. Translated by Xiaoxiang Studio. M. Beijing: China Water Resources and Hydropower Press, 2000. [2] Tan Siliang et al. Qiushi Technology. Visual C++ Serial Communication Engineering Development Examples Navigation. M. Beijing: Posts & Telecom Press, 2001. [3] Li Chaoqing. PC and Microcontroller Data Communication Technology. C. Beijing: Beijing University of Aeronautics and Astronautics Press, 2001.