複製鏈接
請複製以下鏈接發送給好友

OPC Server

鎖定
OPC是OLE for Process Control的縮寫。
中文名
OPC Server
類    型
信息系統軟件
性    質
利用微軟的COM/DCOM技術

OPC Server協定簡介

在現今的工業自動化中,我們需要一套整合的信息系統,由底層的各項裝置採集信息 (Field Management),中層的控制系統或圖控應用程序進行程序的控制 (Process Management),再由最上層的整合軟件將這些信息整合起來以供企業決策或效能提升
OPC 為硬件製造商與軟件開發商提供了一條橋樑,透過硬件廠商提供的 OPC Server 接口,軟件開發者不必考慮各項不同硬件間的差異,便可自硬件端取得所需的信息,所以軟件開發者僅需專注於程序本身的控制流程的運作。此外,由於 COM/DCOM 實作並隱藏了網絡的細節,透過 OPC 可以很容易地達成遠程控制的理想。

OPC Server原理

在説明 OPC Server/Client 運作方式之前,我們先簡單介紹一下 Microsoft 發展的 COM/DCOM 是什麼?
COM 是一種發展軟件組件的方法,所謂的軟件組件,是指一個可以提供應用程序、操作系統、以及其它組件服務二進制可執行程序。事實上,發展自訂的 COM 對象就好象是在建構一套可以動態執行的對象導向 API 一般。你可以在應用程序執行的時期隨意拼上或移除所需要的組件。依據 COM 這樣的概念,發展應用程序就像是堆積木一樣,每一個 COM 組件就是一塊積木,你可以利用各式各樣不同的積木,拼湊出你所需要的應用程序。
在實作上,COM 透過一組一組的接口 (Interface) 提供服務,所有 COM 組件的使用者,都必須透過這些 Interface 來使用組件提供的功能。OPC 的規格中便定義了許多 OPC Server 應該提供的 Interface,要撰寫一個 OPC Server 的 COM 組件,你必須在你的組件中加入這些接口,並提供它們的實作,Client 便可以透過這些接口,操作連接到 OPC Server 的硬件裝置,這也就是 OPC Server/Client 運作的方式。以下的圖標可以讓這樣的概念更清晰。

OPC Server架構

圖1 圖1 [1]
如前面所述,OPC Server 透過一組一組的接口提供服務,不過在實作的架構上,OPC Server 共分為三層:分別是 OPCServer, OPCGroup, OPCItem
其中每一個 OPCItem 對應到一個實際的硬件裝置上的某一個 channel 或 port;每一個 OPCGroup 則包含了許多的 OPCItem,同時並定義這些 OPCItem 更新的時間、方式,以及提供讀取 OPCItem 值的接口;而每一個 OPCServer 則包含若干個 OPCGroup,同時提供操作這些 OPCGroup 的接口。
圖1可以較清楚地説明 OPCServer/OPCGroup/OPCItem 間的關係:

OPC Server定製接口

OPC Server OPC Server [2]
接下來的這一節,我們將討論 OPC Server 的重點,也就是每一組接口的定義,以及它所提供的功能。
OPCServer 提供的接口:
IOPCCommon
HRESULT SetLocaleID ( dwLcid ) 設定位置信息
HRESULT GetLocaleID ( pdwLcid ) 取得位置信息
HRESULT QueryAvailableLocaleIDs ( pdwCount, pdwLcid ) 查詢可用的位置ID
HRESULT GetErrorString ( dwError, ppString) 取得錯誤信息字符串
HRESULT SetClientName (szName)設定Client的名稱
HRESULT AddGroup(szName, bActive, dwRequestedUpdateRate, hClientGroup, pTimeBias, pPercentDeadband, dwLCID, phServerGroup, pRevisedUpdateRate, riid, ppUnk) 新增一個 OPCGroup
HRESULT GetErrorString(dwError, dwLocale, ppString) 取得錯誤信息字符串
HRESULT GetGroupByName(szName, riid, ppUnk) 依據名稱取得OPCGroup 的介面
HRESULT GetStatus(ppServerStatus) 取得 OPCServer的狀態信息
HRESULT SetClientName (szName)設定Clinet的名稱RemoveGroup(hServerGroup, bForce)移除一個OPCGroup
HRESULT CreateGroupEnumerator(dwScope, riid, ppUnk) 產生一個OPCGroup 列舉器
IConnectionPointContainer
HRESULT EnumConnectionPoints( IEnumConnectionPoints ppEnum) 列舉所有的 Connection Points
HRESULT FindConnectionPoint( REFIID riid, IConnectionPoint ppCP)找出一個 Connection Point
IOPCItemProperties
HRESULT QueryAvailableProperties(szItemID, pdwCount,ppPropertyIDs, ppDescriptions, ppvtDataTypes ); 查詢可用的 OPCItem屬性
HRESULT GetItemProperties (szItemID, dwCount, pdwPropertyIDs,ppvData, ppErrors ) 取得 OPCItem 的屬性
HRESULT LookupItemIDs( szItemID, dwCount, pdwPropertyIDs,ppszNewItemIDs, ppErrors )使用名稱查詢OPCItem 的 ID
IOPCBrowseServerAddressSpace (optional)
HRESULT QueryOrganization(pNameSpaceType )查詢組織名稱
HRESULT ChangeBrowsePosition(dwBrowseDirection, szString )變更瀏覽的位置
HRESULT BrowseOPCItemIDs( dwBrowseFilterType, szFilterCriteria, vtDataTypeFilter, dwAccessRightsFilter, ppIEnumString )瀏覽 OPCServer 內所有定義的 OPCItem
HRESULT GetItemID( szItemDataID, szItemID ) 取得 OPCItem 的 ID
HRESULT BrowseAccessPaths( szItemID, ppIEnumString )瀏覽存取OPCItem 的路徑
OPCGroup Object 提供的介面
IOPCGroupStateMgt
HRESULT GetState(pUpdateRate, pActive, ppName, pTimeBias, pPercentDeadband, pLCID, phClientGroup, phServerGroup)取得OPCGroup的狀態即設定信息
HRESULT SetState(pRequestedUpdateRate, pRevisedUpdateRate, pActive, pTimeBias, pPercentDeadband, pLCID, phClientGroup)設定OPCGroup 的狀態信息
HRESULT SetName(szName)設定OPCGroup的名稱
HRESULT CloneGroup(szName, riid, ppUnk) 複製一個OPCGroup
IOPCSyncIO
HRESULT Read(dwSource, dwCount, phServer, ppItemValues, ppErrors); 以同步方式讀取OPCGroup內的OPCItem(s) 的值
HRESULT GetItemProperties (szItemID, dwCount, pdwPropertyIDs,ppvData, ppErrors ) 取得 OPCItem 的屬性
HRESULT Write(dwCount, phServer, pItemValues, ppErrors) 以同步方式將值寫入OPCGroup 內的 OPCItem(s)
IOPCAsyncIO2
HRESULT Read(dwCount, phServer, dwTransactionID, pdwCancelID, ppErrors,) 以非同步方式讀取OPCGroup內的OPCItem(s)的值,值會在讀取硬件的動作結束後以callback的形式傳回
HRESULT SetState(pRequestedUpdateRate, pRevisedUpdateRate, pActive, pTimeBias, pPercentDeadband, pLCID, phClientGroup)設定OPCGroup的狀態信息
HRESULT Write(dwCount, phServer, pItemValues, dwTransactionID, pdwCancelID, ppErrors)以非同步方式將值寫入OPCGroup內的OPCItem(s)
HRESULT Cancel2 (dwCancelID) 取消前一次的非同步讀取/寫入
HRESULT Refresh2(dwSource, dwTransactionID, pdwCancelID) 更新 OPCGroup 內 OPCItem(s) 的值
HRESULT SetEnable(bEnable) 將 OPCGroup 設為 Enable
HRESULT GetEnable(pbEnable) 傳回 OPCGroup 是否為 Enable
IOPCItemMgt
HRESULT AddItems(dwCount, pItemArray, ppAddResults, ppErrors); 在OPCGroup內新增OPCItem(s)
HRESULT ValidateItems(dwCount, pItemArray, bBlobUpdate, ppValidationResults, ppErrors)檢查OPCItem(s)的名稱是否可用
HRESULT RemoveItems(dwCount, phServer, ppErrors) 移除OPCGroup 內的 OPCItem(s)
HRESULT SetActiveState(dwCount, phServer, bActive, ppErrors)高定 OPCItem(s)是否為Avtive
HRESULT SetClientHandles(dwCount, phServer, phClient, ppErrors)設定 OPCItem(s) 的 handle
HRESULT SetDatatypes(dwCount, phServer, pRequestedDatatypes, ppErrors) 設定OPCItem(s)的數據類型
HRESULT CreateEnumerator(riid, ppUnk)產生OPCItems的列舉器
IConnectionPointContainer
HRESULT EnumConnectionPoints( IEnumConnectionPoints ppEnum)列舉所有的 Connection Points
HRESULT FindConnectionPoint( REFIID riid, IConnectionPoint ppCP)找出一個Connection Point

OPC Server結論

OPC Server/Client 是一套利用微軟的 COM/DCOM 技術達成工業自動化資料取得的架構。OPC Server 提供了許多的接口,Client 端通過這些接口,可以取得與 OPC Server 相連的硬件裝置的信息,而無須瞭解這些硬件裝置的細節信息。也就是説,程序設計者可以使用相同的程序代碼,操作不同的硬件裝置,充分達成 software reuse 的理想。同時,由於 COM/DCOM 已實作了網絡部分的細節,也使得 Client 對 OPC Server 進行遠程訪問非常容易,使程序設計者很容易地達成遠程控制的目標。
參考資料