Excel als OPC Client (Datenaustausch)
23.01.2014 14:45:03
Stephan
ich bin dabei ein Prozessleitsystem (Freelance 2013 von ABB) für eine verfahrenstechnische Anlage zu programmieren und möchte eine Datenaustausch über einen OPC-Server realisieren. Ich habe dafür auch ein Makro von ABB bekommen, was dies bewerkstelligen soll. Das funktioniert auch. Ich kann Messwerte auslesen und mir in excel anzeigen lassen, aber ich kann bei laufenden Makro von ABB "OPCExample Macro()" keine anderen Makros starten und excel ist blockiert (oben die ganze toolbar ist grau). Ich kann das Makro auch nur über Strg+Pause beenden.
Mein Ziel ist es, dass mein selbst geschriebenes Makro, welches die Messwerte im sekundentakt abgreift und forlaufend in ein Tabellenblatt schreibt auch während des
"OPCExample Macro()" läuft.
Meine Frage ist nun: Woran liegt es das mir dieses Makro es nicht ermöglicht andere Makros aufzurufen. Ich habe auch schon versucht einen Aufruf meines Makros aus dem "OPCExample Macro()" auszulösen aber leider ohne erfolg. Es scheint so als würde dieses Makro alle anderen killen.
Version Excel 2010
Option Explicit 'to avoid that variables automativally
'variable declaration
Dim Server As IOPCServerDisp 'the reference to the server object
Dim Group As IOPCItemMgtDisp 'the reference to the group object
Dim ServerUpdateRate As Long 'will contain the update rate the server is using for the group
Dim ServerGroupHandle As Long 'will contain the server generated group handle
Dim NrOfItems As Long 'number of items in this example
Dim ItemIDs() As String 'array for the item names
Dim ActiveStates() As Boolean 'array for the active states of each item
Dim ClientHandles() As Long 'array for the client handles of each item
Dim AccessPaths() As String 'array for the access path of each item
Dim RequestedDataTypes() As Integer 'array for the requested datatype of each item
Dim ItemErrors As Variant 'will contain the error code of each item
Dim ServerHandles As Variant 'will contain the server handle of each item
Dim ItemObjects As Variant 'will contain the references to each item
Sub OPCExampleMacro()
Dim i, ActualHour, ActualMinute, ActualSecond, WaitingTime, ServerName 'local variables
NrOfItems = 14 'change this value if you want to read more items
'resize all arrays to the necessary size
ReDim ItemIDs(NrOfItems)
ReDim ActiveStates(NrOfItems)
ReDim ClientHandles(NrOfItems)
ReDim AccessPaths(NrOfItems)
ReDim RequestedDataTypes(NrOfItems)
' start the specified server, change the name for your configuration
ServerName = "Freelance2000OPCServer.23"
Set Server = CreateObject(ServerName)
' add a group to the server
Set Group = Server.AddGroup( _
"Group 1", _
True, _
1000, _
1, _
0, _
&H409, _
ServerGroupHandle, _
ServerUpdateRate)
'set the name of the item
ItemIDs(0) = "TRI12" 'change the name for your configuration
ItemIDs(1) = "TRI18" ' meine temperaturwerte
ItemIDs(2) = "TRI21"
ItemIDs(3) = "TRI22"
ItemIDs(4) = "TRI25"
ItemIDs(5) = "TRI32"
ItemIDs(6) = "TRI34"
ItemIDs(7) = "TRI36"
ItemIDs(8) = "TRI312"
ItemIDs(9) = "TRI316"
ItemIDs(10) = "TRI42"
ItemIDs(11) = "TRI44"
ItemIDs(12) = "TRI48"
ItemIDs(13) = "e"
'set client handles and active state of the items
For i = 0 To (NrOfItems - 1)
ClientHandles(i) = i + 1 'use a differen client handle for each item
ActiveStates(i) = True 'all items are active
Next
'add items to the group
Group.AddItems _
NrOfItems, _
ItemIDs, _
ActiveStates, _
ClientHandles, _
ServerHandles, _
ItemErrors, _
ItemObjects, _
AccessPaths, _
RequestedDataTypes
'as long as CTRL-BREAK is not pressed update every 1 seconds the sheet
Worksheets(1).Visible = True
While True
ActualHour = Hour(Now())
ActualMinute = Minute(Now())
ActualSecond = Second(Now()) + 1 'change 1 to x if you want faster execution
WaitingTime = TimeSerial(ActualHour, ActualMinute, ActualSecond)
Application.Wait WaitingTime
For i = 0 To (NrOfItems - 1)
' Worksheets(1).Cells(1 + 2, i + 1) = "Server:"
' Worksheets(1).Cells(1 + 2, i + 2) = ServerName
' Worksheets(1).Cells(1 + 3, i + 1) = "Name:"
' Worksheets(1).Cells(1 + 3, i + 2) = ItemObjects(i).ItemID
' Worksheets(1).Cells(2 + 3, i + 1) = "Access Rights:"
' Worksheets(1).Cells(2 + 3, i + 2) = ItemObjects(i).AccessRights
' Worksheets(1).Cells(3 + 3, i + 1) = "Value:"
' Worksheets(1).Cells(3 + 3, i + 2) = ItemObjects(i).Value
' Worksheets(1).Cells(4 + 3, i + 1) = "Quality:"
' Worksheets(1).Cells(4 + 3, i + 2) = ItemObjects(i).Quality
' Worksheets(1).Cells(5 + 3, i + 1) = "Timestamp:"
' Worksheets(1).Cells(5 + 3, i + 2) = ItemObjects(i).Timestamp
'ab hier werden die gewünschten Variablen auf das Tabellenblatt Data1 als Momentanwert _
_
geschrieben
Worksheets(2).Cells(1, i + 6) = ItemObjects(i).ItemID
Worksheets(2).Cells(3, i + 6) = ItemObjects(i).Value
Worksheets(1).Cells(10, 1) = ItemObjects(13).Value 'gibt die variable e auf _
tabellenblatt 1 aus um startlog zu aktivieren
Next
Wend
End Sub
Vielen Dank für eure Hilfe.
Stephan