自己写了一个串口接收下位机程序,就光接收,发现数据出错,希望高手看看指导下啊。
 while (Dlg->m_hSerial != INVALID_HANDLE_VALUE) 
       {
           //等待串口的事件发生
   WaitCommEvent (Dlg->m_hSerial, &dwCommModemStatus, 0);
 
              if (dwCommModemStatus & EV_RXCHAR) 
  {
  Sleep(100);                
  ClearCommError(Dlg->m_hSerial,&dwErrorFlags,&ComStat);
                 //cbInQue返回输入缓冲区中的字符数
                 dwLength=ComStat.cbInQue;                 if(dwLength>0)
                 {
                    //从串口读取数
do
{
  fReadState=ReadFile(Dlg->m_hSerial,&Byte,1,&dwBytes,NULL);
  if(!fReadState)
  {
                              
      MessageBox(NULL,TEXT("无法读取数据"),TEXT("错误"),MB_OK);

  }

  if(dwBytes==1)
{ ReceiveBuf[iCounter++] = Byte;
if(iCounter == 10)
{
 iCounter=0;
}
strInChar=ReceiveBuf; }
 }while (dwBytes == 1);
      
                  }     
               }
              
              GetCommModemStatus (Dlg->m_hSerial, &dwCommModemStatus);
       }
下面是初始化串口
       PortDCB.DCBlength = sizeof (DCB);     
 
       //得到端口的默认设置信息
       GetCommState (m_hSerial, &PortDCB);
 
       //改变DCB结构设置
       PortDCB.BaudRate = CBR_115200; //波特率 
       PortDCB.fParity = FALSE; //无奇偶校验 
       PortDCB.fNull = TRUE; //设为TRUE将使串行驱动程序忽略收到的空字节 
       PortDCB.ByteSize = 8; //每字节的位数 
       PortDCB.Parity = NOPARITY; //无奇偶校验 
       PortDCB.StopBits = ONESTOPBIT; //每字节一位停止位 
 
       //根据DCB结构配置端口 
       if (!SetCommState (m_hSerial, &PortDCB))
       {
               dwError = GetLastError ();
  //不能配置串行端口
              MessageBox (_T("配置串口失败"),  _T("错误"), MB_OK | MB_ICONWARNING);
             
              return FALSE;
       }
   COMMTIMEOUTS CommTimeouts;
   GetCommTimeouts(m_hSerial,&CommTimeouts);
   CommTimeouts.ReadIntervalTimeout = MAXDWORD; 
   CommTimeouts.ReadTotalTimeoutMultiplier = 0; 
       CommTimeouts.ReadTotalTimeoutConstant = 0;    
       CommTimeouts.WriteTotalTimeoutMultiplier = 50; 
       CommTimeouts.WriteTotalTimeoutConstant = 100; 

解决方案 »

  1.   

    我写了个只用MSCOMM控件接收数据的代码,也是想用线程实现,可总是实现不了,楼主代码好了能发我一下参考吗?谢谢!
      

  2.   

    int ReaddwBytes;
    do
    {
    fReadState=ReadFile(Dlg->m_hSerial,&Byte,1,&ReaddwBytes,NULL);//改动这块,换成ReaddwBytes这个变量
    if(!fReadState)
    {
      
    MessageBox(NULL,TEXT("无法读取数据"),TEXT("错误"),MB_OK);}
    /*全部这么改
    if(dwBytes==1)//这句不对,改成:
    if(fReadState==1)//fReadState返回的是实际读取的字节数
    { ReceiveBuf[iCounter++] = Byte;
    if(iCounter == 10)
    {
    iCounter=0;
    }
    strInChar=ReceiveBuf;}
    */
    iCounter=0;
    for(int i=0;i<fReadState;i++)
    {
        ReceiveBuf[iCounter++] = Byte;
    }
    if(iCounter >= 10)
    {
    iCounter=0;
    }
    strInChar=ReceiveBuf;
    dwBytes=0;}while (dwBytes >0);
      

  3.   

    我有一个“带流控制的串口通讯程序.rar” 0分
    http://download.csdn.net/detail/schlafenhamster/4195462
    是一个用线程接受或发生的。
      

  4.   

    “就光接收”那就看 “class CUload : public CDialog”
      

  5.   

    fReadStart=ReadFile(m_idComDev,pBlock,dwLength,&dwLength,&m_osRead);
    if(!fReadStart)
    { // if there was a problem, or the async. operation's still pending ... 
    if(GetLastError()==ERROR_IO_PENDING)
    { // asynchronous i/o is still in progress 
    if (WaitErr=WaitForSingleObject(m_osRead.hEvent,60000))// 1 minute
    {  // time over
    if(WaitErr==WAIT_TIMEOUT) 
    {  // time out
    dwLength=0;
    AfxMessageBox("Time out !");
    }// end time out
    }// end waiterr
    } // end io_pending
    else
    { // WaitErr=0.if you SetEvent(m_osRead.hEvent) anywhere else
    GetOverlappedResult(m_idComDev,&m_osRead,&dwLength,FALSE);
    m_osRead.Offset +=dwLength;
    }
    }
    接收到数据后pBlock没处理吗?
      

  6.   

    串口DCB配置放到读数据之前。
    确认波特率、数据位、停止位等参数是否与设备的一致。
      

  7.   

    在CUload::OnCommNotify();case 1 中ReadCommBlock()
    读到后:
    uldFile.Write()就把文件写入 uldFile (UpLoadFile) 中了。并没有处理和显示。