First, create a communication control in the dialog box. If this control is missing in the control toolbar, you can insert it through the menu project --> Add to project --> components and control, then pull the control from the toolbox to the dialog box. In this case, you only need to care about the API functions provided by the control for the Windows communication driver. In other words, you only need to set and monitor attributes and events of the MSComm control.
After opening the required serial port, you need to consider the serial port communication time. In the process of receiving or sending data, you may need to monitor and respond to some events and errors. Therefore, event-driven is a very effective method for handling serial port interactions. Capture and check the values of communication events and errors using the oncomm event and commevent attributes. When a communication event or error occurs, the oncomm event is triggered, and the value of the commevent attribute is changed. The application checks the attribute value of commevent and responds accordingly.
// If this control is used in SDI, you need to call the next two sentences. In the dialog box program, this statement has been created by MFC.
// Do not manually add
DWORD style = ws_visible;
M_mscomm.create (null, style, crect (0, 0, 0), this, idc_mscomm1 );
// Initialize the serial control
DWORD style = ws_visible;
M_mscomm.create (null, style, crect (0, 0, 0), this, idc_mscomm1 );
If (m_mscomm.getportopen () // If the serial port is enabled, the serial port is closed.
{
M_mscomm.setportopen (false );
}
M_mscomm.setcommp ORT (1); // select COM1
M_mscomm.setinbuffersize (1024); // receives the buffer.
M_mscomm.setoutbuffersize (1024); // sending Buffer
M_mscomm.setinputlen (0); // set the length of data in the current receiving area to 0, indicating that all data is read.
M_mscomm.setinputmode (1); // read and write data in binary mode
M_mscomm.setrthreshold (1); // when the receiving buffer has 1 or more characters, the oncomm event that receives data is triggered.
M_mscomm.setsettings ("9600, N, 8, 1"); // The baud rate is 9600, which has no check bits, 8 data bits, and 1 Stop bits.
If (! M_mscomm.getportopen () // open if the serial port is not enabled
M_mscomm.setportopen (true); // open the serial port
Else
M_mscomm.setoutbuffercount (0 );
// Control Event Response Declaration
// *. H
// {Afx_msg (cgolfview)
Afx_msg bool oncomm ();
Declare_eventsink_map ()
//} Afx_msg
// *. Cpp
Begin_eventsink_map (cgolfview, cview)
// {Afx_eventsink_map (caboutdlg)
On_event (cgolfview, idc_mscomm1, 1/* oncomm */, oncomm, vts_none)
//} Afx_eventsink_map
End_eventsink_map ()
// Control Event Response
Bool cgolfview: oncomm ()
{
Variant variant_indium;
Colesafearray safearray_indium;
Long Len, K;
Byte rxdata [2048]; // sets byte array an 8-bit integerthat is not signed.
Cstring strtemp;
Switch (m_mscomm.getcommevent ())
{
Case 1: // comevsend sends data
Break;
Case 2: // comevreceive read data
// MessageBox (_ T ("read data event"), _ T ("trace"), mb_ OK );
Variant_indium = m_mscomm.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.
// Accept data
For (k = 0; k {
Safearray_inp.getelement (& K, rxdata + k); // convert to byte array
Byte bt = * (char *) (rxdata + k); // character type
Strtemp. Format ("% C", BT); // send the character to the Temporary Variable strtemp for storage
Recd + = strtemp;
}
// Updatedata (true );
Break;
Default: // transfer event Error
M_mscomm.setoutbuffercount (0 );
Break;
}
Updatedata (false); // update the image content
Return true;
}