(reprinted) using VS2010 to develop MFC serial communication based on VC + + * * * * * * Two computers communication between the same serial number

Source: Internet
Author: User

This article takes the Visual C + + data acquisition and the serial communication measurement and control application actual combat as the reference tutorialThis article is suitable for VC + + Serial Communication Introduction

One, page layout and add controls

1, install the vs2010.


2, create a new MFC project based on VC + + Comm


Note: Click OK, then next, to change the application type to dialog base, then next to the last dialog is to change generated dasses to Ccommdlg, then finish


4, the new Generated Project dialog box defaults to dialog edit,


5, add two static text in the dialog box, two edit text, two buttons,

The finished picture is as follows


6, adding Comm controls

1) Right-click the new solution in the Solution window and click Add->class

2) Select MFC class from ActiveX control


Click Add,available ActiveX controls to select Microsoft Communication Controls versions 6.0, then click OK.

At this time the dialog box will appear a phone icon, half of the white side can not go, when right click on the phone icon Click Edit Control can be removed.

7, define the type, ID and related properties of each control


Note: This item only adds the Send and Exit Programs button

The Complete Serial Communication dialog box is now available:


8, add member variables, right click on the dialog, click on the Class Wizard, click Member Variables tab, select the ID you want to add, double click to add

Add a variable to the ID in the following table, in turn


While the IDC_MSCOMM1 control is not in the label, right-click the Comm Control in the generated dialog box and click Add Variables.

9, add response events for MSComm, two buttons, switch to virtual function of Class Wizard double-click the control ID, add response event, default or change the title you want

Adding a response event for the Comm control may not double-click (I'm just having this problem), just right-click the Comm control icon and click Add Event handler.



This time the basic interface has been set up, and began to add code.

Second, add code

1, find the solution (Solution Explorer) sources files point open, double-click on the mscommDlg.cpp in which all our code will be added to this source file

2, serial port initialization and other serial port settings

Add the following code to the Oninitialdialog function

M_ctrlcomm.put_commport (3);//select COM3 port
M_ctrlcomm.put_inputmode (1);//input mode is binary mode
M_ctrlcomm.put_inbuffersize (1024);//input buffer size is 1024byte
M_ctrlcomm.put_outbuffersize (512);//output buffer size is 512byte
M_ctrlcomm.put_settings (_t ("9600,n,8,1"));//Set serial port parameters: 9600 baud rate, no parity check, 8 data bits, 1 stop bit
if (!m_ctrlcomm.get_portopen ())
M_ctrlcomm.put_portopen (1);//Open the serial port
M_ctrlcomm.put_rthreshold (1)//The OnComm event that receives data is raised whenever the serial receive buffer has an extra or equal to 1 characters
M_ctrlcomm.put_inputlen (0);//Set the current receive area data length to 0
M_ctrlcomm.get_input ();//read-ahead buffer to empty residual data

M_ctrlcomm.put_commport (3);//select COM3 port
M_ctrlcomm.put_inputmode (1);//input mode is binary mode
M_ctrlcomm.put_inbuffersize (1024);//input buffer size is 1024byte
M_ctrlcomm.put_outbuffersize (512);//output buffer size is 512byte
M_ctrlcomm.put_settings (_t ("9600,n,8,1"));//Set serial port parameters: 9600 baud rate, no parity check, 8 data bits, 1 stop bit
if (!m_ctrlcomm.get_portopen ())
M_ctrlcomm.put_portopen (1);//Open the serial port
M_ctrlcomm.put_rthreshold (1)//The OnComm event that receives data is raised whenever the serial receive buffer has an extra or equal to 1 characters
M_ctrlcomm.put_inputlen (0);//Set the current receive area data length to 0
M_ctrlcomm.get_input ();//read-ahead buffer to empty residual data
2. Implement Send button, exit button corresponding response function

void Cmscommdlg::onbnclickedok ()
{
Todo:add your control notification handler code here
Cdialogex::onok ();
UpdateData (1);//Read the contents of the edit box
M_ctrlcomm.put_output (COleVariant (m_strsend));//Send data
}




void Cmscommdlg::onbnclickedexit ()
{
Todo:add your control notification handler code here
M_ctrlcomm.put_portopen (0);//close the serial port
Cdialog::oncancel ();//Exit Program
}
3. Implement the corresponding response function of MSComm control ONONCOMMMSCOMM1 ()

VARIANT Variant_inp;
COleSafeArray Safearray_inp;
LONG len,k;
byte rxdata[2048];//setting byte array
CString strtemp;
if (m_ctrlcomm.get_commevent () ==2)//Event 2 indicates that the accept buffer has characters
{
variant_inp=m_ctrlcomm.get_input ();//Read Buffer
conversion of safearray_inp=variant_inp;//variant data into COleSafeArray type variable
LEN=SAFEARRAY_INP. Getonedimsize ();//Get Valid data length
For (k=0;k<len;k++)
SAFEARRAY_INP. GetElement (&k,rxdata+k);//Convert to byte array
For (k=0;k<len;k++) {//convert array to CString variant
BYTE bt=* (char *) (RXDATA+K);//character type
strtemp. Format (char) bt;//send character into temporary variable strtemp store
m_strreceive+=strtemp;//Add to receive edit box corresponding string
}
}
updatedata (0);//Update the contents of the edit box

4. Compile and run the program

When debugging, you must run the program at the same time on both machines and open the same serial number

(reprinted) using VS2010 to develop MFC serial communication based on VC + + * * * * * * Two computers communication between the same serial number

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.