0 Introduction
In modern measurement and control systems, since PCs have powerful data processing capabilities and good user interaction interfaces, and single-chip microcomputers have strong on-site anti-interference capabilities and good cost-effectiveness, distributed intelligent control systems that use PCs as host computers to implement user control and single-chip microcomputers as slave computers to collect real-time data undoubtedly have good application prospects. In this article, the single-chip microcomputer realizes temperature detection with good real-time performance; the PC completes data display, storage and statistical analysis, draws real-time temperature curves, and makes judgments on the current status of the system. The two communicate with each other via serial ports.
l Hardware foundation of data acquisition system
The lower computer of this system uses Atreel's AT89S51 microcontroller as the control core, which is responsible for collecting the temperature value on site. The temperature sensor converts the temperature into a voltage signal, which is converted into an 8-bit digital quantity through the analog/digital converter ADC0809 and stored in the microcontroller through the P1 port of AT89S51. The upper PC communicates with the lower computer through the serial port, sends control commands to the lower computer, receives data uploaded by the lower computer, and performs human-computer interaction. The upper computer uses VB 6.0 to design the human-computer interaction interface, and uses its MSComm control to achieve simple and efficient serial communication with the lower computer. The advantages of the microcontroller in real-time data acquisition and the PC in
graphics processing, display and database management are fully utilized. The application of the microcontroller is no longer limited to automatic monitoring or control in the traditional sense, but has formed a development trend of distributed multi-point systems with the network as the core. Since the working levels of the serial ports of the upper and lower computers are inconsistent, the MAX232 chip is used to convert the TTL level of the microcontroller into the RS 232 level, forming a system structure diagram as shown in Figure 1.
2 Implementation Method of Serial Communication in VB Environment
The design style of VB visual interface is simple, easy to learn and efficient. It has become a tool adopted by many developers and is widely used in various fields. VB's MSComm communication control only requires users to write a small amount of program code to complete the development process of communication software.
2.1 Introduction to MSComm Control
The MSComm control is a serial communication ActiveX control provided by Microsoft. The MSComm control sends and receives data through the serial port, providing serial communication functions for applications. The main properties of the MS-Comm communication control are as follows:
(1) The CommPort property is used to set or return the communication connection port. During design, the CommPort property value can be set to any number from 1 to 16 (the default value is 1).
(2) The Settings property is used to set initialization parameters. It sets or returns four parameters, namely, the connection rate, parity check, data bits, and stop bits, in the form of a string. The format is: "BBBB, P, D, S". BBBB represents the connection rate; P represents the parity check mode; D represents the number of data bits; and S represents the number of stop bits. This system sets it to "9600, N, 8, 1".
(3) The Portopen property is used to set or return the status of the communication serial port (on or off). The Portopen property is of Boolean type. When the value is True, it means opening the serial port; when the value is False, it means closing the serial port and clearing the receive and send buffers.
(4)The Input property is used to read data from the receive buffer.
(5) The Output property is used to write data to the send buffer.
(6) The InputMode property is used to read or set the format of data read from the receive buffer. When InputMode=0, the data is read in text form; when InputMode=1, the data is read in binary form.
2.2 Host computer program design
The host computer is mainly responsible for sending control commands, as well as receiving, processing, calculating, and displaying data. The host computer program is divided into four parts: interface design, program initialization, data communication, and data processing. The program flow is shown in Figure 2.
The main operation interface is mainly divided into four components, as shown in Figure 3. To make the interface clear, use frame controls to separate them.
Among them, the temperature limit part is mainly used to display the upper and lower limits of temperature set by the user; the data receiving part uses the external control MSFlexGrid to display the data that has been received; the statistical calculation part is used to display the results of data analysis, such as maximum value, minimum value, average value, etc., and finally make a comprehensive judgment on the current system status; the temperature curve part displays the changes of all current temperatures, and each temperature value corresponds to a prominent point on the curve. The points within the two dotted lines represent that the temperature value is within the limit range, and the two points outside the two dotted lines indicate that the temperature value exceeds the limit range.
After entering the main interface, the user first clicks "Start Communication", at which point the upper computer will send a communication signal to the lower computer. After receiving the preset communication signal, the lower computer will send the data over; then click the "Receive Data" command, and the received data will be displayed in the MSFlexGrid control in sequence; before processing the data, the user can set the temperature limit according to their own needs, which can be achieved by simply clicking the "Limit Setting" command; finally, after processing the data and clicking the corresponding command button, the current temperature curve will be drawn on the interface, and various statistical parameters will be calculated.
2.3 Serial communication initialization program
This program is mainly used to initialize the data buffer and serial port, such as serial port selection, serial port data format, etc. The code of this part of the program is as follows:
2.4 Host computer communication program
The program consists of data sending (contact signal) and data receiving. The two parts of the program are written under the Click events of the corresponding command buttons "Start Contact" and "Receive Data". The specific program code is as follows:
3. Design of communication software for lower computer
The lower computer program includes the initialization part, the data acquisition and receiving communication signal part, and the data sending part.
The AT89S51 microcontroller serial communication uses timer T1 as a baud rate generator, and its baud rate is consistent with that of the PC. The data transmission format is 8 data bits and 1 stop bit. The query method is used to send and receive data, timer T1 is set to work mode 2, and the serial port is set to work mode 1. This design sets the communication rate to 9600 b/s, SMOD=0, and the microcontroller crystal oscillator frequency is 11.0592 MHz. At this time, the initial value of timer T1 should be set to:
3.1 Receiving command program from host computer
Receive command program from host computer:
3.2 Data sending procedure
Data sending procedure:
4 Conclusion
The communication between the microcontroller and the host computer is widely used in industrial control sites. Using the communication control MSComm provided by VB to program the host computer is simpler and more flexible than using API functions. This article analyzes the communication method between the computer and the microcontroller through a specific example. The debugging results prove that the scheme is feasible and can ensure the stability of communication, which has important practical significance.