加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

ABBAC900F学习笔记232:OPC通讯17

(2022-04-13 20:02:05)
标签:

abb

ac900f

freelance

opc

分类: ABBDCS
接着下午的学习,在同一台计算机上使用excel,通过VBA脚本向freelance OPC server写数据。参照下午的练习搭建基础结构之后,双击OPC网关站,然后点击存取权限按钮,勾选写入权限,确保数据可以通过OPC client修改。
ABBAC900F学习笔记232:OPC通讯17
保存,检查错误。重新启动OPC服务器,然后加载更改。
excel文件的VBA编辑环境下点击工具-引用,勾选那几个和OPC相关的
ABBAC900F学习笔记232:OPC通讯17
在下午的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
       ClientHandles(i) = i + 1 '每个项目的客户端句柄不同
       ActiveStates(i) = True
Next

'添加项目到组
Group.AddItems NrOfItems, ItemIDs, ActiveStates, ClientHandles, ServerHandles, ItemsErrors, ItemObjects, AccessPaths, RequestedDataTypes
'没有按下Ctrl+Break,那么每2秒更新一次数据
Worksheets(1).Visible = True
While 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
Wend
End Sub
其他脚本不变,保存,运行这个VBA,就可以看到LT1001的数值被写入成了12.34
ABBAC900F学习笔记232:OPC通讯17
这里需要注意,如果OPC客户端和OPC服务器都同时给一个变量赋值,那会出现问题的。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有