A Preliminary Study on the Design of a Multi-threaded Pyrotechnic Compressor Control Program
2026-04-06 03:30:32··#1
Introduction Industrial control software used in industrial control systems often needs to perform multiple tasks simultaneously, such as data acquisition, real-time calculation and output of control information, real-time display of various field data and statuses, real-time display and printing of various reports, real-time reading and response to various keyboard commands, and data communication. Although these tasks have slightly different real-time requirements, they are essentially performed in parallel, thus requiring the developed control system to achieve real-time multitasking. Pyrotechnics are widely used in launch vehicles and missile weapons. Pyrotechnics presses are mainly specialized automated pressing equipment used in the pressing process of pyrotechnics production. This paper, combined with the development of control software for a precision pneumatic press, analyzes how to utilize the multithreading technology provided by Windows in industrial control software. Multithreading Overview As a multithreaded operating system, Windows implements preemptive multitasking. In the Windows environment, each running program creates a process, each process consists of at least one thread, and each process can execute multiple threads simultaneously. Threads and processes are two related concepts; a process is the unit of resource allocation, while a thread is the unit of system scheduling. Multiple threads within a process need to synchronize and communicate to achieve complex logical functions, such as sharing memory-mapped files, accessing shared data, and using the same message queue. After the system creates the process, it actually starts the main thread, which provides the program's starting point to the Windows system and then executes according to the message trigger queue order. The main thread and the process exist and disappear simultaneously. In multi-threaded execution, the system allocates time units for executing multiple threads based on thread priority and synchronization requirements. This achieves time-sharing CPU usage for multiple tasks, allowing multiple tasks to be completed in parallel within a short period, meeting real-time requirements, greatly improving system resource utilization, and facilitating users to complete complex tasks as required. The system's processor scheduling algorithm is based on the following two points: ① a preemptive scheduling algorithm based on thread priority; ② a time-slice round-robin algorithm for threads of the same priority. The CPU scheduling process method is shown in Figure 1. [align=center]Figure 1. Operating system's CPU allocation method for each thread in a round-robin fashion[/align] Multithreaded OperationsThread Startup To create a user interface thread, you must first generate a derived class from the CwinThread class. You must also declare and implement this CwinThread derived class using DECLARE_DYNCREATE and IMPLEMENT_DYNCREATE. Then, as needed, some member functions of this derived class, such as ExitInstance(), InitInstance(), OnIdle(), PreTranslateMessage(), etc., are overridden. Finally, the user interface thread is started by calling a version of the AfxBeginThread() function: CWinThread* AfxBeginThread( CRuntimeClass* pThreadClass, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL ); The first parameter is a pointer variable to the defined user interface thread class, the second parameter is the thread priority, the third parameter is the stack size corresponding to the thread, and the fourth parameter is the additional flags when the thread is created. The default is the normal state, and if it is CREATE_SUSPENDED, the thread will be in a suspended state after starting. For worker threads, starting a thread first requires writing a function, such as `exec()`, that you want to run in parallel with the rest of the application. Then, define a pointer variable `pThread` pointing to a `CwinThread` object, and call the function `AfxBeginThread(exec, param, priority)`. The return value is assigned to the `pThread` variable, and the thread is started to execute the `exec()` function. Here, `exec` is the name of the function the thread will run, which is also the name of the control function mentioned above. `param` is any 32-bit value to be passed to the thread function `exec`, and `priority` defines the thread's priority level; it is a predefined constant. Thread Termination There are three ways to terminate a thread: The thread can call `AfxEndThread()` internally to terminate its own execution; it can call `BOOL TerminateThread(HANDLE hThread, DWORD dwExitCode)` externally to forcibly terminate a thread's execution, and then call `CloseHandle()` to release the stack occupied by the thread; the third method is to change a global variable to cause the thread's execution function to return, thus terminating the thread. Thread Synchronization When developing user programs using multithreading, it is often necessary to coordinate two or more actions. This process is called synchronization. The reasons for using synchronization are: (1) when two or more threads need to access a shared resource, and this resource can only be used by one thread at a time; (2) when one thread is waiting for an event caused by another thread. The Win32 operating system provides several synchronization objects that allow threads to synchronize their behavior. These synchronization objects include critical sections, mutexes, semaphores, and events. (1) Critical Section A critical section is a small piece of code that requires exclusive access to certain shared data before execution. (2) Mutex Mutexes are very similar to critical sections, except that they can be used to synchronize data access between multiple processes. (3) Semaphores Semaphore kernel objects are used for system resource counting. They provide threads with the ability to query the number of available resources. If a thread has one or more resources available, the available resource count is decremented by 1. Only after the resource count is incremented by 1 will the system allow other processes to access this resource. (4) Events Event objects are the most basic form of synchronization objects, which are very different from mutexes and semaphores. Mutexes and semaphores are usually used to control access to data, but events are used to signal that an operation has been completed. Implementation of multi-tasks in the control system of the press press machineThread allocation Due to the complexity of the software, four threads were used in the software design. In addition to the first main interface thread, there are execution threads, display threads, and A/D conversion monitoring threads. 1. Main functions of the main thread: (1) Create two timers, one is a 50ms Windows timer, which sends a message to the display thread to drive the display thread, and the other is a 20ms multimedia timer, which sends a message to the execution thread to drive the execution thread. (2) The main thread generates a user thread to monitor A/D conversion events. (3) Receive information input by the user from the keyboard and execute the corresponding process. The flowchart of the main thread is shown in Figure 2. [align=center]Figure 2 Main Thread Flowchart[/align] 2. The execution thread is responsible for executing functions such as automatic drug loading and stopping automatic drug loading. Most of the work in this program is completed by the execution thread. The execution thread flowchart is shown in Figure 3. [align=center]Figure 3 Execution Thread Flowchart[/align] 3. The real-time parameter display thread is responsible for displaying the pressure value, holding time, and error information in real time. 4. The A/D conversion monitoring thread is used to monitor the A/D conversion. Implementation of the Execution Thread As shown in Figure 3, the execution thread completes most of the system's work in this system software. The definition of the execution thread function is as follows: LRESUL CJmqyView::OnTimerProc(WPARAM wParam, LPARAM lParam) { if ( !bfPCL812PG ) // Data acquisition card does not exist return ( 1 ); InputFromPCL812PG(); // Input operation of data acquisition card PCL-812PG if (inBit.start && !inBit0.start ) // Start button pressed OnStart(); else if ( inBit.stop && !inBit0.stop ) // Stop button pressed OnStop(); execTask(); // Execute task OutputToPCL812PG(); // Output operation of data acquisition card PCL-812PG} A certain medicine press control system has a rich human-machine interface to accept the user's keyboard and mouse operations. The system completes the tasks of the entire control system through these operations. When the control system starts, it first establishes a main thread to accept user keyboard and mouse operations, complete the input of user process parameters, and starts the A/D conversion monitoring thread. Simultaneously, the main thread also creates two timers to drive the execution thread and the display thread. The main thread responds to the timer's arrival message and sends messages to drive the execution thread or display thread respectively. The display thread presents the real-time pressure and holding time of the press on the user interface, facilitating the user's next operation. Conclusion In traditional DOS environment, control system software uses interrupt-based scheduling and loop-based methods to achieve parallel multitasking, resulting in low CPU utilization. However, in the Windows environment, multithreading technology can be used to easily achieve the above-mentioned parallel multitasking while making full use of CPU time. Practice has proven that the control system software for a certain type of press developed using multithreading technology better ensures the system's real-time performance and stability in a multitasking environment. As industrial control systems become increasingly complex and the number of tasks to be executed simultaneously continues to increase, the advantages of multithreaded programming technology will become increasingly apparent. References: 1. Ge Jingguo, Chen Ligong, Ni Chunzhen, Development of real-time monitoring software for weld deviation [J], Computer Engineering, 2004 (4) 2. He Qichang, Development of industrial control software under Windows [J]. Manufacturing Automation, 2002 (24) 3. Zheng Lianqing, Liu Rong, Wang Jue, Liu Qikun, Improvement of the pressing process of pyrotechnic products [J], Pyrotechnics, 1999 (1) 4. Liao Chunlan, Research on real-time measurement and control system based on multithreading [J]. Electromechanical Engineering Technology, 2006 (35) 5. Qiu Zhongpan, Ke Yu, Xie Yanhua et al., Visual C++6 from beginner to expert [M], Electronic Industry Press, 2005