This article takes the Visual C + + data collection and the serial communication to measure the application actual combat as the reference tutorialThis article is suitable for VC + + Serial Communication Introduction
First, page layout and add control
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. Next to the last dialog is to change generated dasses to Ccommdlg and finish
4, the new Generated Project dialog box defaults to dialog edit,
5, add two static text, two edit text, two button in the dialog box.
Finished drawings such as the following
6, Add Comm Control
1) In the workaround form, right-click the new solution, 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
This time the dialog box will appear a phone icon, half of the white side may not go. Right click on the phone icon to click Edit Control can be removed.
7. Define the type, ID, and related properties of each control at the same time
Note: This item only joins the Send and Exit Programs button
The Complete Serial Communication dialog box is now available:
8, add the member variable. Right-click the dialog box, click the Class Wizard, and click the member Variables tab. Select the ID you need to add, and double-click to add
Add variables to the IDs in the following table, in turn
While the IDC_MSCOMM1 control is not in the tag, right-click the Comm Control in the generated dialog box and click Add Variables to
9, for MSComm, two buttons to join the response event, switch to the Class Wizard virtual function Double-click the control ID, add response events, by default, you can also change the title you want
Adding a response event for the Comm control may not double-click (I am experiencing such a problem). Just right-click the Comm control icon. Click Add event handler to do so.
This time the basic interface has been arranged. Started adding the code.
Second, the code to join
1, find the solution (Solution Explorer) sources files point open, double-click on the MscommDlg.cpp 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 number of parameters: 9600 baud rate. No parity check. 8 data bits, 1 stop bits
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 number of parameters: 9600 baud rate, no parity, 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 MSComm control corresponding response function 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 receive edit box corresponding string
}
}
updatedata (0);//Update the contents of the edit box
4. Compiling the Execution program
While debugging is in execution. This program must be performed by both machines at the same time. And you have to start the same serial number.
(reprinted) using VS2010 to develop MFC serial communication based on VC + + * * * * * * Two computers communication between the same serial number