Communication mode between PLC and PC based on multi-thread technology

March 26, 2023

0 Preface

In modern industrial control systems , PLC is widely used for its high reliability, adapt to industrial process sites, and powerful networking functions. It can realize sequential control, PID loop adjustment, high-speed data acquisition and analysis, and computer upper management. It is an important means and development direction for achieving mechatronics. However, the PLC cannot form a complete control system alone, and cannot perform complicated calculations and display various real-time control charts and curves. It has no good user interface and is not easy to monitor. The combination of a personal computer (PC) and a PLC can complement each other and make full use of the powerful human-machine interface function of the personal computer, rich application software and low price advantage to form a high-performance and cost-effective control system.

1. System composition

In the propulsion system, the PC selects the industrial computer. It is the core of the entire control system and is the upper computer. It mainly uses a good graphical user interface to display the amount of switch received from the PLC and the position of the control handle, perform some more complicated data operations, and issue control commands to the PLC.

PLC is the lower position machine of the system, responsible for on-site high-speed data acquisition (position of control handle), realize logic, timing, counting, PID adjustment and other functions, and transfer PLC working status and related data to PC through serial communication port. The PC accepts commands, issues commands to the buzzer, indicator light, oil pump, control handle, etc., realizes the management of the PC by the PC, improves the control capability and control range of the PLC, and makes the whole system become a distributed control system. .

2. Communication protocol

The communication between the computer and the PLC is based on the asynchronous two-way communication based on the RS232 standard. The FX series PLC has its specific communication format, and the whole communication system adopts the active communication mode of the upper computer, and the PLC does not need to write a special internal. The communication program only needs to store the data in the corresponding data register, and each data register has a corresponding physical communication address, and the computer directly operates the physical communication address during communication. In the communication process, the transmission characters and command words are based on the ASCII code, and the commonly used characters and their ASCII codes correspond.

When the computer communicates with the PLC, the computer and the PLC exchange information in units of frames. The control characters ENQ, ACK, and NAK can constitute single-character frame transmission and reception, and the rest of the information frames are sent and received. It consists of the character STX, the command word, the data, the character ETX, and the checksum 5.

The checksum is used to judge the correctness of the transmission at the end of the information frame, and the check code is calculated by adding the ASCII code (hexadecimal number) of all characters between the command code and ETX. The minimum 2 digits of the sum will be mentioned later in the communication program design. There are many methods for error checking. Commonly used are parity check codes and horizontal vertical redundancy check LRC. Currently, CRC check codes are widely used, which can detect 99% or more of 18 or more outstanding errors. When the computer and PLC point-to-point short-distance communication, the probability of error is small, so the checksum method is adopted, which can basically meet the requirements.

3. Multi-threading technology and implementation in VC++ serial communication program

Within a process of Windows, it contains one or more threads, each thread sharing all process resources, including open files, signal identification, and dynamically allocated memory.

All threads within a process use the same 32-bit address space, and the execution of these threads is controlled by the system scheduler, which determines which thread is executable and when the thread is executed. Threads have priority levels, and threads with lower priority must wait until the higher priority thread executes the task. On a multiprocessor machine, the scheduler can run multiple threads on different processors, which balances the processor's tasks and improves system efficiency.

Windows' preemptive scheduler allocates CPU time between active threads. Windows distinguishes between two different types of threads. One is the user interface thread (UserInterfaceThread), which contains a message loop or message pump for processing received messages. The other is the worker thread (WorkThread) which has no message loop. The thread that executes the background task and monitors the serial port event is the worker thread.

1

This system uses the MFC programming method. MFC handles the serial port as a file device. It uses CreateFile() to open the serial port and obtain a serial port handle. SetCommState() is used for port configuration, including buffer setting, timeout setting and data format. Wait. Then call the functions ReadFile() and WriteFile() to read and write data, and WaitForSingleObject() to monitor communication events. When reading and writing serial ports with ReadFile() and WriteFile(), the overlap mode is generally used. Because the synchronous I / O mode is returned when the program is finished, this will block other threads and reduce the efficiency of program execution. The overlapping mode enables the called function to return immediately, and the I/O operation is performed in the background, so that the thread can process other transactions, and at the same time, the thread can implement read and write operations on the same serial port handle.

When using the overlapping I/O mode, the thread needs to create an OVERLAPPED structure for use by the read and write functions. The most important member of the structure is the hEvent event handler. It will be used as a synchronization object of the thread. When the read/write function is completed, hEvent is in a signal state, indicating that read and write operations can be performed; when the read/write function is not completed, hEvent is set to no signal.

Using Windows multi-threading technology, monitoring the serial port in the auxiliary thread, relying on event-driven when data arrives, reading in the data and reporting to the main thread; and relying on overlapping read and write operations, the serial port read and write operations run in the background.

4. Host computer communication program design

For example, a two-byte data headed by the PLC output coil Y0 is read, and a communication program is written. Check the PLC device address table, the first address of the output coil Y0 is 00A0H, and the data of 2 bytes is Y0-Y7 and Y10-Y17. According to the returned data, the status of the PLC at this time can be known to achieve PLC monitoring. Before each read operation, a handshake is required. Send a request signal ENQ to the PLC, and then read the response signal of the PLC. If the response signal read is ACK, the PLC is ready to receive communication data.

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

{CSerialSerial;//Class for serial communication

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

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

Sleep(20);//wait for 20ms seconds

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

If(read_BUFFER==ACK){

......

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

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

Datasum_check+=CMD0_read;

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

Serial.SendData(&Read_address[i>,1);//ASCII code to send the start component address

......

Serial.SendData(&ETX_end,1);//Send end flag code

Change_to_ASCI i (senddatasum_CHECK, datasum_check); / / convert "and" into ASCII code

Sleep(40);//waiting for PLC reaction

......

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

If(*readdatasum_CHECK==*readdatasum_check)//"and" validation

{AfxMessageBox("Data read success!");

returnTRUE;}

Else{AfxMessageBox("checksum error!");

returnFALSE;}}

}

5. Conclusion

The author of this paper innovates: The author proposes a multi-thread based PC and PLC communication, the communication program uses VC to have better real-time performance than VB; and uses MFC programming method to read and write the serial port with overlapping structure, so that the serial port Reading and writing takes place in the background. The communication program is reliable and portable.

As an important part of the system, this communication program is proved to be simple and practical, and has good practical value. At the same time, the system has an intuitive human-machine interface and convenient operation mode, and has broad application prospects.

FM Transmitter

A personal FM transmitter is a low-power FM radio transmitter that broadcasts a signal from a portable audio device (such as an MP3 player) to a standard FM radio. Most of these transmitters plug into the device's headphone jack and then broadcast the signal over an FM broadcast band frequency, so that it can be picked up by any nearby radio. This allows portable audio devices to make use of the louder or better sound quality of a home audio system or car stereo without requiring a wired connection. They are often used in cars but may also be in fixed locations such as broadcasting from a computer sound card throughout a building.

Fm Transmitter,Fm Transmitter System,Personal Radio Fm Transmitte,Fm Stereo Transmitter

Anshan Yuexing Technology Electronics Co., LTD , https://www.yxhtfmtv.com