matlab excel opc,用VBA从OPC服务器都数据

本文提供了一个使用VBA进行OPC自动化操作的示例,包括服务器连接、组的创建及项目的读取等过程,并展示了如何将实时数据更新到Excel工作表中。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值