为什么我在调试用API写的串口通信程序时,以文本形式发送能接收到,文本不能显示的接收不到呢?
例如:0x34能接收到,而0x11就接收不到。

解决方案 »

  1.   

    我是调试时收到数据长度为0
    代码如下:void CMySerialComDlg::OnTimer(UINT nIDEvent) 
    {
    // TODO: Add your message handler code here and/or call default
    int Len;
    char buffer[1024];
    //buffer[Len]='\0';
    Len=::ReadCommBlock(m_ComConfig,buffer,1024);
    //buffer[Len]='\0';
    if(Len!=0)
    {
    buffer[Len]='\0';
    m_receive+=buffer;
      UpdateData(FALSE);
    }
    CDialog::OnTimer(nIDEvent);
    }
    int ReadCommBlock(CComStatus& comDev,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( comDev.m_hCom, &dwErrorFlags, &ComStat ) ;
       dwLength = min( (DWORD) nMaxLength, ComStat.cbInQue ) ;   if (dwLength > 0)
       {
          fReadStat = ReadFile( comDev.m_hCom, lpszBlock,
                        dwLength, &dwLength, &(comDev.m_rdos) ) ;
      if(dwLength!=0)
      dwLength=32;
          if (!fReadStat)
          {
             if (GetLastError() == ERROR_IO_PENDING)
             {
                OutputDebugString("\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( comDev.m_hCom ,
                   &(comDev.m_rdos), &dwLength, TRUE ))
                {
                   dwError = GetLastError();
                   if(dwError == ERROR_IO_INCOMPLETE)
                      // normal result if not finished
                      continue;
                   else
                   {
                      // an error occurred, try to recover
                      wsprintf( szError, "<CE-%u>", dwError ) ;
                      ClearCommError( comDev.m_hCom , &dwErrorFlags, &ComStat ) ;
                      break;
                   }            }       }
             else
             {
                // some other error occurred
                dwLength = 0 ;
                ClearCommError( comDev.m_hCom , &dwErrorFlags, &ComStat ) ;
             }
          }
       }   return ( dwLength ) ;}