<< Click to Display Table of Contents >> Manual > Server Creation Guide > Interface > OPC Interface > OPC connection from VisualBasic.NET > Step 2: Basic construction of VB.NET |
Step 2: Basic construction of VB.NET
We will explain the basic construction details on the VB.NET side.
Please note that the following is only an overview, so please check the sample for details.
The “OPCDAAuto.dll” used in this section can be referenced from VB.NET etc. by installing FA-Server. |
Library References
To connect to the OPC server, you need to use "OPCDAAuto.dll", so you need to set a reference to the library.
1.Select "Project" - "Add Reference"
2.Select "OPC DA Automation Wrapper" from "COM"
If it is not in the list, click "Reference" and reference the following DLL directly.
C:\Program Files (x86)\Common Files\Roboticsware\Ver6\opc\OPCDAAuto.dll
3.Select "View" - "Object Browser"
4.Verify that the OPCAutomation library is referenced and the list of published interfaces is displayed.
Creating a OPC object
To access an OPC server from Visual or Basic, use the OPC Automation interface.
There are four main interfaces:
•Server Interface
•Group Collection Interface
•Item Collection Interface
•Browser interface
This section describes how to get and create server, group, and item objects.
Specifically, we will establish a connection with the OPC server, create a group, and add items.
1.Preparing properties for automation
Declares various constants and properties that store the OPC automation object. The property type specifies the OPC automation class.
Private mvServer As OPCAutomation.OPCServer Private mvGroups As OPCAutomation.OPCGroups Private mvItems As OPCAutomation.OPCItems Private mvCHandles(mcItemCount) As Integer Private mvSHandles As Array = Nothing Private mvReadTxId As Integer Private mvReadCancelId As Integer Private mvWriteTxId As Integer Private mvWriteCancelId As Integer Private WithEvents mvGroup As OPCAutomation.OPCGroup |
type |
Property Name |
meaning |
OPCServer |
mvServer |
Used to connect to the OPC server. |
OPCGroup |
mvGroups |
Used to generate OPC groups. |
OPCitems |
mvItems |
Used to register OPC items. |
futeger |
mvCHandles(Nitems) |
OPC The handle by which a client identifies an item. |
Array |
mvSHandles |
OPCThe handle by which the server identifies the item. |
OPCGroup |
mvGroup |
Used to set up the OPC group |
2.Connecting to the OPC Server
Create an instance of the "OPCAutomation.OPCServer" class and issue the Connect method to the created object.
Private Function FnConnect(ByVal vProgId As String, Optional ByVal vNode As Object = Nothing) Try mvServer = New OPCAutomation.OPCServer mvServer.Connect(vProgId, vNode)
3.Adding a group
Issue the Add method to the OPC group object to add a group. Specify the properties accordingly.
Here, we will use the asynchronous method later, so set the lsSubscribed property to True.
Private Function FnAddGroup() Try 'AddGroup mvGroups = mvServer.OPCGroups mvGroup = mvGroups.Add(mvGroupName) 'Property Set mvGroup.IsActive = False mvGroup.IsSubscribed = True mvGroup.UpdateRate = CInt(txtUpdateRate.Text)
4.Add items
Issue the Add method to the OPC item object to add the item.
Here we will register fixed items (U01.F01.T01 to U01.F01.T04), but in reality, items will be registered based on user specifications or configuration files.
Private Function FnAddItem() Try 'AddItem mvItems = mvGroup.OPCItems Dim Errors As Array = Nothing mvItems.AddItems(mcItemCount, mvItemName, mvCHandles, mvSHandles, Errors)
5.Disconnecting from the OPC server
The connection must be closed when the OPC application is closed.
The OPC server does not know when the OPC application has ended, so it will always perform a disconnection process.
Private Function FnDisconnect() Try ' Delete items and groups,Server disconnect If mvItems IsNot Nothing And mvSHandles IsNot Nothing Then Dim vErrors As Array = Nothing mvItems.Remove(mcItemCount, mvSHandles, vErrors) End If If mvGroups IsNot Nothing Then mvGroups.Remove(mvGroupName) End If If mvServer IsNot Nothing Then mvServer.Disconnect() End If 'Property Initialize mvSHandles = Nothing mvItems = Nothing mvGroup = Nothing mvGroups = Nothing mvServer = Nothing GC.Collect()