Posted 16 Oct 2007 Does any one know of any examples of any Visual Basic OPC to RSLinx Examples? I have found plenty of DDE examples, but none using OPC. Any help is greatly appreciated! Share this post Link to post Share on other sites
Posted 16 Oct 2007 (edited) Here is a quick sample to read data... ' ' VB Example Code: Read single data element using OPC ' Dim OPCServer1 As OPCServer 'Create references for OPC Server Dim WithEvents OPCGroup1 As OPCGroup 'and Group objects Private Sub Form_Load() Set OPCServer1 = New OPCServer 'Create new instance of OPC Server OPCServer1.Connect "RSLinx OPC Server" 'Connect to OPC Server application Set OPCGroup1 = OPCServer1.OPCGroups.Add("MyOPCData") 'Create new OPC Group in defined OPC Server OPCGroup1.OPCItems.AddItem "[PLC]S:23", 1 'Add an item to defined OPC Group '"[AccessPath]DataItem", ClientHandle OPCGroup1.IsSubscribed = True 'Instruct OPC Server to send OCP Group data 'changes End Sub Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) Set OPCGroup1 = Nothing 'Remove OPC Group OPCServer1.Disconnect 'Disconnect from OPC server application Set OPCServer1 = Nothing 'Remove OPC Server End Sub Private Sub OPCGroup1_DataChange(ByVal TransactionID As Long, ByVal NumItems As Long, ClientHandles() As Long, ItemValues() As Variant, Qualities() As Long, TimeStamps() As Date) Text1.Text = ItemValues(1) 'Assign data to text box ' For x = 1 To NumItems 'Another method of handling incoming data using ' Select Case ClientHandles(x) 'the ClientHandle assigned in the AddItem method ' Case 1 ' ' Text1.Text = ItemValues(x) 'Do this when ClientHandle = 1 ' ' ' Case 2 'Can be easily adapted to handle more than one item ' 'ClientHandle=2 ' Case 3 ' ' 'Clienthandle=3 ' End Select ' ' Next x ' End Sub Be sure to add a ref to RSLink OPC Automation Edited 17 Oct 2007 by trn_psycho Share this post Link to post Share on other sites
Posted 17 Oct 2007 http://www.opcconnect.com/tooltech.php#vb Share this post Link to post Share on other sites
Posted 18 Oct 2007 I haven't been able to get trn_psycho's (thank you, trn_psycho) working properly, but I am pretty sure I know what the problem was. I was able to find another thread that had another example( http://forums.mrplc.com/index.php?showtopic=9292 ). I was able to get that to work, although both of these only shows "Reading" of values, and I am not having a lot of luck Writing values. After looking at Jesper's link (thank you, Jesper), I found an example of a Mitsubishi, which got me close, but not close enough. If any one knows of any other examples, I would definatly appricate it. Share this post Link to post Share on other sites
Posted 18 Oct 2007 (edited) Sorry, my examples have been taken directly from the RSTrainer for RSLinx CDs... Here is the write. In order for them to work, you MUST include a reference to RSLink OPC Automation, and you must configure the following to match your setup... . . . Private Sub Form_Load() Set OPCServer1 = New OPCServer 'Create new instance of OPC Server OPCServer1.Connect "RSLinx OPC Server" 'Connect to OPC Server application Set OPCGroup1 = OPCServer1.OPCGroups.Add("MyOPCData") 'Create new OPC Group in defined OPC Server OPCGroup1.OPCItems.AddItem "[PLC]T4:0.PRE", 1 'Add an item to defined OPC Group ' "[AccessPath]DataItem", ClientHandle OPCGroup1.IsSubscribed = True 'Instruct OPC Server to send OCP Group data 'changes End Sub . . . Write sample... ' ' VB Example Code: Write single data element using OPC ' Dim OPCServer1 As OPCServer 'Create references for OPC Server Dim WithEvents OPCGroup1 As OPCGroup 'and Group objects Private Sub Command1_Click() Dim NumItems As Long 'Create variables to write data Dim ServerHandles() As Long ' Dim Values() As Variant ' Dim Errors() As Long ' NumItems = OPCGroup1.OPCItems.Count 'Adjust arrays used to ReDim ServerHandles(1 To NumItems) 'write data based on ReDim Values(1 To NumItems) 'number of items in OPC For x = 1 To NumItems 'group ServerHandles(x) = OPCGroup1.OPCItems(x).ServerHandle ' Next x ' ' Values(1) = Text1.Text 'Assign value to array used in Write method OPCGroup1.SyncWrite NumItems, ServerHandles(), Values(), Errors() 'Perform OPC Write End Sub Private Sub Form_Load() Set OPCServer1 = New OPCServer 'Create new instance of OPC Server OPCServer1.Connect "RSLinx OPC Server" 'Connect to OPC Server application Set OPCGroup1 = OPCServer1.OPCGroups.Add("MyOPCData") 'Create new OPC Group in defined OPC Server OPCGroup1.OPCItems.AddItem "[PLC]T4:0.PRE", 1 'Add an item to defined OPC Group ' "[AccessPath]DataItem", ClientHandle OPCGroup1.IsSubscribed = True 'Instruct OPC Server to send OCP Group data 'changes End Sub Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) Set OPCGroup1 = Nothing 'Remove OPC Group OPCServer1.Disconnect 'Disconnect from OPC server application Set OPCServer1 = Nothing 'Remove OPC Server End Sub PS: This is for VB6 NOT vb.net. Edited 18 Oct 2007 by trn_psycho Share this post Link to post Share on other sites
Posted 18 Oct 2007 Thank you very much, I don't have it functioning quite yet, but I believe it is getting really close. I believe that the problem that I am having with yours / RSTrainer's, might be related to the version difference. I am running VS 2005 SP1, and RSLix 2.5. I only say that because 2005 the "Let and Set" are not supported any more, along with Variant. I have the Write working with your method and some modifcations, I am just stuck on getting the Read working in this method Where I am at with this is "ServerHandles(x) = OPCGroup1.OPCItems(x).ServerHandle" is returing "Value does not fall with in the expected range" I will post the working project as soon it is complete. Share this post Link to post Share on other sites
Posted 18 Oct 2007 The write has an OPCGroup1.syncwrite but the read has no OPCGroup1.syncread or OPCGroup1.AsyncRead. Somewhere you'll need to add these I think. Share this post Link to post Share on other sites
Posted 18 Oct 2007 That would be a major factor... VS2005 is all .Net, whereas the samples I posted were for VB Classic. .Net really doesn't do OPC as easily as classic vb. Share this post Link to post Share on other sites
Posted 19 Oct 2007 I Finnally finished it, hope it helps http://forums.mrplc.com/index.php?autocom=downloads&showfile=741 Let me know if you find any problems. Share this post Link to post Share on other sites
Posted 20 Mar 2012 HeLLO, I was very helpful codes but I have a question, want to work with more than 6 tags, can you help me? I just want to write each tag with textbox and button to make the change thanks Share this post Link to post Share on other sites