Below are some of the key aspects of MFC implementationCode(For reference only)
Senddlg: senddlg (cwnd * pparent/* = NULL */)
: Cdialog (senddlg: IDD, pparent)
{
// {Afx_data_init (senddlg)
M_baud =-1;
//} Afx_data_init
Currentlydisplaymax = 0;
M_text0 = "Make sure the cable has been correctly connected to the COM 1 port ,";
M_text = "reset the display, and click" send. ";
Issend = false;
M_bconnected = false;
M_nbaud = 9600;
M_baud = 1;
M_ndatabits = 8;
M_becho = false;
M_nflowctrl = 0;
M_bnewline = false;
M_nparity = 1;
M_sport = "COM1 ";
M_nstopbits = 0;
}
Senddlg ::~ Senddlg ()
{
If (m_bconnected)
Closeconnection ();
}
Bool senddlg: configconnection () // enable serial port COM1 settings
{
DCB;
If (! Getcommstate (m_hcom, & DCB ))
Return false;
DCB. fbinary = true;
DCB. baudrate = m_nbaud; // baud rate
DCB. bytesize = m_ndatabits; // number of bytes
DCB. fparity = true;
Switch (m_nparity) // check settings
{
Case 0: DCB. Parity = noparity;
Break;
Case 1: DCB. Parity = evenparity;
Break;
// Case 2: DCB. Parity = oddparity;
// Break;
// Default :;
}
Switch (m_nstopbits) // stop bit
{
Case 0: DCB. stopbits = onestopbit;
Break;
Case 1: DCB. stopbits = one5stopbits;
Break;
Case 2: DCB. stopbits = twostopbits;
Break;
// Default :;
}
// Hardware traffic control settings
DCB. foutxctsflow = m_nflowctrl = 1;
DCB. frtscontrol = m_nflowctrl = 1?
Rts_control_handshake: rts_control_enable;
// Xon/xoff traffic control settings
DCB. finx = DCB. foutx = m_nflowctrl = 2;
DCB. xonchar = Xon;
DCB. xoffchar = xoff;
DCB. xonlim = 50;
DCB. xofflim = 50;
Return setcommstate (m_hcom, & DCB );
}
Bool senddlg: openconnection () // open COM1
{
Commtimeouts timeouts;
If (m_bconnected)
Return false;
M_bconnected = true;
M_hcom = createfile (m_sport, generic_read | generic_write, 0, null,
Open_existing, file_attribute_normal, null); // non-overlapping Mode
If (m_hcom = invalid_handle_value)
Return false;
Setupcomm (m_hcom, maxblock, maxblock );
Setcommmask (m_hcom, ev_cts | ev_dsr | ev_rlsd );
// Set the interval timeout value to the maximum value. Setting the total timeout value to 0 causes the readfile to return immediately and complete the operation.
Timeouts. readintervaltimeout = maxdword;
Timeouts. readtotaltimeoutmultiplier = 0;
Timeouts. readtotaltimeoutconstant = 0;
/* Set write timeout to specify
Wait time of the getoverlappedresult function */
Timeouts. writetotaltimeoutmultiplier = 50;
Timeouts. writetotaltimeoutconstant = 2000;
Setcommtimeouts (m_hcom, & Timeouts );
If (! Configconnection ())
{
Closehandle (m_hcom );
Return false;
}
Return true;
}
Void senddlg: closeconnection () // close COM1
{
If (! M_bconnected)
Return;
M_bconnected = false;
Closehandle (m_hcom );
}
DWORD senddlg: readcomm (unsigned char * Buf, DWORD dwlength)
{// Read COM1 byte
DWORD length = dwlength;
COMSTAT;
DWORD dwerrorflags;
Clearcommerror (m_hcom, & dwerrorflags, & COMSTAT );
Length = min (dwlength, COMSTAT. cbinque );
Readfile (m_hcom, Buf, length, & length, null );
Return length;
}
DWORD senddlg: writecomm (unsigned char Buf, DWORD dwlength)
{// Write COM1 byte
Bool fstate;
DWORD length = dwlength;
COMSTAT;
DWORD dwerrorflags;
Overlapped m_oswrite;
Clearcommerror (m_hcom, & dwerrorflags, & COMSTAT );
Fstate = writefile (m_hcom, & Buf, length, & length, null );
If (! Fstate)
{
If (getlasterror () = error_io_pending)
Getoverlappedresult (m_hcom, & m_oswrite, & length, true); // wait
Else
Length = 0;
}
Return length;
}
Void senddlg: onradio1 ()
{
M_nbaud = 4800;
M_baud = 0;
}
Void senddlg: onradio2 ()
{
M_nbaud = 9600;
M_baud = 1;
Bool senddlg: senddata (Int & W) // send data
{
If (! Openconnection () // connection Error
{
Closeconnection ();
Return false;
}
For (INT y = 0; y <2; y ++) // MCU for sending signal interruption
{
Writecomm (Num, 1 );
}
If (comstat1.cbinque) // sends a signal
{
Readcomm (readcom1v, 1 );
...
}
Closeconnection ();
Return false;
}
}