Abstract: Through the application of multi-serial communication technology in diamond synthesis control system, the design and implementation method of VC multi-serial communication technology under 32-bit Windows operating system is discussed. The object-oriented method and multi-threading technology are used to design a perfect one. Serial communication class. The design method and implementation technology of serial communication program between PC and PLC are developed by VC.
1 Introduction
The conventional diamond synthesizer control system consists of a PLC and a displayable terminal. This conventional control system generally has the following disadvantages:
(1) All the work of the system is completed by PLC, and its control precision is poor, resulting in poor quality of the synthesized diamond;
(2) The plane size of the display terminal is too small, which makes it difficult for the operator to observe the state of the system, and on the other hand, it often causes misoperation;
(3) The diamond synthesis process is complicated and there are many parameters to be controlled, but the original control system cannot save the parameters. Therefore, when adjusting some parameters according to different product and process requirements, all parameters must be reset every time. very troublesome;
(4) The interface is not friendly;
(5) The quality of the operator's work cannot be automatically assessed by the control system.
In order to improve control accuracy and facilitate operation, it is extremely urgent to develop a new control system. In response to the above problems, the author has integrated IPC and PLC to develop a new control system. Through this system, a large number of serial communication can be performed between the host computer (IPC) and the PLC through RS-232 and RS-485.
2 VC serial communication analysis
Using VC to develop serial communication programs under 32-bit Windows systems usually has the following four methods:
(1) using a communication control called MSCOMM provided by Microsoft Corporation;
(2) directly use the Windows application programming interface (API);
(3) Design a serial communication class by yourself;
(4) Realize serial communication function by developing an ActiveX control.
In the above several methods, the Windows API function is actually used, and then the details of the serial communication are encapsulated, and several simple interface functions are provided to the user. The above several methods have their own advantages and disadvantages, but in actual situations, most programmers like to use the API function to design their own serial communication class.
The programming flow of serial communication using Windows API function is shown in Figure 1. The serial port is opened to determine the serial port number and the serial port opening mode; the initial serial port is used to configure the baud rate of the communication, the number of bits per byte, the parity bit, the stop bit, and the read/write timeout; the read/write serial port is used for the serial port. Send data and receive data from the serial port; close the serial port to close the serial port and release the serial port resources (the serial port in Windows system is the system resource).
Since the serial 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 by AfxBeginThread() function to manage serial communication, so that the main process It is possible to process data and complete the response of user instructions while performing serial port reading and writing, but the data sharing problem must be handled during design.
Serial port read and write can be selected from synchronous or asynchronous mode, as well as query, timed read and write, and event driven. Since the synchronization method is easy to cause thread blocking, the asynchronous method is generally adopted; and the query method takes up a lot of CPU time, so the timing read/write or event-driven mode is generally adopted, and the event-driven method has many related documents, so the focus is on the regular reading and writing method. . The timed reading and writing mode means that the upper computer sends a fixed format data to the lower computer, and returns the status information data to the upper computer after receiving the lower computer. Since the data transmission takes time, all the upper computer sends the data and then calls the _sleep() function to sleep. 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 well and handle other transactions.
3 VC serial communication design and implementation
In the Windows system, the author uses object-oriented method and multi-threading technology, and uses Visual C6.0 as a programming tool to develop a general-purpose serial communication class CSerialPort. The CSerialPort class encapsulates the basic data and methods of serial communication. A brief introduction to the CSerialPort class.
The main member variables and member functions in the CSerialPort class header file are as follows:
Class CSerialPort
{
Private:
HANDEL m_hPort;
DCB m_Dcb;
COMMTIMEOUTS m_TimeOuts;
DWORD m_Error;
Public:
CSerialPort(); ?? // constructor
Virtual~CSerialPort(); ?? //Destructor
/ / InitPort () function to initialize the serial port
BOOL InitPort(
Char* str=“com1â€,
UINT BaudRate=9600,
UINT Parity=0,
UINT ByteSize=8,
UINT StopBits=1,
UINT ReadMultiplier=0,
UINT ReadConstant=0,
UINT WriteMultiplier=10,
UINT WriteConstant=1000);
DCB GetDCB();? //Get DCB parameters
/ / SetDCB () function to achieve the setting of DCB parameters
BOOL SetDCB(
UINT BaudRate=9600,
UINT Parity=0,
UNIT ByteSize=8,
UINT StopBits=1);
// GetTimeOuts() function gets the timeout parameter
COMMTIMEOUTS GetTimeOuts();
/ / SetTimeOuts () function sets the timeout parameter
BOOL SetTimeOuts(
UINT ReadMultiplier=0,
UINT ReadConstant=0,
UINT WriteMultiplier=10,
UINT WriteConstant=1000);
/ / WritePort () function to achieve write serial port operation
Void WritePort(HANDLE port,CString);
CString ReadPort(HANDLE port); //Read serial port operation
BOOL ClosePort();? //Close the serial port
};
The following important functions of this class are explained:
(1) The data member of the class has been initialized in the constructor CSerialPort().
(2) Initialize the serial port function The InitPort() function is used to complete the initialization of the serial port, including opening the serial port, setting the DCB parameters, and setting the communication timeout period.
Open the serial port using the CreateFile () function, where the first parameter in the InitPort () function is the serial port to be opened, usually the parameter is assigned to the first parameter in the CreateFile () function; the DCB parameter should be called in the class SetDCB() function, and assign the 2nd to 5th parameters in the InitPort() function to the SetDCB() function; set the communication timeout time to call the SetTimeOuts() function in the class, and set the InitPort() function. The 6th through 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 baud rate of the transmission, whether to perform parity, the length of each byte, and the stop bit.
(4) The SetTimeOuts() function is used to set the timeout value of the 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 of the read operation, and the latter two parameters are used to set the total timeout value of the write operation.
(5) The WritePort() function is used to write data to the serial port. Since the system needs to communicate with multiple serial ports, the serial port number should first be passed as a parameter to the function; then the function encodes the data to be sent as passed by the parameter (that is, adding the checksum, which can reduce Bit error rate), then call the Windows API function WriteFile() and send the data to the serial port.
(6) ReadPort () function is used to complete reading data from the serial port. Since there are multiple serial ports, the serial port should be passed in as a parameter, then the API function ReadFile() is called, and the lower computer is sent to the serial port, and the data is read out. Go inside the cache, then process the data to transform it into a string (CString) type and return.
(7) GetDCB () function is mainly used to obtain the current configuration of the serial port, can be achieved by calling the API function GetCommState (), and then 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, and the upper computer is the microcomputer and the lower computer is the PLC. The main function of the upper computer is to monitor the system in real time. 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 with RS-485 serial port through RS-232. The communication baud rate between them is 9600bps, no parity. 8 bits per byte and 1 stop bit. The data format transmitted between the upper and lower computers can be defined by itself. A checksum algorithm was added because it could cause errors when transferring data. The system sends data to the lower computer through the upper computer, and the lower computer receives the status parameter of the current system and returns it to the upper computer. Since the parameters controlled in the system have hysteresis, the method of periodically transmitting data should be used to collect the on-site status information.
When programming the host computer, you can use VC6.0 to generate a dialog-type program framework, then add your own CSerialPort class to the project, and add a CSerialPort class member variable serial in the main interface class CCrystal?. When the monitoring system starts working, the AfxBeginThread?? function can be used to create a secondary thread to manage serial communication. When the WritePort? function in the CSerialPort class is called to send data to the serial port, the _sleep? function can be called to make the auxiliary thread sleep for a period of time. Let the PLC have enough time to return data; then call the ReadPort() function in the CSerialPort class and read the data from the serial port, and then call the _sleep() function to make the secondary thread sleep for a certain period of time. After this design, when serial communication is performed, the main thread can continue to perform monitoring functions and other transactions. The main code of the auxiliary thread function is as follows:
UINT SerialPro(void* param)
{
Ccrystal* mdlg=(Ccrystal*)param?
CString str;
Int flag=1;
/ / If the initialization of the serial port fails to return
If(!InitPort("com2"))
{AfxMessageBox ("Open Serial Port 2 Failed");
Return 0;
}
/ / Loop read and write serial port until the end
While(flag)
{
/ / Here to send the data to be sent to the variable str
......
/ / Write data to the serial port
Mdlg->serial. WritePort(hport,str);
/ / Let the auxiliary thread sleep 100ms
_sleep(100);
/ / Read data from the serial port and assign it to the variable str
Str=mdlg->serial. ReadPort(hport);
/ / Here to process the data obtained from the serial port
......
5 Conclusion
The CSerialPort class, a universal serial communication class designed with object-oriented methods and multi-threading technology, makes serial communication simple, convenient and easy to maintain by encapsulating Windows API functions. At present, the software system has been successfully applied to the diamond synthesis control system, and successfully solved the problem of RS-232 and RS-485 serial communication. After several months of operation, the serial communication software worked stably and completed the real-time monitoring and display tasks of the system. In addition, due to the object-oriented method and modular design, the software is very convenient to maintain and upgrade; at the same time, the system has good portability, and some code can be applied to other control systems according to different requirements.
Gel Battery,Gel Cell Battery,Gel Battery For Car,Gel Type Battery
Power X (Qingdao) Energy Technology Co., Ltd. , https://www.qdpowerxsolar.com