- Imports System.Runtime.InteropServices
- Imports System.Net
- Imports System.IO
-
- Imports Opc
-
- Public Class SuperVision
-
- 'déclaration
-
-
- Private WithEvents Srv As Opc.Da.Server
-
-
- 'Connection au serveur au OPC
- Private Sub Supervision_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- 'Objet OPC.Factory
-
- Dim OPCFact As New OpcCom.Factory()
- 'URL de l'OPC (recopie celui que te donne l'exemple OPC .Net Sample Data Access fourni avec OFS3.3
- Dim OPCUrl As New Opc.URL("opcda://localhost/Schneider-Aut.OFS/{e7675090-9ff9-11d1-ac46-0060978add48}")
- 'Creation du client
- Dim Opc As New Opc.Server(OPCFact, OPCUrl)
-
-
- Try
- 'Connection au serveur
- TextBox2.Text = "--> En attente de connexion "
- 'Saut au programme "Connect"
- Opc.Connect()
-
-
- Catch
- TextBox2.Text = "--> La connection n'a pas été établie avec le Serveur Schneider-Aut.OFS "
- End Try
-
- End Sub
-
-
-
- 'Programme de Connection au serveur OPC
- Private Sub Connect()
-
-
- Dim Url As Opc.URL = New Opc.URL("opcda://localhost/Schneider-Aut.OFS/{e7675090-9ff9-11d1-ac46-0060978add48}")
- Srv = New Opc.Da.Server(New OpcCom.Factory, Url)
- '
- Try
-
- Srv.Connect()
-
- Catch ex As Exception
- 'Gestion des erreurs
- MsgBox("Erreur Fonction Cnx: " & ex.Message)
- '
- Finally
-
- End Try
-
- End Sub
-
-
- '
- '
- '
- '
- 'récupération donnée
- Private Bol As Boolean = False
-
- 'Ajouter en référence
- 'OPCNETAPI.dll
- 'OPCNETAPI.COM.dll
- 'OPCRCW.AE.dll
- 'OPCRCW.COMM.dll
- 'OPCRCW.DA.dll
- 'OPCRCW.DX.dll
- 'OPCRCW.HDA.dll
-
- #Region "VAR OPC"
- 'Serveur OPC
-
- 'Groupe
- Private WithEvents OFSCompteur1 As Opc.Da.ISubscription
- 'Création des Items
- Dim m_ItemCptr1() As Opc.Da.Item
- Dim m_ItemCptr2() As Opc.Da.Item
- Dim m_itemOf() As Opc.Da.Item
- 'Resultat item
- Dim ItemCptr1() As Opc.Da.ItemResult
- Dim ItemCptr2() As Opc.Da.ItemResult
- Dim ItemOF() As Opc.Da.ItemResult
-
-
- Dim m_handle As Integer
- Dim Cptr1_state As New Opc.Da.SubscriptionState
- 'Structure des résultats
- Public Structure ItemsResults
- Dim ItemsName As String
- Dim ItemsClientHnd As String
- Dim ItemsSrvHnd As Integer
- Dim ItemsValue As Object
- Dim ItemsQuality As String
- Dim ItemsTimeStamp As Date
- Dim ItemsDescription As String
- Dim ItemsType As String
- Dim ItemsTopologicAdress As String
- End Structure
- #End Region
-
- #Region "Ajouter Variable"
- Private Sub BtnAdd()
- 'Création de Groupe
- Cptr1_state.ClientHandle = Guid.NewGuid.ToString
- Cptr1_state.ServerHandle = Cptr1_state.ClientHandle
- Cptr1_state.Name = "LECTURE"
- Cptr1_state.Active = True
- OFSCompteur1 = Srv.CreateSubscription(Cptr1_state)
-
-
-
- 'Création de Item1
- m_ItemCptr1 = New Opc.Da.Item(0) {}
- m_ItemCptr1(0) = New Opc.Da.Item()
- 'Nom de l'item ALIAS ! Nom de la variable
- m_ItemCptr1(0).ItemName = "UTW!%MD200"
- m_ItemCptr1(0).ItemPath = "Cptr1"
- m_ItemCptr1(0).ClientHandle = Guid.NewGuid.ToString()
- ItemCptr1 = OFSCompteur1.AddItems(m_ItemCptr1)
- m_ItemCptr1(0).ServerHandle = ItemCptr1(0).ServerHandle
-
-
-
- 'Création de Item2
- m_ItemCptr2 = New Opc.Da.Item(0) {}
- m_ItemCptr2(0) = New Opc.Da.Item()
- 'Nom de l'item ALIAS ! Nom de la variable
- m_ItemCptr2(0).ItemName = "UTW!%MD210"
- m_ItemCptr2(0).ItemPath = "Cptr2"
- m_ItemCptr2(0).ClientHandle = Guid.NewGuid.ToString()
- ItemCptr2 = OFSCompteur1.AddItems(m_ItemCptr2)
- m_ItemCptr2(0).ServerHandle = ItemCptr2(0).ServerHandle
-
-
-
-
- 'Création de Item1Of
- m_ItemOF = New Opc.Da.Item(0) {}
- m_itemOf(0) = New Opc.Da.Item()
- 'Nom de l'item ALIAS ! Nom de la variable
- m_itemOf(0).ItemName = "UTW!%MW102"
- m_itemOf(0).ItemPath = "Of"
- m_itemOf(0).ClientHandle = Guid.NewGuid.ToString()
- ItemOF = OFSCompteur1.AddItems(m_itemOf)
- m_itemOf(0).ServerHandle = ItemOF(0).ServerHandle
- End Sub
-
- #End Region
-
- Private Sub OFSCompteur1_DataChanged(ByVal subscriptionHandle As Object, ByVal requestHandle As Object, ByVal values() As Opc.Da.ItemValueResult) Handles OFSCompteur1.DataChanged
- 'Procédure appelée sur changement d'état des Items
- Dim GrpHnd As String
- GrpHnd = subscriptionHandle
- Dim NotifyItems(values.Length - 1) As ItemsResults
- Dim index As Integer
- Try
- For index = 0 To values.Length - 1
- NotifyItems(index).ItemsClientHnd = values(index).ClientHandle
- NotifyItems(index).ItemsName = values(index).ItemName
- NotifyItems(index).ItemsQuality = values(index).Quality.ToString
- NotifyItems(index).ItemsSrvHnd = values(index).ServerHandle
- NotifyItems(index).ItemsTimeStamp = values(index).Timestamp
- NotifyItems(index).ItemsValue = values(index).Value
-
- Dim ValueCptr As String = Convert.ToString(NotifyItems(index).ItemsValue)
-
- If index = 0 Then
- Me.BeginInvoke(New EventHandler(AddressOf SetValueCptr1), ValueCptr)
- End If
- If index = 1 Then
- Me.BeginInvoke(New EventHandler(AddressOf SetValueCptr2), ValueCptr)
- End If
-
- Next
-
-
-
- '
- Catch ex As Exception
- 'Gestion des erreurs
- MsgBox("Erreur sur la Lecture du Compteur 1 : " & ex.Message)
- End Try
- End Sub
-
-
-
- 'écriture des items
-
- Private Sub SetValueCptr1(ByVal sender As Object, ByVal e As EventArgs)
- 'Ecriture du résultat de la variable
- cpte.Text = sender
- End Sub
- Private Sub SetValueCptr2(ByVal sender As Object, ByVal e As EventArgs)
- 'Ecriture du résultat de la variable
- cpts.Text = sender
- End Sub
-
- Private Sub SetValueOF(ByVal sender As Object, ByVal e As EventArgs)
- 'Ecriture du résultat de la variable
- numof.Text = sender
- End Sub
-
- 'Private Sub raffraichissement()
- ' While (BtnConnect.Enabled)
- ' BtnAdd()
- ' End While
-
- ' End Sub
-
- #Region "Connexion Deconnexion"
- Private Sub BtnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnConnect.Click
- Connect()
- TextBox2.Text = "--> Connection réussie au Serveur Schneider-Aut.OFS "
- BtnAdd()
- End Sub
- Public Sub Deconnect_OPCServer()
- Try
- Srv.Disconnect()
- Catch ex As Exception
-
- End Try
- End Sub
-
- Private Sub BtnDeconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDeconnect.Click
- Deconnect_OPCServer()
-
- BtnConnect.Enabled = True
- BtnDeconnect.Enabled = False
- 'Me.Close() fermeture de la fenetre
- End Sub
- #End Region
-
-
-
- End Class
Imports System.Runtime.InteropServices
Imports System.Net
Imports System.IO
Imports Opc
Public Class SuperVision
'déclaration
Private WithEvents Srv As Opc.Da.Server
'Connection au serveur au OPC
Private Sub Supervision_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Objet OPC.Factory
Dim OPCFact As New OpcCom.Factory()
'URL de l'OPC (recopie celui que te donne l'exemple OPC .Net Sample Data Access fourni avec OFS3.3
Dim OPCUrl As New Opc.URL("opcda://localhost/Schneider-Aut.OFS/{e7675090-9ff9-11d1-ac46-0060978add48}")
'Creation du client
Dim Opc As New Opc.Server(OPCFact, OPCUrl)
Try
'Connection au serveur
TextBox2.Text = "--> En attente de connexion "
'Saut au programme "Connect"
Opc.Connect()
Catch
TextBox2.Text = "--> La connection n'a pas été établie avec le Serveur Schneider-Aut.OFS "
End Try
End Sub
'Programme de Connection au serveur OPC
Private Sub Connect()
Dim Url As Opc.URL = New Opc.URL("opcda://localhost/Schneider-Aut.OFS/{e7675090-9ff9-11d1-ac46-0060978add48}")
Srv = New Opc.Da.Server(New OpcCom.Factory, Url)
'
Try
Srv.Connect()
Catch ex As Exception
'Gestion des erreurs
MsgBox("Erreur Fonction Cnx: " & ex.Message)
'
Finally
End Try
End Sub
'
'
'
'
'récupération donnée
Private Bol As Boolean = False
'Ajouter en référence
'OPCNETAPI.dll
'OPCNETAPI.COM.dll
'OPCRCW.AE.dll
'OPCRCW.COMM.dll
'OPCRCW.DA.dll
'OPCRCW.DX.dll
'OPCRCW.HDA.dll
#Region "VAR OPC"
'Serveur OPC
'Groupe
Private WithEvents OFSCompteur1 As Opc.Da.ISubscription
'Création des Items
Dim m_ItemCptr1() As Opc.Da.Item
Dim m_ItemCptr2() As Opc.Da.Item
Dim m_itemOf() As Opc.Da.Item
'Resultat item
Dim ItemCptr1() As Opc.Da.ItemResult
Dim ItemCptr2() As Opc.Da.ItemResult
Dim ItemOF() As Opc.Da.ItemResult
Dim m_handle As Integer
Dim Cptr1_state As New Opc.Da.SubscriptionState
'Structure des résultats
Public Structure ItemsResults
Dim ItemsName As String
Dim ItemsClientHnd As String
Dim ItemsSrvHnd As Integer
Dim ItemsValue As Object
Dim ItemsQuality As String
Dim ItemsTimeStamp As Date
Dim ItemsDescription As String
Dim ItemsType As String
Dim ItemsTopologicAdress As String
End Structure
#End Region
#Region "Ajouter Variable"
Private Sub BtnAdd()
'Création de Groupe
Cptr1_state.ClientHandle = Guid.NewGuid.ToString
Cptr1_state.ServerHandle = Cptr1_state.ClientHandle
Cptr1_state.Name = "LECTURE"
Cptr1_state.Active = True
OFSCompteur1 = Srv.CreateSubscription(Cptr1_state)
'Création de Item1
m_ItemCptr1 = New Opc.Da.Item(0) {}
m_ItemCptr1(0) = New Opc.Da.Item()
'Nom de l'item ALIAS ! Nom de la variable
m_ItemCptr1(0).ItemName = "UTW!%MD200"
m_ItemCptr1(0).ItemPath = "Cptr1"
m_ItemCptr1(0).ClientHandle = Guid.NewGuid.ToString()
ItemCptr1 = OFSCompteur1.AddItems(m_ItemCptr1)
m_ItemCptr1(0).ServerHandle = ItemCptr1(0).ServerHandle
'Création de Item2
m_ItemCptr2 = New Opc.Da.Item(0) {}
m_ItemCptr2(0) = New Opc.Da.Item()
'Nom de l'item ALIAS ! Nom de la variable
m_ItemCptr2(0).ItemName = "UTW!%MD210"
m_ItemCptr2(0).ItemPath = "Cptr2"
m_ItemCptr2(0).ClientHandle = Guid.NewGuid.ToString()
ItemCptr2 = OFSCompteur1.AddItems(m_ItemCptr2)
m_ItemCptr2(0).ServerHandle = ItemCptr2(0).ServerHandle
'Création de Item1Of
m_ItemOF = New Opc.Da.Item(0) {}
m_itemOf(0) = New Opc.Da.Item()
'Nom de l'item ALIAS ! Nom de la variable
m_itemOf(0).ItemName = "UTW!%MW102"
m_itemOf(0).ItemPath = "Of"
m_itemOf(0).ClientHandle = Guid.NewGuid.ToString()
ItemOF = OFSCompteur1.AddItems(m_itemOf)
m_itemOf(0).ServerHandle = ItemOF(0).ServerHandle
End Sub
#End Region
Private Sub OFSCompteur1_DataChanged(ByVal subscriptionHandle As Object, ByVal requestHandle As Object, ByVal values() As Opc.Da.ItemValueResult) Handles OFSCompteur1.DataChanged
'Procédure appelée sur changement d'état des Items
Dim GrpHnd As String
GrpHnd = subscriptionHandle
Dim NotifyItems(values.Length - 1) As ItemsResults
Dim index As Integer
Try
For index = 0 To values.Length - 1
NotifyItems(index).ItemsClientHnd = values(index).ClientHandle
NotifyItems(index).ItemsName = values(index).ItemName
NotifyItems(index).ItemsQuality = values(index).Quality.ToString
NotifyItems(index).ItemsSrvHnd = values(index).ServerHandle
NotifyItems(index).ItemsTimeStamp = values(index).Timestamp
NotifyItems(index).ItemsValue = values(index).Value
Dim ValueCptr As String = Convert.ToString(NotifyItems(index).ItemsValue)
If index = 0 Then
Me.BeginInvoke(New EventHandler(AddressOf SetValueCptr1), ValueCptr)
End If
If index = 1 Then
Me.BeginInvoke(New EventHandler(AddressOf SetValueCptr2), ValueCptr)
End If
Next
'
Catch ex As Exception
'Gestion des erreurs
MsgBox("Erreur sur la Lecture du Compteur 1 : " & ex.Message)
End Try
End Sub
'écriture des items
Private Sub SetValueCptr1(ByVal sender As Object, ByVal e As EventArgs)
'Ecriture du résultat de la variable
cpte.Text = sender
End Sub
Private Sub SetValueCptr2(ByVal sender As Object, ByVal e As EventArgs)
'Ecriture du résultat de la variable
cpts.Text = sender
End Sub
Private Sub SetValueOF(ByVal sender As Object, ByVal e As EventArgs)
'Ecriture du résultat de la variable
numof.Text = sender
End Sub
'Private Sub raffraichissement()
' While (BtnConnect.Enabled)
' BtnAdd()
' End While
' End Sub
#Region "Connexion Deconnexion"
Private Sub BtnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnConnect.Click
Connect()
TextBox2.Text = "--> Connection réussie au Serveur Schneider-Aut.OFS "
BtnAdd()
End Sub
Public Sub Deconnect_OPCServer()
Try
Srv.Disconnect()
Catch ex As Exception
End Try
End Sub
Private Sub BtnDeconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDeconnect.Click
Deconnect_OPCServer()
BtnConnect.Enabled = True
BtnDeconnect.Enabled = False
'Me.Close() fermeture de la fenetre
End Sub
#End Region
End Class