Serial communication of microcomputer control system of bottle making machine using VC ++

Serial communication of microcomputer control system of bottle making machine using VC ++

The basic function of the microcomputer control system of the bottle making machine is to control the various mechanical actions of the bottle making machine to make it work according to the set program. In order to realize the control operation and data management between the host computer and the lower computer, it is necessary to connect the lower computer with data collection and automatic control function through the serial port, and then the operator passes the operation command to the lower computer by operating the upper computer management software to complete each This kind of control and management work, therefore, serial communication technology is one of the key technologies for the development of the microcomputer control system of the bottle making machine.

In the microcomputer control system of the bottle making machine under the VC environment, for the communication program of the PC host computer, there are three main serial communication programming methods: using the MSComm control provided by VC ++ to implement serial communication, based on a serial communication C ++ class, Serial communication based on API. For simple serial port operation, the first two are relatively easy to implement, convenient to use, and simple to control. However, it is not flexible enough for more complicated serial port operations. The serial port programming method based on API is powerful, and the control method is more free and flexible, and an efficient and powerful communication program can be written. For the lower computer, due to the limited storage capacity, if a large amount of data is to be stored, it will be lost, and storing the data in the database of the upper computer can make up for this problem. Because VC ++ provides a variety of database access technologies, such as ODBC, OLEDB, ADO, etc. Among them, ADO technology is based on the access interface of OLEDB, which inherits the advantages of OLEDB technology, and encapsulates the interface of OLEDB, and defines the ADO object Therefore, program development can be simplified, and applications based on ADO technology can access a variety of data through a consistent interface, which is also conducive to the transplantation and expansion of the program.

Based on the above analysis and consideration, the author focuses on how to use API functions and ADO database programming technology to achieve real-time data collection under VC ++, and save the data to the database in real time, so as to complete the host computer and the lower computer of the microcomputer control system of the bottle making machine The specific method of real-time operation and display of inter-data.

Using WindowsAPI function to realize serial communication

In the Windows environment, the serial port is part of the system resources. When data is sent from the CPU through the serial port, the byte data will be converted into serial bits; when receiving data, the serial bits will be converted into bytes. data. If the application program uses the serial port for communication, it must submit a resource request (open the serial port) to the operating system before use, and set the serial port address, baud rate, parity, data bit, and stop bit of the communication. After completion, you must also release resources (close the serial port).

Open the serial port

The serial communication program starts by calling the CreatFile () function, whose return value is a handle. Can be used in other port operations later. Once the port is open, you can automatically allocate a send / receive buffer, of course, you can also change the size of the send / receive buffer by calling the SetComm () function.

1.2 Configure the serial port

After the serial port is opened successfully. Then the serial port should be initialized to configure the serial port communication parameters, such as baud rate, data bits, stop bits, parity bits, etc. Modify these parameters can use the device control block DCB (Device Control Block). DCB is a complex structure with nearly 30 data members; however, for serial communication using 3-wire mode, most of the parameters in the DCB structure can be used, only need to set such as baud rate, data bits, stop Several key parameters such as bit and check digit can work normally. At the same time, Windows provides the GetCommState () function to obtain the current configuration of the serial interface, so you can use SetCommState () to reconfigure each parameter of the serial interface.

1.3 Serial port read / write operation

Through the program, you can use the ReadFile () function in Win32API to read data from the serial port, or use the WriteFile () function to write data to the serial port. If an error occurs during serial interface communication (such as terminal error, parity error, etc.), the I / O operation will be terminated. At this time, if the program wants to further perform the I / O operation of the serial interface, you must call the ClearCommError () function to restore the serial interface.

The ClearCommError () function has two functions: the first is to clear the error condition; the second is to determine the serial interface communication status. In addition, for the serial interface, the system provides a set of communication events and their corresponding processing functions. The Windows system can monitor this set of events that occur in the serial interface during the process, so the application can know when certain conditions occur without checking the port status. By using these events, the application does not need to continuously detect ports for receiving bytes, thereby saving CPU time.

Close the serial port

After the program is executed, when returning to the Windows environment, the serial port should usually be closed for other programs to use. You can use the function C10seHandle (Handle hObject) to stop all serial port input and output. The parameter hObject is the communication handle of the serial port when the CreateFile function returns.

2 Use of ADO database in VC ++

Initialize the OLE / COM library environment

After creating a standard MFC AppWizard (exe) application, you also need to initialize the OLE / COM library in the IniTInstance function of the application class (because the ADO library is a COMDLL library), the code is as follows:



Import ADO library file

Before using ADO, it must be in the project's stdafx. Directly import the symbol #import in the h file to quote the AADO library file, so that the compiler can compile correctly. The code is as follows:



2.3 Use smart pointers for database operations

The ADO library contains three smart pointers: _ConnecTIonPtr, _CommandPtr, _RecordsetPtr. Among them _ConnecTIonPtr is usually used to open and close a library connection. To make a library connection, you can first create an instance pointer and then open a library connection with Open. The following to connect to the database db. mdb as an example to illustrate:

_ConnecTIonPtr connectPtr;
connectPtr. CreateInstance ("ADODB.Connection"); // Create an instance pointer
connectPtr-> Open ("Provider = Microsoft.Jet.OLE.DB.4.0; DataSource =" db.mdb "," "," ",
adModeUnKnown); // Open a library connection with Open

_RecordsetPtr usually returns a recordset to provide a simple way to execute stored procedures and SQL statements that return recordsets. When using the _CommandPtr interface, you can use the global _ConnectionPtr interface, or you can directly use the connection string in the _CommandPtr interface. _RecordsetPtr can be used to open the data table in the library and operate on the records and fields in the table. You can also create a pointer, then use Open to open a record set, and then perform various operations on the records and fields in the record set, including adding, deleting, modifying, and so on.

3 Serial communication of control system of bottle making machine

The lower machine of the control system of the bottle making machine is composed of four segment control boards and one machine control board. The duty of the segment control board is to control all the actions of a section of the bottle making machine. It is generally achieved by driving 20 solenoid valves through 20 outputs; The responsibility of the control board is to provide reference signals and status information of the bottle making machine for the other four segment control boards according to the status of the field machine. The upper computer can set the opening and closing values ​​of the solenoid valves of different control boards and the values ​​needed to generate the reference signal. The setting values ​​corresponding to different products controlled by the bottle making machine are also different. In order to ensure the accuracy of the operation of the lower computer, it is often necessary to monitor the current state of the lower computer and control the start and stop of the single solenoid valve of the different stage control boards of the lower computer. The segment control of this system and the main control chip of the machine control board use PIC24FJ64GA008, and the communication between the upper computer and the lower computer follows the RS-232 protocol.

3.1 Creation of independent serial communication class

The method of creating an independent serial communication class is mainly in the application framework, click the NewClass item under the insert menu in the main menu, and select Generic Class in the Class type in the pop-up dialog box. This is a way to create classes without inheritance. Creating an independent serial communication class to encapsulate the serial port operation can well achieve serial communication under various custom communication protocols, and is in line with object-oriented programming design ideas. The system operates on the opening, initialization, reading and writing of serial ports, sending data from the upper computer to the lower computer, reading data from the lower computer to the upper computer, closing and other related operations, all encapsulated in the independent serial port class CcomPort written by windowsAPI function. When this serial port class is referenced in every form class that requires serial communication, it can be used conveniently. This system connects the PIC microcontroller to the computer through the port COM1, and the code to open the port COM1 is as follows:



Data can be transmitted between the upper computer and the lower computer at a communication rate of 2400 bps / sec without parity, including eight data bits and one stop bit. The DCB parameters can be set by the following parameters to initialize the serial port. The specific code is as follows:

DCB dcb;
dcb. BaudRate = CBR_2400; // Baud rate
deb. fParity = FALSE; // Prohibition of parity
dcb. ByteSize = 8;
dcb. Parity = NOPARITY; // No verification
dcb. StopBits = ONESTOPBIT; // A stop bit

3.2 Access to serial command data

After the host computer communicates with the PIC24FJ64GA MCU through a series of responses, the MCU will begin to send the data stored in the EEPROM to the host computer, so that 11 bytes per frame composed of the set protocol will be received by the host computer frame by frame. , And save the corresponding table in the database according to the field until the end of data communication. In the process of data transmission, a loop structure can be used to read data from the serial port in an inquiring manner. In order to avoid the crash caused by the failure of the line or the interruption of the communication process, an automatic exit mechanism can be added to the program, that is, the data is not received within 1 second, and the program automatically prompts that no data is received. After setting up the database db. After mdb, ADO can access the database to access the serial data. Specific steps are as follows:

(1) Reference the serial port class in the form class

When referring to the serial port class in the form class, if this form class is the first reference, define CcomPort Obj_Commpon; if it has been defined in other window classes, then define it as extern CCommPort obj_Commport. The serial port function can be called to initialize the serial port.

(2) Send control commands to the lower computer

According to the communication protocol to send control commands to the lower computer, you can tell the lower computer to transfer data with the upper computer. After the lower computer receives the control command data of the upper computer, it first checks to check whether the command of the upper computer is correct. If the received data is correct. Then return the correct solenoid valve opening and closing value; if it is not correct, the lower computer will return the control command to the upper computer to inform the upper computer that the data sent is not correct. After that, the upper computer will send the control command to the lower computer again. Prompt communication error, quit this communication, and then send the next command.

(3) Open the database and perform data access

First open the connection to the database. If m_pConnection has been opened during program initialization, you can call it directly in the form class, and then store the data. code show as below:



After saving the data, the upper computer sends control commands to the lower computer, and begins to receive the data from the next lower computer. The process of sending data from the upper computer to the lower computer is the same as the process and steps of sending data from the lower computer to the upper computer. When exiting the program, just close the serial port.

4 Conclusion

Serial communication has the advantages of simple implementation, flexible and convenient use, and reliable data transmission. It has been widely used in industrial monitoring, data acquisition, and real-time control systems. This article describes the specific methods of implementing and controlling the PIC microcontroller in the VC ++ environment using serial programming technology and ADO database programming technology in the VC ++ environment. This method will also deal with communication errors or communication jams. Practice has proved that this method can improve the accuracy of timing, thereby improving the quality and output of products.

ZGAR Vape Pods 6.0S

ZGAR Vape Pods 6.0S

ZGAR electronic cigarette uses high-tech R&D, food grade disposable pod device and high-quality raw material. All package designs are Original IP. Our designer team is from Hong Kong. We have very high requirements for product quality, flavors taste and packaging design. The E-liquid is imported, materials are food grade, and assembly plant is medical-grade dust-free workshops.

From production to packaging, the whole system of tracking, efficient and orderly process, achieving daily efficient output. WEIKA pays attention to the details of each process control. The first class dust-free production workshop has passed the GMP food and drug production standard certification, ensuring quality and safety. We choose the products with a traceability system, which can not only effectively track and trace all kinds of data, but also ensure good product quality.



We offer best price, high quality Pods, Pods Touch Screen, Empty Pod System, Pod Vape, Disposable Pod device, E-cigar, Vape Pods to all over the world.

Much Better Vaping Experience!



ZGAR Vape 6.0 Pods,ZGAR Vape Pods 6.0S,ZGAR Vape Pods 6.0S Pod System Vape,ZGAR Vape Pods 6.0S Disposable Pod Vape Systems

Zgar International (M) SDN BHD , https://www.sze-cigarette.com