Share this

Communication methods between PLC and PC based on multi-threading technology

2026-04-06 06:58:02 · · #1

introduction

In modern industrial control systems, PLCs are widely used due to their high reliability, adaptability to industrial processes, and powerful networking capabilities. They can achieve sequential control, PID loop regulation, high-speed data acquisition and analysis, and computer-based management, making them an important means and development direction for mechatronics. However, PLCs cannot form a complete control system on their own; they cannot perform complex calculations or display various real-time control charts and curves, lack a user-friendly interface, and are inconvenient for monitoring. Combining a personal computer (PC) with a PLC allows for complementary advantages, fully utilizing the PC's powerful human-machine interface capabilities, rich application software, and low price to create a high-performance, cost-effective control system.

1. System Composition

In the propulsion system, an industrial control computer is selected as the PC. It is the core of the entire control system and serves as the host computer. It primarily utilizes a user-friendly graphical interface to display the switching signals received from the PLC and the positions of the control handles, performs some complex data calculations, and issues control commands to the PLC.

The PLC is the lower-level machine of the system, responsible for high-speed data acquisition on site (control handle position), and implementing functions such as logic, timing, counting, and PID regulation. It transmits the PLC's working status and related data to the PC through the serial communication port, and at the same time receives instructions from the PC to issue commands to the buzzer, indicator lights, oil pump, control handle position, etc., so as to realize the PC's management of the control system, improve the PLC's control capability and control range, and make the whole system a distributed control system.

2. Communication Protocol

Communication between the computer and the PLC is based on asynchronous bidirectional communication using the RS232 standard. The FX series PLC has its specific communication format. The entire communication system uses a host computer-initiated communication method; no special communication program needs to be written within the PLC. Data only needs to be stored in the corresponding data registers. Each data register has a corresponding physical communication address, and the computer directly operates on these physical communication addresses during communication. During communication, transmitted characters and command words are in ASCII code. Commonly used characters and their ASCII code correspondences are provided below.

When a computer communicates with a PLC, the two exchange information in units of frames. The control characters ENQ, ACK, and NAK can form a single-character frame for sending and receiving. The remaining information frames are composed of five parts: the character STX, the command word, the data, the character ETX, and the checksum.

The checksum, located at the end of the information frame, is used to determine the correctness of the transmission. The checksum is calculated by adding the ASCII (hexadecimal) values ​​of all characters between the command code and the ETX string, and taking the lowest two digits of the sum. This will be discussed further in the section on communication program design. Many error detection methods exist, commonly including parity check codes and horizontal/vertical redundancy check (LRC). Currently, CRC checksums are widely used, as they can detect over 99% of prominent errors of 18 bits or longer. However, in short-distance point-to-point communication between a computer and a PLC, the probability of errors is relatively low; therefore, the checksum method is generally sufficient.

3. Multithreading technology and its implementation in VC++ serial communication programs

A Windows process contains one or more threads, and each thread shares all process resources, including open files, signal flags, and dynamically allocated memory.

All threads within a process use the same 32-bit address space, and their execution is controlled by the system scheduler, which determines which thread can execute and when. Threads have priorities; lower-priority threads must wait until higher-priority threads have finished their tasks. On multiprocessor machines, the scheduler can run multiple threads on different processors, thus balancing processor workloads and improving system efficiency.

Windows' internal preemptive scheduler allocates CPU time among active threads. Windows distinguishes between two different types of threads: User Interface Threads, which contain a message loop or message pump to process received messages; and Work Threads, which do not have a message loop and are used to perform background tasks or monitor serial port events.

This system uses the MFC programming method. MFC treats the serial port as a file device. It uses CreateFile() to open the serial port and obtain a serial port handle, and SetCommState() to configure the port, including buffer settings, timeout settings, and data format. Then, it calls the functions ReadFile() and WriteFile() to read and write data, and uses WaitForSingleObject() to monitor communication events. When using ReadFile() and WriteFile() to read and write to the serial port, overlapped I/O is generally used. This is because synchronous I/O only returns after the program has finished executing, which can block other threads and reduce program efficiency. Overlapped I/O allows the called functions to return immediately, and the I/O operation is performed in the background. This allows threads to handle other tasks, and it also allows threads to perform read and write operations on the same serial port handle.

When using overlapped I/O, the thread needs to create an OVERLAPPED structure for use by read and write functions. The most important member of this structure is the hEvent event handle. It will be used as a synchronization object for the thread. When the read or write function completes, hEvent is in a signaled state, indicating that read and write operations can be performed; when the read or write function is not completed, hEvent is set to non-signaled.

By utilizing Windows' multithreading technology, the serial port is monitored in an auxiliary thread. When data arrives, it is read in and reported to the main thread based on event-driven mechanisms. Furthermore, by relying on overlapped read and write operations, the serial port read and write operations can run in the background.

4. Host Computer Communication Programming

Taking reading the first two bytes of data from the PLC output coil Y0 as an example, we will write a communication program. Looking up the PLC's soft device address table, we know that the starting address of output coil Y0 is 00A0H, and the two bytes of data are Y0-Y7 and Y10-Y17. Based on the returned data, we can determine the current state of the PLC and thus monitor it. Before each read operation, a handshake is required. Send a request signal ENQ to the PLC, and then read the PLC's response signal. If the received response signal is ACK, it indicates that the PLC is ready and waiting to receive communication data.

BOOLCPlcComDlg::ReadFromPLC(char*Read_char, char*Read_address, intRead_bytes)

{CSerialSerial; // Class used for serial communication

if( Serial.Open (1)) // Initialize serial communication port COM1

{ Serial.SendData (&ENQ_request, 1); // Send contact signal

Sleep(20); // Wait 20ms seconds

Serial.ReadData (&read_BUFFER, 1); // Read the PLC response signal

if(read_BUFFER==ACK){

...

Serial.SendData (&STX_start, 1); // Sends the "start" flag code to the PLC

Serial.SendData (&CMD0_read, 1); // Send the "read" command code

datasum_check += CMD0_read;

for(i=0; i<4; i++){

Serial.SendData (&Read_address[i>, 1); // Send the ASCII code of the starting element address

...

Serial.SendData (&ETX_end, 1); // End-of-send flag code

Change_to_ASCII(senddatasum_CHECK, datasum_check); // Converts "and" to ASCII code

Sleep(40); // Wait for the PLC to respond

...

Serial.ReadData (&Read_char[i>, 1); // Reads Read_bytes bytes

if (*readdatasum_CHECK == *readdatasum_check) // AND check

{AfxMessageBox(“Data read successful!”);

returnTRUE;}

else { AfxMessageBox(“Validation error!”); }

return FALSE; } }

}

5. Conclusion

The innovations of this paper are as follows: The author proposes a communication method between a PC and a PLC based on multi-threading. This communication program, using VC++, offers better real-time performance than VB++. Furthermore, it employs MFC programming with an overlapped structure for reading and writing serial ports, allowing serial port read/write operations to be performed in the background. This communication program is reliable and highly portable.

This communication program, as an important component of the system, has been proven through on-site testing to be both simple and practical, demonstrating significant practical value. Furthermore, the system features an intuitive human-machine interface and user-friendly operation, indicating broad application prospects.

Read next

CATDOLL 148CM Christina Silicone Doll

Height: 148 Silicone Weight: 33kg Shoulder Width: 34cm Bust/Waist/Hip: 70/58/82cm Oral Depth: N/A Vaginal Depth: 3-15cm...

Articles 2026-02-22