【摘 要】簡(jiǎn)要說(shuō)明程序如何通過(guò)OPC 服務(wù)器接口讀取PHD實(shí)時(shí)數(shù)據(jù),并實(shí)現(xiàn)Excel讀取實(shí)時(shí)數(shù)據(jù)庫(kù)數(shù)據(jù),具有行業(yè)應(yīng)用價(jià)值。
關(guān)鍵詞:PHD 實(shí)時(shí)數(shù)據(jù)庫(kù) OPC服務(wù)器 Excel VBA
一、前言
生產(chǎn)控制系統(tǒng)都存在大量的實(shí)時(shí)數(shù)據(jù),這些數(shù)據(jù)都使用實(shí)時(shí)數(shù)據(jù)庫(kù)存儲(chǔ)和管理。查詢都只能依靠廠家系統(tǒng)界面查詢,如果需要對(duì)其數(shù)據(jù)導(dǎo)出并再綜合應(yīng)用,現(xiàn)有廠家系統(tǒng)與工具都無(wú)法實(shí)現(xiàn)。為此,研究實(shí)現(xiàn)Office Excel通過(guò)OPC接口讀取PHD實(shí)時(shí)數(shù)據(jù)庫(kù)數(shù)據(jù)。
二、正文
(一)OPC Server 配置
PHD OPC服務(wù)器是基于微軟的DCOM技術(shù)來(lái)實(shí)現(xiàn),使用OPC Server之前,需要進(jìn)行相關(guān)的DCOM配置。
注意:修改DCOM的相關(guān)設(shè)置之后,需要重新啟動(dòng)計(jì)算機(jī)方可生效。
配置步驟詳見(jiàn)《Uniformance PHD OPC Server User Guide》“View/Modify DCOM Configuration of PHD OPC Server”章節(jié)。
(二)OPC Client 連接測(cè)試
使用opcuser用戶登陸本機(jī),運(yùn)行進(jìn)入opc quick client應(yīng)用程序,測(cè)試能否連接OPC Server成功:
1.菜單Edit: New Server Connection... --> 選OPC Data Access Servers Version 2.0 --> 選OPC.PHDServerDA.1 --> 輸入:HL-MESOPC --> ok
2.選OPC.PHDServerDA.1 右鍵 --> 選New Group... --> Name處隨便輸入可分組信息
3.選中新建的組名:右鍵 --> 選New Item... --> Item ID 處輸入點(diǎn)位號(hào)信息 --> 點(diǎn)一下右邊勾 --> ok
如果連接測(cè)試成功,就可以進(jìn)行VBA程序開(kāi)發(fā)了。
(三) Excel OPC Client開(kāi)發(fā)
開(kāi)發(fā)過(guò)程中需要注意的事項(xiàng):
1. OPC對(duì)象的聲明
Private objServer As OPCServer
Private objGroups As OPCGroups
Private objTestGrp As OPCGroup
Private objItems As OPCItems
2.建立一個(gè)OPC服務(wù)器對(duì)象
Set objServer = New OPCServer
3.連接OPC服務(wù)器
objServer.Connect (\"OPC.PHDServerDA.1\")
4.建立一個(gè)OPC組集合與組
Set objGroups = objServer.OPCGroups
Set objTestGrp = objGroups.Add(\"Test\")
5.添加OPC項(xiàng)
Call objItems.AddItems(8, strItemIDs, lClientHandles, _lServerHandles, lErrors)
6.讀取PHD實(shí)時(shí)數(shù)據(jù)
Call objTestGrp.SyncRead(OPCCache, 8, lServerHandles, _ItemVal, lErrors)
7.清除OPC項(xiàng)、清除OPC組、斷開(kāi)OPC服務(wù)器
Call objItems.Remove(8, lServerHandles, lErrors)
objGroups.Remove (\"Test\")
Call objServer.Disconnect
Excel OPC Client 運(yùn)行測(cè)試
Excel設(shè)置如下圖:
鼠標(biāo)單擊“連接”,如果沒(méi)有出錯(cuò)信息,表示已經(jīng)連接上OPC Server了,再單擊“讀取”,來(lái)完成第一行設(shè)置的點(diǎn)位號(hào)的實(shí)時(shí)數(shù)據(jù)的讀取。上圖為數(shù)據(jù)讀取成功的示例。
三、總結(jié)
簡(jiǎn)要說(shuō)明程序如何通過(guò)OPC 服務(wù)器接口讀取PHD實(shí)時(shí)數(shù)據(jù),并實(shí)現(xiàn)Excel讀取實(shí)時(shí)數(shù)據(jù)庫(kù)數(shù)據(jù),具有行業(yè)應(yīng)用價(jià)值。
參考文獻(xiàn):
[1]Data Access Custom Interface Standard Version 2.05,2001.12.17.
[2]Uniformance PHD OPC Server User Guide 2007.6.27.