Application of Visual C ++ in Asynchronous Serial Communication Control

xiaoxiao2021-03-06  36

Visual C in Asynchronous Serial Communication Control Summary: The system introduces the basic principles of the asynchronous serial communication in Visual C , which tells the implementation of the Windows Library API function and the C operation time library. Programming application instances in the control system. Keywords: control system; serial communication; Visual C application diagram Category: TP311.11 Document code: a the Visual C Application In Asynchronous Serial Communication Control Qiu Hong-Bing Han Feng Abstract: The Paper is Focus on the Principle of asynchronous serial communication by Visual C , and tells of their implement methods using API function in Windows function warehouse and C function singly The paper also adduces program application sample in control system Key Words: control system; serial communication; Visual C application 1.. Introduction Visual C is a visual programming environment built on Window 95 and Window NT 32-bit programs. It provides great convenience for the visual development of the control system, but is introduced for data acquisition, control, and Visual C for data collection. It is very few. Based on this, the basic principles and applications of Visual C in the application of the control system are introduced, with asynchronous serial communication. Its basic composition is: the lower machine (single chip system) completes signal detection, A / D conversion, and simple control functions, connected to the host computer (such as RS-232C), monitoring, control, and forming a master Structure. 2 Working principle The controlled signal and related signal are detected by the sensor. After amplification, the filtering process forms an effective signal, and the digital signal is formed by A / D conversion, and the single-chip microcomputer system is input. The single-chip microcomputer system completes the corresponding algorithm or control function; the PC mainly completes monitoring, controlling the following microcontroller system, and centralized management. Communication between them can be asynchronously through the RS-232C standard bus. The system structure is shown in Figure 1. Figure 1 Control System Network Structure Figure 3 Visual C Realizing Serial Communication Visual C Realizing Serial Communications Available in Several Ways, such as using standard serial ports on your computer, or self-made cards into the PCI bus of the industrial computer ISA expansion slot, etc. The following mainly discusses serial communication using the standard serial port on the computer, which can be divided into utilization using the Windows Function Library API function and utilizing C running time library implementation. The API function is mainly serial communication in the form of creating resources, and the C running library uses a similar C.

3.1 API Functions Implementation Windows Applications To have 24 low-level functions in the Windows Library, which provides basic tools, file input, and file output functions to the communication resource handle with communication with external devices. , Shut down and performing a basic interface, Win32 API also contains a series of communication functions for accessing communication resources. The specific work procedure is as follows: First open a communication resource handle CreateFile (), followed by serial communication resources ( Information including baud rate, parity, stop bit, and data bits), modify and query the initialization settings via setcommstate (), getcommstate (); read and write to serial communication resources via readfile (), Writefile () Complete the transmit and receive data, monitoring a serial communication resource Some possible events can be done through WaitCommEvent (), send a control command to the device driver associated with a communication resource to enable the driver to perform a specific task. See the book for specific parameter settings. [1] 3.2 C Runtime Library Implementation For serial communication to implement serial communication using C runtime library, it will be discussed in detail. It is closely related to hardware, and its core device is a universal asynchronous receiving transmitter (UART) and a 25-pin (or 15 pin, 9 pin) D-connector is taken as a data terminal device (DTE) in the rear of the chassis. On the side, it is connected to the other side data communication device (DCE) (MODEM or other communication device) by asynchronous communication cable (RS-232). 3.2.1 RS-232 Asynchronous Serial Port Connection Asynchronous Serial Communication is extremely sensitive to the application interface, the commonly used RS-232C interface has a Modem interface standard connection and a non-standard connection of zero MODEM, but in practical applications, it is not required to control Signal lines, and in some case, several non-standard connection methods are also allowed, as shown in FIG. Fig. 2A is mainly suitable for long distance serial communication, and Figure 2B is suitable for short-range asynchronous communication without the need for modem type DCE device intervention. Figure 2 RS-232 interface connection 3.2.2 Asynchronous serial communication principle The asynchronous communication program is used to perform asynchronous communication, in fact, the read or write operation of the UART internal register. The VC can be programmed with the -inp () / - outp () function, and their port addresses are listed in Table 1. Table 1 UART internal register port assignment

Port address ** instruction condition * Register name and action 0x3F8 (0x2F8) 0x3F8 (0x2F8) 0x3F9 (0x2F9) 0x3F9 (0x2F9) 0x3FA (0x2FA) 0x3FB (0x2fb) 0x3FC (0x2FC) 0x3FD (0x2FD) 0x3FD (0x2FE ) _outp_inp_outp_outp_outp_inp_outp_outp_inp_inpdlab = 0dLAB = 0DLAB = 1DLAB = 1DLAB = 0 ---- Writing Transmitter Hold Register Readout Receiver Data Register Writing Portal Rate Factor (LSB) Writing Write Rate Factor (MSB) Writing Interrupt Allow registers to read out interrupt identification registers write line control registers Write into the MODEM control register readout line status register reads the MODEM status register

* DLAB refers to the line controller D7; ** Before the bracket refers to COM1, the bracket refers to COM2. Next, the role of each register of Table 1 is described in the order of programming applications (the port address is taken as an example of COM1). First, determine the data format of asynchronous communication, including the latching of the baud rate factor, the setting of parity bit, the determination of the stop bits, and the number of data bits, each defined as follows: D7 set 1 pair division latch, D6 set 1 Allow interruption, D5D4D3 parity, D2 stop bits, D1D0 data bits. Next, the communication between the communication between the communication is determined, the method of determining the baud rate is to write the baud rate factor (16th) twice to the baud rate factor register, baud rate and baud rate factor before communication. Relationship example: If the baud rate is 75, the baud rate factor is 0x06 (MSB), 0x00 (LSB); the baud rate is 1200, and its baud rate factor is 0x00 (MSB), 0x60 (LSB); 1800 0x00 (LSB), 0x40 (MSB); 2400 corresponds to 0x00 (LSB), 0x30 (MSB); 3600 corresponds to 0x00 (LSB), 0x20 (MSB); 9600 corresponds to 0x00 (LSB), 0x0C (MSB), etc. Then, the reading line status is judged, and it is suitable for the query I / O of the URAT. If interrupt I / O is taken, it is not necessary to judge. The query I / O mode is taken to the URAT, first read the line status to determine whether it is ready and is used to send or receive. The definitions of the line status registers in the UART internal state are as follows: D7 constant 0, the D6 transmission shift register is empty, the D5 transmission hold register is empty, D4 receives the interruption condition, D3 receives the frame format, D2 receives the parity school The error, D1 receives the error, the D0 receiver data is ready. Then it is judged. Determining the chip operation mode and control MODEM can be done by the write operation of the Modem control register. [2] 3.2.3 Application Example VC The application instance of the asynchronous serial communication programming application. The main control computer mainly completes the data collection, processing control and alarm function of the microcontroller system under which. Control system structure diagram is shown in Figure 1. For the serial communication of the control software, the following components are composed: the initialization of the serial port, data collection processing, and control alarms (with serial port COM1 as an example, port base address 0x3f8). Initializing serial communication ports generally do the following things: Determine the data transfer frame format (including data bit length, stop bit length, and parity and type selection), determine the transfer baud rate (with the data signal transmission rate) and Determine the UART mode of operation. The operation method refers to normal communication or loop feedback for diagnosis, which is the program query I / O or communication interrupt I / O. Void ccommu :: init () {_outp (0x3fb, 0x80); // Set line control register set DLAB = 1 to set the baud rate _outp (0x3f8, 0x30); // Write the bass factor, Porter Rate is 2400 _OUTP (0x3f9, 0x00); _outp (0x3fb, 0x03); // Set line control register table DLAB = 0 Determine the data frame transmission format _outp (0x3f9, 0x00); // Setting the interrupt allowed register using query I / O} Data Collection Process Mainly completed data reception transmission. The MODEM status register and line status register must be detected during the transceiver. The communication process is shown in FIG. 3A is a transmission process, and FIG. 3B is a reception process. Some procedures are as follows:

转载请注明原文地址:https://www.9cbs.com/read-62789.html

New Post(0)