-
Notifications
You must be signed in to change notification settings - Fork 1
/
Unit_main.pas
116 lines (99 loc) · 3.48 KB
/
Unit_main.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
unit Unit_main;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
dOPCDA, dOPC, Vcl.StdCtrls, Vcl.ExtCtrls;
type
TForm_Main = class(TForm)
Timer_ReaderTags: TTimer;
Timer_Starter: TTimer;
Memo_Tags: TMemo;
procedure Timer_ReaderTagsTimer(Sender: TObject);
procedure Timer_StarterTimer(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form_Main: TForm_Main;
OPCServer: TdOPCDAServer; // Êîìïîíåíò äîñòóïà ê OPC-ñåðâåðó
OPCGroup: TdOPCGroup; // Ãðóïïà òåãîâ äëÿ OPC-ñåðâåðà
const
StartDelay = 500; // Âðåìÿ äëÿ îòðèñîâêè GUI-îêíà ïåðåä àêòèâàöèåé OPC
UpdateRate = 2000; // Âðåìÿ îáíîâëåíèÿ äàííûõ ñ OPC-ñåðâåðà
DoubleFormat = '########0.0000'; // Ôîðìàò âûâîäà çíà÷åíèé òåãîâ
implementation
{$R *.dfm}
procedure TForm_Main.Timer_StarterTimer(Sender: TObject);
// Èíèöèàëèçàöèÿ äîñòóïà ê OPC-ñåðâåðó
begin
try
Timer_Starter.Enabled := false;
OPCServer := TdOPCServer.Create(nil);
OPCServer.ComputerName := 'application'; // Èìÿ êîìïüþòåðà ñ OPC-ñåðâåðîì
OPCServer.ServerName := '{C3B72AB1-6B33-11D0-9007-0020AFB6CF9F}'; // Èìÿ OPC-ñåðâåðà
OPCGroup := OPCServer.OPCGroups.Add('test'); // Èìÿ ñîçäàâàåìîé ãðóïïû äëÿ OPC-ñåðâåðà
screen.Cursor := crHourGlass;
OPCServer.Active := true; // Àêòèâàöèÿ ïîäêëþ÷åíèÿ ê OPC-ñåðâåðó
screen.Cursor := crDefault;
Application.ProcessMessages;
Randomize;
Timer_ReaderTags.Enabled := true;
except
showmessage('Îøèáêà ïîäêëþ÷åíèÿ ê OPC-ñåðâåðó! Âûõîä èç ïðîãðàììû');
close;
end;
end;
procedure TForm_Main.Timer_ReaderTagsTimer(Sender: TObject);
// ×òåíèå äàííûõ ñ OPC-ñåðâåðà
var
ItemList: TdOPCItemList;
Item: TdOPCItem;
s: string; i: integer;
begin
try
Memo_Tags.Lines.Clear;
OPCGroup.OPCItems.Clear;
if random(10) > 2 then OPCGroup.OPCItems.AddItem('PT24_01/AI1/OUT.CV');
if random(10) > 3 then OPCGroup.OPCItems.AddItem('PT25_01/AI1/OUT.CV');
if random(10) > 4 then OPCGroup.OPCItems.AddItem('PT26_01/AI1/OUT.CV');
if OPCGroup.OPCItems.Count<1 then OPCGroup.OPCItems.AddItem('PT24_01/AI1/OUT.CV');
ItemList := TdOPCItemList.Create(OPCGroup.OPCItems);
if OPCGroup.SyncRead(ItemList, false) = true then begin
s := '';
for i := 0 to ItemList.Count - 1 do begin
Item := ItemList[i];
s := s + Item.ItemID + ': ' + formatfloat(DoubleFormat, Item.Value) +
#$0D + #$0A;
end;
Memo_Tags.Text := s;
end
else begin Memo_Tags.Text := ''; abort; end;
ItemList.Free;
Application.ProcessMessages;
except
Timer_ReaderTags.Enabled := false;
showmessage('Îøèáêà ÷òåíèÿ òåãîâ ñ OPC-ñåðâåðà! Âûõîä èç ïðîãðàììû');
OPCServer.Free; // Îòêëþ÷åíèå îò OPC-ñåðâåðà
Application.Terminate;
end;
end;
procedure TForm_Main.FormShow(Sender: TObject);
// Èíèöèàëèçàöèÿ ïðîãðàììû
begin
Timer_Starter.Enabled := false;
Timer_ReaderTags.Enabled := false;
Timer_Starter.Interval := StartDelay;
Timer_ReaderTags.Interval := UpdateRate;
Timer_Starter.Enabled := true;
end;
procedure TForm_Main.FormClose(Sender: TObject; var Action: TCloseAction);
// Çàêðûòèå ïðîãðàììû
begin
OPCServer.Free; // Îòêëþ÷åíèå îò OPC-ñåðâåðà
end;
end.