计算机串口通信中,如何监测上一次IO写操作是否已经完成?或者如何监测串口已无数据,可以发送后续数据

解决方案 »

  1.   

    你检查一下串口那里的Buffer.
    缓冲区
    ----------------------------------------
    相信自己,相信明天!快给我分,不然我抢的啦^_^
    ----------------------------------------
                           时光.漫步
      

  2.   

    可以用COMSTAT ComStat;
    ComStat.cbInQue看它是否是空。
      

  3.   

    //配置Comm2口   DCB dcb;
       HANDLE hCom;
       BOOL fSuccess;
       char *pcCommPort = "COM2";   hCom = CreateFile( pcCommPort,
                        GENERIC_READ | GENERIC_WRITE,
                        0,    // must be opened with exclusive-access
                        NULL, // no security attributes
                        OPEN_EXISTING, // must use OPEN_EXISTING
                        0,    // not overlapped I/O
                        NULL  // hTemplate must be NULL for comm devices
                        );   if (hCom == INVALID_HANDLE_VALUE) 
       {
           // Handle the error.
           printf ("CreateFile failed with error %d.\n", GetLastError());
           return (1);
       }   // Build on the current configuration, and skip setting the size
       // of the input and output buffers with SetupComm.   fSuccess = GetCommState(hCom, &dcb);   if (!fSuccess) 
       {
          // Handle the error.
          printf ("GetCommState failed with error %d.\n", GetLastError());
          return (2);
       }   // Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.   dcb.BaudRate = CBR_57600;     // set the baud rate
       dcb.ByteSize = 8;             // data size, xmit, and rcv
       dcb.Parity = NOPARITY;        // no parity bit
       dcb.StopBits = ONESTOPBIT;    // one stop bit
       dcb.fAbortOnError=TRUE;       //出错调用ClearCommError();   fSuccess = SetCommState(hCom, &dcb);   if (!fSuccess) 
       {
          // Handle the error.
          printf ("SetCommState failed with error %d.\n", GetLastError());
          return (3);
       }   printf ("Serial port %s successfully reconfigured.\n", pcCommPort);  //出错时候的代码:
       COMSTAT comsat;
       DWORD dwError;
     
       ClearCommError(hComm,&dwError,comsat);
       int  m_nDataRemain=comsat.cbOutQue;//获得没有发送的数据个数
       
      

  4.   

    可以通过TRY/CATCH捕捉串口引起的错误
    TRY
    {
        //发送数据
       WriteFile(hComm,databuf,datalen,nSentCount,NULL);}
    CATCH(CException, e)
    {
        COMSTAT comsat;
        DWORD dwError;
     
        ClearCommError(hComm,&dwError,comsat);
        int  m_nDataRemain=comsat.cbOutQue;//获得没有发送的数据个数
    }
    END_CATCH
      

  5.   

    我可能没有说清楚,我是想确认串口缓冲区中已无数据,再发送后续数据,以免覆盖上次未发完的数据。而不是发送完后,检查是否有数据未发。
    我使用的是CSerialPort类,它发送串口数据的时候,没有检查串口缓冲区是否有数据,而是直接发送,不管覆不覆盖。
      

  6.   

    为什么不用MsComm控件呢???
      

  7.   

    CSerialPort是多线程异步串口通信类,用起来方便,主要是用到它的多线程
      

  8.   

    上这儿察看是否有启发:
    http://www.vckbase.com/SYS/script/viewcomment.asp?gclsid=100&itemid=612
      

  9.   

    在接收数据的端口看ComStat.cbOutQue是否为空
      

  10.   

    串口发送根本就没有什么缓冲区,是一个链表,把每个WriteFile的数据都串起来了.
    要查看是否已经发送完,用SetWaitMask,在一个线程里用WaitCommEvent