Sign in to follow this  
Followers 0
TechJunki

OPC and RSlinx using Visual Basic 2005

12 posts in this topic

I recently downloaded the new Visual Basic 2005 Express version from Microsoft for FREE, yes I said free. I know I have seen quite a few posts about using OPC with with the newer .NET versions of VB. Being quite comfortable with VB6 and OPC i gave this a shot and have successfully compiled code for both SyncRead and AsyncRead operations. I have not yet looked into the write operations but thought I would post the code I have for everyones viewing pleasure.. If anyone has an older version of VB.. .NET 2003 or .NET 2002 I would be interested in knowing if this code is compatible. ++ Be sure to add the reference "Rockwell Software OPC Automation" ++ Imports RsiOPCAuto Public Class Form1 Public MyRsLinxOPCServer As RsiOPCAuto.OPCServer Public WithEvents LinxOPCGroup As RsiOPCAuto.OPCGroup Public LinxItem1 As RsiOPCAuto.OPCItem Public LinxItem2 As RsiOPCAuto.OPCItem Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load Dim ItemCount As Short Dim TransID As Int64 Dim CancelID As Int64 Dim i As Integer 'Create Server MyRsLinxOPCServer = New RsiOPCAuto.OPCServer 'Connect to Server MyRsLinxOPCServer.Connect("RSLinx OPC Server") 'Add Group to Server LinxOPCGroup = MyRsLinxOPCServer.OPCGroups.Add("TestGroup") 'Group name can be anything you want it to be 'Set Group Active for Data Updates and Events LinxOPCGroup.IsActive = True 'Group Update Rate LinxOPCGroup.UpdateRate = 1000 LinxOPCGroup.IsSubscribed = True 'Add Items to Group, the specifier on the end Needs to be Unique for each Tag LinxItem1 = LinxOPCGroup.OPCItems.AddItem("[PLC_TOPIC]TAGE_NAME1,L10,C1", 1) LinxItem2 = LinxOPCGroup.OPCItems.AddItem("[PLC_TOPIC]TAGE_NAME2,L10,C1", 2) End Sub Private Sub SyncRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SyncRead.Click Dim ItemCount As Short Dim TransID As Int64 Dim CancelID As Int64 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 '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) Dim MyData As System.Array MyData = SyncValues End Sub Private Sub AsyncRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AsyncRead.Click Dim ItemCount As Short Dim TransID As Int64 Dim CancelID As Int64 Dim i As Integer 'Get Number of Items in Group From Server ItemCount = LinxOPCGroup.OPCItems.Count 'Dim Arrays for SyncRead Dim AsyncServerHandles(ItemCount) As Integer Dim AsyncErrors As System.Array Dim AsyncValues As System.Array 'Pass in the ServerHandles for the items For i = 1 To ItemCount AsyncServerHandles(i) = LinxOPCGroup.OPCItems.Item(i).ServerHandle Next 'Execute statement to Read Data from Group LinxOPCGroup.AsyncRead(ItemCount, AsyncServerHandles, AsyncErrors, TransID, CancelID) 'Call Sub When Read is Completed -- AddHandler (LinxOPCGroup.AsyncReadComplete), AddressOf LinxOPCGroup_AsyncReadComplete End Sub Private Sub LinxOPCGroup_AsyncReadComplete(ByVal TransactionID As Integer, ByVal NumItems As Integer, ByRef ClientHandles As System.Array, ByRef ItemValues As System.Array, ByRef Qualities As System.Array, ByRef TimeStamps As System.Array, ByRef Errors As System.Array) Handles LinxOPCGroup.AsyncReadComplete Dim MyData As System.Array MyData = ItemValues End Sub End Class Code.txt Edited by TechJunki

Share this post


Link to post
Share on other sites
That's defiantly a start, but I keep getting an error when it reaches this operation: LinxOPCGroup.SyncRead(1, ItemCount, SyncServerHandles, SyncValues, SyncErrors) It may just be because I've tried to convert it to C#, but it says that there isn't an overload for SyncRead that only takes 5 arguments. It's looking for 7. The last two are quality and timestamp objects????????????????????? Edited by trn_psycho

Share this post


Link to post
Share on other sites
I added the last two arguments as Objects in VB an it still works fine. Does the AsyncRead work?? Dim SyncQualities As Object Dim SyncTime As Object LinxOPCGroup.SyncRead(1, ItemCount, SyncServerHandles, SyncValues, SyncErrors, SyncQualities, SyncTime) Edited by TechJunki

Share this post


Link to post
Share on other sites
Care to share the URL and save us all a lot of searching?

Share this post


Link to post
Share on other sites
This should get you going.... http://msdn.microsoft.com/vstudio/express/ I started taking the class this quater and was told that downloading and installing from the NET was a PIA. My copy came in the text. Hope this helps, Bob

Share this post


Link to post
Share on other sites
No... But I figured it was for the same reason... After adding them in, I"m faced with more errors. (I may have to just stick with VB on this.) For starters, it's telling me that there are invalid arguments. Then when I correct what I think is an invalid arg, I'll get messages about not being able to convert type System.Array[] to int[]. I've tried playing with different types for the calling function, but there is too much type conversions going on that you don't have to worry about in VB that C# just screams about!

Share this post


Link to post
Share on other sites
Cool I just started working on a project using Visual Studio 2005 to build a Data Collection package can you show a screen shot of the form you are using? Also here is a link I found helpful from AB it is using OPC connection Sync and Async reads in a Excel Spreadsheet (VBA). http://domino.automation.rockwell.com/appl...5256AFB0059FE9F

Share this post


Link to post
Share on other sites
Thank you for an excellent example. I modified it to use with a PLC 5 and have been able to read individual PLC words using it. However, I would like to read and write entire blocks and so far have not been able to accomplish that. It appears that your example is for Control Logix. In your code, the following line appears to contain a length designation 'L10'. LinxItem1 = LinxOPCGroup.OPCItems.AddItem("[PLC_TOPIC]TAGE_NAME1,L10,C1", 1) I have not been able to translate this line into something that lets me read more than a single word of data. After a lot of searching and little success it seemed to me that I needed to instead use AddItems rather than AddItem. With AddItems, I have not been able to figure out how to define the second parameter in the method (ItemIDs). The method wants an array but it also needs to know 'where to look'. So I am quite confused about how to achieve block reads/writes. From the Visual Basic 2005 Object Browser: AddItems(ByVal NumItems As Integer, ByRef ItemIDs As System.Array, ByRef ClientHandles As System.Array, ByRef ServerHandles As System.Array, ByRef Errors As System.Array, Optional ByVal RequestedDataTypes As Object = Nothing, Optional ByVal AccessPaths As Object = Nothing) Any help would be greatly appreciated. Thanks,

Share this post


Link to post
Share on other sites
I have recently completed a project that uses Microsoft visual basic 2005 express edition, Microsoft sql 2005 express edition and Microsoft report server. I gather data from my machine using Softwaretoolbox opc server bring that into vb 2005 EE from there log the data to sql server (download the advanced sql) it has Business Intelligence Development Studio that has Report Server built in. Report Server is a handy tool for creating simple web pages to display on your intranet. The one thing to watch out for with 2005 EE is that vb and sql need to be on same machine (no remote access of database).

Share this post


Link to post
Share on other sites
You might find this useful: http://www.plctalk.net/qanda/showthread.ph...ht=free+utility

Share this post


Link to post
Share on other sites
Thank you for excellent example. please help me to understand the meaning of following line. Especially I have no idea about "L10" and "C1".... [PLC_TOPIC]TAGE_NAME1,L10,C1
1 person likes this

Share this post


Link to post
Share on other sites

Hey guys, I'm new to this subject. I've been trying to make a utility to help with the checkout of AB PLC controlled machines. I already have made a VBA macro which uses the DDE commands to read from the PLC and now would like to make a similar program in VBA or vb.net which uses OPC. Right now, I can setup the OPC server in RSLINX and then look at it with the RSLINX OPC test client. I have tried to make my own OPC test client using the OPCautomation reference in vb but with no success. So, for now I'm limited to pasting links into excel.  Any help would be greatly appreciated. Thanks

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