[信息与通信]手把手教您用VB编一个OPC客户端程序.ppt
《[信息与通信]手把手教您用VB编一个OPC客户端程序.ppt》由会员分享,可在线阅读,更多相关《[信息与通信]手把手教您用VB编一个OPC客户端程序.ppt(31页珍藏版)》请在三一办公上搜索。
1、Using Visual Basic As An OPC Client,John WeberPresident&FounderSoftware Toolbox,Iwebsite:http:/,Your OPC Server,Copyright Software Toolbox,1999,2000,2001 All Rights Reserved Worldwide.Software Toolbox is a registered trademark of Software Toolbox,Inc.,Presentation Updated 3/2001,Agenda,OPC-Review of
2、 the BasicsOPC-TerminologyOPC&VB 101-Automation Wrapper&Object ModelBuilding Your VB Client-Read data-7 easy steps with code from start to finishHousekeepingHandles Handles Everywhere-how the server and client correlate the data each is managing in your programWriting DataFuture ideas to considerNew
3、 developments since original presentation in 10/99Resources for future learning,OPC-Review of the Basics,OPC is based on the Microsoft Component Object Model(COM)OPC stands for OLE for Process ControlOPC is managed by the independent OPC Foundation(www.opcfoundation.org)There are over 200 member com
4、panies supporting the OPC specificationOPC defines a standard method for connecting automation application software,OPC and VB 101-Terminology,OPC Server-a piece of software supporting the OPC specification-an OPC Data Access Server is a driver that handles connectivity to PLCs or other automation h
5、ardwareOPC Item-A single tag or data point managed by the OPC server-i.e.one data point in a PLCOPC Group-a user defined grouping of OPC items.Created by the OPC client as a way of organizing data items around logical process areas or equipment.Collection-a grouping of objects of the same data type-
6、for example,each OPC server has an OPC items collection containing one software object for each OPC item.,OPC and VB 101,Visual Basic supports COMCOM implementations from Visual Basic use what is called an“Automation”interfaceThe OPC Foundation supplies the source code to an“Automation Wrapper”DLL-m
7、ost OPC vendors provide a compiled versionAutomation Wrapper connects VB to OPC,OPC and VB 101,Automation Wrapper DLL lets VB access OPC Servers and their underlying Groups and Items,Object model for the Automation Wrapper-the wrapper lets the VB user connect to an OPC server using standard Object.p
8、roperty and Object.method syntaxes,OPC and VB 101,Your VB Program with the Automation Wrapper object included in it,Your OPC Server and its object model,COM/DCOM,The Automation Wrapper connects to the OPC server and creates the groups and items in the server and givesyou references to them in yourVB
9、 program in an Objectmodel that mirrors that of theserver,OPC&VB-Making The Connections,Connect to the OPC Server,Add Group(s),Add Item(s)to Group(s),Activate Groups&Items,Data Change Events Fire,De-Activate Groups&Items,Remove Item(s)from Group(s),Remove Group(s),Disconnect from the OPC Server,User
10、 Program Handles Data-Log,Display,Graph,etc,Optional-your program can add/remove items and groups and activate/deactive items at runtime based on user defined conditions,Tools Youll Need To Build Your Application,If you want to build an OPC client in VB and test it,youll need the following tools:Vis
11、ual Basic 5 or 6 running on Windows 95,98,or NT any VB edition will do the jobAn OPC ServerThe OPC Automation WrapperYou can get the last two items including a sample test OPC server at(see last slide for exact link).There is no charge for the Automation Wrapper and sample OPC demonstration servers
12、are free also.,Building Your VB Client-Step 1,Install the OPC Automation Wrapper DLL on your PCStart a new VB projectIn VB,click on Project-References on the VB menu barThe OPC Automation Wrapper appears on the dialog as“OPC Automation 2.0”-select it as shown here.,Building Your VB Client-Step 2,Fir
13、st you need to declare some variables in the VB code window in the General Declarations area as shown here,Option ExplicitOption Base 1 Makes all arrays start with an index of 1Dim WithEvents AnOPCServer As OPCServerDim WithEvents ConnectedOPCServer As OPCServerDim ConnectedServerGroup As OPCGroupsD
14、im WithEvents ConnectedGroup As OPCGroupDim OPCItemCollection As OPCItemsDim ItemCount As LongDim OPCItemIDs(10)As StringDim ItemServerHandles()As LongDim ItemServerErrors()As LongDim ClientHandles(10)As Long,These lines create objects you will use to manage your OPC server connection and a group-yo
15、u could add more than one group if you wanted to,These lines create objects you will use to manage your OPC Items-we are setting up our sample to read 10 items-you setup as many as you need,Building Your VB Client-Step 3,If you want your VB project to connect to your server at startup,use the name o
16、f your server(ask your server vendor)and enter the following code in the Form Load subroutine for your project-our server name is“KepServer”,Dim ConnectedServerName As StringCreate a new OPC Server objectSet ConnectedOPCServer=New OPCServerLoad the selected server name to start the interfaceConnecte
17、dServerName=“Developer-Enter Your OPC Server Name Here in quotes”Attempt to connect with the server(Local only in this example)ConnectedOPCServer.Connect(ConnectedServerName),Building Your VB Client-Step 4,Next,youll go ahead and add a group right after you get your connection to the OPC server.Ente
18、r this code right after your code for connecting to the server in the Form Load subroutine of your project,Prepare to add a group to the current OPC Server Get the group interface from the server objectSet ConnectedServerGroup=ConnectedOPCServer.OPCGroups Set the desire active state for the groupCon
19、nectedServerGroup.DefaultGroupIsActive=TrueSet the desired percent deadband-enter an integer from 0 to 100 for the deadbandConnectedServerGroup.DefaultGroupDeadband=0 Add the group and set its update rate-enter whatever group name you want in place of“DataGroup1”Set ConnectedGroup=ConnectedServerGro
20、up.Add(“DataGroup1”)Set the update rate for the group-enter an long integer value representing the millisecond group update rateConnectedGroup.UpdateRate=500 The following line is crucial-without it you wont be subscribed to the server and DataChange events will not fire!ConnectedGroup.IsSubscribed=
21、True,Building Your VB Client-Step 5,Next youll go ahead and add some items.This code follows right after the group add code in the Form Load subroutineFor the item names,enter valid item names for your OPC server.Refer to your OPC servers documentation for valid item naming conventions.We are going
22、to be reading some data from a GE PLC here so we will use their memory conventions.The OPC server we are using uses the syntax“itemnameupdaterate”,ItemCount=4Dim i As IntegerFor i=0 To 3 This line builds a string like“GE9030.R110”-a valid item name for the OPC server we are using OPCItemIDs(i+1)=“GE
23、9030.R”&(I+1)&“10”ClientHandles(i+1)=I Sets a reference pointer number for this point OPCItemActiveState(i).Value=1 Tells the server we want this item to be activeNext ISet OPCItemCollection=ConnectedGroup.OPCItems Gets an items collection from the current GroupOPCItemCollection.DefaultIsActive=True
24、Sets the items collection to activeThis line adds the items weve chosen to the items collection and in turn to the group in the OPC ServerOPCItemCollection.AddItems ItemCount,OPCItemIDs,ClientHandles,ItemServerHandles,ItemServerErrors,Building Your VB Client-Step 6,Now,assuming you have all valid it
25、em names and your OPC server is ready to go,all you need to do is add code to react to the DataChange events that your OPC server will fire back to the Automation DLL,which in turn will fire an event in VB for youIn your program,you created an object variable called“ConnectedGroup”in step 2-this obj
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 信息与通信 信息 通信 手把手 教您用 VB 一个 OPC 客户端 程序
链接地址:https://www.31ppt.com/p-5615151.html