Home > Software engineering >  MFC to write a serial port communication terminal, call the mscomm control, can't trigger the O
MFC to write a serial port communication terminal, call the mscomm control, can't trigger the O

Time:10-10

Recently in a serial communication experiment, a serial port terminal is written in MFC mscomm control, the problems are as follows:
(1) using the Vitual Serial Port tool measuring, sending and receiving data no problem
(2) communicate with STM32 development board, can only send data, receive data due to trigger the OnComm event, so can't receive data
(3) to a serial port turn USB module TX and RX sub tests, a serial port terminal can send data, but will not trigger the OnComm event,
Don't know how to solve this problem?
OnCommon event created when I was adding msmcomm controls, this part should be no problem,

Appendix part code is as follows:
 
Void CTerminalDlg: : OnCbnSelchangeCombo1 ()
{
//TODO: add the control notification handler code
//TODO: add the control notification handler code
Int nSel;
NSel=m_comboCom. GetCurSel ();//combo box control for the index of the selected items in a list box

M_CtrlComm. Put_CommPort (nSel + 1);//select serial number (here for the list box index number is from 0, so it's (nSel + 1) the corresponding we selected serial number)
//set the serial port
M_CtrlComm. Put_InBufferSize (1024);//receive buffer size
M_CtrlComm. Put_OutBufferSize (1024);//send buffer size
M_CtrlComm. Put_InputLen (0);//set the current receiving area data length of 0
M_CtrlComm. Put_InputMode (1);//input mode selected as binary
M_CtrlComm. Put_RThreshold (1);//parameter 1 said whenever in the serial receive buffer more than or equal to 1 characters will trigger a receiving data OnComm event

M_CtrlComm. Put_Settings (_T (" 115200, n, 8, 1 "));//set the serial port parameters, baud rate, parity, stop bits, a data
M_CtrlComm. Put_PortOpen (TRUE);//open the serial port

If (m_CtrlComm get_PortOpen ())
{

//light lamp
M_cstatSerial. SetIcon (m_hIconLedON);
M_setOK=true;//tag serial port Settings OK
}
The else
MessageBox (_T (" a serial port open failure!" ));
}

BEGIN_EVENTSINK_MAP (CTerminalDlg CDialogEx)
ON_EVENT (CTerminalDlg IDC_MSCOMM, 1, CTerminalDlg: : OnCommMscomm, VTS_NONE)
END_EVENTSINK_MAP ()


Void CTerminalDlg: : OnCommMscomm ()
{
//TODO: add message handler code here
The VARIANT variant_inp;//the Variant is a special kind of data type, in addition to the fixed-length String data and user defined type, can contain any kind of data,
COleSafeArray safearray_inp;
LONG len, k;
BYTE rxdata [2048].//set An 8-bit BYTE array integer that is not signed.
Cstrings strtemp;
If (m_CtrlComm get_CommEvent ()==CMscomm: : comEvReceive)//event value of 2 signifies a character
the receive buffer{
////////the following you can according to their own communication protocol join processing code
Variant_inp=m_CtrlComm. Get_Input ();//read buffer
Safearray_inp=variant_inp;//the VARIANT type variables into ColeSafeArray type
Len=safearray_inp. GetOneDimSize ();//to get effective data length
for(k=0; kSafearray_inp. GetElement (& amp; K, rxdata + k);//type into a BYTE array
for(k=0; k{
BYTE bt=* (char *) (rxdata + k);//character
Strtemp. The Format (_T (" % c "), bt);//the character into temporary variables strtemp store
M_strRXData +=strtemp;//add receive corresponding string edit box
}
M_strRXData +=_T (" \ r \ n "); After//show the data word wrap
}
The UpdateData (FALSE);//update the content of the edit box
}


Void CTerminalDlg: : OnBnClickedBtnSend ()
{
//TODO: add the control notification handler code
If (m_setOK==true)//determine whether open and initialize serial port
{
The UpdateData (TRUE);//read the edit box content
M_CtrlComm. Put_Output (COleVariant (m_strTXData));//send data
M_strTXData. Empty ();//to empty input box
The UpdateData (FALSE);//update the input box content
}
The else
{
MessageBox (_T (please select "COM port"));
}
}



CodePudding user response:

Program spontaneous since I looked into the problem you are closed test? Do you use a serial port assistant to see add breakpoints can be received in a buffer function there are data automatically trigger receiving function

CodePudding user response:

Add a breakpoint debugging, using a serial debugging assistants there is no problem, but can't send data is triggering OnComm event

CodePudding user response:

Found that in debug serial port module, TX and RX sub, send information, you can enter the OnCommMscomm () function, which can trigger the receiving data, but through this statement: variant_inp=m_CtrlComm. Get_Input ();//read buffer
The system will automatically quit, and throw two exceptions, is as the following:


When I put the software in other machine to test the above and can be normal to send and receive data, so strange?

CodePudding user response:

Reshipment system try
  • Related