Serial Admission Assistant VC Source Procedure
And programming details
Author: Gong Jianwei
Can be reproduced, indicate the author and the description from ◆ Gong Jianwei technology homepage ◆
Top:
Establish a project
2. Insert the MSCOMM control in the project
3. Using ClassWizard to define CMSCOMM class control variables
4. Add controls in the dialog
5. Add a serial event message processing function oncomm ()
6. Open and set serial parameters
7. Send data
If you haven't downloaded the source, you are interested in this article, please download now.
With the support of many netizens, the serial debugging assistant released from May 21, 2001, in just one month, nearly 5,000 downloads across the country, more than 200 emails, more than 20 people provide use Test opinions, more than 50 friends put forward the source code for serial debug assistants, in order to answer thank you friends, publicly launch the source code of the serial port communication program, and write detailed programming processes, and write detailed programming processes. Just called the serial debug assistant source program V1.0 or VC serial communication source, I believe that if you use VC programming, then this code can easily complete the serial programming task. (Maybe this article is too detailed, the master is not looking)
let's start:
1. Establish a project: Open VC 6.0, establish a dialog-based MFC application Scommtest (consistent with my source code, etc. You will be convenient);
2. Insert the MSCOMM control in the project Select the Components and Controls ... option in the Add to Project submenu under the project menu. Double-click the Registered ActiveX Controls item (a little later, this process is slow) in the pop-up dialog box. The registered ActiveX control appears in the list box. Select Microsoft Communications Control, Version 6.0, and click the Insert button to insert it into our project, accept the default option. (If you can't see Microsoft Communications Control, Version 6.0 in the list of controls, that may be that you don't have an ActiveX when you install the VC6, reinstall the VC6, and select ActiveX.),
At the ClassView window, you can see the CMScomm class, (Note: This class can not see in the classwizard, the reconstruction CLW file is also the same), and the phone icon appears in the control toolbar controls (Figure 1 Show) Now, it is now using the mouse to drag this icon to the dialog box. After the program runs, this icon is not seen.
3. Use the classwizard to define the CMSCOMM class control object to open the ClassWizard-> MEMBER Viariables tab, select the CScommtestDLG class, add control variables for IDC_MScomm1: m_ctrlcomm, then you can take a look, automatically add // {{{{入 加 加 {{ {AFX_INCLUDES () #include "mscomm.h" //}} AFX_INCLUDES (At this time, if it is wrong, then start from the beginning).
4. Add a control to the main dialog to add two edit boxes to the main dialog, one for receiving the display data ID to IDC_EDIT_RXDATA, the other for inputting sending data, IDC_EDit_txdata, add a button, function is once Just send the content in the Send Editing box once, set its ID to IDC_Button_manualsend. Don't forget to select the manufacture of the MILTILINE and VERTICAL SCROLL properties in Properties-> Styles to receive the edit box, send the edit box. If you want to enter multiple lines, you can also choose Miltiline. Open the ClassWizard-> MEMBER VIARIABLES tab, select the CScommtestDLG class, add a cstring variable M_StrRRRXDATA for IDC_EDIT_RXDATA, add a cString variable M_STRTXDATA for IDC_EDIT_TXDATA. Note: M_StrRxData and M_STRTXDATA are used to place the received and transmitted character data, respectively.
5. Add serial event message processing function oncomm () Open ClassWizard-> Message Maps, select Class Cscommtestdlg, select IDC_MSCOMM1, double-click the message oncomm, change the function name to OK in the pop-up dialog box, (Good memory) OK.
This function is used to handle serial port message events. If the serial port is received, a serial port receiving data buffer is generated, and the functions we have just implemented will be executed, we are in an oncommmm () function. Join the corresponding processing code to implement the own desired function. Please add the following code in the function:
void CSCommTestDlg :: OnComm () {// TODO: Add your control notification handler code here VARIANT variant_inp; COleSafeArray safearray_inp; LONG len, k; BYTE rxdata [2048]; // set BYTE array An 8-bit integerthat is not signed. CString Strtemp; if (m_ctrlcomm.getcommEvent () == 2) // Event value 2 indicates that the received buffer has character {below you can join the processing code variant_inp = m_ctrlcomm.getinput (); // read according to your own communication protocol Buffer SafeArray_inp = variant_inp; // variant type variable converted to a COLESAFEARRAY type variable len = SafeArray_inp.getonedIMSIZE (); // Get effective data length for (k = 0; k 6. Open the serial port and set the serial parameter You can open the serial port when you need, for example, make a start button in the program, open the serial port in the process function of the button. Now we open the serial port at the CSCommtestdlg :: OnItDialog () of the main dialog box, add the following code: // TODO: (! M_ctrlComm.GetPortOpen ()) Add extra initialization hereif (m_ctrlComm.GetPortOpen ()) m_ctrlComm.SetPortOpen (FALSE);; m_ctrlComm.SetCommPort (1) // Select com1if m_ctrlComm.SetPortOpen (TRUE); // Open the serial port ElseafxMessageBox ("Cannot Open Serial Port"); m_ctrlcomm.setSettings ("9600, N, 8, 1"); // Baud rate 9600, no verification, 8 data bits, 1 stop bit m_ctrlcomm.setInputMode (1); // Take data m_ctrlcomm.sethRTHRSHRSHRESHOLD (1); // parameter 1 in the binary method indicates an ONCOMM that receives data whenever there is more than or equal to 1 characters in the serial port receiving buffer. Event m_ctrlcomm.setInputlen (0); // Set the current receiving area data length is 0m_ctrlcomm.getinput (); // Pre-read buffer to clear the residual data Now you can try the program, then connect the serial line after picking the serial line (no Will pick up the basic method of the serial wiring I wrote, open the serial debugging assistant, and set the serial port in COM2, select automatic transmission, or wait for manual transmission. Perform the program you write, there should be data displayed in the receiving box. 7. Send data First add a click message to the send button, the BN_Clicked handler, open the classwizard-> message maps, select Class Cscommtestdlg, select IDC_Button_Manualsend, double-click BN_Clicked to add the onButtonManualSend () function, and add the following code in the function: Void cscommtestdlg :: OnbuttonManualsend () {// Todo: add your control notification handler code hereUpdatedata (true); // Read edit box content m_ctrlcomm.setoutput (Colevariant (m_strtxdata)); // Send data} Run the program, enter your point in the Send Edit box, click the Send button, ah! Look, what has appeared in the receiving box at the other end of the serial debug assistant (or other debugging tool). If you really go to the serial programming, once again, then thank you, because I can do a serial program for the first time, then the information on the Internet is not good. Open a joke, thank you for your support, there is something good thing to give me a copy. Last explanation, due to the use of VC controls, copy MSComm32.oCX, MSVCRT.DLL, MFC42.DLL to the system subdirectory in the Windows directory when running on a computer where VC is installed (Win2000 is System32) Gong Jianwei 2001.6.20 What is a Variant data type? How to use a Variant data type? How to send and receive hexadecimal or binary delivery? If you still want to know more, please see: Serial Admission Assistant Source Program and Programming Details (2) 2001.8.26