IUfMessung = interface
['{6C258E04-BCC9-4349-912B-57A38F103570}']
    function MacheMessung(Ifl,Ufl: double): double;
end;

  TUFMessungMitHalten = class(TInterfacedObject,IUfMessung)
  private
    SWZeitHalten: double;
  public
    constructor Create(ASWZeitHalten: double); // Time to keep
    destructor Destroy; override;
    function MacheMessung(Ifl,Ufl: double): double; // do measuring
  end; //   measuring with holding

  TUFMessungMitPause = class(TInterfacedObject,IUfMessung)
  private
    SWZeitPause: double;
    IfMin: double;
  public
    constructor Create(ASWZeitPause: double; AIfMin: double); // Time to keep + I[A]
    destructor Destroy; override;
    function MacheMessung(Ifl,Ufl: double): double;
  end; //   measuring with Pause

  TUFMessung = class(TInterfacedObject)
  private
    //...
    Messungsart: IUfMessung;
  public
    procedure SetMessungsart(art: IUfMessung); // set measuring type
    procedure MessungsArtAswahl; // measuring type choice
    //...
  end; // do measuring

{ TUFMessung }
    procedure TUFMessung.MessungsArtAswahl;
    begin
        if  not FMitHalten and not FMitPause then  // Uf simple measuring
      begin
        SetMessungsart(TUFMessungEinfach.Create);
      end;

      if FMitHalten and not FMitPause then   // Uf with holding
      begin
        SetMessungsart(TUFMessungMitHalten.Create(FSWZeitHalten));
      end;

      if  not FMitHalten and FMitPause then  // Uf with pause
      begin
        SetMessungsart(TUFMessungMitPause.Create(FSWZeitPause,      FStartIf));
      end;
    end;

procedure TUFMessung.Start(StartIf, EndIf, StepIf, Uleer: double);
begin
//...
  while not FIstFertig and FUfKannStart do
  begin
    Uf:= Messungsart.MacheMessung(Ifl, FUleer); // <= CALL the measuring
    //...
  end;
end;

{ TUFMessungMitHalten }
function TUFMessungMitHalten.MacheMessung(Ifl, Ufl: double): double;
var i_Zeit: integer;
begin    // Messvorgang
  hole_Uf(true, Ifl, Ufl); // set value
  i_Zeit:= Trunc(SWZeitHalten*1000);
  Application.ProcessMessages;
  Sleep(i_Zeit);  // wait Time ms.
  result:= hole_Uf(false, Ifl, Ufl); // measuring
end;

{ TUFMessungMitPause }
function TUFMessungMitPause.MacheMessung(Ifl, Ufl: double): double;
var i_Zeit: integer;
begin  // Messvorgang
  result:= hole_Uf(false, Ifl, Ufl); // measuring
  hole_Uf(true, IfMin, Ufl); // set value
  i_Zeit:= Trunc(SWZeitPause*1000);
  Application.ProcessMessages;
  Sleep(i_Zeit);  // wait Time ms.
end;


我需要在测量过程之间等待0到5秒的时间。使用sleep()的解决方案仅在1秒钟之前有效,因为我在程序中具有RS232通信和一些计时器。
是否有sleep()函数的替代方法,以便该程序恰好在某个时间正在等待?先感谢您。

最佳答案

我无法从您的消息来源得知,但是如果您将RS232与等待相结合,睡眠听起来是个坏主意。最好的办法是让系统在数据输入后立即响应您,而不是盲目等待。根据您用于进行RS232通信的方式,您应该查找类似SetCommTimeouts的内容,并微调读取操作的行为:如果数据尚未输入,则暂停接收超时,然后响应为零字节收到。最好通过专用线程来完成(这可能需要一点学习才能掌握)。另一种选择是使用异步调用(这还需要一些学习才能掌握)。

07-24 17:33
查看更多