科技猫

导航

[转载]Delphi7.0开发OPC客户端程序(Siemens_Opc_client)

http://topic.csdn.net/u/20100322/14/677d20f6-4dd9-49b6-bfa0-4b28deba9c66.html

 

OPC是OLE for Process Control的缩写,即把OLE应用于工业控制领域,OPC是建立在OLE规范之上,它为工业控制领域提供了一种标准的数据访问机制。OPC服务器由三类对象组成:服务器(Server)、组(Group)、数据项(Item)。服务器对象(Server)拥有服务器的所有信息,同时也是组对象(Group)的容器。组对象(Group)拥有本组的所有信息,同时包容并逻辑组织OPC数据项(Item)。在这里数据项(Item)就是指所有的PLC信号的输入与输出。OPC组对象(Group)提供了客户组织数据的一种方法。客户可对之进行读写,还可以设置客户端的数据更新速率。当服务器缓冲区内的数据发生改变时,OPC将向客户发出通知,客户得到通知后再进行必要的处理。而无须浪费大量的时间进行查询。OPC规范定义了两种组对象:公共组和局部组(私有组)。公共组由多个客户共有,局部组只隶属于一个OPC客户。一般来说,客户和服务器的一对连接只需要定义一个组对象。在每个组对象中,客户可以加入多个OPC数据项(Item)。OPC数据项(Item)是服务器端定义的对象,通常指向设备的一个寄存器单元。OPC客户对设备寄存器的操作都是通过其数据项来完成的,通过OPC数据项(Item),OPC规范尽可能地隐藏了设备的特殊信息,也使OPC服务器的通用性大大增强。OPC数据项(Item)并不提供对外接口,客户不能直接对之进行操作,所有的操作都是通过组对象进行的。每个OPC数据项(Item)的数据结构包括三个成员变量:即数据值、数据质量和时间戳。数据值是以Variant形式表示的。

Delphi7.0加载Siemens动态库:
1.打开Delphi7.0,点击Project->Import Type Library
2.在Import Type Library里面找到Siemens OPC DAAutomation 2.0 (Version 1.0),在Class Nams里面可以看三个类方法(TOPCGroups,TOPCGroup,TOPCServer),默认的Palette Page为ActiveX,单击Install…

Delphi7.0中编写客户端程序:
1.连接OPC服务器
  在Form上面添加三个OPC控件:
  OPCGroup1: TOPCGroup;
  OPCGroups1: TOPCGroups;
  OPCServer1: TOPCServer;

Delphi(Pascal) code
var Group:OPCGroup; testItem:OPCItem; OPCItems:string; OPCItemsValues:string; OPCServer1.Connect1('OPC服务名称(Intellution.OPCiFIX.1)','结点名称(OPC主机名称)');//连接OPC服务器 OPCServer1.OPCGroups.DefaultGroupUpdateRate:=500;//设置更新频率 Group:=OPCServer1.OPCGroups.Add('MyGroup'); //添加Group testItem:=Group.OPCItems.AddItem('SCADA.YPA1_SILO_CHARGEWEIGHTACT.F_CV',1);//添加OPC数据项


2.读取OPC数据项
Delphi(Pascal) code
var myvalue,myquality,mytimestamp:OleVariant; testItem.Read(OPCDevice,myvalue,myquality,mytimestamp); Edit1.Text:=myvalue;


3.写OPC数据项
Delphi(Pascal) code
var myvalue:OleVariant; testItem.Write(myvalue);


4.断开OPC
Delphi(Pascal) code
OPCServer1.OPCGroups.RemoveAll; OPCServer1.Disconnect;


完整代码:
Delphi(Pascal) code
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, OPCSiemensDAAutomation_TLB, OleServer; type TForm1 = class(TForm) OPCGroup1: TOPCGroup; OPCGroups1: TOPCGroups; OPCServer1: TOPCServer; Button1: TButton; Edit1: TEdit; Button2: TButton; Button3: TButton; ListBox1: TListBox; ListBox2: TListBox; Button4: TButton; procedure Button1Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button4Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; Group:OPCGroup; testItem:OPCItem; OPCItems:string; OPCItemsValues:string; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin //连接OPC OPCServer1.Connect1('Intellution.OPCiFIX.1','OPSServer01'); OPCServer1.OPCGroups.DefaultGroupUpdateRate:=500; Group:=OPCServer1.OPCGroups.Add('MyGroup'); testItem:=Group.OPCItems.AddItem('SCADA.YPA1_SILO_CHARGEWEIGHTACT.F_CV',1); end; procedure TForm1.Button3Click(Sender: TObject); begin //断开OPC OPCServer1.OPCGroups.RemoveAll; OPCServer1.Disconnect; end; procedure TForm1.Button2Click(Sender: TObject); var myvalue,myquality,mytimestamp:OleVariant; begin //读OPC数据项 testItem.Read(OPCDevice,myvalue,myquality,mytimestamp); Edit1.Text:=myvalue; end; procedure TForm1.Button4Click(Sender: TObject); var myvalue:OleVariant; begin //写OPC数据项 testItem.Write(myvalue); end; end.


具体操作图文说明请到网盘下载

注:具体设备的OPC驱动虽然不一致,但是都有统一的标准,本文只是针对Siemens设备,如果有错误欢迎批评指教! 

 

posted on 2010-07-06 00:44  科技猫  阅读(1832)  评论(7编辑  收藏  举报