Home > Software engineering >  MFC a serial port receives the data, display readfile success, but have not heard from the data.
MFC a serial port receives the data, display readfile success, but have not heard from the data.

Time:11-10

Bottom:
Void CComPort: : ComReadData ()
{
BOOL bRead=TRUE;
BOOL bResult=TRUE;
DWORD dwError=0;
DWORD BytesRead=0;
Char RXBuff;
The COMSTAT COMSTAT;


//m_ROverLapped hEvent=CreateEvent (NULL, TRUE, FALSE, NULL);

M_ROverLapped. Offset=0;
M_ROverLapped. OffsetHigh=0;



//& lt;> A EV_RXCHAR trigger the data reception, circulation receives all data buffer;
While (TRUE)
{
ComDetectStop ();
ClearCommError (m_hPort, & amp; DwError, & amp; The comstat);

If (bRead)
{
BResult=ReadFile (m_hPort, & amp; RXBuff, 1, & amp; BytesRead, & amp; M_ROverLapped);
if (! BResult)
{
The switch (dwError=GetLastError ())
{
Case ERROR_IO_PENDING://asynchronous I/o is still in progress go to GetOverlappedResults ();
{
BRead=FALSE;
break;
}
Default:
{
break;
}
}
}
}
if (! BRead)
{
BRead=TRUE;
BResult=the GetOverlappedResult (m_hPort, & amp; M_ROverLapped, & amp; BytesRead, false);
while (! BResult)
{
ComDetectStop ();
BResult=the GetOverlappedResult (m_hPort, & amp; M_ROverLapped, & amp; BytesRead, false);
}
}
Comm_RXCHAR (RXBuff);

If (comstat. CbInQue==0)
break;
}
}
Void CDevice: : Comm_RXCHAR (char=0 c/* */)
{
The static cstrings sTempCode;
If (c==- 52)
{
M_sCodeReceived=sTempCode;
M_sCodeReceived. The Trim ();//save the feedback data;
STempCode. Empty ();
M_bReceived=true;
}
}
Caller:
BOOL CDevice: : GetDevice (cstrings & amp; SCode, short nTimeout/*=3000 */)
{
M_bReceived=false;

ComReadData ();
Short nIndex=0;
While (nIndex & lt; NTimeout)
{
If (m_bReceived)
{
SCode=m_sCodeReceived;
return true;
}
NIndex++;
Sleep (1);
}
ComReset ();
return false;
}
Button calls:
Void CRS485_2_1Dlg: : OnBnClickedButton2 ()
{
//TODO: add the control notification handler code
Cstrings sCode;
if (! M_device. GetDevice (sCode))
{
SetDlgItemText (IDC_EDIT2, _T (" access failure "));
return;
}
SetDlgItemText (IDC_EDIT2, _T (" "));//to empty;
SetDlgItemText (IDC_EDIT2 sCode);
}

CodePudding user response:

Comm_RXCHAR logic in the incomplete!

 
Void CDevice: : Comm_RXCHAR (char=0 c/* */)
{
The static cstrings sTempCode;
If (c==- 52)
{
M_sCodeReceived=sTempCode;
M_sCodeReceived. The Trim ();//save the feedback data;
STempCode. Empty ();
M_bReceived=true;
}
The else
{
STempCode +=c;
}
}

CodePudding user response:

After added that there will be noise, such as I send 123, received 1233 hot,

CodePudding user response:

At the receiving place a breakpoint debugging, debugging is the essential skills for a programmer

CodePudding user response:

Set synchronization mode to handle simple, overlapped I/o read actual quantity not=the number of you want to read, you this is normal phenomenon
  • Related