我调试的时候能正确写数据,就是读数据的时候多了一些乱吗
大侠们帮我看一下这个读读串口数据函数哪有问题
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 (!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 ) ;}

解决方案 »

  1.   

    // attempt an asynchronous read operation
    bResult = ReadFile(hFile, &inBuffer, nBytesToRead, &nBytesRead, 
        &gOverlapped) ; 
     
    // if there was a problem, or the async. operation is still pending. 
    if (!bResult) 

        // deal with the error code 
        switch (dwError = GetLastError()) 
        { 
            case ERROR_HANDLE_EOF: 
            { 
                // we have reached the end of the file 
                // during the call to ReadFile 
     
                // code to handle that 
            } 
     
            case ERROR_IO_PENDING: 
            { 
                // asynchronous i/o is still in progress 
     
                // do something else for a while 
                GoDoSomethingElse() ; 
     
                // check on the results of the asynchronous read 
                bResult = GetOverlappedResult(hFile, &gOverlapped, 
                    &nBytesRead, FALSE) ; 
     
                // if there was a problem ... 
                if (!bResult) 
                { 
                    // deal with the error code 
                    switch (dwError = GetLastError()) 
                    { 
                        case ERROR_HANDLE_EOF: 
                        { 
                            // we have reached the end of
                            // the file during asynchronous
                            // operation
                        } 
     
                        // deal with other error cases 
                    }   //end switch (dwError = GetLastError()) 
                 } 
            } // end case 
     
            // deal with other error cases, such as the default 
     
        } // end switch (dwError = GetLastError()) 
     } // end if发个msdn上的例子,可以对比下,不知道能否有些启发
      

  2.   

        ReadFile( comDev.m_hCom, lpszBlock,dwLength, &dwLength, &(comDev.m_rdos) ) ;
       中dwLength参数是从串口中读取的字节数, &dwLength是实际读出的字节数。这里是不是有问题呢?
      

  3.   

    哎总算是问题解决了,不是读函数问题,而是缓冲区的问题。读数据之前我加了下面的代码就OK了!呵呵
             unsigned int i;
    for(i=0;i<1024;i++)
    {
      buffer[i]='\0';
    }