Sign in to follow this  
Followers 0
phuz

Talking OPC to RSLinx with VB.NET (VS2010)

3 posts in this topic

I am aware that VB6 used the OPCDAAUTO.dll wrapper to talk to the OPC server, but I am having some trouble figuring out how to communicate to RSLinx through VB.NET in Visual Studio 2010. Anyone have experience with this?

Share this post


Link to post
Share on other sites
OK, I've managed to get this far but I'm not getting any data returned and yet the code is executing properly. Any ideas? Imports OPCAutomation Imports System.Threading Public Class Form1 Private WithEvents _ConnectedOPCServer As OPCServer Private WithEvents _ConnectedGroup As OPCGroup Public GlobalOPCItems(0 To 2) As OPCItem Private Sub Connect() Handles Button1.Click Try ' Create a new OPC Server object _ConnectedOPCServer = New OPCServer ' Attempt to connect with the server _ConnectedOPCServer.Connect("RSLinx OPC Server") _ConnectedOPCServer.OPCGroups.DefaultGroupIsActive = True _ConnectedOPCServer.OPCGroups.DefaultGroupDeadband = 0 ' Add the group _ConnectedGroup = _ConnectedOPCServer.OPCGroups.Add() ' Set the update rate for the group _ConnectedGroup.UpdateRate = 1000 ' Mark this group to receive asynchronous updates via the DataChange event. _ConnectedGroup.IsSubscribed = True ' Setting the '.DefaultIsActive' property forces all items we are about to ' add to the group to be added in a non active state. _ConnectedGroup.OPCItems.DefaultIsActive = False GlobalOPCItems(0) = _ConnectedGroup.OPCItems.AddItem("[HOME]N7:10,L1,C1", 1) Catch ex As Exception _ConnectedOPCServer = Nothing MessageBox.Show("OPC server connect failed : " + ex.Message, "OPCSample", MessageBoxButtons.OK) End Try TextBox1.Text = GlobalOPCItems(0).Value End Sub End Class

Share this post


Link to post
Share on other sites
Module Module1 Public RsLinxOPCServer As OPCAutomation.OPCServer Public WithEvents LinxOPCGroup As OPCAutomation.OPCGroup Public Sub AddOPCTags(ByVal Tags, ByVal NumOfTags) Dim AddItemServerErrors As System.Array Dim TagName As String Dim i As Integer ' OPC Item related data Dim ItemServerHandles As System.Array Dim OPCItemIDs(NumOfTags) As String Dim ClientHandles(NumOfTags) As Int32 'Create Server RsLinxOPCServer = New OPCAutomation.OPCServer 'Connect to Server RsLinxOPCServer.Connect("RSLinx OPC Server") 'Add Group to Server LinxOPCGroup = RsLinxOPCServer.OPCGroups.Add("MyGroup") 'Set Group Active for Data Updates and Events LinxOPCGroup.IsActive = False 'Group Update Rate LinxOPCGroup.UpdateRate = 500 LinxOPCGroup.IsSubscribed = False 'Add Items to Group, the specifier on the end Needs to be Unique for each Tag 'Lengths of Arrays Can Not be longer than 100 Elements or RSLINX Will Error For i = 1 To 1 TagName = "" & "MyTagArray,L8,C1" OPCItemIDs(i) = TagName ClientHandles(i) = i Next ' Add Items to RsLinx Server LinxOPCGroup.OPCItems.AddItems(NumOfTags, OPCItemIDs, ClientHandles, ItemServerHandles, AddItemServerErrors) End Sub Public Sub SyncRead(ByVal Tags) Dim CurrentServerState As Integer Dim ItemCount As Short Dim i As Integer ' Get Number of Items in Group From Server ItemCount = LinxOPCGroup.OPCItems.Count ' Dim Arrays for SyncRead Dim SyncServerHandles(ItemCount) As Integer Dim SyncErrors As System.Array Dim SyncValues As System.Array Dim SyncQualities As Object Dim SyncTime As Object ' Pass in the ServerHandles for the items For i = 1 To ItemCount SyncServerHandles(i) = LinxOPCGroup.OPCItems.Item(i).ServerHandle Next ' Read Items From Group LinxOPCGroup.SyncRead(1, ItemCount, SyncServerHandles, SyncValues, SyncErrors, SyncQualities, SyncTime) ' Copy Data to MyData Globals.MyData = SyncValues 'Remove Groups RsLinxOPCServer.OPCGroups.RemoveAll() ' Disconnect OPC Server RsLinxOPCServer.Disconnect() End Sub End Module

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0