Answered by:
Connecting a OPC-Server in VB.Net

Question
-
Hi all!
I would like to create a OPC Client... But fist of all how do I connect a OPC-Server to me application...anyone who have done this? and maby hade some sample code??
Sunday, December 9, 2007 9:22 PM
Answers
-
Hi edmund,
Please check this sample about how to connect and disconnect to OPC server.
Prerequisites: ComboBox control named cbbServerList, Button1 and Button2 on Form1.
Code BlockPublic Class Form1
Dim GlobalOPCServer As OPCAutomation.OPCServerClass
Dim WithEvents ConnectedOPCServer As OPCAutomation.OPCServerClass
Dim WithEvents ConnectedGroup As OPCAutomation.OPCGroupClass
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)
cbbServerList.Items.Add(ServerList(index))
Next
If cbbServerList.Items.Count > 0 Then
cbbServerList.SelectedIndex = 0
End If
GlobalOPCServer = Nothing
Catch Ex As Exception
MessageBox.Show("List OPC servers failed: " + Ex.Message, "OPCSample", MessageBoxButtons.OK)
End Try
End Sub
' Connect to OPC server
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If cbbServerList.Text <> "" Then
ConnectedOPCServer = New OPCAutomation.OPCServerClass()
Try
ConnectedOPCServer.Connect(cbbServerList.Text)
'Set property for Group connection
ConnectedOPCServer.OPCGroups.DefaultGroupIsActive = True
ConnectedOPCServer.OPCGroups.DefaultGroupDeadband = 0
'Add group
ConnectedGroup = ConnectedOPCServer.OPCGroups.Add()
ConnectedGroup.UpdateRate = 3 * 1000
ConnectedGroup.IsSubscribed = True
'Add items
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)
Catch ex As Exception
ConnectedOPCServer = Nothing
MessageBox.Show("OPC server connect failed : " + ex.Message, "OPCSample", MessageBoxButtons.OK)
End Try
End If
End Sub
' Disconnect to OPC server
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.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
End Try
End If
End Sub
End Class
Check this thread for detail code sample:
http://www.cnblogs.com/Phoenix-Rock/archive/2007/11/07/541696.html
Similar issue: OPC Automation
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=324042&SiteID=1
Regards,
Martin
Wednesday, December 12, 2007 5:47 AM
All replies
-
Hi edmund,
Please check this sample about how to connect and disconnect to OPC server.
Prerequisites: ComboBox control named cbbServerList, Button1 and Button2 on Form1.
Code BlockPublic Class Form1
Dim GlobalOPCServer As OPCAutomation.OPCServerClass
Dim WithEvents ConnectedOPCServer As OPCAutomation.OPCServerClass
Dim WithEvents ConnectedGroup As OPCAutomation.OPCGroupClass
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)
cbbServerList.Items.Add(ServerList(index))
Next
If cbbServerList.Items.Count > 0 Then
cbbServerList.SelectedIndex = 0
End If
GlobalOPCServer = Nothing
Catch Ex As Exception
MessageBox.Show("List OPC servers failed: " + Ex.Message, "OPCSample", MessageBoxButtons.OK)
End Try
End Sub
' Connect to OPC server
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If cbbServerList.Text <> "" Then
ConnectedOPCServer = New OPCAutomation.OPCServerClass()
Try
ConnectedOPCServer.Connect(cbbServerList.Text)
'Set property for Group connection
ConnectedOPCServer.OPCGroups.DefaultGroupIsActive = True
ConnectedOPCServer.OPCGroups.DefaultGroupDeadband = 0
'Add group
ConnectedGroup = ConnectedOPCServer.OPCGroups.Add()
ConnectedGroup.UpdateRate = 3 * 1000
ConnectedGroup.IsSubscribed = True
'Add items
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)
Catch ex As Exception
ConnectedOPCServer = Nothing
MessageBox.Show("OPC server connect failed : " + ex.Message, "OPCSample", MessageBoxButtons.OK)
End Try
End If
End Sub
' Disconnect to OPC server
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.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
End Try
End If
End Sub
End Class
Check this thread for detail code sample:
http://www.cnblogs.com/Phoenix-Rock/archive/2007/11/07/541696.html
Similar issue: OPC Automation
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=324042&SiteID=1
Regards,
Martin
Wednesday, December 12, 2007 5:47 AM -
Hey Martin... Thanks for your reply...
How shuld I declare GlobalOPCItems???
Iv´e been trying Dim GlobalOPCItems(0 To 2) As OPCAutomation.OPCItems ... but not sure about this...
When I run the form, I can find my installed OPC Server in the combobox but when I try to connect it get
"OPC-Server connection Failed : Not possible to convert an object of type OPC.Automation.OPCServerClass to type OPC.Automation.IOPCGroups"
(Get the message in swedish so not 100% sure of the translation).
Hopefully you can help me with this...
Regards
/edmund
Sunday, January 27, 2008 1:06 PM -
http://www.faweb.net/us/opc/sample_vbnet.html
Here is one easy sample program (VB.NET) to communicate with the OPC Server using OPC Automation Wrapper (OPCDAAUTO.DLL).
The sample program can be downloaded here.
In this sample, it executes connecting to OPC Server, reading data automatically, reading data manually and writing data manually.
Tuesday, March 25, 2008 11:52 AM -
There are several threads on this forum dealing with the OPC Automation wrapper, so I thought I'd throw in my two cents...
Some important background to know about the Automation wrappers and versions. There actually is a "General" OPC interface dll. It's called the Automation Wrapper, or OPCDAAuto.dll. When the first OPC DA specifications were released, the OPC Foundation did not wish to support complied applications, so the source code for the OPCDAAuto.dll was supplied to all OPC members. Each vendor then recompiled the code under their own GUID (for example Rockwell renamed it RSiOPCAuto.dll). In November of 2005, the OPC Foundation decided to take the responsibility of maintaining various applications (DA Wrapper, Compliance tests, proxy stubs etc). All the bug fixes various vendors had applied over the years was compiled and the OPC Foundation released the 'official' OPCDAAuto.dll v2.02. Many vendors already were supporting 'their' wrapper so continue to ship the vendor branded version.
Version 2.02 of the automation wrapper has been tested with VB.NET on Win2000 and WinXP. You can read the details on the Foundation website at
http://www.opcfoundation.org/DownloadFile.aspx?CM=3&RI=305&CN=KEY&CI=274&CU=29
If you are an OPC Foundation member you can download the files directly. If you are an end user who is not a member, you need to get the binaries from your OPC Server Vendor.
Here's some additional information from the OPC Foundation supplied ntoes:
It is important to note that both the DA Automation Wrapper and the OPC.NET API allow .NET based OPC client applications to communicate with existing COM based OPC servers. However users should keep in mind that the DA Automation Wrapper was intended to used only as a way to facilitate porting of existing VB 6.0 applications to VB .NET. The OPC Foundation recommends that any new VB .NET client development use the .NET API for the following reasons:- it provides almost all of the features of the DA automation wrapper;
- it supports Data Access 3.00 and XML Data Access 1.00 servers;
- it takes full advantage of new .NET features such as object serialization;
- it requires one less layer of code between the application and the server;
- it provides a common API framework for all specifications;
- it will be the starting point for the Unified Architecture (UA) .NET API;
The OPC.NET API is available to OPC Foundation members.
Hope that helps!
Eric Murphy - OPC Exchange - http://blog.matrikonopc.com
Tuesday, May 5, 2009 5:33 PM -
Hi Everybody,
I had the following problem, when im trying to connect a OPC Server: "The RPC server is not avaliable."
OBS: "RPC = Remote Procedure Call"
Someone can help-me ?
Regards
Thiago Arreguy
IT Gerdau Acominas - BrasilSunday, September 27, 2009 3:46 PM -
I have the same problem .. what have you resolved?Thursday, April 1, 2010 12:19 AM
-
I have the same problem .. what have you resolved?
http://bytes.com/topic/net/answers/451395-outlook-maiiteml-move-rpc-server-not-available
http://www.geekinterview.com/talk/1420-the-rpc-server-is-not-available.html
Just Be Humble Malange!Friday, April 2, 2010 12:22 PM -
I used Martin's code and received the following error:
Exception from HRESULT: 0xC004007
System.Runtime.InteropServices.COMException
When I stepped into the Debug everything worked fine until I reached the "GlobalOPCItems(0) = " lines. This is the point at which it errors out. My belief is that I may not be declaring or defining GlobalOPCItems correctly. Some clarification on this would be helpfull.
There are no apparent errors in the code, and my research came up with little in the way of being able to solve this problem. Any assistance would be much appreciated.
- Edited by leocok2057 Thursday, April 5, 2012 5:30 PM More Research in cause.
Thursday, April 5, 2012 4:23 PM -
try this instead:
Public GlobalOPCItems(0 To 2) As OPCItem
Friday, October 5, 2012 10:48 AM