Use Serial Communication Control (GPS data reception) in VC ++ 6.0)

Source: Internet
Author: User

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:
Commp ORT: 1;
Dtrenable: 1;
Inbuffersize: 4096;
Eofenable: true;
Handshaking: nohandshaking;
Inputmode: 1-binary;
Nulldiscard: false;
Rthreshold: 250;
Rtsenable: false;
Settings: 9600, N, 8, 1;
Sthreshold: 0.
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 ())
// Open the serial port
M_comm.setportopen (true );
Updatedata (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 );
Updatedata (false );
}
// Handle the oncomm event
Void cgpsdlg: oncomm ()
{
M_comdata.empty ();
Variant m_input1;
Colesafearray m_input2;
Long length, I;
Byte data [1024];
Cstring STR;
// Receives characters in the buffer.
If (m_comm.getcommevent () = 2)
{
M_comdata.empty ();
// Read data in the buffer
M_input1 = m_comm.getinput ();
// Convert variant variables to colesafearray Variables
M_input2 = m_input1;
// 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 );
Str. Format ("% C", );
M_comdata + = STR;
}
/* 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
Cstring m_zjz;
M_zjz = m_comdata;
// Find the GPS signal Header
Int S;
S = m_zjz.find ("$ uplmc ,");
Cstring m_gps;
// Num indicates the length of the extracted GPS signal.
M_gps = m_zjz.mid (S, num );
Int X;
X = m_gps.getlength ();
// Reject if the signal length does not meet the requirements
If (X! = Num ){
M_gps.empty ();
}
// Find the GPS signal Header
Int y;
Y = m_gps.find ("$ uplmc ,");
If (y! = 0)
{
M_gps.empty ();
}
// M-gpsdata is a declared class member variable
M_gpsdata = m_gps;
}
}
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.