Forums.MrPLC.com: OPC and RSlinx using Visual Basic 2005 - Forums.MrPLC.com

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

OPC and RSlinx using Visual Basic 2005 Rate Topic: -----

#1
User is offline   TechJunki 

  • Sparky
  • PipPipPip
  • Group: MrPLC Member
  • Posts: 272
  • Joined: 27-February 05
  • Location:Midwest
  • Country:United States
    United States
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

PublicClass Form1

Public MyRsLinxOPCServer As RsiOPCAuto.OPCServer

PublicWithEvents LinxOPCGroup As RsiOPCAuto.OPCGroup

Public LinxItem1 As RsiOPCAuto.OPCItem

Public LinxItem2 As RsiOPCAuto.OPCItem

PrivateSub Form1_Load(ByVal sender AsObject, ByVal e As EventArgs) HandlesMyBase.Load

Dim ItemCount AsShort

Dim TransID As Int64

Dim CancelID As Int64

Dim i AsInteger

'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)




EndSub

PrivateSub SyncRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SyncRead.Click

Dim ItemCount AsShort

Dim TransID As Int64

Dim CancelID As Int64

Dim i AsInteger

'Get Number of Items in Group From Server

ItemCount = LinxOPCGroup.OPCItems.Count

'Dim Arrays for SyncRead

Dim SyncServerHandles(ItemCount) AsInteger

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

EndSub

PrivateSub AsyncRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AsyncRead.Click

Dim ItemCount AsShort

Dim TransID As Int64

Dim CancelID As Int64

Dim i AsInteger

'Get Number of Items in Group From Server

ItemCount = LinxOPCGroup.OPCItems.Count

'Dim Arrays for SyncRead

Dim AsyncServerHandles(ItemCount) AsInteger

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

EndSub

PrivateSub LinxOPCGroup_AsyncReadComplete(ByVal TransactionID AsInteger, ByVal NumItems AsInteger, 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

EndSub



End
Class

Attached File(s)

  • Attached File  Code.txt (3.47K)
    Number of downloads: 1844

This post has been edited by TechJunki: 11 October 2006 - 02:50 AM

0

#2
Guest_trn_psycho_*

  • Group: Guest
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?????????????????????

This post has been edited by trn_psycho: 11 October 2006 - 01:39 PM

0

#3
User is offline   TechJunki 

  • Sparky
  • PipPipPip
  • Group: MrPLC Member
  • Posts: 272
  • Joined: 27-February 05
  • Location:Midwest
  • Country:United States
    United States

View Posttrn_psycho, on Oct 11 2006, 12:39 PM, said:

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?????????????????????


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)

This post has been edited by TechJunki: 12 October 2006 - 01:55 AM

0

#4
User is offline   BobLfoot 

  • The Wizard
  • Group: MrPLC Admin
  • Posts: 3,149
  • Joined: 27-March 06
  • Gender:Male
  • Location:Southern Indiana
  • Country:United States
    United States

View PostTechJunki, on Oct 11 2006, 03:41 AM, said:

I recently downloaded the new Visual Basic 2005 Express version from Microsoft for FREE, yes I said free.


Care to share the URL and save us all a lot of searching?
BobLfoot

"Poor Planning on your part does not a crisis on my part make"
0

#5
User is offline   Bob O 

  • Sparky
  • PipPipPip
  • Group: MrPLC Member
  • Posts: 479
  • Joined: 16-March 05
  • Country:United States
    United States

View PostBobLfoot, on Oct 12 2006, 04:37 AM, said:

View PostTechJunki, on Oct 11 2006, 03:41 AM, said:

I recently downloaded the new Visual Basic 2005 Express version from Microsoft for FREE, yes I said free.


Care to share the URL and save us all a lot of searching?


This should get you going....
http://msdn.microsof...studio/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
0

#6
Guest_trn_psycho_*

  • Group: Guest

View PostTechJunki, on Oct 12 2006, 01:52 AM, said:


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)


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!
0

#7
User is offline   autocontrolit 

  • Sparky
  • PipPipPip
  • Group: MrPLC Member
  • Posts: 43
  • Joined: 11-April 04
  • Country:United States
    United States
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
0

#8
User is offline   jmbvianney 

  • Hi, I am New!
  • Pip
  • Group: MrPLC Member
  • Posts: 2
  • Joined: 12-September 07
  • Country:United States
    United States
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,
0

#9
User is offline   sadlerej 

  • Sparky
  • PipPipPip
  • Group: MrPLC Member
  • Posts: 41
  • Joined: 07-December 04
  • Country:United States
    United States
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).
0

#10
User is offline   Mickey 

  • Propeller Head
  • PipPipPipPipPipPip
  • Group: MrPLC Member
  • Posts: 1,472
  • Joined: 01-December 04
  • Country:United States
    United States

View Postjmbvianney, on Oct 4 2007, 02:28 AM, said:

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,


You might find this useful:

http://www.plctalk.net/qanda/showthread.ph...ht=free+utility
0

#11
User is offline   kiyoung 

  • Hi, I am New!
  • Pip
  • Group: MrPLC Member
  • Posts: 1
  • Joined: 11-March 09
  • Country:Korea - South
    Korea - South
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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users