




已阅读5页,还剩12页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
OPC客户端的自动化实现OPC是建立在COM,DCOM的基础商的,因此绝大多数语言都可以很好的进行开发。在Net中开发客户端有以下几种方式:(1) 使用OPCNetAPI,需要用到OPCNetAPI.dll,OPCNetAPI.Com.dll(2) 使用自动化接口,需要用到OPCDAAuto.dll(3) 使用自定义接口,需要用到多个Wrapper:OpcRcw.Ae.dll,OpcRcw.Batch.dll,OpcRcw.Comn.dll,OpcRcw.Da.dll,OpcRcw.Dx.dll,OpcRcw.Hda.dll,OpcRcw.Sec.dll以上开发方式所需的动态链接库可以从OPC基金会(/)的网站上下载,一些下载项目可能需要注册,或成为基金会的成员。不同的方式有各自的有缺点,请参见本文使用自动化接口,VB.Net语言进行开发,开发项目是无线射频(RFID)卡方面的应用,典型的如公交车,或公司考勤使用的刷卡机。需要注意的是自动化接口存在一个“不是问题”的问题,数组下标是以1开始的,而不是传统计算机开发上的以0开始。不知道设计者头脑是怎么想(有人知道吗?);这可能会给一些语言的开发造成问题(有人碰到吗,没有你就是幸运的)需求:OPCDAAuto.dll或该Dll的Interop(一) :客户端开发流程OPC客户端的开发主要遵循下图所示的开发流程,下面就从以下几个开发步骤进行说明(二) :枚举OPC服务器列表枚举服务器主要是通过OPCServer接口的GetOPCServers方法来实现的,该方法会返回OPC服务器数组(以1为下界,上面已有说明),以下是代码段 枚举OPC服务器列表 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Try GlobalOPCServer = New OPCAutomation.OPCServerClass() Dim ServerList As Object = GlobalOPCServer.GetOPCServers For index As Short = LBound(ServerList) To UBound(ServerList) 加入控件列表中,注意这里使用LBound和UBound cbbServerList.Items.Add(ServerList(index) Next If cbbServerList.Items.Count 0 Then cbbServerList.SelectedIndex = 0 End If ResetControlStatus() 设置控件状态 GlobalOPCServer = Nothing Catch Ex As Exception MessageBox.Show(List OPC servers failed: + Ex.Message, OPCSample, MessageBoxButtons.OK) End TryEnd Sub(三) :连接OPC服务器自动化接口中连接到服务器是使用connect方法Public Overridable Sub Connect(ByVal ProgID As String, Optional ByVal Node As Object = Nothing)ProgID指服务器的ProgID,Node代表网络节点,如果是本机则放空即可。连接到服务器后,以下属性需要特别注意:OPCServer.StartTime:服务器的启动时间OPCServer.CurrentTime:服务器的当前时间,各个客户端可以通过这个属性值完成一些同步的操作OPCGroups.DefaultGroupIsActive:以后添加的Group是否默认激活OPCGroups.DefaultGroupDeadBand:Group的默认死区,变化量超过死区后将会触发DataChange事件,合理的设置该值可以提高程序性能OPCGroups.Count:下属组(Group)的数量OPCGroups.DefaultGroupLocalID:组(Group)的默认通信区域编号,如1024OPCGroups.DefaultGroupUpdateRate:组(Group)的默认刷新率,该属性也比较重要OPCGroups.DefaultGroupTimeBias:组(Group)的默认时间偏差(四) :添加组(Group)和项 (Item)添加组和项需要用到Groups.Add和Items.AddItem方法,以下是原型:Function Add(Optional ByVal Name As Object = Nothing) As OPCAutomation.OPCGroupFunction AddItem(ByVal ItemID As String, ByVal ClientHandle As Integer) As OPCAutomation.OPCItem组也有两个重要的属性Group.UpdateRate:刷新率,该属性通Groups的UpdateRate意义一样,如果这个值有设置,则以这个值为准Group. IsSubscribed:是否使用订阅功能以下是代码段 连接到指定的OPC服务器 Private Sub btnConnectServer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnectServer.Click If cbbServerList.Text Then ConnectedOPCServer = New OPCAutomation.OPCServerClass() Try ConnectedOPCServer.Connect(cbbServerList.Text) 设置组集合的默认属性 ConnectedOPCServer.OPCGroups.DefaultGroupIsActive = True ConnectedOPCServer.OPCGroups.DefaultGroupDeadband = 0 添加组 ConnectedGroup = ConnectedOPCServer.OPCGroups.Add() ConnectedGroup.UpdateRate = 3 * 1000 刷新虑,用于下面的DataChange事件 ConnectedGroup.IsSubscribed = True 使用订阅功能 添加项 GlobalOPCItems(0) = ConnectedGroup.OPCItems.AddItem(Reader_Device.OpenCard, 0) GlobalOPCItems(1) = ConnectedGroup.OPCItems.AddItem(Reader_Device.CloseCard, 1) GlobalOPCItems(2) = ConnectedGroup.OPCItems.AddItem(Reader_Device.CardNO, 2) RefreshServerStatus() 刷新服务器状态 Catch ex As Exception ConnectedOPCServer = Nothing MessageBox.Show(OPC server connect failed : + ex.Message, OPCSample, MessageBoxButtons.OK) End Try ResetControlStatus() End If End Sub(五) :读写操作与事件控制读写操作包括同步和异步两种操作方式,以下是这几个方法的原型:Group的同步读事件Sub SyncRead(ByVal Source As Short, ByVal NumItems As Integer, ByRef ServerHandles As System.Array, ByRef Values As System.Array, ByRef Errors As System.Array, Optional ByRef Qualities As Object = Nothing, Optional ByRef TimeStamps As Object = Nothing)Group的同步写事件Sub SyncWrite(ByVal NumItems As Integer, ByRef ServerHandles As System.Array, ByRef Values As System.Array, ByRef Errors As System.Array)Group的异步读事件Sub AsyncRead(ByVal NumItems As Integer, ByRef ServerHandles As System.Array, ByRef Errors As System.Array, ByVal TransactionID As Integer, ByRef CancelID As Integer)Group的异步写事件Sub AsyncWrite(ByVal NumItems As Integer, ByRef ServerHandles As System.Array, ByRef Values As System.Array, ByRef Errors As System.Array, ByVal TransactionID As Integer, ByRef CancelID As Integer)如果使用异步的读写操作,那么还需要实现Group中的ReadComplete和WriteComplete两个事件Public Event AsyncReadComplete(ByVal TransactionID As Integer, ByVal NumItems As Integer, ByRef ClientHandles As System.Array, ByRef ItemValues As System.Array, ByRef Qualities As System.Array, ByRef TimeStamps As System.Array, ByRef Errors As System.Array)Public Event AsyncWriteComplete(ByVal TransactionID As Integer, ByVal NumItems As Integer, ByRef ClientHandles As System.Array, ByRef Errors As System.Array)其他相关的重要事件包括:Group数据变化时的通知事件Public Event DataChange(ByVal TransactionID As Integer, ByVal NumItems As Integer, ByRef ClientHandles As System.Array, ByRef ItemValues As System.Array, ByRef Qualities As System.Array, ByRef TimeStamps As System.Array)Group的异步取消事件Public Event AsyncCancelComplete(ByVal CancelID As Integer)Server(服务器)关闭通知事件Public Event ServerShutDown(ByVal Reason As String)以下是这些实现的代码段 读取卡片指定的块号的值 Private Sub btnReadCard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) If Not (ConnectedGroup Is Nothing) Then Try 获取块号 Dim BlockNo As Short = CByte(ReadBlockNo.Text) 如果要获取数据的块所对应的项还没有创建,就创建它 If GlobalOPCBlockItems(BlockNo) Is Nothing Then GlobalOPCBlockItems(BlockNo) = ConnectedGroup.OPCItems.AddItem(Reader_Device.Block & CStr(BlockNo), 200 + BlockNo) End If 准备参数数组 Dim ServerResults As System.Array Dim ServerErrors As System.Array Dim ServerHandles(1) As Integer ServerHandles(1) = GlobalOPCBlockItems(BlockNo).ServerHandle 读取值 ConnectedGroup.SyncRead(OPCAutomation.OPCDataSource.OPCDevice, 1, ServerHandles, ServerResults, ServerErrors) If ServerErrors(1) 0 Then MsgBox(Read Card Failed: & ServerErrors(1) Else txtReadBlockNo.Text = ServerResults(1) End If Catch ex As Exception MessageBox.Show(OPC server Read Card failed: + ex.Message, OPCSample, MessageBoxButtons.OK) End Try End IfEnd Sub 写卡片指定块的值 Private Sub btnWriteCard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) If Not (ConnectedGroup Is Nothing) Then Try 获取块号 Dim BlockNo As Short = CByte(WriteBlockNo.Text) 如果要写入数据的块所对应的项还没有创建,就创建它 If GlobalOPCBlockItems(BlockNo) Is Nothing Then GlobalOPCBlockItems(BlockNo) = ConnectedGroup.OPCItems.AddItem(Reader_Device.Block & CStr(BlockNo), 200 + BlockNo) End If 准备参数数组 Dim ServerValues(1) As Object Dim ServerErrors As Array Dim ServerHandles(1) As Integer ServerHandles(1) = GlobalOPCBlockItems(BlockNo).ServerHandle ServerValues(1) = txtWriteBlockNo.Text 写入值 ConnectedGroup.SyncWrite(1, ServerHandles, ServerValues, ServerErrors) If ServerErrors(1) 0 Then MsgBox(Write Card Failed: & ServerErrors(1) Else MsgBox(Write Card Succeed) End If Catch ex As Exception MessageBox.Show(OPC server Write Card failed: + ex.Message, OPCSample, MessageBoxButtons.OK) End Try End IfEnd Sub(六) :断开服务器断开服务器只要使用OPCServer的Disconnect方法几个,以下是代码段: 断开到指定OPC服务器的连接 Private Sub btnDisconnectServer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnectServer.Click If Not (ConnectedOPCServer Is Nothing) Then Try ConnectedOPCServer.Disconnect() Catch ex As Exception MessageBox.Show(OPC server disconnect failed: + ex.Message, OPCSample, MessageBoxButtons.OK) Finally ConnectedOPCServer = Nothing ResetControlStatus() End Try End IfEnd Sub(七) :相关链接非常好的一个OPC技术网站/OPC基金会网址/国内的一个比较好的OPC网站/Index.html(八):全部源码ImportsSystem.Runtime.InteropServicesPublicClassForm1ClassForm1DimGlobalOPCServerAsOPCAutomation.OPCServerClassDimWithEventsConnectedOPCServerAsOPCAutomation.OPCServerClassDimWithEventsConnectedGroupAsOPCAutomation.OPCGroupClassDimGlobalOPCItems(4)AsOPCAutomation.OPCItemDimGlobalOPCBlockItems(64)AsOPCAutomation.OPCItem枚举OPC服务器列表PrivateSubForm1_Load()SubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.LoadTryGlobalOPCServer=NewOPCAutomation.OPCServerClass()DimServerListAsObject=GlobalOPCServer.GetOPCServersForindexAsShort=LBound(ServerList)ToUBound(ServerList)加入控件列表中,注意这里使用LBound和UBoundcbbServerList.Items.Add(ServerList(index)NextIfcbbServerList.Items.Count0ThencbbServerList.SelectedIndex=0EndIfResetControlStatus()设置控件状态GlobalOPCServer=NothingCatchExAsExceptionMessageBox.Show(ListOPCserversfailed:+Ex.Message,OPCSample,MessageBoxButtons.OK)EndTryEndSub连接到指定的OPC服务器PrivateSubbtnConnectServer_Click()SubbtnConnectServer_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesbtnConnectServer.ClickIfcbbServerList.TextThenConnectedOPCServer=NewOPCAutomation.OPCServerClass()TryConnectedOPCServer.Connect(cbbServerList.Text)设置组集合的默认属性ConnectedOPCServer.OPCGroups.DefaultGroupIsActive=TrueConnectedOPCServer.OPCGroups.DefaultGroupDeadband=0添加组ConnectedGroup=ConnectedOPCServer.OPCGroups.Add()ConnectedGroup.UpdateRate=3*1000刷新虑,用于下面的DataChange事件ConnectedGroup.IsSubscribed=True使用订阅功能添加项GlobalOPCItems(0)=ConnectedGroup.OPCItems.AddItem(Reader_Device.OpenCard,0)GlobalOPCItems(1)=ConnectedGroup.OPCItems.AddItem(Reader_Device.CloseCard,1)GlobalOPCItems(2)=ConnectedGroup.OPCItems.AddItem(Reader_Device.CardNO,2)RefreshServerStatus()刷新服务器状态CatchexAsExceptionConnectedOPCServer=NothingMessageBox.Show(OPCserverconnectfailed:+ex.Message,OPCSample,MessageBoxButtons.OK)EndTryResetControlStatus()EndIfEndSub服务器断开事件通知PrivateSubOnServerShutDown()SubOnServerShutDown(ByValReasonAsString)HandlesConnectedOPCServer.ServerShutDownbtnDisconnectServer_Click(Nothing,NewEventArgs()EndSubPrivateSubOnGroupDataChange()SubOnGroupDataChange(ByValTransactionIDAsInteger,ByValNumItemsAsInteger,ByRefClientHandlesAsSystem.Array,ByRefItemValuesAsSystem.Array,ByRefQualitiesAsSystem.Array,ByRefTimeStampsAsSystem.Array)HandlesConnectedGroup.DataChangeForiAsInteger=1ToNumItemsIfQualities(i)=OPCAutomation.OPCQuality.OPCQualityGoodThenSelectCaseClientHandles(i)Case2txtCardNo.Text=CStr(ItemValues(i)Case200测试7张卡片txtValueBlock0.Text=CStr(ItemValues(i)Case201txtValueBlock1.Text=CStr(ItemValues(i)Case202txtValueBlock2.Text=CStr(ItemValues(i)Case203txtValueBlock3.Text=CStr(ItemValues(i)Case204txtValueBlock4.Text=CStr(ItemValues(i)Case205txtValueBlock5.Text=CStr(ItemValues(i)Case206txtValueBlock6.Text=CStr(ItemValues(i)Case207txtValueBlock7.Text=CStr(ItemValues(i)CaseElseEndSelectEndIfNextEndSub断开到指定OPC服务器的连接PrivateSubbtnDisconnectServer_Click()SubbtnDisconnectServer_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesbtnDisconnectServer.ClickIfNot(ConnectedOPCServerIsNothing)ThenTryConnectedOPCServer.Disconnect()CatchexAsExceptionMessageBox.Show(OPCserverdisconnectfailed:+ex.Message,OPCSample,MessageBoxButtons.OK)FinallyConnectedOPCServer=NothingResetControlStatus()EndTryEndIfEndSub开卡,并返回卡号PrivateSubbtnOpenCard_Click()SubbtnOpenCard_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)IfConnectedGroupIsNotNothingThenTry准备参数数组DimServerHandles(1)AsIntegerDimServerValues(1)AsObjectDimServerErrorsAsSystem.ArrayServerHandles(1)=GlobalOPCItems(0).ServerHandleServerValues(1)=1写入值,用于执行OpenCard的操作ConnectedGroup.SyncWrite(1,ServerHandles,ServerValues,ServerErrors)IfServerErrors(1)0ThenMsgBox(OpenCardError:&ServerErrors(1)EndIfServerHandles(1)=GlobalOPCItems(2).ServerHandleDimServerResultAsSystem.Array读取卡号ConnectedGroup.SyncRead(OPCAutomation.OPCDataSource.OPCDevice,1,ServerHandles,ServerResult,ServerErrors)IfServerErrors(1)0ThenMsgBox(ReadCardNoError:&ServerErrors(1)ElsetxtCardNo.Text=ServerResult(1)EndIfCatchexAsExceptionMessageBox.Show(OPCserverOpenCardfailed:+ex.Message,OPCSample,MessageBoxButtons.OK)EndTryResetControlStatus()EndIfEndSub读取卡片指定的块号的值PrivateSubbtnReadCard_Click()SubbtnReadCard_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)IfNot(ConnectedGroupIsNothing)ThenTry获取块号DimBlockNoAsShort=CByte(ReadBlockNo.Text)如果要获取数据的块所对应的项还没有创建,就创建它IfGlobalOPCBlockItems(BlockNo)IsNothingThenGlobalOPCBlockItems(BlockNo)=ConnectedGroup.OPCItems.AddItem(Reader_Device.Block&CStr(BlockNo),200+BlockNo)EndIf准备参数数组DimServerResultsAsSystem.ArrayDimServerErrorsAsSystem.ArrayDimServerHandles(1)AsIntegerServerHandles(1)=GlobalOPCBlockItems(BlockNo).ServerHandle读取值ConnectedGroup.SyncRead(OPCAutomation.OPCDataSource.OPCDevice,1,ServerHandles,ServerResults,ServerErrors)IfServerErrors(1)0ThenMsgBox(ReadCardFailed:&ServerErrors(1)ElsetxtReadBlockNo.Text=ServerResults(1)EndIfCatchexAsExceptionMessageBox.Show(OPCserverReadCardfailed:+ex.Message,OPCSample,MessageBoxButtons.OK)EndTryEndIfEndSub写卡片指定块的值PrivateSubbtnWriteCard_Click()SubbtnWriteCard_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)IfNot(ConnectedGroupIsNothing)ThenTry获取块号DimBlockNoAsShort=CByte(WriteBlockNo.Text)如果要写入数据的块所对应的项还没有创建,就创建它IfGlobalOPCBlockItems(BlockNo)IsNothingThenGlobalOPCBlockItems(BlockNo)=ConnectedGroup.OPCItems.AddItem(Reader_Device.Block&CStr(BlockNo),200+BlockNo)EndIf准备参数数组DimServerValues(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 汽车智能车载二手车评估系统创新创业项目商业计划书
- 呼吸系统营养治疗
- 护理工作创新
- 脊柱骨科专科护理进修
- 工厂冬季四防安全知识培训课件
- 工厂健康安全知识培训总结课件
- 工厂体系基本知识培训内容
- 数字信号处理高西全课件
- 医用教学课件下载
- 散货货代专业知识培训课件
- 危险化学品应急演练计划
- 2025-2030中国催化裂化催化剂行业前景展望及需求趋势预测报告
- 电厂设备清洁管理制度
- 左上颌骨囊肿护理查房
- 公司六一活动家属开放日活动方案
- 2025至2030年中国继电保护及自动化设备行业市场现状调查及发展趋向研判报告
- 2025年重庆市中考数学试卷真题及答案详解(精校打印版)
- 关于医院“十五五”发展规划(2026-2030)
- 民航气象专业面试题及答案
- 浙江仙琚制药股份有限公司年产2.5亿粒性激素软胶囊生产线技术改造项目环评报告
- DB37/T 3658-2019地质灾害治理工程施工技术规范
评论
0/150
提交评论