I have seen an article before, saying that if you use general attention without using CFormView or Dialog, you cannot use MSCOMM controls. But the fact is not such a thing. Like standard controls, you can use the CREATE function for ActiveX controls to create controls at runtime and implement all functions.
The steps are described below:
1> Add Idc_Mscomm using resource symbols
2> Join CREATE when setting the serial port
Void init_com () {m_serial.create ("Windowname", 0, CRECT (0, 0, 0, 0), this, IDC_MScomm); // Specify the serial number m_serial.setcommport (2);
// Turn off the serial port IF (m_serial.getPortopen ()) m_serial.setportopen (false);
// Communication parameter setting m_serial.setSettings ("9600, N, 8, 1"); // Specify the reception buffer size m_serial.setinbuffersize (1024); / / Clear the receive buffer m_serial.setinbuffercount (0); // setting Data acquisition mode m_serial.setInputMode (1); // Parameter 1 indicates that the ONCOMM event m_serial.sethRTRsHold (1) that receives data will be raised whenever more or equal to 1 characters in the serial port receiving buffer. The receiving area data length is 0 m_serial.setInputlen (0);
IF (! m_serial.getPortopen ()) m_serial.setportopen (TRUE); // Open the serial port ELSE AFXMESSAGEBOX ("Cannot Open Serial Port");
m_serial.getinput (); // Pre-read buffer to clear the residual data}
3> Send functions
Void Send_Data () {m_serial.setoutput (Colevariant ("got");}
4> Join the mapping of data reception and error messages. Be handle:
Add AFX_MSG void oncomm () in the header file;
Was added BEGIN_EVENTSINK_MAP (CxxxView, CView) // in the implementation file {{AFX_EVENTSINK_MAP (CTestCommView) ON_EVENT (CxxxView, IDC_MSCOMM, 1 / * OnComm * /, OnComm, VTS_NONE) //}} AFX_EVENTSINK_MAPEND_EVENTSINK_MAP ()
Void cxxxview :: oncomm () {// Todo: add your control notification handler code here afxMessageBox ("got");}
Datual!