完整word版OPC client 在VC环境下编程文档格式.docx
- 文档编号:8024811
- 上传时间:2023-05-09
- 格式:DOCX
- 页数:17
- 大小:24.34KB
完整word版OPC client 在VC环境下编程文档格式.docx
《完整word版OPC client 在VC环境下编程文档格式.docx》由会员分享,可在线阅读,更多相关《完整word版OPC client 在VC环境下编程文档格式.docx(17页珍藏版)》请在冰点文库上搜索。
使用到的函数说明
CoInitialize(NULL);
初始化COM库
CoInitialize是Windows提供的API函数,用来告诉Windows以单线程的方式创建com对象。
应用程序调用com库函数(除CoGetMalloc和内存分配函数)之前必须初始化com库。
返回值S_OK:
该线程中COM库初始化成功S_FALSE该线程中COM库已经被初始化CoInitialize()标明以单线程方式创建。
使用CoInitialize创建可以使对象直接与线程连接,得到最高的性能。
CoInitialize并不装载COM库,它只用来初始化当前线程使用什么样的套间。
使用这个函数后,线程就和一个套间建立了对应关系。
线程的套间模式决定了该线程如何调用COM对象,是否需要列集等。
CoInitialize()并不会干扰客户和服务器之间的通信,它所做的事情是让线程注册一个套间,而线程运行过程中必然在此套间。
CoInitialize和CoUninitialize必须成对使用
查找OPC服务
CLSIDFromProgID(L"
OPC服务的名称"
&
clsid);
通过ProgID,查找注册表中的相关CLSID
参数:
1.服务的名称
2.CLSID型变量,用来接收注册表中查找到的CLSID
创建OPC服务器对象
CoCreateInstance(clsid,NULL,CLSCTX_LOCAL_SERVER,IID_IOPCServer,(void**)&
m_IOPCServer);
创建OPC服务器对象,并查询对象的IID_IOPCServer接口
1.CLSID型变量,使用CLSIDFromProgID函数查找到的CLSID
2.
3.
函数功能描述:
用指定的类标识符创建一个Com对象,用指定的类标识符创建一个未初始化的对象。
当在本机中只创建一个对象时,可以调用CoCreateInstance;
在远程系统中创建一个对象时,可以调用CoCreateInstanceEx;
创建多个同一CLSID的对象时,可以参考CoGetClassObject函数。
函数原形:
STDAPICoCreateInstance(
REFCLSIDrclsid,//创建的Com对象的类标识符(CLSID)
LPUNKNOWNpUnkOuter,//指向接口IUnknown的指针
DWORDdwClsContext,//运行可执行代码的上下文
REFIIDriid,//创建的Com对象的接口标识符
LPVOID*ppv//用来接收指向Com对象接口地址的指针变量
);
参数:
rclsid
[in]用来唯一标识一个对象的CLSID(128位),需要用它来创建指定对象。
pUnkOuter
[in]如果为NULL,表明此对象不是聚合式对象一部分。
如果不是NULL,则指针指向一个聚合式对象的IUnknown接口。
dwClsContext
[in]组件类别.可使用CLSCTX枚举器中预定义的值.
riid
[in]引用接口标识符,用来与对象通信。
ppv
[out]用来接收指向接口地址的指针变量。
如果函数调用成功,*ppv包括请求的接口指针。
返回值:
S_OK
指定的Com对象实例被成功创建。
REGDB_E_CLASSNOTREG
指定的类没有在注册表中注册.也可能是指定的dwClsContext没有注册或注册表中的服务器类型损坏
CLASS_E_NOAGGREGATION
这个类不能创建为聚合型。
E_NOINTERFACE
指定的类没有实现请求的接口,或者是IUnknown接口没有暴露请求的接口.
注释:
CoCreateInstance帮助者函数通过使用对象的CLSID,提供了一种便洁的方式与类对象连接,创建未初始化的实例,以及释放类对象。
它封装了以下的功能:
CoGetClassObject(rclsid,dwClsContext,NULL,IID_IClassFactory,&
pCF);
hresult=pCF->
CreateInstance(pUnkOuter,riid,ppvObj);
pCF->
Release();
当在本机中只创建一个对象时,调用CoCreateInstance是最方便的;
如果要在远程系统中创建一个对象时,可以调用CoCreateInstanceEx;
创建多个同一CLSID的对象时,可以参考CoGetClassObject函数;
如果创建多个对象实例,可以获得类对象的IClassFactory接口指针,并使用需要的方法,可以使用CoGetClassObject函数。
在CLSCTX枚举器中,你可以指定用来管理对象的服务器类型.这些常量可以是CLSCTX_INPROC_SERVER,CLSCTX_INPROC_HANDLER,CLSCTX_LOCAL_SERVER或是它们的任何组合.常量CLSCTX_ALL被定义为这三个值的组合.想获得更多的有关这些常量的用法,请参考CLSCTX.
添加组到OPC服务器
m_IOPCServer->
AddGroup(L"
grp1"
//[in]组名
TRUE,//[in]活动状态
500,//[in]向服务器发送请求的刷新率
1,//[in]客户端的操作句柄
&
TimeBias,//[in]与标准时间的校正值
PercentDeadband,//[in]要舍弃的数据
LOCALE_ID,//[in]服务器使用的语言
m_GrpSrvHandle,//[out]添加组以后服务器返回的组句柄
RevisedUpdateRate,//[out]服务器的数据刷新率
IID_IOPCItemMgt,//[in]添加组的接口类型
(LPUNKNOWN*)&
m_IOPCItemMgt);
//[out]服务器返回的接口对象指针
AddGroup函数的说明
HRESULTAddGroup(
[in,string]LPCWSTRszName,
[in]BOOLbActive,
[in]DWORDdwRequestedUpdateRate,
[in]OPCHANDLEhClientGroup,
[unique,in]LONG*pTimeBias,
[in]FLOAT*pPercentDeadband,
[in]DWORDdwLCID,
[out]OPCHANDLE*phServerGroup,
[out]DWORD*pRevisedUpdateRate,
[in]REFIIDriid,
[out,iid_is(riid)]LPUNKNOWN*ppUnk
);
Parameters
Description
szName
Nameofthegroup.Thenamemustbeuniqueamongtheothergroupscreatedbythisclient.Ifnonameisprovided(szNameispointertoaNULstring)theserverwillgenerateauniquename.Theservergeneratednamewillalsobeuniquerelativetoanyexistingpublicgroups.
bActive
FALSEiftheGroupistobecreatedasinactive.
TRUEiftheGroupistobecreatedasactive.
dwRequestedUpdateRate
ClientSpecifiesthefastestrateatwhichdatachangesmaybesenttoOnDataChangeforitemsinthisgroup.ThisalsoindicatesthedesiredaccuracyofCachedData.Thisisintendedonlytocontrolthebehavioroftheinterface.Howtheserverdealswiththeupdaterateandhowoftenitactuallypollsthehardwareinternallyisanimplementationdetail.Passing0indicatestheservershouldusethefastestpracticalrate.Therateisspecifiedinmilliseconds.
hClientGroup
Clientprovidedhandleforthisgroup.[refertodescriptionofdatatypes,parameters,andstructuresformoreinformationaboutthisparameter]
pTimeBias
PointertoLongcontainingtheinitialTimeBias(inminutes)fortheGroup.PassaNULLPointerifyouwishthegrouptousethedefaultsystemTimeBias.SeediscussionofTimeBiasinGeneralPropertiesSectionSeeCommentsbelow.
pPercentDeadband
Thepercentchangeinanitemvaluethatwillcauseasubscriptioncallbackforthatvaluetoaclient.ThisparameteronlyappliestoitemsinthegroupthathavedwEUTypeofAnalog.[SeediscussionofPercentDeadbandinGeneralPropertiesSection].ANULLpointerisequivalentto0.0.
dwLCID
Thelanguagetobeusedbytheserverwhenreturningvalues(includingEUenumeration’s)astextforoperationsonthisgroup.Thiscouldalsoincludesuchthingsasalarmorstatusconditionsordigitalcontactstates.
phServerGroup
Placetostoretheuniqueservergeneratedhandletothenewlycreatedgroup.Theclientwillusetheserverprovidedhandleformanyofthesubsequentfunctionsthattheclientrequeststheservertoperformonthegroup.
pRevisedUpdateRate
TheserverreturnsthevalueitwillactuallyusefortheUpdateRatewhichmaydifferfromtheRequestedUpdateRate.
Notethatthismayalsobeslowerthantherateatwhichtheserverisinternallyobtainingthedataandupdatingthecache.
Ingeneraltheservershould‘roundup’therequestedratetothenextavailablesupportedrate.Therateisspecifiedinmilliseconds.ServerreturnsHRESULTofOPC_S_UNSUPPORTEDRATEwhenitreturnsavalueinrevisedUpdateRatethatisdifferentthanRequestedUpdateRate.
riid
Thetypeofinterfacedesired(e.g.IID_IOPCItemMgt)
ppUnk
Wheretostorethereturnedinterfacepointer.NULLisreturnedforanyFAILEDHRESULT.
添加项目
m_IOPCItemMgt->
AddItems(1,//[in]添加1个item
m_Items,//[in]添加的item的指针
&
m_ItemResult,//[out]添加item的结果
m_pErrors);
//[out]发生的错误
m_Items的说明
定义:
OPCITEMDEFm_Items[1];
这个参数为OPCITEMDEF结构,包含着项的存取路径,定义和被请求的数据类等。
AddItems函数说明
HRESULTAddItems(
[in]DWORDdwCount,
[in,size_is(dwCount)]OPCITEMDEF*pItemArray,
[out,size_is(,dwCount)]OPCITEMRESULT**ppAddResults,
[out,size_is(,dwCount)]HRESULT**ppErrors
dwCount
Thenumberofitemstobeadded
pItemArray
ArrayofOPCITEMDEFs.Thesetelltheservereverythingitneedstoknowabouttheitemincludingtheaccesspath,definitionandrequesteddatatype
ppAddResults
ArrayofOPCITEMRESULTs.Thistellstheclientadditionalinformationabouttheitemincludingtheserverassigneditemhandleandthecanonicaldatatype.
ppErrors
ArrayofHRESULTs.Thistellstheclientwhichoftheitemswassuccessfullyadded.Foranyitemwhichfaileditprovidesareason.
OPCITEMDEF结构说明
typedefstruct{
[string]LPWSTRszAccessPath;
//OPC服务器存取路径
[string]LPWSTRszItemID;
//item的名称
BOOLbActive;
//活动状态
OPCHANDLEhClient;
//操作句柄
DWORDdwBlobSize;
//item的pBlob大小
[size_is(dwBlobSize)]BYTE*pBlob;
//二进制指针
VARTYPEvtRequestedDataType;
//数据类型由客户端请求
WORDwReserved;
//保留字
}OPCITEMDEF;
Member
Usedby
szAccessPath
both
Theaccesspaththeservershouldassociatewiththisitem.ByconventionapointertoaNULstringspecifiesthattheservershouldselecttheaccesspath.Supportforaccesspathisoptional
NOTE:
version1indicatedthataNULLpointerwouldallowtheservertopickthepathhoweverpassingaNULLpointerwillcauseafaultintheproxy/stubcodeandthusisnotallowed.
szItemID
Anull-terminatedstringthatuniquelyidentifiestheOPCdataitem.SeetheItemIDdiscussionandtheAddItemsfunctionforspecificinformationaboutthecontentsofthisfield.
add
ThisBooleanvalueaffectsthebehaviorvariousmethodsasdescribedelsewhereinthisspecification.
hClient
Thehandletheclientwishestoassociatewiththeitem.SeetheOPCHANDLEformorespecificinformationaboutthecontentsofthisfield.
dwBlobSize
ThesizeofthepBlobforthisitem.
pBlob
pBlobisapointertotheBlob.
vtRequestedDataType
Thedatatyperequestedbytheclient.Anerrorisreturned(SeeAdditemsorValidateItems)iftheservercannotprovidetheiteminthisformat.PassingVT_EMPTYmeanstheclientwillaccepttheserverscanonicaldatatype.
查询同步接口
QueryInterface(IID_IOPCSyncIO,(void**)&
m_IOPCSyncIO);
参数:
1.IID_IOPCSyncIO同步接口
2.服务器返回的用于操作同步接口的指针。
同步接口读取数据
m_IOPCSyncIO->
Read(OPC_DS_DEVICE,1,phServer,&
pItemValue,&
pErrors);
1.OPC_DS_DEVICE从OPC设备读取另一个选择是OPC_DS_CACHE从缓存读取
2.读取的数量
3.这个item的服务句柄
4.返回值的OPCITEMSTATE结构数值指针
5.返回的错误指针
HRESULTRead(
[in]OPCDATASOURCEdwSource,
[in]DWORDdwCount,
[in,size_is(dwCount)]OPCHANDLE*phServer,
[out,size_is(,dwCount)]OPCITEMSTATE**ppItemValues,
dwSource
The‘datasource’;
OPC_DS_CACHEorOPC_DS_DEVICE
Thenumberofitemstoberead.
phSer
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 完整word版OPC client 在VC环境下编程 完整 word OPC VC 环境 编程