unit Kommunikation;
interface
uses
Globals,
DataModul,
tbUtils,
Winapi.Windows,
Winapi.Messages,
System.SysUtils,
System.Variants,
System.Classes,
System.Threading,
Vcl.Controls,
Vcl.ExtCtrls,
Vcl.Forms;
type
TMaschinenDaten = record
MaschineOnline: Boolean;
end;
TOnDataEvent = procedure(Sender: TObject; Value: TMaschinendaten) of object;
TMaschine = class(TObject)
private
FOwner: TComponent;
FHostIP: WideString;
FActive: Boolean;
PollTimer: TTimer;
FOpcTask: ITask;
FOnData: TOnDataEvent;
FOnTaskStart: TNotifyEvent;
FOnTaskReady: TNotifyEvent;
FInterval: Cardinal;
FMaschinenID: Integer;
FMaschinenName: WideString;
FMaschinendaten: TMaschinendaten;
procedure SetHostIp(Value: WideString);
procedure SetActive(Value: Boolean);
procedure SetInterval(Value: Cardinal);
procedure ReadOpcData;
procedure OnPollTimer(Sender: TObject);
procedure OnOpcData(Sender: TObject; Value: TMaschinendaten);
procedure OnOpcTaskStart(Sender: TObject);
procedure OnOpcTaskReady(Sender: TObject);
public
constructor Create(AOwner: TComponent); virtual;
destructor Destroy; override;
property HostIP: WideString read FHostIp write SetHostIp;
property Active: Boolean read FActive write SetActive;
property OnData: TOnDataEvent read FOnData write FOnData;
property OnTaskStart: TNotifyEvent read FOnTaskStart write FOnTaskStart;
property OnTaskReady: TNotifyEvent read FOnTaskReady write FOnTaskReady;
property PollInterval: Cardinal read FInterval write SetInterval;
property MaschinenID: Integer read FMaschinenID write FMaschinenID;
property MaschinenName: WideString read FMaschinenName write FMaschinenName;
end;
implementation
(*** TMaschine *********************************************************************************************************************************************************************)
constructor TMaschine.Create(AOwner: TComponent);
begin
FOwner := AOwner;
PollTimer := TTimer.Create(FOwner);
PollTimer.Interval := FInterval;
PollTimer.OnTimer := OnPollTimer;
PollTimer.Enabled := False;
end;
destructor TMaschine.Destroy;
begin
PollTimer.Enabled := False;
Active := False;
inherited;
end;
procedure TMaschine.SetInterval(Value: Cardinal);
begin
FInterval := Value;
PollTimer.Interval := Value;
end;
procedure TMaschine.OnPollTimer(Sender: TObject);
begin
ReadOpcData;
end;
procedure TMaschine.SetHostIp(Value: WideString);
begin
FHostIp := Value;
end;
procedure TMaschine.SetActive(Value: Boolean);
begin
FActive := Value;
PollTimer.Enabled := Value;
end;
procedure TMaschine.ReadOpcData;
var
FirstTickCount: DWord;
begin
if FActive then begin
PollTimer.Enabled := False;
OnOpcTaskStart(Self);
FOpcTask := TTask.Create(procedure begin
{*** HIER WERDEN DIE OPC-DATEN DANN GELESEN DIES IST NUR EINE DUMMYROUTINE UM ZEIT ZU VERBRAUCHEN ***}
FirstTickCount := GetTickCount;
while ((GetTickCount - FirstTickCount) < 1000) do begin
Application.ProcessMessages;
if Application.Terminated then
Exit;
end;
{****************************************************************************************************}
end);
FOpcTask.Start;
OnOpcTaskReady(Self);
OnOpcData(Self, FMaschinendaten);
PollTimer.Enabled := True;
end;
end;
procedure TMaschine.OnOpcTaskStart(Sender: TObject);
begin
if Assigned(FOnTaskStart) then
FOnTaskStart(Self);
end;
procedure TMaschine.OnOpcTaskReady(Sender: TObject);
begin
if Assigned(FOnTaskReady) then
FOnTaskReady(Self);
end;
procedure TMaschine.OnOpcData(Sender: TObject; Value: TMaschinendaten);
begin
if Assigned(FOndata) then
FOnData(Self, FMaschinendaten);
end;
(**********************************************************************************************************************************************************************************)
end.