为什么我从串口读取的数据在变??
为什么我从串口读取的数据在变??事实上发送的数据是固定的,我用ReadFile检测第一个数据,老在变,好象后面的数据冲到前面了
int CSerialComm::ReadData( LPSTR lpszBlock, int nMaxLength )
{
BOOL fReadStat ;
COMSTAT ComStat ;
DWORD dwErrorFlags;
DWORD dwLength;
DWORD dwError;
// char szError[ 10 ] ;
// only try to read number of bytes in queue
ClearCommError( m_hCommDevice, &dwErrorFlags, &ComStat ) ;
dwLength = min( (DWORD) nMaxLength, ComStat.cbInQue ) ;
if (dwLength > 0)
{
fReadStat = ReadFile( m_hCommDevice, lpszBlock,
dwLength, &dwLength, &m_osRead ) ;
if (!fReadStat)
{
if (GetLastError() == ERROR_IO_PENDING)
{
TRACE("\n\rIO Pending");
// We have to wait for read to complete.
// This function will timeout according to the
// CommTimeOuts.ReadTotalTimeoutConstant variable
// Every time it times out, check for port errors
while(!GetOverlappedResult( m_hCommDevice,
&m_osRead, &dwLength, TRUE ))
{
dwError = GetLastError();
if(dwError == ERROR_IO_INCOMPLETE)
// normal result if not finished
continue;
else
{
// an error occurred, try to recover
ClearCommError( m_hCommDevice, &dwErrorFlags, &ComStat ) ;
break;
}
}
}
else
{
// some other error occurred
dwLength = 0 ;
ClearCommError( m_hCommDevice, &dwErrorFlags, &ComStat ) ;
}
}
}
return ( dwLength ) ;
} // end
other function:
char msg_block[1000];
char sz[100];
bytes_in = serialComm.ReadData(msg_block, sizeof msg_block );
sprintf(sz,"%x ",msg_block[0]);
TextOut(::GetWindowDC(0),100,100,sz,sizeof(sz));
就是msg_block[0]在变化