OPC客户端的自动化实现

                                                        OPC客户端的自动化实现

OPC是建立在COMDCOM的基础商的,因此绝大多数语言都可以很好的进行开发。在Net中开发客户端有以下几种方式:

(1)       使用OPCNetAPI,需要用到OPCNetAPI.dllOPCNetAPI.Com.dll

(2)       使用自动化接口,需要用到OPCDAAuto.dll

(3)       使用自定义接口,需要用到多个WrapperOpcRcw.Ae.dllOpcRcw.Batch.dllOpcRcw.Comn.dllOpcRcw.Da.dllOpcRcw.Dx.dllOpcRcw.Hda.dllOpcRcw.Sec.dll

以上开发方式所需的动态链接库可以从OPC基金会http://www.opcfoundation.org/的网站上下载,一些下载项目可能需要注册,或成为基金会的成员。

不同的方式有各自的有缺点,请参见

本文使用自动化接口,VB.Net语言进行开发,开发项目是无线射频(RFID)卡方面的应用,典型的如公交车,或公司考勤使用的刷卡机。需要注意的是自动化接口存在一个“不是问题”的问题,数组下标是以1开始的,而不是传统计算机开发上的以0开始。不知道设计者头脑是怎么想(有人知道吗?);这可能会给一些语言的开发造成问题(有人碰到吗,没有你就是幸运的)

需求:OPCDAAuto.dll或该DllInterop

(一)  :客户端开发流程

OPC客户端的开发主要遵循下图所示的开发流程,下面就从以下几个开发步骤进行说明
 OPC客户端的自动化实现_第1张图片



(二)  :枚举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) '加入控件列表中,注意这里使用LBoundUBound

                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 Try

End Sub

(三)  :连接OPC服务器

自动化接口中连接到服务器是使用connect方法

Public Overridable Sub Connect(ByVal ProgID As String, Optional ByVal Node As Object = Nothing)

ProgID指服务器的ProgIDNode代表网络节点,如果是本机则放空即可。

连接到服务器后,以下属性需要特别注意:

OPCServer.StartTime:服务器的启动时间

OPCServer.CurrentTime:服务器的当前时间,各个客户端可以通过这个属性值完成一些同步的操作

OPCGroups.DefaultGroupIsActive:以后添加的Group是否默认激活

OPCGroups.DefaultGroupDeadBandGroup的默认死区,变化量超过死区后将会触发DataChange事件,

合理的设置该值可以提高程序性能

OPCGroups.Count:下属组(Group)的数量

OPCGroups.DefaultGroupLocalID:组(Group)的默认通信区域编号,如1024

OPCGroups.DefaultGroupUpdateRate:组(Group)的默认刷新率,该属性也比较重要

OPCGroups.DefaultGroupTimeBias:组(Group)的默认时间偏差

(四)  :添加组(Group)和项 Item

添加组和项需要用到Groups.AddItems.AddItem方法,以下是原型:

Function Add(Optional ByVal Name As Object = Nothing) As OPCAutomation.OPCGroup

Function AddItem(ByVal ItemID As String, ByVal ClientHandle As Integer) As OPCAutomation.OPCItem

       组也有两个重要的属性

       Group.UpdateRate:刷新率,该属性通GroupsUpdateRate意义一样,如果这个值有设置,则以这个值为准

       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中的ReadCompleteWriteComplete两个事件

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 If

End 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 If

End Sub

(六)  :断开服务器

断开服务器只要使用OPCServerDisconnect方法几个,以下是代码段:

    '断开到指定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 If

End Sub

(七)  :相关链接

非常好的一个OPC技术网站http://www.opcconnect.com/

OPC基金会网址http://www.opcfoundation.org/

国内的一个比较好的OPC网站http://www.opc-china.com/Index.html

(八):全部源码

  1 None.gif Imports  System.Runtime.InteropServices
  2 ExpandedBlockStart.gifContractedBlock.gif Public   Class Form1 Class Form1
  3InBlock.gif
  4InBlock.gif    Dim GlobalOPCServer As OPCAutomation.OPCServerClass
  5InBlock.gif    Dim WithEvents ConnectedOPCServer As OPCAutomation.OPCServerClass
  6InBlock.gif    Dim WithEvents ConnectedGroup As OPCAutomation.OPCGroupClass
  7InBlock.gif
  8InBlock.gif    Dim GlobalOPCItems(4As OPCAutomation.OPCItem
  9InBlock.gif    Dim GlobalOPCBlockItems(64As OPCAutomation.OPCItem
 10InBlock.gif
 11InBlock.gif
 12InBlock.gif    '枚举OPC服务器列表
 13ExpandedSubBlockStart.gifContractedSubBlock.gif    Private Sub Form1_Load()Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 14InBlock.gif        Try
 15InBlock.gif            GlobalOPCServer = New OPCAutomation.OPCServerClass()
 16InBlock.gif            Dim ServerList As Object = GlobalOPCServer.GetOPCServers
 17InBlock.gif            For index As Short = LBound(ServerList) To UBound(ServerList) '加入控件列表中,注意这里使用LBound和UBound
 18InBlock.gif                cbbServerList.Items.Add(ServerList(index))
 19InBlock.gif            Next
 20InBlock.gif            If cbbServerList.Items.Count > 0 Then
 21InBlock.gif                cbbServerList.SelectedIndex = 0
 22InBlock.gif            End If
 23InBlock.gif            ResetControlStatus() '设置控件状态
 24InBlock.gif            GlobalOPCServer = Nothing
 25InBlock.gif        Catch Ex As Exception
 26InBlock.gif            MessageBox.Show("List OPC servers failed: " + Ex.Message, "OPCSample", MessageBoxButtons.OK)
 27InBlock.gif        End Try
 28ExpandedSubBlockEnd.gif    End Sub

 29InBlock.gif
 30InBlock.gif    '连接到指定的OPC服务器
 31ExpandedSubBlockStart.gifContractedSubBlock.gif    Private Sub btnConnectServer_Click()Sub btnConnectServer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnectServer.Click
 32InBlock.gif        If cbbServerList.Text <> "" Then
 33InBlock.gif            ConnectedOPCServer = New OPCAutomation.OPCServerClass()
 34InBlock.gif            Try
 35InBlock.gif                ConnectedOPCServer.Connect(cbbServerList.Text)
 36InBlock.gif                '设置组集合的默认属性
 37InBlock.gif                ConnectedOPCServer.OPCGroups.DefaultGroupIsActive = True
 38InBlock.gif                ConnectedOPCServer.OPCGroups.DefaultGroupDeadband = 0
 39InBlock.gif                '添加组
 40InBlock.gif                ConnectedGroup = ConnectedOPCServer.OPCGroups.Add()
 41InBlock.gif                ConnectedGroup.UpdateRate = 3 * 1000 '刷新虑,用于下面的DataChange事件
 42InBlock.gif                ConnectedGroup.IsSubscribed = True '使用订阅功能
 43InBlock.gif                '添加项
 44InBlock.gif                GlobalOPCItems(0= ConnectedGroup.OPCItems.AddItem("Reader_Device.OpenCard"0)
 45InBlock.gif                GlobalOPCItems(1= ConnectedGroup.OPCItems.AddItem("Reader_Device.CloseCard"1)
 46InBlock.gif                GlobalOPCItems(2= ConnectedGroup.OPCItems.AddItem("Reader_Device.CardNO"2)
 47InBlock.gif                RefreshServerStatus() '刷新服务器状态
 48InBlock.gif            Catch ex As Exception
 49InBlock.gif                ConnectedOPCServer = Nothing
 50InBlock.gif                MessageBox.Show("OPC server connect failed : " + ex.Message, "OPCSample", MessageBoxButtons.OK)
 51InBlock.gif            End Try
 52InBlock.gif            ResetControlStatus()
 53InBlock.gif        End If
 54ExpandedSubBlockEnd.gif    End Sub

 55InBlock.gif
 56InBlock.gif    '服务器断开事件通知
 57ExpandedSubBlockStart.gifContractedSubBlock.gif    Private Sub OnServerShutDown()Sub OnServerShutDown(ByVal Reason As StringHandles ConnectedOPCServer.ServerShutDown
 58InBlock.gif        btnDisconnectServer_Click(NothingNew EventArgs())
 59ExpandedSubBlockEnd.gif    End Sub

 60InBlock.gif
 61ExpandedSubBlockStart.gifContractedSubBlock.gif    Private Sub OnGroupDataChange()Sub OnGroupDataChange(ByVal TransactionID As IntegerByVal NumItems As IntegerByRef ClientHandles As System.Array, ByRef ItemValues As System.Array, ByRef Qualities As System.Array, ByRef TimeStamps As System.Array) Handles ConnectedGroup.DataChange
 62InBlock.gif        For i As Integer = 1 To NumItems
 63InBlock.gif            If Qualities(i) = OPCAutomation.OPCQuality.OPCQualityGood Then
 64InBlock.gif                Select Case ClientHandles(i)
 65InBlock.gif                    Case 2
 66InBlock.gif                        txtCardNo.Text = CStr(ItemValues(i))
 67InBlock.gif                    Case 200 '测试7张卡片
 68InBlock.gif                        txtValueBlock0.Text = CStr(ItemValues(i))
 69InBlock.gif                    Case 201
 70InBlock.gif                        txtValueBlock1.Text = CStr(ItemValues(i))
 71InBlock.gif                    Case 202
 72InBlock.gif                        txtValueBlock2.Text = CStr(ItemValues(i))
 73InBlock.gif                    Case 203
 74InBlock.gif                        txtValueBlock3.Text = CStr(ItemValues(i))
 75InBlock.gif                    Case 204
 76InBlock.gif                        txtValueBlock4.Text = CStr(ItemValues(i))
 77InBlock.gif                    Case 205
 78InBlock.gif                        txtValueBlock5.Text = CStr(ItemValues(i))
 79InBlock.gif                    Case 206
 80InBlock.gif                        txtValueBlock6.Text = CStr(ItemValues(i))
 81InBlock.gif                    Case 207
 82InBlock.gif                        txtValueBlock7.Text = CStr(ItemValues(i))
 83InBlock.gif                    Case Else
 84InBlock.gif
 85InBlock.gif                End Select
 86InBlock.gif
 87InBlock.gif            End If
 88InBlock.gif        Next
 89ExpandedSubBlockEnd.gif    End Sub

 90InBlock.gif
 91InBlock.gif
 92InBlock.gif    '断开到指定OPC服务器的连接
 93ExpandedSubBlockStart.gifContractedSubBlock.gif    Private Sub btnDisconnectServer_Click()Sub btnDisconnectServer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnectServer.Click
 94InBlock.gif        If Not (ConnectedOPCServer Is NothingThen
 95InBlock.gif            Try
 96InBlock.gif                ConnectedOPCServer.Disconnect()
 97InBlock.gif            Catch ex As Exception
 98InBlock.gif                MessageBox.Show("OPC server disconnect failed: " + ex.Message, "OPCSample", MessageBoxButtons.OK)
 99InBlock.gif            Finally
100InBlock.gif                ConnectedOPCServer = Nothing
101InBlock.gif                ResetControlStatus()
102InBlock.gif            End Try
103InBlock.gif        End If
104ExpandedSubBlockEnd.gif    End Sub

105InBlock.gif
106InBlock.gif    '开卡,并返回卡号
107ExpandedSubBlockStart.gifContractedSubBlock.gif    Private Sub btnOpenCard_Click()Sub btnOpenCard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
108InBlock.gif        If ConnectedGroup IsNot Nothing Then
109InBlock.gif            Try
110InBlock.gif                '准备参数数组
111InBlock.gif                Dim ServerHandles(1As Integer
112InBlock.gif                Dim ServerValues(1As Object
113InBlock.gif                Dim ServerErrors As System.Array
114InBlock.gif                ServerHandles(1= GlobalOPCItems(0).ServerHandle
115InBlock.gif                ServerValues(1= 1
116InBlock.gif                '写入值,用于执行OpenCard的操作
117InBlock.gif                ConnectedGroup.SyncWrite(1, ServerHandles, ServerValues, ServerErrors)
118InBlock.gif                If ServerErrors(1<> 0 Then
119InBlock.gif                    MsgBox("OpenCardError: " & ServerErrors(1))
120InBlock.gif                End If
121InBlock.gif
122InBlock.gif                ServerHandles(1= GlobalOPCItems(2).ServerHandle
123InBlock.gif                Dim ServerResult As System.Array
124InBlock.gif                '读取卡号
125InBlock.gif                ConnectedGroup.SyncRead(OPCAutomation.OPCDataSource.OPCDevice, 1, ServerHandles, ServerResult, ServerErrors)
126InBlock.gif                If ServerErrors(1<> 0 Then
127InBlock.gif                    MsgBox("ReadCardNoError: " & ServerErrors(1))
128InBlock.gif                Else
129InBlock.gif                    txtCardNo.Text = ServerResult(1)
130InBlock.gif                End If
131InBlock.gif            Catch ex As Exception
132InBlock.gif                MessageBox.Show("OPC server Open Card failed: " + ex.Message, "OPCSample", MessageBoxButtons.OK)
133InBlock.gif            End Try
134InBlock.gif            ResetControlStatus()
135InBlock.gif        End If
136ExpandedSubBlockEnd.gif    End Sub

137InBlock.gif
138InBlock.gif    '读取卡片指定的块号的值
139ExpandedSubBlockStart.gifContractedSubBlock.gif    Private Sub btnReadCard_Click()Sub btnReadCard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
140InBlock.gif        If Not (ConnectedGroup Is NothingThen
141InBlock.gif            Try
142InBlock.gif                '获取块号
143InBlock.gif                Dim BlockNo As Short = CByte(ReadBlockNo.Text)
144InBlock.gif                '如果要获取数据的块所对应的项还没有创建,就创建它
145InBlock.gif                If GlobalOPCBlockItems(BlockNo) Is Nothing Then
146InBlock.gif                    GlobalOPCBlockItems(BlockNo) = ConnectedGroup.OPCItems.AddItem("Reader_Device.Block" & CStr(BlockNo), 200 + BlockNo)
147InBlock.gif                End If
148InBlock.gif                '准备参数数组
149InBlock.gif                Dim ServerResults As System.Array
150InBlock.gif                Dim ServerErrors As System.Array
151InBlock.gif                Dim ServerHandles(1As Integer
152InBlock.gif                ServerHandles(1= GlobalOPCBlockItems(BlockNo).ServerHandle
153InBlock.gif                '读取值
154InBlock.gif                ConnectedGroup.SyncRead(OPCAutomation.OPCDataSource.OPCDevice, 1, ServerHandles, ServerResults, ServerErrors)
155InBlock.gif                If ServerErrors(1<> 0 Then
156InBlock.gif                    MsgBox("Read Card Failed:" & ServerErrors(1))
157InBlock.gif                Else
158InBlock.gif                    txtReadBlockNo.Text = ServerResults(1)
159InBlock.gif                End If
160InBlock.gif            Catch ex As Exception
161InBlock.gif                MessageBox.Show("OPC server Read Card failed: " + ex.Message, "OPCSample", MessageBoxButtons.OK)
162InBlock.gif            End Try
163InBlock.gif        End If
164ExpandedSubBlockEnd.gif    End Sub

165InBlock.gif
166InBlock.gif    '写卡片指定块的值
167ExpandedSubBlockStart.gifContractedSubBlock.gif    Private Sub btnWriteCard_Click()Sub btnWriteCard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
168InBlock.gif        If Not (ConnectedGroup Is NothingThen
169InBlock.gif            Try
170InBlock.gif                '获取块号
171InBlock.gif                Dim BlockNo As Short = CByte(WriteBlockNo.Text)
172InBlock.gif                '如果要写入数据的块所对应的项还没有创建,就创建它
173InBlock.gif                If GlobalOPCBlockItems(BlockNo) Is Nothing Then
174InBlock.gif                    GlobalOPCBlockItems(BlockNo) = ConnectedGroup.OPCItems.AddItem("Reader_Device.Block" & CStr(BlockNo), 200 + BlockNo)
175InBlock.gif                End If
176InBlock.gif                '准备参数数组
177InBlock.gif                Dim ServerValues(1As Object
178InBlock.gif                Dim ServerErrors As Array
179InBlock.gif                Dim ServerHandles(1As Integer
180InBlock.gif                ServerHandles(1= GlobalOPCBlockItems(BlockNo).ServerHandle
181InBlock.gif                ServerValues(1= txtWriteBlockNo.Text
182InBlock.gif                '写入值
183InBlock.gif                ConnectedGroup.SyncWrite(1, ServerHandles, ServerValues, ServerErrors)
184InBlock.gif                If ServerErrors(1<> 0 Then
185InBlock.gif                    MsgBox("Write Card Failed:" & ServerErrors(1))
186InBlock.gif                Else
187InBlock.gif                    MsgBox("Write Card Succeed")
188InBlock.gif                End If
189InBlock.gif            Catch ex As Exception
190InBlock.gif                MessageBox.Show("OPC server Write Card failed: " + ex.Message, "OPCSample", MessageBoxButtons.OK)
191InBlock.gif            End Try
192InBlock.gif        End If
193ExpandedSubBlockEnd.gif    End Sub

194InBlock.gif
195InBlock.gif    '重设控件状态
196ExpandedSubBlockStart.gifContractedSubBlock.gif    Private Sub ResetControlStatus()Sub ResetControlStatus()
197InBlock.gif        If ConnectedOPCServer Is Nothing Then
198InBlock.gif            btnConnectServer.Enabled = True
199InBlock.gif            btnDisconnectServer.Enabled = False
200InBlock.gif            btnReadCard.Enabled = False
201InBlock.gif            btnWriteCard.Enabled = False
202InBlock.gif            btnOpenCard.Enabled = False
203InBlock.gif            btnCloseCard.Enabled = False
204InBlock.gif            ReadBlockNo.Value = 0
205InBlock.gif            WriteBlockNo.Value = 0
206InBlock.gif            txtReadBlockNo.Text = ""
207InBlock.gif            txtWriteBlockNo.Text = "00000000000000000000000000000000"
208InBlock.gif            txtCardNo.Text = ""
209InBlock.gif
210InBlock.gif            txtSrvStartTime.Text = ""
211InBlock.gif            txtSrvCurrTime.Text = ""
212InBlock.gif            txtSrvGroupCount.Text = ""
213InBlock.gif            txtSrvGroupDeadBand.Text = ""
214InBlock.gif            txtSrvGroupDefActive.Text = ""
215InBlock.gif            txtSrvGroupLocalID.Text = ""
216InBlock.gif            txtSrvGroupTimeBias.Text = ""
217InBlock.gif            txtSrvRequestRate.Text = ""
218InBlock.gif        Else
219InBlock.gif            btnConnectServer.Enabled = False
220InBlock.gif            btnDisconnectServer.Enabled = True
221InBlock.gif            If txtCardNo.Text = "" Then
222InBlock.gif                btnReadCard.Enabled = False
223InBlock.gif                btnWriteCard.Enabled = False
224InBlock.gif                btnOpenCard.Enabled = True
225InBlock.gif                btnCloseCard.Enabled = False
226InBlock.gif            Else
227InBlock.gif                btnReadCard.Enabled = True
228InBlock.gif                btnWriteCard.Enabled = True
229InBlock.gif                btnOpenCard.Enabled = True
230InBlock.gif                btnCloseCard.Enabled = True
231InBlock.gif            End If
232InBlock.gif        End If
233ExpandedSubBlockEnd.gif    End Sub

234InBlock.gif
235InBlock.gif    '刷新服务器状态属性信息
236ExpandedSubBlockStart.gifContractedSubBlock.gif    Private Sub RefreshServerStatus()Sub RefreshServerStatus()
237InBlock.gif        If ConnectedOPCServer IsNot Nothing Then
238InBlock.gif            txtSrvStartTime.Text = ConnectedOPCServer.StartTime.ToString()
239InBlock.gif            txtSrvCurrTime.Text = ConnectedOPCServer.CurrentTime.ToString()
240InBlock.gif            With ConnectedOPCServer.OPCGroups
241InBlock.gif                txtSrvGroupCount.Text = CStr(.Count)
242InBlock.gif                txtSrvGroupDeadBand.Text = CStr(.DefaultGroupDeadband)
243InBlock.gif                If .DefaultGroupIsActive Then
244InBlock.gif                    txtSrvGroupDefActive.Text = "True"
245InBlock.gif                Else
246InBlock.gif                    txtSrvGroupDefActive.Text = "False"
247InBlock.gif                End If
248InBlock.gif                txtSrvGroupLocalID.Text = CStr(.DefaultGroupLocaleID)
249InBlock.gif                txtSrvGroupTimeBias.Text = CStr(.DefaultGroupTimeBias)
250InBlock.gif                txtSrvRequestRate.Text = CStr(.DefaultGroupUpdateRate)
251InBlock.gif            End With
252InBlock.gif        End If
253ExpandedSubBlockEnd.gif    End Sub

254InBlock.gif
255InBlock.gif    '关闭卡片
256ExpandedSubBlockStart.gifContractedSubBlock.gif    Private Sub btnCloseCard_Click()Sub btnCloseCard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
257InBlock.gif        If ConnectedGroup IsNot Nothing Then
258InBlock.gif            Try
259InBlock.gif                Dim ServerHandles(1As Integer
260InBlock.gif                Dim ServerValues(1As Object
261InBlock.gif                Dim ServerErrors As System.Array
262InBlock.gif                ServerHandles(1= GlobalOPCItems(1).ServerHandle
263InBlock.gif                ServerValues(1= 1
264InBlock.gif                ConnectedGroup.SyncWrite(1, ServerHandles, ServerValues, ServerErrors)
265InBlock.gif                If ServerErrors(1<> 0 Then
266InBlock.gif                    MsgBox("Close Card Error: " & ServerErrors(1))
267InBlock.gif                End If
268InBlock.gif            Catch ex As Exception
269InBlock.gif                MessageBox.Show("OPC server Close Card failed: " + ex.Message, "OPCSample", MessageBoxButtons.OK)
270InBlock.gif            End Try
271InBlock.gif        End If
272ExpandedSubBlockEnd.gif    End Sub

273ExpandedBlockEnd.gifEnd Class

274 None.gif

转载于:https://www.cnblogs.com/Phoenix-Rock/archive/2006/10/27/OPC.html

你可能感兴趣的:(OPC客户端的自动化实现)