Accueil > > > SERVEUR OPC
SERVEUR OPC
Information sur la source
Description
Connexion à un serveur opc et récupération des items.Programme assez simple.On met directement les adresses des variables souhaitées.
Source
- 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
Conclusion
petit souci,je récupère bien les valeurs des variables de l'api.mais elles s'écrivent n'importe ou.
Sources du même auteur
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
[SHAREPOINT] LES SESSIONS TECHDAYS 2012.[SHAREPOINT] LES SESSIONS TECHDAYS 2012. par Patrick Guimonet
Voici donc pour ceux qui n'ont pas pu venir, ou ceux qui n'ont pas pu toutes les suivre la liste des sessions SharePoint aux TechDays 2012, que je mettrais à jour dès que les liens des vidéo seront disponibles. Ou ici : http...
Cliquez pour lire la suite de l'article par Patrick Guimonet TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3 par ROMELARD Fabrice
Speaker: Bernard Ourghanlian Cette session est comme chaque jour transmise en live par BrainSonic, et j'ai donc suivi cette troisième pleinière par ce moyen sur mon iPad . Elle est dédiée comme chaque année à la mise en perspective de l'é...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE !MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE ! par Vko
Hier durant une session dédiée aux Techdays 2012, j'ai eu le plaisir d'annoncer la sortie de la Béta 2 de Mishra Reader. C'est quoi ? Pour les utilisateurs, c'est une vraie expérience de lecture de flux RSS sur Windows. Rien à voir avec les produit...
Cliquez pour lire la suite de l'article par Vko [FRAMEWORK 4] LES TASKS ET LE THREAD UI[FRAMEWORK 4] LES TASKS ET LE THREAD UI par fathi
Je viens de passer quelques temps au TechDay's et j'ai pu voir pas mal de session intéressante. Par contre une chose m'a un peu étonné lors de certaines de ces sessions qui abordaient les améliorations du framework .NET (donc le 4.5) : en gros, bea...
Cliquez pour lire la suite de l'article par fathi WORKFLOW FOUNDATION 3 A UN PIED DANS LA TOMBEWORKFLOW FOUNDATION 3 A UN PIED DANS LA TOMBE par JeremyJeanson
Depuis déjà un an, je conseille vivement les utilisateurs de Workflow Foundation 3 à migrer vers la version 4. L'information qui va suivre ne devrait donc pas trop prendre au dépourvu les personnes qui m'ont suivi. Je profite de ce poste, pour faire le re...
Cliquez pour lire la suite de l'article par JeremyJeanson
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|