版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、OPC客户端的自动化实现OPC是建立在COM,DCOM的基础商的,因此绝大多数语言都可以很好的进行开发。在Net中开发客户端有以下几种方式:(1) 使用OPCNetAPI,需要用到OPCNetAPI.dll,(2) 使用自动化接口,需要用到OPCDAAuto.dll(3) 使用自定义接口,需要用到多个Wrapper:,以上开发方式所需的动态链接库可以从OPC基金会()的网站上下载,
2、一些下载项目可能需要注册,或成为基金会的成员。不同的方式有各自的有缺点,请参见本文使用自动化接口,VB.Net语言进行开发,开发项目是无线射频(RFID)卡方面的应用,典型的如公交车,或公司考勤使用的刷卡机。需要注意的是自动化接口存在一个“不是问题”的问题,数组下标是以1开始的,而不是传统计算机开发上的以0开始。不知道设计者头脑是怎么想(有人知道吗?);这可能会给一些语言的开发造成问题(有人碰到吗,没有你就是幸运的)需求:OPCDAAuto.dll或该Dll的Interop(一) :客户端开发流程OPC客户端的开发主要遵循下图所示的开发流程,下面就从以下几个开发步骤进行说明
3、0;(二) :枚举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 Objec
4、t = 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() '设置控件状态 GlobalOPCSe
5、rver = 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 = Nothin
6、g)ProgID指服务器的ProgID,Node代表网络节点,如果是本机则放空即可。连接到服务器后,以下属性需要特别注意:OPCServer.StartTime:服务器的启动时间OPCServer.CurrentTime:服务器的当前时间,各个客户端可以通过这个属性值完成一些同步的操作OPCGroups.DefaultGroupIsActive:以后添加的Group是否默认激活OPCGroups.DefaultGroupDeadBand:Group的默认死区,变化量超过死区后将会触发DataChange事件,合理的设置该值可以提高程序性能OPCGroups.Count:下属组(Group)的数
7、量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.O
8、PCGroupFunction 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
9、 e As System.EventArgs) Handles btnConnectServer.Click If cbbServerList.Text <> "" Then ConnectedOPCServer = New OPCAutomation.OPCServerClass() Try ConnectedOPCServer.Connect(cbbServerList.Text) '设置组集合的默认属性 ConnectedOPCServer.OPCGroups.DefaultGroupIsActive = True ConnectedOPCServ
10、er.OPCGroups.DefaultGroupDeadband = 0 '添加组 ConnectedGroup = ConnectedOPCServer.OPCGroups.Add() ConnectedGroup.UpdateRate = 3 * 1000 '刷新虑,用于下面的DataChange事件 ConnectedGroup.IsSubscribed = True '使用订阅功能 '添加项 GlobalOPCItems(0) = ConnectedGroup.OPCItems.AddItem("Reader_Device.OpenCard&
11、quot;, 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("O
12、PC 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, ByR
13、ef 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.Arr
14、ay) 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 Sy
15、stem.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
16、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数据变化时的通
17、知事件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)
18、 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)
19、'如果要获取数据的块所对应的项还没有创建,就创建它 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 S
20、erverHandles(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)
21、 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
22、 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" &
23、amp; 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, Server
24、Values, 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", MessageBoxButt
25、ons.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 ConnectedOPCS
26、erver.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网站(八):全部源码
27、0; 1Imports System.Runtime.InteropServices 2Public Class Form1Class Form1 3 4 Dim GlobalOPCServer As OPCAutomation.OPCServerClass 5 Dim WithEvents ConnectedOPCS
28、erver As OPCAutomation.OPCServerClass 6 Dim WithEvents ConnectedGroup As OPCAutomation.OPCGroupClass 7 8 Dim GlobalOPCItems(4) As OPCAutomation.OPCItem 9
29、 Dim GlobalOPCBlockItems(64) As OPCAutomation.OPCItem 10 11 12 '枚举OPC服务器列表 13 Private Sub Form1_Load()Sub Form1_Load(ByVal sender As System.Object, ByVal e As&
30、#160;System.EventArgs) Handles MyBase.Load 14 Try 15 GlobalOPCServer = New OPCAutomation.OPCServerClass() 16
31、0; Dim ServerList As Object = GlobalOPCServer.GetOPCServers 17 For index As Short = LBound(ServerList) To UBound(ServerList)
32、;'加入控件列表中,注意这里使用LBound和UBound 18 cbbServerList.Items.Add(ServerList(index) 19 Next 20
33、 If cbbServerList.Items.Count > 0 Then 21 cbbServerList.SelectedIndex = 0 22
34、60; End If 23 ResetControlStatus() '设置控件状态 24 GlobalOPCServer = Nothing 25
35、0; Catch Ex As Exception 26 MessageBox.Show("List OPC servers failed: " + Ex.Message, "OPCSample", MessageBoxButtons.OK)
36、27 End Try 28 End Sub 29 30 '连接到指定的OPC服务器 31 Private Sub btnConnectServer_Click()Sub btnConnectServer_Click(ByVal sender As
37、160;System.Object, ByVal e As System.EventArgs) Handles btnConnectServer.Click 32 If cbbServerList.Text <> "" Then 33
38、 ConnectedOPCServer = New OPCAutomation.OPCServerClass() 34 Try 35 ConnectedOPCServer.Connect(cbbS
39、erverList.Text) 36 '设置组集合的默认属性 37 ConnectedOPCServer.OPCGroups.DefaultGroupIsActive =
40、True 38 ConnectedOPCServer.OPCGroups.DefaultGroupDeadband = 0 39 '添加组 40
41、60; ConnectedGroup = ConnectedOPCServer.OPCGroups.Add() 41 ConnectedGroup.UpdateRate = 3 *
42、160;1000 '刷新虑,用于下面的DataChange事件 42 ConnectedGroup.IsSubscribed = True '使用订阅功能 43
43、60; '添加项 44 GlobalOPCItems(0) = ConnectedGroup.OPCItems.AddItem("Reader_Device.OpenCard", 0) 45
44、 GlobalOPCItems(1) = ConnectedGroup.OPCItems.AddItem("Reader_Device.CloseCard", 1) 46 GlobalOPCItems(2) = ConnectedGroup.OPCItems.AddIte
45、m("Reader_Device.CardNO", 2) 47 RefreshServerStatus() '刷新服务器状态 48 Catch ex As Excepti
46、on 49 ConnectedOPCServer = Nothing 50 MessageBox.Show("OPC server connect
47、160;failed : " + ex.Message, "OPCSample", MessageBoxButtons.OK) 51 End Try 52 ResetControlStatus()
48、 53 End If 54 End Sub 55 56 '服务器断开事件通知 57 Private Sub OnServerShutDown()Sub OnServerShutDown(ByVal Reason As String
49、) Handles ConnectedOPCServer.ServerShutDown 58 btnDisconnectServer_Click(Nothing, New EventArgs() 59 End Sub 60 61 Private Sub OnGroupDataChange()Sub
50、60;OnGroupDataChange(ByVal TransactionID As Integer, ByVal NumItems As Integer, ByRef ClientHandles As System.Array, ByRef ItemValues As System.Array, ByRef Qualities As System.Array, ByRef TimeS
51、tamps As System.Array) Handles ConnectedGroup.DataChange 62 For i As Integer = 1 To NumItems 63 If Qualities(i)&
52、#160;= OPCAutomation.OPCQuality.OPCQualityGood Then 64 Select Case ClientHandles(i) 65
53、; Case 2 66 txtCardNo.Text = CStr(ItemValues(i) 67
54、; Case 200 '测试7张卡片 68 txtValueBlock0.Text = CStr(ItemValues(i)&
55、#160;69 Case 201 70 txtValue
56、Block1.Text = CStr(ItemValues(i) 71 Case 202 72 &
57、#160; txtValueBlock2.Text = CStr(ItemValues(i) 73 Case 203 74
58、 txtValueBlock3.Text = CStr(ItemValues(i) 75 Case 204 76
59、; txtValueBlock4.Text = CStr(ItemValues(i) 77 C
60、ase 205 78 txtValueBlock5.Text = CStr(ItemValues(i) 79
61、 Case 206 80 txtValueBlock6.Text = CStr(ItemValues(i) 81
62、; Case 207 82 txtValueBlock7.Text = CStr(ItemValues(i)
63、 83 Case Else 84 85 End Select 86 87
64、60; End If 88 Next 89 End Sub 90 91 92 '断开到指定OPC服务器的连接 93 Private Sub btnDisc
65、onnectServer_Click()Sub btnDisconnectServer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnectServer.Click 94 If Not (ConnectedOPCServer Is N
66、othing) Then 95 Try 96 ConnectedOPCServer.Disconnect() 97
67、60; Catch ex As Exception 98 MessageBox.Show("OPC server disconnect failed: " + ex.Message, "OPCSample", MessageBo
68、xButtons.OK) 99 Finally100 ConnectedOPCServer = Nothing101 &
69、#160; ResetControlStatus()102 End Try103 End If104 End Sub105106 '开卡,并返回卡号107
70、; Private Sub btnOpenCard_Click()Sub btnOpenCard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)108 If ConnectedGroup IsNot Nothing Then109
71、60; Try110 '准备参数数组111 Dim ServerHandles(1)
72、As Integer112 Dim ServerValues(1) As Object113 Dim ServerErrors As System.A
73、rray114 ServerHandles(1) = GlobalOPCItems(0).ServerHandle115 ServerValues(1) = 1116 &
74、#160; '写入值,用于执行OpenCard的操作117 ConnectedGroup.SyncWrite(1, ServerHandles, ServerValues, ServerErrors)118
75、 If ServerErrors(1) <> 0 Then119 MsgBox("OpenCardError:
76、 " & ServerErrors(1)120 End If121122 ServerHandles(1) = GlobalOPCItems(
77、2).ServerHandle123 Dim ServerResult As System.Array124 '读取卡号125
78、0; ConnectedGroup.SyncRead(OPCAutomation.OPCDataSource.OPCDevice, 1, ServerHandles, ServerResult, ServerErrors)126 If
79、60;ServerErrors(1) <> 0 Then127 MsgBox("ReadCardNoError: " & ServerErrors(1)128
80、0; Else129 txtCardNo.Text = ServerResult(1)130
81、 End If131 Catch ex As Exception132 MessageBox.Show("OPC server Open Card fail
82、ed: " + ex.Message, "OPCSample", MessageBoxButtons.OK)133 End Try134 ResetControlStatus()135
83、60; End If136 End Sub137138 '读取卡片指定的块号的值139 Private Sub btnReadCard_Click()Sub btnReadCard_Click(ByVal sender As System.Object, ByVal e As Syste
84、m.EventArgs)140 If Not (ConnectedGroup Is Nothing) Then141 Try142
85、160;'获取块号143 Dim BlockNo As Short = CByte(ReadBlockNo.Text)144 '如果要获取数据的块所对应的
86、项还没有创建,就创建它145 If GlobalOPCBlockItems(BlockNo) Is Nothing Then146
87、GlobalOPCBlockItems(BlockNo) = ConnectedGroup.OPCItems.AddItem("Reader_Device.Block" & CStr(BlockNo), 200 + BlockNo)147 End If148
88、0; '准备参数数组149 Dim ServerResults As System.Array150
89、; Dim ServerErrors As System.Array151 Dim ServerHandles(1) As Integer152
90、160; ServerHandles(1) = GlobalOPCBlockItems(BlockNo).ServerHandle153 '读取值154 Co
91、nnectedGroup.SyncRead(OPCAutomation.OPCDataSource.OPCDevice, 1, ServerHandles, ServerResults, ServerErrors)155 If ServerErrors(1) <> 0 Then156
92、; MsgBox("Read Card Failed:" & ServerErrors(1)157 Else158 &
93、#160; txtReadBlockNo.Text = ServerResults(1)159 End If160
94、160; Catch ex As Exception161 MessageBox.Show("OPC server Read Card failed: " + ex.Message, "OPCSamp
95、le", MessageBoxButtons.OK)162 End Try163 End If164 End Sub165166 '写卡片指定块的值167 Privat
96、e Sub btnWriteCard_Click()Sub btnWriteCard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)168 If Not (ConnectedGroup Is Nothing) Then169
97、 Try170 '获取块号171 Dim BlockNo As Short =
98、 CByte(WriteBlockNo.Text)172 '如果要写入数据的块所对应的项还没有创建,就创建它173 If GlobalOPCBlockItems(BlockNo) I
99、s Nothing Then174 GlobalOPCBlockItems(BlockNo) = ConnectedGroup.OPCItems.AddItem("Reader_Device.Block" & CStr(BlockNo), 200 +
100、160;BlockNo)175 End If176 '准备参数数组177
101、; Dim ServerValues(1) As Object178 Dim ServerErrors As Array179
102、; Dim ServerHandles(1) As Integer180 ServerHandles(1) = GlobalOPCBlockItems(BlockNo).ServerHandle181
103、60; ServerValues(1) = txtWriteBlockNo.Text182 '写入值183 ConnectedGroup.Sync
104、Write(1, ServerHandles, ServerValues, ServerErrors)184 If ServerErrors(1) <> 0 Then185
105、 MsgBox("Write Card Failed:" & ServerErrors(1)186 Else187 &
106、#160; MsgBox("Write Card Succeed")188 End If189 Catch ex As Exception190 MessageBox.Show("OPC server Write Card failed: " + ex.Message, "OPCSample",
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 火车站煤炭装卸合同范本
- 开开股份合同范本
- 2024秋季大纲:《青玉案元夕》创新教案解析
- 2024年青海私人二手房销售协议
- 2024教师招聘面试高中英语听力逐字稿
- 数学人教版2024版一年级上册第2章2.3.3连加、连减教学课件04
- 2024年高空作业协议样本大全
- 养护承包合同
- 关于玩沙子的日记300字
- 关于心理宣传的内容
- 派驻海外工作员工薪酬福利实施专项方案
- 广东高职高考数学题分类
- 中风中医护理个案
- 居住建筑节能65%(绿色建筑)设计标准
- 公交有限公司触电事故现场处置预案
- 盆景工-国家职业技能标准
- 蓝蓝的夜蓝蓝的梦三部童声合唱谱
- 付款条件与支付方式
- 屠宰行业价值分析
- 数字化赋能绿色智能制造案例分析
- 新生儿常见问题及护理 课件
评论
0/150
提交评论