Read/Write To SoftLogix Tags With OPC And VB


Thread Starter

Kris Grindstaff

I want to read and write to tags in a SoftLogix (ControlLogix) using Visual Basic. I will be using RSLinx Pro with OPC. Can someone show me how this is done? Any code samples would be great. Thanks!!!
Within the VB Development enviroment add a reference to the RSLinx OPC automation DLL. For
RsLinx Versions lower than 2.30 it is called RsLinxOPCAuto.DLL otherwise it is called RsiOPCAuto.DLL. This DLL exposes the RSLinx OPC Server automation interfaces to the client application .

Within your program or class module declare the following objects :

[Dim or Private] WithEvents oServer As OPCServer
[Dim or Private] WithEvents oGroup As OPCGroup
[Dim or Private] oItems As OPCItems

** The WithEvents keyword is used if you want to utilize the Event sink attributes of the OPCServer and OPCGroup Objects.

The following code illustrates how to connect to the OPC server and create an OPC group:

set oServer = new OPCServer
oServer.Connect "RSLinx OPC Server"
Set oGroup = oServer.OPCGroups.Add(mvaropcGroup)
oGroup.OPCItems.DefaultAccessPath = mDAccessPath
Set oItems = oGroup.OPCItems
oGroup.IsActive = False
oGroup.IsSubscribed = True
oGroup.UpdateRate = 250

** The Default access path is a string that contains the name of a valid DDE/OPC topic that has been created in RSLinx and points to appropriate controller (Softlogix or ControlLogix). This access path applies to all
items within a specific group. To specify individual paths per item use the following syntax: [Path]Tag Name for each item.

** to see the Interfaces/objects exposed by the RSLinx OPC Automation DLL use the VB Object browser.

Next you need to add OPC items to the created OPCGroup.
These items represent PLC tags and should follow the Allen-Bradley naming conventions eg. PLC-5 N7:0 or N7:0,L10 (specifies an array of ten contigious elements). This is accomplished by utilizing the "oGroup.OpcItems.AddItems" function. It is important to dimension the associated input arrays with a base of 1. This is a requirement imposed in the OPC automation specification. otherwise you will get errors. When you have successfully added the appropriate items you need to set the "oGroup.IsActive" property to "True" such that RSLinx will start
reading the desired Tags.

To read/write specific Tags you can utilize the following functions:
1) oGroup.AsyncRead or oGroup.AsyncWrite (asynchronous access)
2) oGroup.SyncRead or oGroup.SyncWrite
3) oItem.Read or oItem.Write (Synchronous only)
4) If you only need the values when they change add code to the oGroup_DataChange event (the "oGroup.IsSubscribed" property must be set to True)

For additional information check the OPC foundation site for "OPCDA20_AUTO.doc" or "" and the Rockwell Automation knowledge base for sample code "".

I hope this gets you on your way.
P. West

Kris Grindstaff

Do I have to add each tag individually? I would like to be able to access all tags in the PLC no matter what they are.