用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;