Option Explicit 'to avoid that variables automatically
'variable declaration
Dim Server As OPCAutomation.OPCServer 'the reference to the server object
Dim Group As OPCAutomation.OPCGroup 'the reference to the group object
Dim ServerUpdateRate As Long 'contains the update rate, server is using for groups
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 'local variables
Dim servername As String
NrOfItems = 3 '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.26" '?... ??z.B. "Freelance2000OPCServer.26")
Set Server = CreateObject(servername)
'
' add a group to the server
'
Set Group = Server.AddGroup( _
"Group 1", _
1000, _
1, _
0, _
&H409, _
ServerGroupHandle, _
ServerUpdateRate)
'
'set the name of the item
'
ItemIDs(0) = "int01" 'change the name for your configuration
ItemIDs(1) = "fsda"
'
'set client handles and active state of the items
'
For i = 0 To (NrOfItems - 1)
ClientHandles(i) = i + 1 'use a different 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 2 seconds the sheet
Worksheets(1).Visible = True
While True
ActualHour = Hour(Now())
ActualMinute = Minute(Now())
ActualSecond = Second(Now()) + 2 'change 2 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) = Server.Value '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) = Server.timestamp 'ItemObjects(i).Timestamp
Next
Wend
End Sub
本文提供了一个使用VBA进行OPC自动化操作的示例,包括服务器连接、组的创建及项目的读取等过程,并展示了如何将实时数据更新到Excel工作表中。
2174

被折叠的 条评论
为什么被折叠?



