Step 3: Reading and writing values

<< Click to Display Table of Contents >>

Manual > Server Creation Guide > Interface > OPC Interface > OPC connection from VisualBasic.NET >

Step 3: Reading and writing values

Step 3: Reading and writing values

reading

 
There are several ways to read values from an OPC server.

 

method

Methods/Properties

explanation

Synchronous Read

SyncRead

Synchronously reads the data values, quality flags, and timestamps of items in a group.

Asynchronous Read

AsyncRead

Asynchronously reads the data value, quality flag, and timestamp of the item in the collection. The result is returned via the AsyncReadComplete event.

Change Notification

Subscription

If there is a change in the data, the OPC server will notify the OPC client of the value. The result will be returned via the Datachange event.

 

In the sample, you will select the method from a combo box when connecting.

Both synchronous and asynchronous reads are performed at regular intervals using a timer.

 

c_interface_0208

 

 

■ Synchronous reading

 

Synchronous reading is performed by issuing the SyncRead method to the OPC group object.

Private Sub timSyncRead_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timSyncRead.Tick
    Try
        Dim Errors As Array = Nothing
        Dim ItemValues As Array = Nothing
        Dim Qualities As Array = Nothing
        Dim TimeStamps As Array = Nothing
        mvGroup.SyncRead(OPCAutomation.OPCDataSource.OPCDevice, mcItemCount, mvSHandles, ItemValues, Errors, Qualities, TimeStamps)
        SbDrawData(mcItemCount, mvCHandles, ItemValues, Qualities, TimeStamps)

 

 

■ Asynchronous reading

 

Asynchronous reading is performed by issuing the AsyncRead method to the OPC group object.

Private Sub timAsyncRead_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timAsyncRead.Tick
    Try
        Dim Errors As Array = Nothing
        mvReadTxId = mvReadTxId + 1
        mvGroup.AsyncRead(mcItemCount, mvSHandles, Errors, mvReadTxId, mvReadCancelId)

 

The result will be received in the AsyncReadComplete event.

Private Sub Group_AsyncReadComplete(ByVal TransactionID As Integer, ByVal NumItems As Integer, ByRef ClientHandles As Array, ByRef ItemValues As Array, ByRef Qualities As Array, ByRef TimeStamps As Array, ByRef vErrors As Array) Handles mvGroup.AsyncReadComplete
    SbDrawData(NumItems, ClientHandles, ItemValues, Qualities, TimeStamps)

 

 

■ Change notification

 

When you set the IsActive property of a OPC group object to True, the group becomes active.

    Select Case CmbType.SelectedIndex
        Case 0  'SyncRead
            timSyncRead.Enabled = True
        Case 1  'AsyncRead
            timAsyncRead.Enabled = True
        Case 2  'Subscription
            mvGroup.IsActive = True
    End Select

 

When a change occurs in the data value or quality flag of an item in the group at the UpdateRate period, the Datachange event is generated.

Private Sub Group_DataChange(ByVal vTransactionID As Integer, ByVal NumItems As Integer, ByRef ClientHandles As Array, ByRef ItemValues As Array, ByRef Qualities As Array, ByRef TimeStamps As Array) Handles mvGroup.DataChange
    SbDrawData(NumItems, ClientHandles, ItemValues, Qualities, TimeStamps)
End Sub

 

 


write

 

There are several ways to write values to the OPC server.

 

method

Methods/Properties

explanation

Synchronous Write

SyncWrite

Writes data synchronously to the item specified by the server handle within the group.

Asynchronous Write

AsyncWrite

Asynchronously writes a data value to the item specified by the server handle in the group, and issues a transaction ID at the same time.

The write result is returned via the AsyncWriteComplete event.

 

 

■ Synchronous writing

 

Synchronous writing is performed by issuing the SyncWrite method to the OPC group object.

Private Sub btnSyncWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSyncWrite.Click
    Dim vServerHandles(mcItemCount) As Integer
    Dim vItemValues(mcItemCount) As Object
    Dim vNumItems = FnGetInputData(vServerHandles, vItemValues)
    If vNumItems > 0 Then
        Dim Errors As Array = Nothing
        mvGroup.SyncWrite(vNumItems, vServerHandles, vItemValues, Errors)
    End If
End Sub

 

 

■ Asynchronous writing

 

Asynchronous writing is performed by issuing the AsyncWrite method to the OPC group object.

Private Sub BtnASyncWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnASyncWrite.Click
    Dim vServerHandles(mcItemCount) As Integer
    Dim vItemValues(mcItemCount) As Object
    Dim vNumItems = FnGetInputData(vServerHandles, vItemValues)
    If vNumItems > 0 Then
        Dim Errors As Array = Nothing
        mvWriteTxId = mvWriteTxId + 1
        mvGroup.AsyncWrite(vNumItems, vServerHandles, vItemValues, Errors, mvWriteTxId, mvWriteCancelId)
    End If
End Sub

 

If you want to receive the write result, define AsyncWriteComplete and receive it.

 

 

hint

By using an array, one tag can handle a large amount of data as a block. For example, when transferring 10,000 words of data, instead of creating 10,000 individual tags, you can reduce the load by using one array tag for communication. When reading and writing the values of an array tag, you need to use an array variable to exchange values. However, in OPC programming, array variables are used as argument formats to exchange multiple items. Therefore, if you want to use an array tag here, you need to handle a two-dimensional array variable. For example, in the example of using the "SyncRead" method, you would write "mvoV(1)" to reference the item value, but in the case of an array tag, you can write "mvoV(1)(1)" to reference each device value of the array tag elements. For information on how to use arrays in VB.NET, see the VB.NET help.