OPC Server概述is OPC?OPC是OLE for Process Control的缩写。
顾名思义,OPC是一种利用微软的COM/DCOM技术来达成自动化控制的协定,根据OPC Specification 的定义,OPC is "a standard mechanism for communicating to numerous data sources, either devices on the factory floor, or a database in a control room."在现今的工业自动化中,我们需要一套整合的信息系统,由底层的各项装置采集信息 (Field Management),中层的控制系统或图控应用程序进行程序的控制 (Process Management),再由最上层的整合软件将这些信息整合起来以供企业决策或效能提升,如下图所示:OPC 为硬件制造商与软件开发商提供了一条桥梁,透过硬件厂商提供的OPC Server 接口,软件开发者不必考虑各项不同硬件间的差异,便可自硬件端取得所需的信息,所以软件开发者仅需专注于程序本身的控制流程的运作。
此外,由于 COM/DCOM 实作并隐藏了网络的细节,透过 OPC 可以很容易地达成远程控制的理想。
does OPC work?在说明 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 运作的方式。
以下的图标可以让这样的概念更清晰。
3. OPC Server 架构如前面所述,OPC Server 透过一组一组的接口提供服务,不过在实作的架构上,OPC Server 共分为三层:分别是 OPCServer, OPCGroup, OPCItem其中每一个 OPCItem 对应到一个实际的硬件装置上的某一个 channel或 port;每一个 OPCGroup 则包含了许多的 OPCItem,同时并定义这些OPCItem 更新的时间、方式,以及提供读取 OPCItem 值的接口;而每一个 OPCServer 则包含若干个 OPCGroup,同时提供操作这些 OPCGroup 的接口。
下图可以较清楚地说明 OPCServer/OPCGroup/OPCItem 间的关系:4. OPC Custom Interface接下来的这一节,我们将讨论 OPC Serve 的重点,也就是每一组接口的定义,以及它所提供的功能。
OPCServer 提供的接口:IOPCCommonHRESULT SetLocaleID ( dwLcid ) 设定位置信息HRESULT GetLocaleID ( pdwLcid ) 取得位置信息HRESULT QueryAvailableLocaleIDs ( pdwCount, pdwLcid ) 查询可用的位置IDHRESULT GetErrorString ( dwError, ppString) 取得错误信息字符串HRESULT SetClientName (szName)设定Clinet的名称IOPCServerHRESULT AddGroup(szName, bActive, dwRequestedUpdateRate, hClientGroup, pTimeBias, pPercentDeadband, dwLCID, phServerGroup, pRevisedUpdateRate, riid, ppUnk) 新增一个 OPCGroupHRESULT GetErrorString(dwError, dwLocale, ppString) 取得错误信息字符串HRESULT GetGroupByName(szName, riid, ppUnk) 依据名称取得OPCGroup 的介面HRESULT GetStatus(ppServerStatus) 取得 OPCServer的状态信息HRESULT SetClientName (szName)设定Clinet的名称RemoveGroup(hServerGroup, bForce)移除一个OPCGroupHRESULT CreateGroupEnumerator(dwScope, riid, ppUnk) 产生一个OPCGroup 列举器IConnectionPointContainerHRESULT EnumConnectionPoints( IEnumConnectionPoints ppEnum) 列举所有的 Connection PointsHRESULT FindConnectionPoint( REFIID riid, IConnectionPoint ppCP)找出一个 Connection PointIOPCItemPropertiesHRESULT QueryAvailableProperties(szItemID,pdwCount,ppPropertyIDs, ppDescriptions, ppvtDataTypes ); 查询可用的 OPCItem属性HRESULT GetItemProperties (szItemID, dwCount,pdwPropertyIDs,ppvData, ppErrors ) 取得 OPCItem 的属性HRESULT LookupItemIDs( szItemID, dwCount,pdwPropertyIDs,ppszNewItemIDs, ppErrors )使用名称查询OPCItem 的IDIOPCBrowseServerAddressSpace (optional)HRESULT QueryOrganization(pNameSpaceType )查询组织名称HRESULT ChangeBrowsePosition(dwBrowseDirection, szString )变更浏览的位置HRESULT BrowseOPCItemIDs( dwBrowseFilterType, szFilterCriteria, vtDataTypeFilter, dwAccessRightsFilter, ppIEnumString )浏览OPCServer 内所有定义的 OPCItemHRESULT GetItemID( szItemDataID, szItemID ) 取得 OPCItem 的 ID HRESULT BrowseAccessPaths( szItemID, ppIEnumString )浏览存取OPCItem 的路径OPCGroup Object 提供的介面IOPCGroupStateMgtHRESULT 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) 复制一个OPCGroupIOPCSyncIOHRESULT 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)IOPCAsyncIO2HRESULT 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 设为 EnableHRESULT GetEnable(pbEnable) 传回 OPCGroup 是否为 Enable IOPCItemMgtHRESULT 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)是否为AvtiveHRESULT SetClientHandles(dwCount, phServer, phClient, ppErrors)设定 OPCItem(s) 的 handleHRESULT SetDatatypes(dwCount, phServer, pRequestedDatatypes, ppErrors) 设定OPCItem(s)的数据类型HRESULT CreateEnumerator(riid, ppUnk)产生OPCItems的列举器IConnectionPointContainerHRESULT EnumConnectionPoints( IEnumConnectionPoints ppEnum)列举所有的 Connection PointsHRESULT FindConnectionPoint( REFIID riid, IConnectionPoint ppCP)找出一个Connection Point5. 结论OPC Server/Client 是一套利用微软的 COM/DCOM 技术达成工业自动化资料取得的架构。