请问一下 在做串口通信的时候,如何获取当前发送或接收缓冲区的大小?

saver_gao 2009-02-19 11:22:51
如题:
在做串口通信的时候,比如: 当我往串口里面发送很多条数据的数据的时候,当缓冲区的数据全部被取走的时候,我再继续发送?请问一下,我怎么知道缓冲区里面数据还剩下有多少个字节啊?
...全文
590 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
gxw145 2010-06-11
  • 打赏
  • 举报
回复
?????
Storm2008 2009-03-13
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 mypiip 的回复:]
引用 4 楼 Storm2008 的回复:
Delphi(Pascal) codeprocedureTForm1.tmReceiveTimer(Sender: TObject);varTemp : String;
inbuff :array[0..2047]ofChar;
nBytesRead, dwError : DWORD;
cs : TCOMSTAT;beginClearCommError(hComm, dwError, @cs);//若未接收到数据或者接收的数据大于缓冲区大小责推出
ifcs.cbInQue=0thenExit;ifcs.cbInQue>sizeof(inbuff)thenbeginPurgeComm(hComm, PURGE_RXCLEAR);
Exi…
[/Quote]


应该是你没收到什么数据
先用串口调试助手一类的软件看看能不能正常接受到数据
再看看你的串口设置
mypiip 2009-03-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 Storm2008 的回复:]
Delphi(Pascal) codeprocedureTForm1.tmReceiveTimer(Sender: TObject);varTemp : String;
inbuff :array[0..2047]ofChar;
nBytesRead, dwError : DWORD;
cs : TCOMSTAT;beginClearCommError(hComm, dwError, @cs);//若未接收到数据或者接收的数据大于缓冲区大小责推出
ifcs.cbInQue=0thenExit;ifcs.cbInQue>sizeof(inbuff)thenbeginPurgeComm(hComm, PURGE_RXCLEAR);
Exit;end;

ReadFi…
[/Quote]
为什么俺引用了这个接收过程,却什么都收不到,cbInQue总是为0
saver_gao 2009-02-20
  • 打赏
  • 举报
回复
谢谢了,结贴。
Storm2008 2009-02-19
  • 打赏
  • 举报
回复
if cs.cbInQue = 0 then Exit;
if cs.cbInQue > sizeof(inbuff)
then

//---------------------------------------------------//
这两句是
if cs.cbInQue = 0 then Exit;
if cs.cbInQue > sizeof(inbuff) then
我想弄成红色的,结果成那样了
Storm2008 2009-02-19
  • 打赏
  • 举报
回复
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;
Storm2008 2009-02-19
  • 打赏
  • 举报
回复
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 就是你想要的
Storm2008 2009-02-19
  • 打赏
  • 举报
回复
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.
saver_gao 2009-02-19
  • 打赏
  • 举报
回复
路过的也帮忙顶一下 啊。。

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧