一个关于ApdComPort的奇怪问题

iwillgo2 2003-10-09 03:38:59
用ApdComPort发送接收数据,有两种处理方式,一是我自己等待接收,二是通过Trigger事件接收处理,同一个程序里我两种情况都使用了,不知道是否有限制Trigger事件触发的属性,我是通过标志来判断是否处理Trigger事件。
同样一个函数,执行所用的时间相差很大,单独执行用0.05秒时间,但放在下面Trigger事件理执行用3-4秒的时间,为什么相差那么多?下面把函数,和Trigger事件列出来:
函数体:
function TF_Watch.LawLess(StrAddr:String):Boolean;
var
SendBuf1:array[0..0] of byte;
SendBuf2:array[0..2] of byte;
RecBuf:array of Byte;
Timer1:TTime;
IntI:Integer;
begin
Result:=False;
SendBuf1[0]:=StrToInt(StrAddr);
ComPort1.PutBlock(SendBuf1,length(SendBuf1));

Timer1:=Time;
Repeat
Application.ProcessMessages;
until ((Time-Timer1) * 86400 >=10) Or (ComPort1.InBuffUsed>=2);
if ComPort1.InBuffUsed=0 then Exit;
try
SetLength(RecBuf,ComPort1.InBuffUsed);
for IntI:=0 to ComPort1.InBuffUsed-1 do
begin
RecBuf[IntI]:=ord(ComPort1.GetChar);
end;

except
end;
end;

Trigger事件:
APW_TRIGGERAVAIL :
begin
try
if BoolFlag<>True then Exit;//判断是否处理(因为有部分函数的数据有我自己等待处理,不在事件里处理)
if Data<10 then//等待接收完数据
begin
StrCommand:='S';
Exit;
end;
SetLength(RecBuf,Data);
for IntI:=0 to Data-1 do
begin
RecBuf[IntI]:=ord(ComPort1.GetChar);
end;
//...其它操作
BoolFlag:=False;
a:=Time;
LawLess('18');
Caption:=FloatToStr((Time-a)*86400);
end;

Button3Click:
begin
BoolFlag:=False;
a:=Time;
LawLess('18');
Caption:=FloatToStr((Time-a)*86400);//时间显示0.05秒
end;
...全文
63 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
jpyc 2003-10-09
  • 打赏
  • 举报
回复
不要混用,问题很多。

参考一下我的一个工程的流程:

http://218.56.11.178:8009/Browers.aspx?FID=689

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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