khris81 Inserito: 8 ottobre 2016 Segnala Share Inserito: 8 ottobre 2016 Ciao a tutti, devo gestire 6 plc tramite kepserver, già configurati dal una ditta esterna. Il mio compito è quello di creare un programma in vb .net per lettura/Scirttura tag del kepserver. Riesco ad effettuare la connessione, il browsing dei tag presenti sul server e la creazione dei relativi itam lato client. questo è il mio codice vb: Public Class Form1 Dim ConnectedServerName As String = “Kepware.KEPServerEX.V5” Dim ConnectedServerIP As String = "192.168.0.xxx" Dim OPCGroupName As String = "Gruppo1" Dim WithEvents AnOPCServer As OPCAutomation.OPCServer Dim WithEvents ConnectedOPCServer As OPCAutomation.OPCServer Dim ConnectedServerGroup As OPCAutomation.OPCGroups Dim WithEvents ConnectedGroup As OPCAutomation.OPCGroup Dim OPCItemCollection As OPCAutomation.OPCItems Dim OPCItem As OPCAutomation.OPCItem Dim ItemServerHandles As System.Array Dim ItemServerErrors As System.Array Public WithEvents WriteItem0 As System.Windows.Forms.Button Dim WithEvents MyOPCBrowser As OPCAutomation.OPCBrowser Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Try 'Create a new OPC Server object ConnectedOPCServer = New OPCAutomation.OPCServer 'Attempt to connect with the server (remote in this example) ConnectedOPCServer.Connect(ConnectedServerName, ConnectedServerIP) 'Prepare to add a group to the current OPC Server ' Get the group interface from the server object ConnectedServerGroup = ConnectedOPCServer.OPCGroups ' Set the desire active state for the group ConnectedServerGroup.DefaultGroupIsActive = True 'Set the desired percent deadband - enter an integer from 0 to 100 for the deadband ConnectedServerGroup.DefaultGroupDeadband = 0 ' Add the group and set its update rate - enter whatever group name you want in place of “DataGroup1” ConnectedGroup = ConnectedServerGroup.Add(OPCGroupName) ' Set the update rate for the group - enter an long integer value representing the millisecond group update rate ConnectedGroup.UpdateRate = 500 ' The following line is crucial -- without it you won’t be subscribed to the server and DataChange events will not fire! ConnectedGroup.IsSubscribed = True ' ottiene elenco tag dal server TagList = GetServerTag() ' creazione item a livello di client AddItem(TagList) Catch ex As Exception ' Error handling MessageBox.Show("OPC Connect: " + ex.Message, "OPCInterface Exception", MessageBoxButtons.OK) End Try End Sub Private Sub AddItem(TagList() As String) Try Dim OPCItemIDs() As String Dim ClientHandles() As Int32 Dim ItemNum As Short ' conta il numero di tag presenti sul server For i As Short = 1 To TagList.Length - 1 If TagList(i) = "" Then Exit For End If ItemNum += 1 Next i ReDim OPCItemIDs(ItemNum) ReDim ClientHandles(ItemNum) For i As Short = 1 To ItemNum If TagList(i) = "" Then Exit For End If OPCItemIDs(i) = TagList(i) ClientHandles(i) = i Next i 'Gets an items collection from the current Group OPCItemCollection = ConnectedGroup.OPCItems 'Sets the items collection to active OPCItemCollection.DefaultIsActive = True 'This line adds the items we’ve chosen to the items collection and in turn to the group in the OPC Server OPCItemCollection.AddItems(ItemNum, OPCItemIDs, ClientHandles, ItemServerHandles, ItemServerErrors) Catch ex As Exception ' Error handling MessageBox.Show("OPC Connect: " + ex.Message, "OPCInterface Exception", MessageBoxButtons.OK) End Try End Sub Private Function GetServerTag() As String() Try Dim ChannelName(999) As String Dim DeviceName(999) As String Dim TagName(999) As String Dim TagListServer(999) As String Dim i4 As Short = 0 MyOPCBrowser = ConnectedOPCServer.CreateBrowser 'per estrarre i soli tag di tipo integer MyOPCBrowser.DataType = vbInteger MyOPCBrowser.ShowBranches() For i = 1 To MyOPCBrowser.Count ChannelName(i) = MyOPCBrowser.Item(i) MyOPCBrowser.MoveDown(MyOPCBrowser.Item(i)) MyOPCBrowser.ShowBranches() For i2 = 1 To MyOPCBrowser.Count DeviceName(i2) = MyOPCBrowser.Item(i2) MyOPCBrowser.MoveDown(MyOPCBrowser.Item(i2)) MyOPCBrowser.ShowLeafs() For i3 = 1 To MyOPCBrowser.Count TagName(i3) = MyOPCBrowser.Item(i3) If TagName(i3).Substring(0, 1) = "_" Then Continue For End If ListBox1.Items.Add(ChannelName(i) + "." + DeviceName(i2) + "." + TagName(i3)) i4 += 1 TagListServer(i4) = ChannelName(i) + "." + DeviceName(i2) + "." + TagName(i3) Next i3 MyOPCBrowser.MoveUp() MyOPCBrowser.ShowBranches() Next i2 MyOPCBrowser.MoveUp() MyOPCBrowser.ShowBranches() Next i Return TagListServer Catch ex As Exception ' Error handling MessageBox.Show("OPC Connect: " + ex.Message, "OPCInterface Exception", MessageBoxButtons.OK) End Try End Function Private Sub ConnectedGroup_DataChange(TransactionID As Integer, NumItems As Integer, ByRef ClientHandles As Array, ByRef ItemValues As Array, ByRef Qualities As Array, ByRef TimeStamps As Array) Handles ConnectedGroup.DataChange Try Dim i As Integer End sub Il problema sono le variabili, ItemServerHandles e ItemServerErrors, che non restituiscono alcun valore, itemserverhandles dovrebbe restituire un intero che è il numero relativo al tag presente sul server e itemservererrors, il numero di errore se presente. Di conseguenza se cambio il valore di un tag, l'evento connectedgroup_datachange si scatena, ma le variabili, ClientHandles, ItemValues, Qualities, TimeStamps, non riportano alcun valore. Non capisco cosa sto sbagliando, qualcuno riesce ad aiutarmi? grazie, ciao. Link al commento Condividi su altri siti More sharing options...
Messaggi consigliati
Crea un account o accedi per commentare
Devi essere un utente per poter lasciare un commento
Crea un account
Registrati per un nuovo account nella nostra comunità. è facile!
Registra un nuovo accountAccedi
Hai già un account? Accedi qui.
Accedi ora