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.
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.
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
- Popular Resources
- Popular amplifiers
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- When you have self-doubt, you can talk to your friends more often.
- High Speed Digital Design Handbook (Black Book)
- [Raspberry Pi 4B Review] Install the game system Lakka on Raspberry Pi 4 to play retro games
- [Atria Development Board AT32F421 Review] - TEST02 Initial FFT Test Results
- Please recommend cheap rental houses near Zhangjiang, Shanghai
- Why can't LM7805 regulate voltage to 5V?
- China's fast charging standard released! Download and study
- Anqu David starts updating: motor drive development learning and communication, starting from the system and basics
- [Top Micro Intelligent Display Module Review] 9. Touch screen input value (PIP keyboard application)
- A brief discussion on the "layered thinking" in single-chip computer programming