⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.pas

📁 VUOPCclient Trial version
💻 PAS
字号:
unit Main;

interface

uses
  Variants, Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, VUOPCclient, ComCtrls, StdCtrls;

type
  TfMain = class(TForm)
    VUOPCclient1: TVUOPCclient;
    ComboBox1: TComboBox;
    Button1: TButton;
    Button2: TButton;
    ListBox1: TListBox;
    CheckBox1: TCheckBox;
    Edit2: TEdit;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    GroupBox1: TGroupBox;
    Button3: TButton;
    Edit1: TEdit;
    Button7: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    ListBox2: TListBox;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure VUOPCclient1Error(Sender: TObject; NError: Integer;
      Message: String);
    procedure Button4Click(Sender: TObject);
    procedure VUOPCclient1DataChange(Sender: TObject; Group: TVUOPCGroup;
      Item: TVUOPCItem);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
  private
    { Private declarations }
    Group: TVUOPCGroup;
  public
    { Public declarations }
  end;

var
  fMain: TfMain;

implementation

{$R *.dfm}

procedure TfMain.Button1Click(Sender: TObject);
begin
  ComboBox1.Items.Assign(VUOPCclient1.ServerList);
  ComboBox1.ItemIndex:=0;
end;

procedure TfMain.Button2Click(Sender: TObject);
var
  i:integer;
  Item:TVUOPCItem;
begin
  if Button2.Tag=0 then begin
    VUOPCclient1.RootOmitted:=CheckBox1.Checked;
    VUOPCclient1.OPCName:=ComboBox1.Text;
    VUOPCclient1.Connect:=true;
    if not VUOPCclient1.Connect then begin
      MessageBox(0,'OPC-server not found.','Error',MB_OK);
      exit;
    end;
    sleep(500);
    VUOPCclient1.Browse;
    for i:=0 to VUOPCclient1.ItemList.Count-1 do begin
      Item:=VUOPCclient1.ItemList.Item[i];
      ListBox1.Items.AddObject(Item.Name,Item);
    end;
    Button2.Tag:=1;
    Button2.Caption:='Disconnect';
    Group:=nil;
    ListBox1.SetFocus;
  end else begin
    ListBox1.Items.Clear;
    VUOPCclient1.Connect:=false;
    Button2.Tag:=0;
    Button2.Caption:='Connect';
  end;
end;

procedure TfMain.ListBox1Click(Sender: TObject);
var
  i:integer;
  Item:TVUOPCItem;
  s:string;
  V:Variant;
  j,k:integer;
begin
  if ListBox1.ItemIndex<0 then exit;
  if Group=nil then begin
    MessageBox(0,'The group is not created.','Warning',MB_OK);
    exit;
  end;
  Label1.Caption:='';
  Label2.Caption:='';
  Label3.Caption:='';
  ListBox2.Clear;
  Item:=Group.Items.ItemOf(ListBox1.Items.Strings[ListBox1.ItemIndex]);
  if Item=nil then exit;
  VarCopy(v,Item.Value);
  if VarIsArray(v) then begin
    s:='It is a array of values.';
  end else begin
    try
      s:=VarToStr(v);
    except
      s:='Error of transformation.';
    end;
  end;
  Label1.Caption:='Value = '+s;
  Label2.Caption:='Time = '+DateTimeToStr(Item.TimeStamp);
  s:=Format('%4.4x',[Item.Quality]);
  Label3.Caption:='Quality = '+s;
  GroupBox1.Caption:=Item.Name;
  if Item.PropertyList.Count=0 then begin
    ListBox2.Items.Add('The properties of item are absent.');
  end else begin
    for i:=0 to Item.PropertyList.Count-1 do begin
      s:='Property '+Item.PropertyList.Propertys[i].Description;
      s:=s+'  ID='+IntToStr(Item.PropertyList.Propertys[i].ID);
      s:=s+'  V='+IntToStr(Item.PropertyList.Propertys[i].Value);
      ListBox2.Items.Add(s);
    end;
  end;
end;

procedure TfMain.Button3Click(Sender: TObject);
var
  v:Variant;
  Item:TVUOPCItem;
  s:string;
  i:integer;
begin
  try
    v:=Edit1.Text;
    if ListBox1.ItemIndex<0 then exit;
    Item:=Group.Items.ItemOf(ListBox1.Items.Strings[ListBox1.ItemIndex]);
    if Group.WriteItemValue(Item,v) then begin
      MessageBox(0,'Write is successfully.','Information',MB_OK);
      ListBox1Click(Sender);
    end else begin
      MessageBox(0,'Write error.','Information',MB_OK);
    end;
  except end;
  ListBox1.SetFocus;
end;

procedure TfMain.VUOPCclient1Error(Sender: TObject; NError: Integer;
  Message: String);
var
  s:string;
begin
  s:=Format('%4.4x',[NError]);
  MessageBox(0,PCHAR(Message),PCHAR('Error 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -