如题:
    在做串口通信的时候,比如: 当我往串口里面发送很多条数据的数据的时候,当缓冲区的数据全部被取走的时候,我再继续发送?请问一下,我怎么知道缓冲区里面数据还剩下有多少个字节啊?

解决方案 »

  1.   

    BOOL ClearCommError(
      HANDLE hFile,     // handle to communications device
      LPDWORD lpErrors, // error codes
      LPCOMSTAT lpStat  // communications status
    );
    typedef struct _COMSTAT { 
      DWORD fCtsHold : 1; 
      DWORD fDsrHold : 1; 
      DWORD fRlsdHold : 1; 
      DWORD fXoffHold : 1; 
      DWORD fXoffSent : 1; 
      DWORD fEof : 1; 
      DWORD fTxim : 1; 
      DWORD fReserved : 25; 
      DWORD cbInQue; 
      DWORD cbOutQue; 
    } COMSTAT, *LPCOMSTAT; 
    cbInQue 
      Number of bytes received by the serial provider but not yet read by a ReadFile operation. 
    cbOutQue 
      Number of bytes of user data remaining to be transmitted for all write operations. This value will be zero for a nonoverlapped write. 
      

  2.   

    BOOL ClearCommError(
      HANDLE hFile,     // handle to communications device
      LPDWORD lpErrors, // error codes
      LPCOMSTAT lpStat  // communications status
    );COMSTAT
    The COMSTAT structure contains information about a communications device. This structure is filled by the ClearCommError function. typedef struct _COMSTAT { 
      DWORD fCtsHold : 1; 
      DWORD fDsrHold : 1; 
      DWORD fRlsdHold : 1; 
      DWORD fXoffHold : 1; 
      DWORD fXoffSent : 1; 
      DWORD fEof : 1; 
      DWORD fTxim : 1; 
      DWORD fReserved : 25; 
      DWORD cbInQue; 
      DWORD cbOutQue; 
    } COMSTAT, *LPCOMSTAT; cbInQue 
       Number of bytes received by the serial provider but not yet read by a ReadFile operation. 
    cbOutQue 
       Number of bytes of user data remaining to be transmitted for all write operations. This value will be zero for a nonoverlapped write. 调用ClearCommError获得当前串口状态,cbInQue和cbOutQue 就是你想要的
      

  3.   

    procedure TForm1.tmReceiveTimer(Sender: TObject);
    var
      Temp : String;
      inbuff : array[0..2047] of Char;
      nBytesRead, dwError : DWORD;
      cs : TCOMSTAT;
    begin
      ClearCommError(hComm, dwError, @cs);
      //若未接收到数据或者接收的数据大于缓冲区大小责推出
      if cs.cbInQue = 0 then Exit;
      if cs.cbInQue > sizeof(inbuff)
     then
        begin
          PurgeComm(hComm, PURGE_RXCLEAR);
          Exit;
        end;  ReadFile(hComm, inbuff, cs.cbInQue, nBytesRead, nil);
      Temp := Copy(inbuff, 1, cs.cbInQue);
      mReceive.Text := mReceive.Text + Temp;
      //将memo组件的显示位置移到最下端
      mReceive.SetFocus;
      mReceive.SelStart := Length(mReceive.Text);
      mReceive.SelLength := 0;end;
      

  4.   

      if cs.cbInQue = 0 then Exit;
      if cs.cbInQue > sizeof(inbuff)
     then//---------------------------------------------------//
    这两句是
    if cs.cbInQue = 0 then Exit;
    if cs.cbInQue > sizeof(inbuff) then
    我想弄成红色的,结果成那样了
      

  5.   

    为什么俺引用了这个接收过程,却什么都收不到,cbInQue总是为0