On my VS2010. Write a serial communication program, and then turn use STM32 development board through a USB serial port to send 0 x55 COM3 interval of 1 second, after debugging found application in variant_inp=m_mscomm. Get_Input (); This sentence appeared abnormal, but with a serial debugging assistants can receive 0 x55, program code is as follows, please everybody to give some Suggestions:
Void CComSet: : OnBnClickedButtonComset ()
{
//TODO: add the control notification handler code
Int nSel;
Cstrings strMsg;
//combo box control for the index of the selected items in a list box
NSel=m_comboComNum. GetCurSel () + 1;
M_mscomm. Put_CommPort (nSel);//set com3
M_mscomm. Put_InputMode (1);//select send form for binary
M_mscomm. Put_InputLen (0);//set the current receiving area data length
M_mscomm. Put_InBufferSize (1024);//set the input buffer size
M_mscomm. Put_OutBufferSize (1024);//set the output buffer size
M_mscomm. Put_RThreshold (1);//when a serial port input buffer when one or more than one character inspired ONCOMM event into the ONCOMM event handler
//m_mscomm put_SThreshold (0);//each send one character at a time, do not trigger the OnComm event
M_mscomm. Put_Settings (_T (" 115200, n, 8, 1 "));//set the serial port baud rate 9600, no check, 8 bits of data, 1 stop bit,
If (m_mscomm get_PortOpen ())
{
AfxMessageBox (_T (" a serial port does not exist or are occupied by the other application!" ));
}//check the serial port state, if it's open just turn it off
The else
{
M_mscomm. Put_PortOpen (TRUE);
M_mscomm. Get_Input ();//to proofread, empty buffer
StrMsg. The Format (_T (" the com % d is ready!" ), nSel);
AfxMessageBox (strMsg);
}
}
BEGIN_EVENTSINK_MAP (CComSet CDialogEx)
ON_EVENT (CComSet IDC_MSCOMM1, 1, CComSet: : OnCommMscomm1, VTS_NONE)
END_EVENTSINK_MAP ()
Void CComSet: : OnCommMscomm1 ()
{
//TODO: add message handler code here
//the VARIANT variant_inp;
COleVariant variant_inp;
COleSafeArray safearray_inp;
Int nEvent=0;
Long len=0, I=0;
BYTE bt=0;
Cstrings STR.
Cstrings strtemp;//cstrings variables
NEvent=m_mscomm. Get_CommEvent ();
Switch (nEvent)//value is 2 skeleton table enclosed in the receive buffer valid characters
{
Case EV_RXFLAG:
Variant_inp=m_mscomm. Get_Input ();
STR. The Format (_T (" nEvent2=% d "), nEvent);
Safearray_inp=variant_inp;
Len=safearray_inp. GetOneDimSize ();
for(i=0; i{
Safearray_inp. GetElement (& amp; I, & amp; Bt);
Strtemp. The Format (_T (" % c "), bt);
STR +=strtemp;
}
AfxMessageBox(str);
break;
}
The UpdateData (FALSE);
}
Void CComSet: : OnBnClickedClosecomm ()
{
//TODO: add the control notification handler code
If (m_mscomm get_PortOpen ())
{
AfxMessageBox (_T (" close port!!!!!! "));
M_mscomm. Put_PortOpen (FALSE);//close a serial port
}
The else
{
AfxMessageBox (_T (" port is not open!!! "));
}
}
CodePudding user response:
I met the same problem with you, no addressCodePudding user response: