Design and implementation of multi-serial communication technology in diamond synthesis control system

Abstract: Through the application of multi-serial communication technology in the diamond synthesis control system, the design and implementation method of VC multi-serial communication technology under 32-bit Windows operating system are discussed, and a more perfect design using object-oriented method and multi-thread technology Serial communication class. The design method and implementation technology of serial communication between the host computer and PLC developed by VC are described.

1 Introduction

The traditional diamond synthesizer control system is composed of a PLC and a displayable terminal. This traditional control system generally has the following disadvantages:

(1) All the work of the system is completed by PLC, and its control accuracy is poor, resulting in poor quality of synthetic diamond;

(2) The plane size of the display terminal is too small. This aspect makes it inconvenient for the operator to observe the state of the system. On the other hand, it often causes misoperation;

(3) The diamond synthesis process is complex, and there are many parameters to be controlled, but the original control system cannot save the parameters. In this way, when adjusting some parameters according to different products and process requirements, all parameters must be reset every time. very troublesome;

(4) The interface is not friendly;

(5) The quality of the operators' work cannot be automatically assessed through the control system.

In order to improve control accuracy and facilitate operation, it is imminent to develop a new control system. In response to the above problems, the author organically combined IPC and PLC to develop a new control system. Through this system, a large number of serial communication can be carried out between the upper computer (IPC) and the PLC through RS-232 and RS-485.

2 VC serial communication analysis

There are usually four methods to develop serial communication programs using VC under 32-bit Windows system:

(1) Use the communication control named MSCOMM provided by Microsoft Corporation;

(2) Use the Windows application program interface (API) directly;

(3) Design a serial communication class by yourself;

(4) Realize the serial communication function by developing an ActiveX control.

In the above methods, in fact, use the Windows API function, and then encapsulate the details of serial communication, and provide users with a few simple interface functions. The above methods have their own advantages and disadvantages, but in actual situations, most programmers like to use the API function to design serial communication classes.

The programming process of serial communication using Windows API function is shown in Figure 1. Among them, opening the serial port is to determine the serial port number and the opening method of the serial port; the initial serial port is used to configure the baud rate of communication, the number of bits per byte, the check bit, the stop bit, and the read and write timeout; Send data and receive data from the serial port; close the serial port is used to close the serial port and release serial port resources (serial port is a system resource under Windows system).

Since the serial port communication in most control systems is time-consuming, and the monitoring system also needs to perform data processing and display, etc., multi-threading technology is generally used, and the auxiliary thread is created with the AxBegginThred () function to manage serial port communication. You can process the data and complete the response to user commands while reading and writing through the serial port, but you must handle the sharing of data when designing.

Serial port read and write can choose synchronous, asynchronous mode, you can also choose query, timing read and write and event-driven mode. Because the synchronous mode is easy to cause thread blocking, the asynchronous mode is generally used; and the query mode takes up a lot of CPU time, so the general read-write or event-driven mode is generally used, and there are many related documents on the event-driven mode, so the focus is on the regular read-write mode . The regular read and write method is that the host computer sends fixed format data to the host computer, and returns the status information data to the host computer after the host computer receives it. Because the data transmission takes time, all the host computers send _sleep () function to sleep after sending the data. The sleep time can be set differently according to the needs. In this way, CPU time can be saved, so that the system can perform monitoring work and handle other transactions well.

3 Design and implementation of VC serial communication

Under the Windows system, the author adopts an object-oriented method and multi-threading technology, and uses Visual C6.0 as a programming tool to develop a general serial communication class CSERIALPORT, which encapsulates the basic data and methods of serial communication A brief introduction to the CsterialPort class.

The main member variables and member functions in the CSerialPort header file are as follows:

Cass CSerialPort

{

private:

HANDEL m_hPort;

DCB m_DCb;

COMMENT TIMES m_Time Outs;

DWORD m_Error;

Public:

CSerialPort (); ?? // Constructor

virtual ~ CserialPort (); ?? // destructor

// InitPort () function to initialize the serial port

BOOL InportPort (

ach * str = "com1",

UINT BadRate = 9600,

UINT Parity = 0,

UINT BySite = 8,

UINT StopBits = 1,

UINT LeadMultiplier = 0,

UINT LeadConstant = 0,

UINT Writable Mulitier = 10,

UINT WrittenConstant = 1000);

DCB GetDCB () ;? // Get DCB parameters

// SetDCB () function realizes setting DCB parameters

BOOL SetDCB (

UINT BadRate = 9600,

UINT Parity = 0,

UNIT BYTESIZE = 8,

UINT StopBits = 1);

// The GetTimeOuts () function gets timeout parameters

COMMIMEOUTS GetTimeOuts ();

// The SetTimeOuts () function sets the timeout parameter

BOOL SetTimeOuts (

UINT LeadMultiplier = 0,

UINT LeadConstant = 0,

UINT Writable Mulitier = 10,

UINT WrittenConstant = 1000);

// The WritePort () function implements the write serial port operation

VOID WRITERPORT (HANDLE port, CSTRING);

CSTRING READPORT (HANDLE port); // Read serial port operation

BOOL Closport () ;? // Close the serial port

};

The important functions of this class are explained below:

(1) The data members of this class have been initialized in the constructor CSerialPort ().

(2) Initialize serial port function InitPort () function is used to complete the serial port initialization, including opening the serial port, setting DCB parameters, setting the communication timeout time, etc.

Open the serial port and use the Creefile () function, in which the first parameter in the InitPort () function is the serial port to be opened, usually this parameter is assigned to the first parameter in the CreeteFile () function; set the DCB parameters should be called in this class SetDCB () function, and assign the 2nd to 5th parameters in the InitPort () function to the SetDCB () function; Set the communication timeout time should call the SetTimeOuts () function in this class, and set the InitPort () function The 6th to 9th parameters are assigned to the SetTimeOuts () function. In addition, the serial port is a system resource, and its security attributes should be set according to different requirements.

(3) The SetDCB () function is used to set the DCB parameters, including the transmission baud rate, whether to perform parity check, the length of each byte, and stop bits.

(4) SetTimeOuts () function is used to set the timeout value of access, and the total timeout interval can be calculated according to the set value. The first two parameters are used to set the total timeout value for read operations, and the latter two parameters are used to set the total timeout value for write operations.

(5) The WritePort () function is used to write data to the serial port. Because the system needs to communicate with multiple serial ports, the serial port number should be passed as a parameter to the function first; then the function encodes the data to be sent by the parameter (that is, adding a check, which can reduce Error rate), and then call the Windows API function WriteFile () and send the data to the serial port.

(6) The ReadPort () function is used to complete the reading of data from the serial port. Since there are multiple serial ports, the serial port should be passed as a parameter, and then the API function RearFile () is called, and the lower computer is sent to the serial port, the data is read out Go to the cache, and then process the data to transform it into a string (CSTRING) type and return.

(7) The GetDCB () function is mainly used to obtain the current configuration of the serial port, which can be achieved by calling the API function GetCommSate (), and then perform the corresponding processing.

(8) The GetTimeOuts () function is used to obtain the access timeout value.

(9) The ClosePort () function can be used to close the serial port. Because the serial port is a system resource in the Windows system, it should be released when not in use so that other processes can use the resource.

4 Diamond synthesis control based on serial communication

The diamond synthesis control system adopts the master-slave control mode, the upper computer is a microcomputer, and the lower computer is a PLC. The main function of the upper computer is to monitor the system in real time, and the main function of the lower computer is to control the system in real time. The upper computer adopts Windows 98 operating system, and its monitoring program can be developed by VC. The upper and lower computers communicate through RS-232 and RS-485 serial ports. The communication baud rate adopted between them is 9600bps, no parity check. There are 8 bits per byte and 1 stop bit. The data format transmitted between the upper and lower computer can be defined by yourself. Because data transmission may cause errors, a verification algorithm is added. The system sends data to the lower computer through the upper computer, and the lower computer returns the status parameters of the current system to the upper computer after receiving it. Because the parameters controlled in this system have hysteresis, the method of sending data regularly should be used to collect on-site status information.

When programming on the host computer, you can use VC6.0 to generate a dialog-type program framework, and then add your own CSRialPort class to the project, and add a member variable of the CSRialPort class in the main interface class? Crystal ?. When the monitoring system starts to work, you can use the AxBeginThreat ?? function to create auxiliary threads to manage serial port communication. When calling the WritePort? Function in the SerialPort class to send data to the serial port, you can call the _sleepe? Function to sleep the auxiliary thread for a period of time, so that Make the PLC have enough time to return data; then call the RedPort () function in the SerialPort class and read the data from the serial port, and then call the _sleep () function to make the auxiliary thread sleep for a certain time. After this design, the main thread can continue to complete the monitoring function and handle other transactions when serial communication is performed. The main code of the auxiliary thread function is as follows:

UINT SerialPro (void * param)

{

Cstar * * mdlg = (CCry * * param?

CSTRING STR;

int flag = 1;

// Return if failed to initialize the serial port

if (! IntPort ("com2"))

{AfxMessageBox ("Failed to open serial port 2");

return 0;

}

// Cycle reading and writing serial port until the end

when (flag)

{

// Transfer the data to be sent to the variable str here

...

// Write data to the serial port

mdlg-> serial. WrotePort (hpor, str);

// Let the auxiliary thread sleep for 100ms

_Sleep (100);

// Read data from the serial port and assign it to the variable str

str = mdlg-> serial. LeadPort (hport);

// The data obtained from the serial port is processed here

...

5 Conclusion

Using the object-oriented method and multithreading technology to design the general serial communication class CerialPort class, through the packaging of Windows API function, serial port communication becomes simple and convenient, easy to maintain. At present, the software system has been successfully applied to the diamond synthesis control system, and successfully solved the problems of RS232 and RS485 communication. After several months of operation, it has been shown that the serial communication software is stable and has successfully completed the real-time monitoring and display tasks of the system. In addition, because of adopting the object-oriented method and modular design, the maintenance and upgrade of the software is very convenient; at the same time, the system has good portability, and some codes can be applied to other control systems with slight changes according to different needs.

Automatic Labeling Machine

Automatic Labeling Machine,Bottle Labeling Machine,Automatic Bottle Labeling Machine,Automatic Label Applicator Machine

Zhijiang BSL battery technology service company , https://www.bslbatteryservice.com