Communication method between PLC and PC based on multithreading technology in industrial control

Publisher:FreeSpirit123Latest update time:2021-10-09 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In modern industrial control systems, PLC is widely used for its high reliability, adaptability 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, and is an important means and development direction for realizing mechatronics. However, PLC cannot constitute a complete control system alone, cannot perform complex calculations and display various real-time control charts and curves, has no good user interface, and is not easy to monitor. Combining personal computers (PCs) with PLCs can complement each other's advantages, make full use of the powerful human-machine interface functions, rich application software, and low price advantages of personal computers, and form a control system with high performance and price ratio.


In the propulsion system, the PC is an industrial computer. It is the core of the entire control system and the host computer. It mainly uses a good graphical user interface to display the switch quantity and the position of the control handle received from the PLC, perform some more complex data calculations, and send control instructions to the PLC.


PLC is the lower computer of the system, responsible for high-speed data acquisition on site (the position of the control handle), realizing functions such as logic, timing, counting, PID adjustment, etc., transmitting PLC working status and related data to PC through serial communication port, receiving instructions from PC at the same time, issuing commands to buzzer, indicator light, lubricating oil pump, position of control handle, etc., realizing PC management of control system, improving the control ability and control range of PLC, and making the whole system a distributed control system.


The communication between computer and PLC is based on asynchronous two-way communication based on RS232 standard. FX series PLC has its own specific communication format. The whole communication system adopts the communication mode of the host computer. There is no need to write a special communication program inside PLC. Just store the data in the corresponding data register. Each data register has a corresponding physical communication address. When communicating, the computer directly operates the physical communication address. During the communication process, the transmission characters and command words are based on ASCII code, and the commonly used characters and their ASCII codes correspond to each other.


When the computer and PLC communicate, the two exchange information in frames. The control characters ENQ, ACK, and NAK can constitute the sending and receiving of single character frames. The remaining information frames are composed of five parts: character STX, command word, data, character ETX, and checksum when sending and receiving.


The checksum at the end of the information frame is used to determine whether the transmission is correct or not. The calculation method of the checksum is to add the ASCII codes (hexadecimal numbers) of all characters from the command code to ETX, and take the lowest 2 digits of the sum. This will be mentioned in the communication program design later. There are many methods for error detection, and the commonly used ones are parity check code and horizontal and vertical redundancy check LRC.

Communication method between PLC and PC based on multithreading technology in industrial control

At present, the CRC checksum is widely used, which can detect more than 99% of prominent errors of 18 bits or longer. In the short-distance point-to-point communication between computers and PLCs, the probability of error is small, so the checksum method can basically meet the requirements. In a Windows process, there are one or more threads, and each thread shares all process resources, including open files, signal identifiers, and dynamically allocated memory.


All threads in 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 can be executed and when to execute the thread. Threads have priority levels, and threads with lower priorities must wait until threads with higher priorities have completed their tasks before they can be executed. On multi-processor machines, the scheduler can put multiple threads on different processors to run, which can balance the tasks of the processors and improve the operating efficiency of the system.


The preemptive scheduler inside Windows allocates CPU time among active threads. Windows distinguishes two different types of threads: one is the user interface thread (UserInterfaceThread), which contains a message loop or message pump to process received messages; the other is the worker thread (WorkThread), which has no message loop. The thread used to execute background tasks and monitor serial port events is the worker thread.


This system uses 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. It uses 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 serial ports, the overlapping method is generally used. Because the synchronous I/O method returns only when the program is executed, it will block other threads and reduce the efficiency of program execution. The overlapping method can make the called function return immediately, and the I/O operation is performed in the background, so that the thread can handle other matters, and at the same time, it also realizes the thread 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 the read and write functions. The most important member of this structure is the hEvent event handle. It will be used as the thread's synchronization object. When the read and write functions are completed, hEvent is in a signaled state, indicating that read and write operations can be performed; when the read and write functions are not completed, hEvent is set to no signal.


Using Windows' multi-threading technology, the serial port is monitored in the auxiliary thread. When data arrives, it is event-driven to read the data and report to the main thread. In addition, the serial port read and write operations are run in the background by overlapping read and write operations.


4. Host computer communication program design

Take the reading of the 2-byte data starting with PLC output coil Y0 as an example to write a communication program. According to the PLC soft element address table, the first address of output coil Y0 is 00A0H, and the 2-byte data is Y0-Y7 and Y10-Y17. According to the returned data, the current state of PLC can be known to realize the monitoring of PLC. Before each read operation, a handshake communication is required. Send the request signal ENQ to PLC, and then read the response signal of PLC. If the response signal read is ACK, it means that 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 for 20ms

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);//Send the ASCII code of the starting component address

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

Change_to_ASCII (senddatasum_CHECK, datasum_check); //Convert "and" into ASCII code

Sleep (40); //Wait for PLC's response

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

if (*readdatasum_CHECK == *readdatasum_check) // "and" verification

{AfxMessageBox("Data read successfully!");

returnTRUE;}

else{AfxMessageBox("Validation error!");

returnFALSE;}}

}


5. Conclusion

The author's innovation: The author proposed a communication between PC and PLC based on multithreading. The communication program uses VC, which has better real-time performance than VB. It also uses MFC programming method to read and write serial port with overlapping structure, so that serial port reading and writing can be performed in the background. The communication program is reliable and portable.


As an important part of the system, the communication program has been proved to be simple and practical through on-site debugging, 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.

Reference address:Communication method between PLC and PC based on multithreading technology in industrial control

Previous article:About the various applications of intelligent RFID systems in industrial control
Next article:Application Analysis of Passive and Active Signals in Industrial Control

Recommended ReadingLatest update time:2024-11-16 11:31

PLC system power supply installation & wiring tips sharing
1. Pay attention to power supply installation There are two types of power supplies for PLC systems: external power supply and internal power supply. The external power supply is used to drive the PLC output device (load) and provide input signals. It is also called the user power supply. The externa
[Embedded]
PLC system power supply installation & wiring tips sharing
Common skills and precautions for PLC ladder diagram programming
The core concept of PLC ladder programming is the ladder diagram. A ladder diagram is a graphic consisting of horizontal and vertical lines, usually used to represent the logical relationship of a circuit. In PLC ladder programming, the ladder diagram is used to represent the logical relationship of a program.
[Embedded]
PLC requirements for frequency converter controllers How to use PLC to control frequency converters
In our actual projects, frequency converters are often used, such as fan frequency conversion speed regulation, water pump frequency conversion speed regulation, conveyor frequency conversion speed regulation, etc. When using frequency converters, PLC is usually used to control the frequency converter. PLC usually u
[Embedded]
PLC requirements for frequency converter controllers How to use PLC to control frequency converters
The world's first HD-PLC communication chip compliant with the IEEE1901-2020 standard has begun shipping samples
Socionext Inc. (hereinafter referred to as "Socionext"), a leading company in SoC design and application technology, announced that samples of the HD-PLC communication chip "SC1320A" based on the IEEE1901-2020 standard will begin shipping in June, and mass production is scheduled to begin in the third quarter of 2022.
[Network Communication]
The world's first HD-PLC communication chip compliant with the IEEE1901-2020 standard has begun shipping samples
Siemens PLC pointer type and indirect addressing tutorial
In the programming of Siemens S7-300 and S7-400, it is often necessary to call some system functions or function blocks. When inputting parameters, you often encounter pointer type parameters. Do you know the pointer type? The first time I came into contact with the term pointer was when I was learning C language. P
[Embedded]
Maxim's dual-pin bidirectional DC power line communication PLC is launched to reduce size
Maxim Integrated Products, Inc (NASDAQ: MXIM) announced the industry's smallest 2-pin bidirectional DC power line communication (PLC) device MAX20340, which reduces the size of the power supply and communication interface of the charging base by 80% in ultra-low power portable and wearable applications. By providing p
[Internet of Things]
Maxim's dual-pin bidirectional DC power line communication PLC is launched to reduce size
Siemens PLC battery failure signal appears after battery replacement
Siemens S7-400 CPU uses lithium battery (lithium/thionyl chloride). When lithium battery is left for a long time, it will form a passivation film, which will directly affect the function of the backup battery. In this case, when the system power is turned on, the CPU will prompt an error message. The backup battery
[Embedded]
Flow accumulation function program based on S7-1200PLC
When using s7-1200PLC to write a program, if the project needs to use the flow accumulation function, but this PLC does not have a built-in flow accumulation function block. At this time, our configuration engineers need to write a program with flow accumulation function by themselves, or encapsulate the program int
[Embedded]
Flow accumulation function program based on S7-1200PLC
Latest Embedded Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号