Applying MSCOMM serial communication control (attachment program) in a single document (SDI) program (attachment program) ◆ Gong Jianwei Technology Home ◆ Gong Jianwei MSCOMM serial communication control is very good in dialog-based programs (can refer to I write "serial debug assistant source Program and detailed programming process one "), but sometimes we need to use in document-based programs," Can I use the serial communication control in a single document-based program, why not directly use the control, based on this article How to use this control in a single document-based program. Download the source program: (SDICOMM.ZIP, WIN98 / 2000, VC6.0, 45KB) 1. Establish a single document application SDICOMM using the MFC wizard, all steps are default, insert MSCOMM controls in the project (Project-> Add to Project -> Components and Control ...-> Registered Active Controls-> MCRIOSoft Commmunications Control, V6.0, click Insert, OK; 2.SDICOMMVIEW.H Process: Add #include "mscomm.h"; then in // {{AFX_MSG (CSDicommview) and //}} AFX_MSG oncomm (); declare_eventsink_map () The last result is: // {{AFX_MSG (csdicommview) AFX_MSG void oncomm (); // Event processing Function declare_eventsink_map () //}} AFX_MSG Add CMSCOMM class PUBLIC object definition: cmscomm m_mscomm; 3. Use ClassWizard to add a WM_CREATE function to the CSDicomMView class, which is executed when the window is initialized.
Selecting MESSAGE MAP CLASSWIZARD card, select CSDICommView in OBJECT IDS, select WM_CREATE in MESSAGES, double add int CSDICommView :: OnCreate (LPCREATESTRUCT lpCreateStruct) function int CSDICommView :: OnCreate (LPCREATESTRUCT lpCreateStruct) {if (CView :: Oncreate (lpcreatestruct) == -1) Return -1; // Todo: add your specialized code code herem_mscomm.create (NULL, 0, CRECT (0, 0, 0), THIS, IDC_MSCOMM1); if (M_MScomm. GetPortopen ()) // If the serial port is open, the line is turned off M_Mscomm.SetPortopen (false); m_mscomm.setcommport (2); // Select COM2M_MSCOMM.SETINBUFFERSIZE (1024); // Receive Buffer M_Mscomm.setoutBuffersize (1024) ); // Send buffer m_mscomm.setinputlen (0); // Set the current receiving area data length is 0, indicating all read m_mscomm.setinputMode (1); // read and write data in binary mode M_Mscomm.SetrhetRshold (1) ; // When the buffer has 1 and 1 or more characters, the ONCOMM event M_Mscomm.setSettings ("9600, N, 8, 1") of the received data will be caused; // baud rate 9600 no inspection position, 8 Data bit, 1 stop bit if (! M_mscomm.getPortopen ()) // If the serial port is not open, open m_mscomm.setportopen (TRUE); // Open the serial port ElseafxMessageBox ("Open Serial Port Failure!"); M_Mscomm.Getinput ); // Pre-read the buffer to clear the residual data return 0;} We should pay attention to M_Mscomm.create (NULL, 0, CRECT (0, 0, 0), this, this sentence, Its function is to initialize the serial port class object M_MS Comm, this is done automatically by the ClassWizard based on the dialog. Note that IDC_MSCOMM1 is derived from the control ID rules in the dialog, and IDC_MSCOMM1 must correspond to the serial control, where we'd better use a dialog box to drag the control into the dialog box, you can save a lot of things, here we use The About dialog box, the method is as follows: Select the IDD_ABOUTBOX dialog box in ResOoureView to drag the control icon into the dialog.
So far, we have not completed the initialization of the serial port receiving data event, but also the following steps: 4. Initialization of the serial port receiving data event: Add the following event driver in the sdicommview.cpp file: begin_eventsink_map (csdmscom1View, cView) // {{AFX_EVENTSINK_MAP (CAboutDlg) ON_EVENT (CSDMSCom1View, IDC_MSCOMM1, 1 / * OnComm * /, OnComm, VTS_NONE) //}} AFX_EVENTSINK_MAPEND_EVENTSINK_MAP () function added manually oncomm (): function in our process to make such tests, whenever Oncomm event, send data "OK, I'VE Received Some Data! / R / N" to the serial port, by the way, in the serial port communication program, the general communication processing is handled in the oncommmm () function. void CSDICommView :: OnComm () {// TODO: Add your control notification handler code hereCString strtemp; strtemp.Format ( "OK, I've received some data / r / n!"); m_MSComm.SetOutput (COleVariant (strtemp) ); // Send data} Finally, after the test program, the serial debug assistant V2.1 (or other debug tool) can be set in COM1, 9600, N, 8, 1 after compiling, click Manual Send, See the effect. Please click on the image to enter ◆ Gong Jianwei technology home page ◆ serial port Tongxun (program source), serial debugging assistant, port network communication technology, VC / C programming, mobile robot control related technology, MATLAB simulation and data processing