ABBAC900F学习笔记232:OPC通讯17

标签:
abbac900ffreelanceopc |
分类: ABBDCS |
接着下午的学习,在同一台计算机上使用excel,通过VBA脚本向freelance OPC
server写数据。参照下午的练习搭建基础结构之后,双击OPC网关站,然后点击存取权限按钮,勾选写入权限,确保数据可以通过OPC
client修改。
ClientHandles(i) = i + 1 '每个项目的客户端句柄不同
ActiveStates(i) = True
ActualHour =
Hour(Now())
ActualMinute =
Minute(Now())
ActualSecond =
Second(Now()) + 2
WaitingTime =
TimeSerial(ActualHour, ActualMinute, ActualSecond)
Application.Wait
WaitingTime
For i = 0 To NrOfItems -
1
ItemObjects(i).Value = 12.34
'OPC项目赋值
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
Right:"
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
Next
excel文件的VBA编辑环境下点击工具-引用,勾选那几个和OPC相关的
在下午的VBA语句OPCExmpleMacro宏脚本中,excel文件单元格复制语句中插入OPC项目复制语句,如下所示
Sub OPCExmpleMacro()
Dim i, ActualHour, ActualMinute, ActualSecond, WaitingTime
'本地变量
NrOfItems = 1 '如果想读取更多的项目,就修改这个数值
'更新那些数组的daxiao
ReDim ItemIDs(NrOfItems)
ReDim AccessPaths(NrOfItems)
ReDim ClientHandles(NrOfItems)
ReDim AccessPaths(NrOfItems)
ReDim RequestedDataTypes(NrOfItems)
ReDim ActiveStates(NrOfItems)
ReDim ItemObjects(NrOfItems)
'开始配置服务器
ServerName = "Freelance2000OPCServer.24.1"
Set Server = CreateObject(ServerName)
'添加一个组到服务器
Set Group = Server.AddGroup("Group 1", True, 1000, 1, 0,
&H409, ServerGroupHandle, ServerUpdateRate)
'设置项目名字
ItemIDs(0) = "LT1001"
'设置客户端句柄,激活项目状态
For i = 0 To NrOfItems - 1
Next
'添加项目到组
Group.AddItems NrOfItems, ItemIDs, ActiveStates,
ClientHandles, ServerHandles, ItemsErrors, ItemObjects,
AccessPaths, RequestedDataTypes
'没有按下Ctrl+Break,那么每2秒更新一次数据
Worksheets(1).Visible = True
While True
Wend
End Sub
其他脚本不变,保存,运行这个VBA,就可以看到LT1001的数值被写入成了12.34