Currently, serial communication is commonly used for data transmission by computers. In Visual C ++ programming, you can use Windows API functions for serial communication programming, or use the control that comes with the environment for programming. When using API functions to compile practical applications, you often need to consider the problem of multithreading. In this way, the compiled program is not only very large, but also complicated in structure, poor inheritance, and difficult to maintain. However, the use of the serial communication control is relatively simple, powerful, and secure and reliable.
This article takes Microsoft Communications Control (version 6.0) as an example to introduce how to use the serial communication control in VC ++. |
Embedded communication control |
Before using the serial communication control, you must register the system. |
1. When creating a new project, select the ActiveX controls check box, indicating that this program supports ActiveX control. |
2. Click the project menu, select Add to project, click the components and controls options in the pop-up dialog box, and then click the registered ActiveX controls option in the pop-up dialog box. |
3. select Microsoft Communications Control (version6.0) in the list, which is a yellow phone with a red base. Click Insert. After confirmation, a confirm classes dialog box appears, listing the class name (cmscomm) and header file name (MSComm. h), execution file name (MSComm. CPP ). |
Close this dialog box. In the control list on the main interface of the program, you can see that the communication control has been added. Drag the control to put it into the program. |
Write a communication program |
The communication control works in a similar way to the interrupt mode. When a communication event occurs, such as sending or receiving data, the oncomm event is triggered. Call the getcommevent () function in the oncomm event processing function. The return value of this function determines the type of event and processes it accordingly. |
The following describes the specific application of the control in programming by taking the GPS signal of the serial port as an example. |
Use MFC to generate a dialog box-based application. The following table lists the controls in the dialog box and their corresponding message and response functions. |
Control name |
Control ID |
Corresponding variable |
Communication Control |
Idc_mscomm |
M_comm |
Receive button |
Idc_button_receivegpsdata |
Onbuttonstartreceivegps () |
GPS signal display |
Idc_edit_comdata |
M_comdata |
Stop receiving button |
Idc_button_endreceivegpsdata |
Onbuttonendreceivegps () |
Set the properties of each part of the control: |
Handshaking: nohandshaking; |
Some programs are as follows: |
// Start to receive GPS signals |
Void cgpsdlg: onbuttonstartreceivegps () |
// Judge the status of the serial port. If it is disabled, it is enabled. |
If (! M_comm.getportopen ()) |
M_comm.setportopen (true ); |
// Stop receiving GPS signals |
Void cgpsdlg: onbuttonendreceivegps (); |
// Judge the status of the serial port. If it is enabled, it is disabled. |
If (m_comm.getportopen ()) |
M_comm.setportopen (false ); |
// Handle the oncomm event |
// Receives characters in the buffer. |
If (m_comm.getcommevent () = 2) |
// Read data in the buffer |
M_input1 = m_comm.getinput (); |
// Convert variant variables to colesafearray Variables |
// Determine the Data Length |
Length = m_input2.getonedimsize (); |
// Convert data into byte Arrays |
For (I = 0; I <length; I ++) |
M_input2.getelement (& I, Data + I ); |
// Convert the array to a cstring variable |
For (I = 0; I <length; I ++ ){ |
Char A = * (char *) (Data + I ); |
/* In the above program, replace the variant variable |
Convert to a colesafearray variable and then convert it |
Byte array, and then convert the array to a cstring variable, |
To meet the needs of displaying data for different variable types. */ |
// Extract the GPS signal |
// Find the GPS signal Header |
S = m_zjz.find ("$ uplmc ,"); |
// Num indicates the length of the extracted GPS signal. |
M_gps = m_zjz.mid (S, num ); |
// Reject if the signal length does not meet the requirements |
// Find the GPS signal Header |
Y = m_gps.find ("$ uplmc ,"); |
// M-gpsdata is a declared class member variable |
In this way, the data stream of the GPS signal is extracted. Then, you can find the longitude, latitude, Greenwich Mean Time, and other positioning information in the GPS signal by using a simple string operation. |
The above program is successfully debugged in the Win98/95/NT, p133 PC, Rockwell Jupiter GPS receiving board environment. |
|
|