Home > Software engineering >  MFC get_Input serial communication program () function is abnormal
MFC get_Input serial communication program () function is abnormal

Time:10-11

Everybody is good:
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 address

CodePudding user response:

reference 1st floor cheng_xu_yuan123 response:
I met the same problem with you, no address


Void CSanTanGPSProgectDlg: : OnCommMscomm1 ()
{
//TODO: add message handler code here
The VARIANT variant_inp;
COleSafeArray safearryay_inp;
Long len, k;
Unsigned int data [1024]={0};
Byte rxdata [1024].
Cstrings stremp;
Cstrings m_RecevieCmd=_T (" ");
Cstrings m_EditReceive=_T (" ");
If (m_mscomm get_CommEvent ()==2)
{
Sleep (80);
Variant_inp=m_mscomm. Get_Input ();//read buffer
Safearryay_inp=variant_inp;//the VARIANT type variables into ColeSafeArray type
Len=safearryay_inp. GetOneDimSize ();//to get effective data length
//to accept data
for(k=0; K{
Safearryay_inp. GetElement (& amp; K, rxdata + k);//type into a BYTE array
}

Sleep (1);
For (k=0; k {
if (! M_bConnect)
{
BYTE bt=* (char *) (rxdata + k);//character
Stremp. The Format (" % c ", bt);//the character into temporary variables strtemp store
M_EditReceive +=stremp;
}
The else
{
BYTE bt=* (char *) (rxdata + k);//character
Stremp. The Format (" % 02 x ", bt);//hex

M_EditReceive +=stremp;
}
}
}

CodePudding user response:

if (! M_bConnect)
{
BYTE bt=* (char *) (rxdata + k);//character
Stremp. The Format (" % c ", bt);//the character into temporary variables strtemp store
M_EditReceive +=stremp;
}
The else
{
BYTE bt=* (char *) (rxdata + k);//character
Stremp. The Format (" % 02 x ", bt);//hex
}
This section would you like to use that character the hexadecimal is above the else statement

CodePudding user response:

reference SWWLLX reply: 3/f
if (! M_bConnect)
{
BYTE bt=* (char *) (rxdata + k);//character
Stremp. The Format (" % c ", bt);//the character into temporary variables strtemp store
M_EditReceive +=stremp;
}
The else
{
BYTE bt=* (char *) (rxdata + k);//character
Stremp. The Format (" % 02 x ", bt);//hex
}
This section would you like to use that character the hexadecimal is above the else statement inside


My program to run on Windows XP, very normal, now in the computer can't serial port communication, mscomm control also registered a success, this is what circumstance? .

CodePudding user response:

reference 4 floor cheng_xu_yuan123 response:
Quote: reference SWWLLX reply: 3/f

if (! M_bConnect)
{
BYTE bt=* (char *) (rxdata + k);//character
Stremp. The Format (" % c ", bt);//the character into temporary variables strtemp store
M_EditReceive +=stremp;
}
The else
{
BYTE bt=* (char *) (rxdata + k);//character
Stremp. The Format (" % 02 x ", bt);//hex
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related