Home > Software engineering >  Mscomm control receiving data
Mscomm control receiving data

Time:09-22

Himself with vs wove a mscomm serial communication program, normal transceiver can receive text in the edit box and associated cstrings variables, I now ask mscomm1. Setoutput () after sending an instruction, the machine will automatically reply to a message (lower machine itself), receives the data and change the cstrings variables and then to deal with, now edit box to receive reply instruction, but cstrings value does not change,,,,
. In addition, I send out a mscomm1 setoutput (), a chance to automatically reply to a message, at this point is directly into OnCommMscomm1 () receiving callback function? Teach,,,,,,,,

CodePudding user response:

You can be in OnCommMscomm1 () to set breakpoints, see what value is returned

CodePudding user response:

 
Void CUseMsComm32OCXDlg: : OnMscomm ()
{
//TODO: Add your the control notification handler code here
Cstrings strEvent strError;
Cstrings STR.
Short evt=m_MyMSComm. GetCommEvent ();
The switch (evt)
{//Events
Case ComEvSend://There are SThreshold number of characters in the transmit buffer.
StrEvent="Sending".
break;
Case ComEvReceive://Received RThreshold number of chars.
{
//strEvent="identifiers";
The VARIANT var.
Var=m_MyMSComm. GetInput ();
//
If (m_MyMSComm GetInputMode ()==ComInputModeText)
{//ComInputModeText
STR=var. BstrVal;
}
The else
{//ComInputModeBinary, over the Text.
COleSafeArray arr.
Arr=var;
Long len=arr. GetOneDimSize ();
BYTE Data [4096].
Data [len]=0;//sz
For (long jj=0; Jj & lt; Len. Jj++)
{
Arr. GetElement (& amp; Jj, & amp; Data [jj]);
}
//if true binary, we should convert the Data to the string
Char * newStr=0;
NewStr=(char *) KillHalfHZ (Data, len);//no sz!
STR=Data;
}
ShowData (STR);
break;
}
Case ComEvCTS://Change in the CTS line.
//strEvent="CTS changed";
break;
Case ComEvDSR://Change in the DSR line.
The DSR strEvent="changed";
break;
Case ComEvCD://Change in the CD line.
//strEvent="CDC changed";
break;
Case ComEvRing://Change in the Ring Indicator.
StrEvent="Phone Ring";
break;
Case ComEvEOF://An EOF charater was found in the input stream
StrEvent="EOF detected";
break;
//Errors
Case ComEventBreak://A Break was received.
The strError="Break Received";
break;
Case: 1002//vbMSCommErCTSTO:
The strError="CTS Timeout";
break;
Case: 1003//vbMSCommErDSRTO:
The strError="DSR Timeout";
break;
Case ComEventFrame://Framing Error
The strError="Framing Error";
break;
Case ComEventOverrun://Data Lost.
The strError="Overrun Error";
break;
Case: 1007//vbMSCommErCDTO:
The strError="CD TimeOut";
break;
Case ComEventRxOver://the Receive buffer overflow.
The strError="R buffer Overflow";
break;
Case ComEventRxParity://Parity Error.
The strError="Parity Error";
break;
Case ComEventTxFull://the Transmit buffer full.
The strError="T buf Full";
break;
Case ComEventDCB://Unexpected error retrieving DCB
The strError="DCB error";
break;
}
//
if (! StrEvent. IsEmpty ())
{
AfxMessageBox (strEvent);
}
if (! The strError. IsEmpty ())
{
AfxMessageBox (" ComError MB_ICONEXCLAMATION);
M_MyMSComm. SetPortOpen (FALSE);
}
}

.cpp
BEGIN_EVENTSINK_MAP (CUseMsComm32OCXDlg CDialog)
//{{AFX_EVENTSINK_MAP (CUseMsComm32OCXDlg)
OnComm ON_EVENT (CUseMsComm32OCXDlg IDC_MSCOMM1, 1/* */, OnMscomm, VTS_NONE)
//}} AFX_EVENTSINK_MAP
END_EVENTSINK_MAP ()
.h
Afx_msg void OnMscomm ();
 
Void CUseMsComm32OCXDlg: : ShowData (LPCTSTR pchData)
{
Cstrings STR.
Int nCharIndex;
Int nBackcount=0;

//Make sure existing text doesn 't get too large.
Int len=m_Edit. GetWindowTextLength ();
If (len & gt;
=16384){
M_Edit. GetWindowText (STR);
M_Edit. SetWindowText (STR. GetBuffer (len) + 4097);
Len -=4097;
}
//Handle backspace characters.
STR=pchData;
While ((nCharIndex=STR. Find (TCHAR (VK_BACK)))!=1)
{
If (nCharIndex==0)
{
NBackcount++;
STR=STR. Mid (nCharIndex + 1);
}
The else
{
STR=STR. Left (nCharIndex - 1) + STR. The mids (nCharIndex + 1);
}
}
//Set the text insertion point "according to the number of backspaces
//at the begining of strData.
If (nBackcount> Len) nBackcount=len;
M_Edit. SetSel (len - nBackcount, len);
//Eliminate line feeds.
While ((nCharIndex=STR. Find (TCHAR (' \ n ')))!=1)
{//delete all ah 0
STR=STR. Left (nCharIndex) + STR. Mid (nCharIndex + 1);
}
//Ensure all carriage returns have a line feed following.
LPCTSTR pchStart=(LPCTSTR STR);
LPTSTR pchIndex=_tcschr (pchStart, (TCHAR) '\ r');

While (pchIndex!=NULL)
{//add
Int nPos=pchIndex - pchStart;
STR=STR. Left (nPos + 1) + cstrings (' \ n ') + STR. Mid (nPos + 1);
//Above operation probably allocated a new string buffer.
PchStart=(LPCTSTR STR);
PchIndex=_tcschr (nPos pchStart + + 1, (TCHAR) '\ r');
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related