S7-200OPCC++讀寫(xiě)PCAccess服務(wù)器_第1頁(yè)
S7-200OPCC++讀寫(xiě)PCAccess服務(wù)器_第2頁(yè)
S7-200OPCC++讀寫(xiě)PCAccess服務(wù)器_第3頁(yè)
已閱讀5頁(yè),還剩3頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、S7-200OPCC+讀寫(xiě)PCAccess服務(wù)器操作系統(tǒng):Windows7旗艦版開(kāi)發(fā)工具:VS2010開(kāi)發(fā)語(yǔ)言:MFC描述:對(duì)S7-200的VD100-VD220的31個(gè)雙字寄存器進(jìn)行寫(xiě);頭文件:OPCComm.h#pragmaonce#include"StdAfx.h"constLPWSTRg_szltemlD31=L"2,VD100,real",L"2,VD104,real",L"2,VD108,real",L"2,VD112,real",L"2,VD116,real",

2、L"2,VD120,real",L"2,VD124,real",L"2,VD128,real",L"2,VD132,real",L"2,VD136,real",L"2,VD140,real",L"2,VD144,real",L"2,VD148,real",L"2,VD152,real",L"2,VD156,real",L"2,VD160,real",L"2,VD164

3、,real",L"2,VD168,real",L"2,VD172,real",L"2,VD176,real",/VD164L"2,VD180,real",L"2,VD184,real",L"2,VD188,real",L"2,VD192,real",L"2,VD196,real",L"2,VD200,real",L"2,VD204,real",L"2,VD208,real&qu

4、ot;,L"2,VD212,real",L"2,VD216,real",L"2,VD220,real"classCOPCCommpublic:COPCComm(void);COPCComm(void);public:DWORDm_dwAdvise;OPCITEMDEFm_ltems31;VARIANTm_ItemValues31;LPWSTRm_ItemID31;OPCITEMATTRIBUTES*m_pItAttr;IUnknown*m_pUnknown;IOPCServer*mOPCServer;IOPCItemMgt*m_IOP

5、CItemMgt;lOPCSynclO*m_pOPCSync;lOPCGroupStateMgt*m_IOPCGroupStateMgt;IOPCAsynclO2*m_IOPCAsynclO2;OPCITEMRESULT*m_ltemResult;HRESULT*m_pErrors;OPCHANDLEm_GrpSrvHandle;MULTI_QIm_arrMultiQI6;DWORDm_TransactionID;OPCITEMDEF*m_pltems;OPCITEMSTATE*bdg;public:boolConnectSrv(void);boolDisconnect(void);boolA

6、ddGroup(void);boolAddItem(DWORDNumltems);boolWrite(DWORDNumltems);boolRemoveGroup(void);boolRemovItem(DWORDNumltems);floatRead(DWORDNumltems);源文件:OPCComm.cpp#include"StdAfx.h"#include"OPCComm.h"COPCComm:COPCComm(void)inti;m_pItAttr=newOPCITEMATTRIBUTES31;for(i=0;i<31;i+)m_pltA

7、ttri.szltemlD=L""m_pltAttri.szAccessPath=L""m_pltAttri.hClient=0;m_pItAttri.hServer=0;m_pltAttri.dwAccessRights=-1;m_pItAttri.bActive=FALSE;mOPCServer=NULL;mOPCItemMgt=NULL;COPCComm:COPCComm(void)if(m_pltAttr!=NULL)deletem_pItAttr;boolCOPCComm:ConnectSrv()/TODO:Addyourcontrolnoti

8、ficationhandlercodehereCLSIDOPCclsid;HRESULTr1;/*r1=CoInitialize(NULL);if(r1!=S_OK)if(r1=S_FALSE)AfxMessageBox(_T("COM庫(kù)已經(jīng)初始化");returntrue;elseAfxMessageBox(_T("COM庫(kù)初始化失敗");returntrue;*/r1=CLSIDFromProglD(L"S72OO.OPCServer",&OPCclsid);if(S_OK!=r1)if(CO_E_CLASSSTRING=

9、r1)AfxMessageBox(_T("TheProgIDisinvalid!");returntrue;elseif(REGDB_E_WRITEREGDB=r1)AfxMessageBox(_T("Regeditoccursanerror!");returntrue;r1=CoCreatelnstance(OPCclsid,NULL,CLSCTX_LOCAL_SERVER,IID_IOPCServer,(void*)&m_IOPCServer);if(S_OK!=r1)AfxMessageBox(_T("Createserverfa

10、iled!");mOPCServer=NULL;CoUninitialize();returntrue;returnfalse;boolCOPCComm:Disconnect()/TODO:Addyourcontrolnotificationhandlercodehereif(mOPCServer)m_IOPCServer->RemoveGroup(m_GrpSrvHandle,true);m_IOPCItemMgt->Release();m_IOPCServer->Release();returnfalse;boolCOPCComm:AddGroup()/TODO

11、:AddyourcontrolnotificationhandlercodehereLONGTimBias;FLOATPercDeadband;DWORDdwLCID;DWORDRevUpRate;/outHRESULTr1;TimBias=0;PercDeadband=0.0;dwLCID=0x409;r1=m0PCServer->AddGroup(L"gru1",TRUE,500,1,&TimBias,&PercDeadband,dwLCID,&m_GrpSrvHandle,&RevUpRate,IID_IOPCItemMgt,(L

12、PUNKNOWN*)&mOPCItemMgt);if(!FAILED(r1)AfxMessageBox(_T("Successfullyaddedgrouptoserver!");returnfalse;elseAfxMessageBox(_T("Can'taddgrouptoserver!");m_IOPCItemMgt=NULL;returntrue;returnfalse;boolCOPCComm:Addltem(DWORDNumItems)/TODO:Addyourcontrolnotificationhandlercodeher

13、eOPCITEMDEF*pItems;OPCITEMRESULT*pItResult;HRESULT*pErrors;HRESULTr1;DWORDi;/DWORDNumItems;/NumItems=31;pItems=newOPCITEMDEFNumltems;for(i=0;i<NumItems;i+)pltemsi.szAccessPath=L""pltemsi.szltemlD=g_szItemIDi;pltemsi.bActive=TRUE;pltemsi.hClient=i+1;pltemsi.dwBlobSize=0;pltemsi.pBlob=NUL

14、L;pltemsi.vtRequestedDataType=VT_R4;r1=m0PCItemMgt->Addltems(Numltems,pltems,&pltResult,&pErrors);if(FAILED(r1)&&(r1!=S_FALSE)AfxMessageBox(_T("Addltems-failed!");if(mOPCServer!=NULL)m_IOPCServer->Release();deletepltems;returntrue;exit(1);for(i=0;i<Numltems;i+)if(F

15、AILED(pErrorsi)AfxMessageBox(_T("erorr!");deletepltems;returntrue;elsem_pltAttri.szltemlD=g_szItemIDi;m_pltAttri.szAccessPath=pltemsi.szAccessPath;m_pltAttri.hClient=pltemsi.hClient;m_pItAttri.hServer=pltResulti.hServer;m_pltAttri.dwAccessRights=pltResulti.dwAccessRights;m_pltAttri.bActive

16、=pltemsi.bActive;if(pltResultO.dwAccessRights!=(OPC_READABLE+OPC_WRITEABLE)AfxMessageBox(_T("Itemcan'twriteandread!");deletepltems;returntrue;/*r1=mOPCItemMgt->QueryInterface(IID_IOPCAsyncIO2,(void*)&m_IOPCAsynclO2);if(r1<0)AfxMessageBox("IOPCAsynclO2沒(méi)有發(fā)現(xiàn),錯(cuò)誤的查詢門;CoTaskMe

17、mFree(m_ltemResult);m_IOPCItemMgt->Release();m_IOPCItemMgt=NULL;m_GrpSrvHandle=NULL;m_IOPCServer->Release();m0PCServer=NULL;CoUninitialize();return;*/deletepItems;returnfalse;boolCOPCComm:Write(DWORDNumItems)/TODO:Addyourcontrolnotificationhandlercodehere/*mtemlD0=g_szltemlDO;m_ltemValuesO.vt=

18、VT_R4;m_ItemValues0.fltVal=258.5;要寫(xiě)入的一Item值m_ItemID1=g_szItemID1;m_ItemValues1.vt=VT_R4;m_ItemValues1.fltVal=345.2;/要寫(xiě)入的另一Item值*/lOPCAsynclO*pAsynclO;OPCHANDLE*phServer;HRESULTr1,r2;HRESULT*pErrors;BOOLfound=FALSE;DWORDi;/NumItems=31;phServer=newOPCHANDLENumltems;for(i=0;i<NumItems;i+)phServeri=m

19、_pItAttri.hServer;r1=mOPCItemMgt->QueryInterface(IID_IOPCAsyncIO,(void*)&pAsynclO);if(FAILED(r1)AfxMessageBox(_T("IOPCAsynclO-Notsupported!");CoTaskMemFree(pErrors);deletephServer;returntrue;else/usingIOPCAsynclO:Write(.),lookatOPCspezifikationr2=pAsynclO->Write(0,NumItems,phServ

20、er,m_ItemValues,&m_TransactionlD,&pErrors);if(FAILED(r2)&&(r2=E_FAIL)AfxMessageBox(_T("Error-Write");deletephServer;returntrue;exit(1);pAsynclO->Release();/CoTaskMemFree(pltemValue);CoTaskMemFree(pErrors);deletephServer;returnfalse;floatCOPCComm:Read(DWORDNumItems)/TODO:

21、AddyourcontrolnotificationhandlercodeherelOPCSynclO*pSynclO;OPCHANDLE*phServer;HRESULTr1,r2;HRESULT*pErrors;BOOLfound=FALSE;DWORDi;CStringtemp;/NumItems=31;phServer=newOPCHANDLENumItems;for(i=0;i<Numitems;i+)phServeri=m_pltAttri.hServer;r1=m0PCItemMgt->Querylnterface(IID_IOPCSynclO,(void*)&

22、;pSynclO);if(FAILED(r1)AfxMessageBox(_T("IOPCAsynclO-Notsupported!");deletephServer;returntrue;else/usingIOPCAsynclO:Read(.),lookatOPCspezifikationr2=pSynclO->Read(OPC_DS_DEVICE,Numltems,phServer,&bdg,&pErrors);if(FAILED(r2)&&(r2=E_FAIL)AfxMessageBox(_T("Error-Read&

23、quot;);VariantClear(&bdgO.vDataValue);CoTaskMemFree(pErrors);deletephServer;return-999.0;/賦不可能值exit(1);temp.Format(_T("%.2f"),bdg0.vDataValue.fltVal);retData=bdg1.vDataValue.fltVal;VariantClear(&bdg0.vDataValue);pSyncIO->Release();CoTaskMemFree(pErrors);deletephServer;return(flo

24、at)_ttof(temp);boolCOPCComm:RemoveGroup()/TODO:AddyourcontrolnotificationhandlercodehereHRESULTr1;r1=mOPCServer->RemoveGroup(m_GrpSrvHandle,TRUE);if(SUCCEEDED(r1)AfxMessageBox(_T("Successfullyremovedgroup!");m_GrpSrvHandle=NULL;m_IOPCItemMgt->Release();m_IOPCItemMgt=NULL;elseAfxMessageBox(_T("Can'tremovegroup!");returntrue;returnfalse;boolCOPCComm:RemovItem(DWORDNumItems

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論