?? opcgroupclass.cls
字號:
' parameters are acceptable to the server no error will be returned.
Function ValidateOPCItem(ByVal OPCItemID As String, ByVal DataType As Integer, ByVal ActiveState As Integer, ByRef Error As Long)
Dim Errors() As Long
Dim ItemCount As Long
Dim OPCItemIDs(1) As String
Dim RequestedDataTypes(1) As Integer
'Set error handling for OPC Function
On Error GoTo ShowOPCItemValidateError
' Establish the initial default conditions for new items added to this
' group.
SetOPCItemsDefaultActive (ActiveState)
SetOPCItemsDefaultDataType (DataType)
OPCItemIDs(1) = OPCItemID
RequestedDataTypes(1) = DataType
ItemCount = 1
' Test the item
OPCGroupObj.OPCItems.Validate ItemCount, OPCItemIDs, Errors, RequestedDataTypes
' return any errors
If Errors(1) <> 0 Then
Error = Errors(1)
ValidateOPCItem = False
GoTo SkipValidateItemError
End If
ValidateOPCItem = True
GoTo SkipValidateItemError
ShowOPCItemValidateError:
Call DisplayOPC_COM_ErrorValue("Validate Item", Err.Number)
ValidateOPCItem = False
SkipValidateItemError:
End Function
' This function performs a single write using the AsyncWrite method
' of the OPCGroup object that is part of the Automation Interface.
' OPC data can be written in one of two ways. Using the Asynchronous
' write as shown here or using a Sychronous write. Both methods have
' their benefits. The Synchronous method has the benefit of simplicity.
' When you perform a sync write operation the call is made to the OPC server
' and the server does not return until the write has been completed. From
' a coding standpoint that can be an easy thing to deal with. From an
' actual application standpoint it can present some interesting problems.
' As the name implies a Synchronous write operation is synchonized to
' operation of the target OPC server. When a sync write is issued the call
' will not return until the server has completed the whole write operation.
' If the communications driver you are using is perhaps a serial device
' going over a slow modem link this pause before the sync write returns
' can be considerable. Having this delay occur in your VB application
' could cause the end user to perceive your VB app as as having bad
' performance simply because it needs to wait for a write to complete.
'
' There is a better way.
'
' The Asynchronous write operation differs from the Synchronous operation
' in two key ways. First, the Async write does not require the VB
' application to wait until the OPC server completes the write. Second,
' the Async write requires an event handler to receive the notification
' that the write has completed.
'
' Returning immediately to your VB application from the AsyncWrite
' call makes using the method as easy as the Sync write call but
' an event handler for the write must be used if you want to know
' the true actual result of the write operation. In this example
' only a single value is written for simplicity. In your
' application a list of items can be passed to the function
' with equal ease. If an error occurs in processing the command
' you will still see that error returned on the return from the
' AsyncWrite method.
'
' The method you use for writing data to an OPC server can be a
' factor of your application. If there is no driving reason to use
' a synchronous operation I strongly urge you to use the asynchronous
' methods. The asynchronous methods allow the OPC server to schedule
' the operaiton you have requested such that it can be processed
' as soon as the server can finish its current operations. The
' synchronous methods force the OPC server to stop in its tracks
' and attempt to complete the sync operation. While this may sound
' ideal, it prevents an OPC server from ever being able to optimize
' your overall communications requirements. If you used all syncchronous
' operations for both read and write data, the OPC server would never
' be able to take advantage of the protocol optimizations that most if
' not all OPC servers perform.
'
Function AsyncWriteOPCItem(ObjectToWrite As OPCItemClass, ValueToWrite As Variant)
Dim ServerHandles(1) As Long
Dim Errors() As Long
Dim Count As Long
Dim TransactionID As Long
Dim CancelID As Long
Dim Values(1) As Variant
'Set error handling for OPC Function
On Error GoTo ShowOPCItemWriteError
' Currently the write function is designe to write only
' one value at a time. This can easily be changed by simly
' increasing the number of item passed in these arrays.
Count = 1
' Get the handle of the server we intend to write.
ServerHandles(1) = ObjectToWrite.GetItemServerHandle
' Since we are doing only one write and it is driven by
' User input the TransactionID can be kept relatively simple.
' If you expand the write function to write a random number of
' item you will want to use a more complex scheme for a
' TransactionID. Also keep in mind that you can issue muliple
' Async write functions which may not complete in any specific
' order. With that said use a TransactionID method that can fit
' your application.
TransactionID = 1
' This example writes only a sinlge value at a time but can be
' to send a list of items to the server in a single call. The
' Transaction ID becomes very crucial when sending more than
' one write to the server. If you send a list of items they may
' not return in a single event handler call and they may not
' return in any specific order.
Values(1) = ValueToWrite
' Perform the write.
OPCGroupObj.AsyncWrite Count, ServerHandles, Values, Errors, TransactionID, CancelID
AsyncWriteOPCItem = True
GoTo SkipItemWriteError
ShowOPCItemWriteError:
Call DisplayOPC_COM_ErrorValue("Write Item", Err.Number)
AsyncWriteOPCItem = False
SkipItemWriteError:
End Function
' Handles displaying any OPC/COM/VB errors that are caught by the exception handler
Sub DisplayOPC_COM_ErrorValue(OPC_Function As String, ErrorCode As Long)
Dim Response
Dim ErrorDisplay As String
ErrorDisplay = "The OPC function '" + OPC_Function + "' has returned an error of " + Str(ErrorCode) + " or Hex 0x" + Hex(ErrorCode)
Response = MsgBox(ErrorDisplay, vbOKOnly, "OPC Function Error")
End Sub
' If you have something to do when an OPCGroupClass object is created put it here.
Private Sub Class_Initialize()
' To Do
End Sub
' If you have somehting to do when an OPCGroupClass object is released put it here.
Private Sub Class_Terminate()
'To Do We have nothing to be released at this time
End Sub
' ************************************************************
' The Next section of the OPCGroupClass are the event handlers
' This sub handles the 'DataChange' call back event which returns data that has
' been detected as changed within the OPC Server. This call back should be
' used primarily to receive the data. Do not make any other calls back into
' the OPC server from this call back. The other item related functions covered
' in this example have shown how the ItemServerHandle is used to control and
' manipulate individual items in the OPC server. The 'DataChange' event allows
' us to see how the 'ClientHandles we gave the OPC Server when adding items are
' used. As you can see here the server returns the 'ClientHandles' as an array.
' The number of item returned in this event can change from trigger to trigger
' so don't count on always getting a 1 to 1 match with the number of items
' you have registered. That's where the 'ClientHandles' come into play. Using
' the 'ClientHandles' returned here you can determine what data has changed and
' where in your application the data should go. In this example the
' 'ClientHandles' were the Index number of each item we added to the group.
' Using this returned index number the 'DataChange' handler shown here knows
' what items in the collection need to be updated with new data.
'
Private Sub OPCGroupObj_DataChange(ByVal TransactionID As Long, ByVal NumItems As Long, ClientHandles() As Long, ItemValues() As Variant, Qualities() As Long, TimeStamps() As Date)
' We don't have error handling here since this is an event called from the OPC interface
On Error Resume Next
Dim UpdateItem As OPCItemClass
Dim i As Integer
For i = 1 To NumItems
' Get OPCItemClass object from the collection that is kept by
' the OPCGroupClass object. This is found by using the client handle
' returned from the server for each item being returned in this event
' as the Item Key within the collection.
Set UpdateItem = OPCGroupItems.Item(Str(ClientHandles(i)))
' If the item is valid then update the contents of the item. For this
' example the contents of the item will then be displayed in the
' list box of the application. For your application you can be assured
' that any time you need to access an OPC item in this group that it
' will contain the latest data. Like the Listbox used in this example,
' your application must keep track of the data item it desires by
' using the item collection key.
If Not UpdateItem Is Nothing Then
UpdateItem.UpdateOPCItemData ItemValues(i), Qualities(i), TimeStamps(i)
End If
Next i
End Sub
' This sub handles 'AsyncWriteComplete' call back event that occurs
' whenever an AsyncWrite operation is issued to the OPC server. As
' discussed in the AsyncWriteOPCItem, the AsyncWriteComplete call back
' event allows your application to determine the results of a write
' operation that has been issued at some other point in your application.
' When using the asynchronous write function there is no way to know how
' or if a write operation has completed successfully when the write is
' issued. The server must inform your application of the results of the
' write operation. Using the transaction ID and ClientHandles you can
' associate each complete event with a specific OPC item. In this example
' the Item quality field of an item is simply updated to reflect the results
' of the write.
'
' In your application may want to use the asynchronous write complete event
' as a trigger to send addtional data to the OPC server. In all cases it is
' suggested that no other OPC operation be issued from within the call
' back event.
Private Sub OPCGroupObj_AsyncWriteComplete(ByVal TransactionID As Long, ByVal NumItems As Long, ClientHandles() As Long, Errors() As Long)
Dim UpdateItem As OPCItemClass
Dim i As Integer
For i = 1 To NumItems
' Get OPCItemClass object from the collection that is kept by
' the OPCGroupClass object. This is found by using the client handle
' returned from the server for each item being returned in this event
' as the Item Key within the collection.
Set UpdateItem = OPCGroupItems.Item(Str(ClientHandles(i)))
' If the item is valid then check the error flag for the item and
' clear the item's quality field. Setting to zero is considered an
' error for an OPC item quality value.
If Not UpdateItem Is Nothing Then
If Errors(i) Then
UpdateItem.SetItemQuality (0)
End If
End If
Next i
End Sub
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -