1. Create a project
: Open VC ++ 6.0 and create a dialog box-based MFC ApplicationProgramScommtest (with meSource codeYes, it will be easier for you to wait );
2. Insert the MSComm control in the project
Select components and from the add to Project Sub-menu under the project menu.
Controls... Option. In the displayed dialog box, double-click registered ActiveX.
Controls items (wait a moment, this process is slow), then all registered ActiveX controls appear in the list box. Select Microsoft
Communications Control, version
6.0. Click the insert button to insert it to our project and accept the default options. (If you cannot see Microsoft
Communications Control, version
6.0, it may be that you did not select ActiveX when installing vc6, re-install vc6, and select ActiveX ),
Now we can see the cmscomm class in the classview window. (Note: This class is not seen in classwizard and is restructured.
The CLW file is the same), and the phone icon (1 shown) appears in the control Toolbar controls. Now, you need to drag the icon to the dialog box with the mouse. After the program runs, the icon
Is invisible.
3. Use classwizard to define cmscomm class control objects
Open classwizard-> Member
On the viariables tab, select the cscommtestdlg class and add the control variable m_ctrlcomm to idc_mscomm1.
// {Afx_includes () # I nclude "MSComm. H" is automatically added to the header file"
//} Afx_mongodes (if there is a mistake in running the program, start from scratch ).
4. Add controls in the dialog box
Add two edit boxes to the Main Dialog Box. One is used to receive and display idc_edit_rxdata, and the other is used to input and send data. The ID is
Idc_edit_txdata, and then add a button to send the content in the edit box once and set its ID
Idc_button_manualsend. Do not forget to put miltiline and
Select the vertical scroll attribute. If you want to enter multiple lines of text in the send and edit boxes, select miltiline.
Then open classwizard-> Member
On the viariables tab, select the cscommtestdlg class and add the cstring variable m_strrxdata to idc_edit_rxdata,
Add the cstring variable m_strtxdata to idc_edit_txdata. Note:
M_strrxdata and m_strtxdata are used to put the received and sent character data respectively.
5. added the serial event message processing function oncomm ()
Open classwizard-> message maps, select cscommtestdlg, select idc_mscomm1, double-click the message oncomm, and change the function name to oncomm in the pop-up dialog box. OK.
This function is used to handle serial message events. For example, when the serial port receives data, a message event with characters in the serial port receiving data buffer is generated. The function we just added will be executed, we add corresponding processing to the oncomm () function.CodeYou can implement the functions you want. Add the following code to the function:
Void cscommtestdlg: oncomm ()
{
// Todo: add your control notification handler code here
Variant variant_indium;
Colesafearray safearray_indium;
Long Len, K;
Byte rxdata [2048]; // sets byte array an 8-bit integerthat is not signed.
Cstring strtemp;
If (m_ctrlcomm.getcommevent () = 2) // if the event value is 2, the receiving buffer contains characters.
{// The following code can be added based on your communication protocol.
Variant_indium = m_ctrlcomm.getinput (); // read the buffer
Safearray_indium = variant_indium; // convert a variable of the variant type to a variable of the colesafearray type.
Len = safearray_inp.getonedimsize (); // obtain the 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 ++) // converts an array to a cstring variable.
{
Byte bt = * (char *) (rxdata + k); // character type
Strtemp. Format ("% C", BT); // send the character to the Temporary Variable strtemp for storage
M_strrxdata + = strtemp; // Add the corresponding string to the receiving and editing box
}
}
Updatedata (false); // updates the content of the edit box.
}
So far, you cannot see the data in the receiving and editing box, because we have not enabled the serial port, but there should be no errors in the running program. Otherwise, you must have not looked at it carefully, because I started vc6 and compared it to write a row in one step, run it. Right? Next step:
6. Enable the serial port and set the serial port Parameters
You can open the serial port as needed. For example, you can create a start button in the program and open the serial port in the processing function of the button. Now, in the cscommtestdlg: oninitdialog () of the Main Dialog Box, open the serial port and add the following code:
// Todo: add extra initialization here
If (m_ctrlcomm.getportopen ())
M_ctrlcomm.setportopen (false );
M_ctrlcomm.setcommp ORT (1); // select COM1
If (! M_ctrlcomm.getportopen ())
M_ctrlcomm.setportopen (true); // enable the serial port
Else
Afxmessagebox ("cannot open serial port ");
M_ctrlcomm.setsettings ("9600, N, 8, 1"); // baud rate 9600, no verification, 8 data bits, 1 Stop bits
M_ctrlcomm.setinputmode (1); // 1: Retrieves data in binary mode.
M_ctrlcomm.setrthreshold (1 );
// Parameter 1 indicates that each time the serial port receives more than or equal to 1 character in the buffer, an oncomm event will be triggered to receive data.
M_ctrlcomm.setinputlen (0); // set the Data Length in the current receiving area to 0
M_ctrlcomm.getinput (); // pre-read the buffer to clear residual data
Now you can try the program. After connecting the serial line, openSerial Port debugging Assistant
And set the serial port to com2. The serial port can be automatically sent or manually sent. Then execute the program you wrote. The data in the receiving box should be displayed.
7. Send data
Add a click message (bn_clicked) handler for the send button to open classwizard-> message
Maps, select cscommtestdlg, select idc_button_manualsend, and double-click bn_clicked to add
Onbuttonmanualsend () function, and add the following code to the function:
Void cscommtestdlg: onbuttonmanualsend ()
{
// Todo: add your control notification handler Co
D
E
Here
Updatedata (true); // read the content of the edit box
M_ctrlcomm.setoutput (colevariant (m_strtxdata); // send data
}
Run the program. In the send and edit box, enter anything and click the send button. We will display the serial port sending and receiving data on one computer by transient the RS232 2.3 port!
in the end, mscomm32.ocx and msvcrt are used in VC when running on a computer without VC installed. DLL and mfc42.dll are copied to the system subdirectory in the Windows directory (system32 for Win2000) and then registered