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.
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 就是你想要的
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;
if cs.cbInQue = 0 then Exit; if cs.cbInQue > sizeof(inbuff) then//---------------------------------------------------// 这两句是 if cs.cbInQue = 0 then Exit; if cs.cbInQue > sizeof(inbuff) then 我想弄成红色的,结果成那样了
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.
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 就是你想要的
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;
if cs.cbInQue > sizeof(inbuff) then//---------------------------------------------------//
这两句是
if cs.cbInQue = 0 then Exit;
if cs.cbInQue > sizeof(inbuff) then
我想弄成红色的,结果成那样了