收藏 分享(赏)

Excel通过OPC与AB实现读写.doc

上传人:精品资料 文档编号:8769385 上传时间:2019-07-11 格式:DOC 页数:4 大小:15.08KB
下载 相关 举报
Excel通过OPC与AB实现读写.doc_第1页
第1页 / 共4页
Excel通过OPC与AB实现读写.doc_第2页
第2页 / 共4页
Excel通过OPC与AB实现读写.doc_第3页
第3页 / 共4页
Excel通过OPC与AB实现读写.doc_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

1、1,ExcelOPCReadBlockTmStmp,EXCEL 通过 opc 服务器读取数据,以及采集时间 Excel VBA Example Code: Read multiple data elements and display Timestamp using OPCDim OPCServer1 As OPCServer Dimension OPC Server andDim WithEvents OPCGroup1 As OPCGroup Group objectsPrivate Sub cmdDisconnect_Click() Terminate OPC Group communi

2、cationsOn Error Resume Next and connection to OPC ServerSet OPCGroup1 = Nothing OPCServer1.Disconnect -Ignore errors if OPC Server orSet OPCServer1 = Nothing Group does not existEnd Sub Private Sub cmdRead_Click()If OPCServer1 Is Nothing Then OPC Server object doesnt existSet OPCServer1 = New OPCSer

3、ver -Create new OPC Server objectOPCServer1.Connect “RSLinx OPC Server“ -Connect to OPC ServerSet OPCGroup1 = OPCServer1.OPCGroups.Add(“MyOPCData“) -Create OPC GroupOPCGroup1.OPCItems.AddItem “SLCT4:0.ACC,L3“, 1 -Add item to OPC GroupOPCGroup1.OPCItems.AddItem “TopicItem“, ClientHandleOPCGroup1.IsSu

4、bscribed = True -Subscribe to OPC GroupEnd IfEnd SubPrivate Sub OPCGroup1_DataChange(ByVal TransactionID As Long, ByVal NumItems As Long, ClientHandles() As Long, ItemValues() As Variant, Qualities() As Long, TimeStamps() As Date)A1 = ItemValues(1)(0) When data changes and is sent to applicationA2 =

5、 ItemValues(1)(1) populate each data element into a separateA3 = ItemValues(1)(2) worksheet cellB1 = TimeStamps(1) For x = 1 To NumItems A better method of handlingincoming data using Select Case ClientHandles(x) the ClientHandle assigned in the AddItem method Case 1 A1 = ItemValues(x)(0) Do this wh

6、en ClientHandle =1 A2 = ItemValues(x)(1) A3 = ItemValues(x)(2) B1 = TimeStamps(x) Case 2 Can be easily adapted to handle more than one item ClientHandle=2 Case 3 Clienthandle=3 End Select Next x End Sub2,ExcelOPCWriteBlock,通过 EXCEL 写入数据 Excel VBA Example Code: Write multiple data elements using OPCN

7、ote: Must set a reference to RSLinx OPC Automation in order for thefollowingprogramming to operate properly. VBA menu Tools ReferencesDim OPCServer1 As OPCServer Create references for OPC ObjectsDim WithEvents OPCGroup1 As OPCGroup Private Sub CommandButton1_Click()Dim NumItems As Long Create requir

8、ed OPC variables & arraysDim ServerHandles() As Long Dim Values() As Variant Dim Errors() As Long If OPCServer1 Is Nothing Then If OPCServer object doesnt exist .Set OPCServer1 = New OPCServer -Create new instance of OPC serverOPCServer1.Connect “RSLinx OPC Server“ -Connect to OPC ServerSet OPCGroup

9、1 = OPCServer1.OPCGroups.Add(“MyOPCData“) -Create OPC Group in OPC ServerOPCGroup1.OPCItems.AddItem “SLCT4:0.PRE,L3“, 1 -Add OPC Item to OPC GroupOPCGroup1.OPCItems.AddItem “TopicItem“, ClientHandle OPCGroup1.IsSubscribed = True -Instruct OPC Server to send changes to OPC Group itemsEnd IfNumItems =

10、 OPCGroup1.OPCItems.Count Determine number of items in OPC GroupReDim ServerHandles(1 To NumItems) Sizearrays based on number of itemsReDim Values(1 To NumItems) For x = 1 To NumItems Populate array with OPC Servers referenceServerHandles(x) = OPCGroup1.OPCItems(x).ServerHandle for each ItemNext x D

11、im TempValues(1 To 3) As Variant Create and populate a temporary array to withTempValues(1) = A1 datato be written to processorTempValues(2) = A2 NOTE: Size of array dependant on number of valuesTempValues(3) = A3 in data blockValues(1) = TempValues Assign data in temporary array to transmit arrayOP

12、CGroup1.SyncWrite NumItems, ServerHandles(), Values(), Errors()Write data to processorNOTE: The execution of VBA code pauses until the SyncWrite completes. Errors are returned in the Errors arrayand should be checked to determine if the write was succesful before leaving this routine.End SubPrivate Sub CommandButton2_Click()On Error Resume Next Ignore errors when OPC Groups or Server dont existSet OPCGroup1 = Nothing -Remove OPC GroupOPCServer1.Disconnect -Disconnect from OPC ServerSet OPCServer1 = Nothing -Remove OPC ServerEnd Sub

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报