S7200OPCC++读写PCAccess服务器.docx
《S7200OPCC++读写PCAccess服务器.docx》由会员分享,可在线阅读,更多相关《S7200OPCC++读写PCAccess服务器.docx(15页珍藏版)》请在冰豆网上搜索。
![S7200OPCC++读写PCAccess服务器.docx](https://file1.bdocx.com/fileroot1/2023-4/16/1bf8f879-ed28-4de3-b10d-380ed98a64f3/1bf8f879-ed28-4de3-b10d-380ed98a64f31.gif)
S7200OPCC++读写PCAccess服务器
S7-200OPCC读写PCAcceS服务器
操作系统:
Windows7旗舰版
开发工具:
VS2010
开发语言:
MFC
描述:
对S7-20C的VD100-VD22的31个双字寄存器进行写;
头文件:
OPCComm.h
#pragmaonce
#include"StdAfx.h"
constLPWSTRg_szltemlD[31]={L"2,VD100,real",L"2,VD104,real",L"2,VD108,real",L"2,VD112,
real",L"2,VD116,real",
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,real",L"2,VD168,real",L"2,VD172,real",
L"2,VD176,real",//VD164
L"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",L"2,VD212,
real",L"2,VD216,real",
L"2,VD220,real"};
classCOPCComm
{
public:
COPCComm(void);
~COPCComm(void);
public:
DWORDm_dwAdvise;
OPCITEMDEfi_ltems[31];
VARIANTm」temValues[31];
LPWSTRm_ItemID[31];
OPCITEMATTRIBUTES*m_pltAttr;
IUnknown*m_pUnknown;
lOPCServer*m」OPCServer;
IOPCItemMgt*m」OPCItemMgt;
IOPCSynclO*m_pOPCSync;
IOPCGroupStateMgt*m」OPCGroupStateMgt;
IOPCAsyncIO2*m」OPCAsynclO2;
OPCITEMRESULT*m_ltemResult;
HRESULT*m_pErrors;
OPCHANDLEm_GrpSrvHandle;
MULTI_QIm_arrMultiQI[6];
DWORDm_TransactionID;
OPCITEMDEF*m_pltems;
OPCITEMSTATE*bdg;
public:
boolConnectSrv(void);
boolDisconnect(void);
boolAddGroup(void);
boolAddltem(DWORDNumitems);
boolWrite(DWORDNumitems);
boolRemoveGroup(void);
boolRemovitem(DWORDNumitems);floatRead(DWORDNumitems);
};
源文件:
OPCComm.cpp
#include"StdAfx.h"
#include"OPCComm.h"
COPCComm:
:
COPCComm(void)
{
inti;
m_pltAttr=new0PCITEMATTRIBUTES[31];
for(i=0;i<31;i++)
{
m_pltAttr[i].szltemlD=L"";m_pltAttr[i].szAccessPath=L"";m_pltAttr[i].hClient=0;m_pItAttr[i].hServer=0;m_pltAttr[i].dwAccessRights=-1;m_pltAttr[i].bActive=FALSE;
}m」OPCServer=NULL;
m」OPCItemMgt=NULL;
}
COPCComm:
:
~COPCComm(void)
{
if(m_pltAttr!
=NULL)
{
delete]]m_pltAttr;
}
}
boolCOPCComm:
:
ConnectSrv()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
CLSIDOPCclsid;
HRESULTr1;
/*
r1=CoInitialize(NULL);
if(r1!
=S_OK)
{
if(r1==S_FALSE)
{
AfxMessageBox(_T("CO库已经初始化"));returntrue;
}
else
{
AfxMessageBox(_T("CO库初始化失败"));
returntrue;;
}
}
*/
r1=CLSIDFromProglD(L"S7200.0PCServer",&OPCclsid);
if(S_0K!
=r1)
if(CO_E_CLASSSTRING==r1)
{
AfxMessageBox(_T("TheProgIDisinvalid!
"));
returntrue;
}
elseif(REGDB_E_WRITEREGDB==r1)
{
AfxMessageBox(_T("Regeditoccursanerror!
"));
returntrue;
}
}
r1=CoCreateInstance(OPCclsid,NULL,CLSCTX_LOCAL_SERVER,IID」OPCServer,(void**)&m_lOPCServer);
if(S_OK!
=r1)
{
AfxMessageBox(_T("Createserverfailed!
"));
m_IOPCServer=NULL;
//CoUninitialize();
returntrue;;
}
returnfalse;
}
boolCOPCComm:
:
Disconnect()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(m」OPCServer)
{
m」OPCServer->RemoveGroup(m_GrpSrvHandle,true);
m」OPCItemMgt->Release();
m」OPCServer->Release();
returnfalse;
}
boolCOPCComm:
:
AddGroup()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
LONGTimBias;
FLOATPercDeadband;
DWORDdwLCID;
DWORDRevUpRate;//out
HRESULTr1;
TimBias=0;
PercDeadband=0.0;
dwLCID=0x409;
r1=m_lOPCServer->AddGroup(L"gru1",TRUE,500,1,&TimBias,&PercDeadband,dwLCID,
&m_GrpSrvHandle,
&RevUpRate,IID」OPCItemMgt,(LPUNKNOWN*)&m_IOPCItemMgt);
if(!
FAILED(r1))
{
〃AfxMessageBox(_T("Successfullyaddedgrouptoserver!
"));returnfalse;
}
else
AfxMessageBox(_T("Can'taddgrouptoserver!
"));m」OPCItemMgt=NULL;
returntrue;
}returnfalse;
}
boolCOPCComm:
:
Addltem(DWORDNumitems)
{
//TODO:
Addyourcontrolnotificationhandlercodehere
OPCITEMDEF*pItems;
OPCITEMRESULT*pItResult;
HRESULT*pErrors;
HRESULTr1;
DWORDi;
//DWORDNumItems;
//NumItems=31;
pItems=newOPCITEMDEF[Numltems];
for(i=0;i{
pltems[i].szAccessPath=L"";
pltems[i].szltemlD=g_szltemlD[i];
pltems[i].bActive=TRUE;
pltems[i].hClient=i+1;
pltems[i].dwBlobSize=0;
pltems[i].pBlob=NULL;
pltems[i].vtRequestedDataType=VT_R4;
}
r1=m」OPCItemMgt->Addltems(Numltems,pltems,&pltResult,&pErrors);
if(FAILED(r1)&&(r1!
=S_FALSE))
{
AfxMessageBox(_T("AddItems-failed!
"));
if(m」OPCServer!
=NULL)
{
m」OPCServer->Release();
}
delete]]pItems;
returntrue;
〃exit
(1);
}
for(i=0;i{
if(FAILED(pErrors[i]))
{
AfxMessageBox(_T("erorr!
"));
delete[]pItems;
returntrue;
}
else
{
m_pltAttr[i].szltemlD=g_szltemlD[i];
m_pltAttr[i].szAccessPath=pltems[i].szAccessPath;m_pltAttr[i].hClient=pltems[i].hClient;m_pltAttr[i].hServer=pltResult[i].hServer;
m_pltAttr[i].dwAccessRights=pltResult[i].dwAccessRights;m_pltAttr[i].bActive=pltems[i].bActive;
if(pltResult[O].dwAccessRights!
=(OPC_READABLE+OPC_WRITEABLE)){
AfxMessageBox(_T("ltemcan'twriteandread!
"));
delete[]pltems;
returntrue;
/*r1=m」OPCItemMgt->Querylnterface(IID」OPCAsyncIO2,(void**)&m」OPCAsynclO2);
if(r1<0)
{
AfxMessageBox("IOPCAsynCO2发现,错误的查询门;
CoTaskMemFree(m」temResult);
m」OPCItemMgt->Release();
m」OPCItemMgt=NULL;
m_GrpSrvHandle=NULL;
m」OPCServer->Release();
m_IOPCServer=NULL;
CoUninitialize();
return;
}*/
delete[]pItems;
returnfalse;
}
boolCOPCComm:
:
Write(DWORDNumItems)
{
//TODO:
Addyourcontrolnotificationhandlercodehere
/*
m_ltemlD[0]=g_szltemlD[O];
m」temValues[0].vt=VT_R4;
m」temValues[0].fltVal=258.5;〃要写入的一Item值
m_ItemID[1]=g_szItemID[1];
m」temValues[1].vt=VT_R4;
m」temValues[1].fltVal=345.2;//要写入的另一Item值
*/
IOPCAsynclO*pAsynclO;
OPCHANDLE*phServer;
HRESULTr1,r2;
HRESULT*pErrors;
BOOLfound=FALSE;
DWORDi;
//Numitems=31;
phServer=newOPCHANDLE[Numltems];
for(i=0;i{
phServer[i]=m_pItAttr[i].hServer;
r1=m」OPCItemMgt->Querylnterface(IID」OPCAsynclO,(void**)&pAsynclO);if(FAILED(r1))
{
AfxMessageBox(_T("IOPCAsynclO-Notsupported!
"));
CoTaskMemFree(pErrors);
delete[]phServer;
returntrue;
}
else
//usinglOPCAsynclO:
:
Write(..),lookatOPCspezifikationr2=pAsyncIO->Write(0,NumItems,phServer,m_ltemValues,
&m_TransactionID,&pErrors);
if(FAILED(r2)&&(r2==E_FAIL))
{
AfxMessageBox(_T("Error-Write"));
delete]]phServer;
returntrue;
//exit
(1);
}
}pAsynclO->Release();
//CoTaskMemFree(pltemValue);
CoTaskMemFree(pErrors);
delete]]phServer;
returnfalse;
}
floatCOPCComm:
:
Read(DWORDNumItems)
{
//TODO:
Addyourcontrolnotificationhandlercodehere
IOPCSynclO*pSynclO;
OPCHANDLE*phServer;
HRESULTr1,r2;
HRESULT*pErrors;
BOOLfound=FALSE;
DWORDi;
CStringtemp;
//Numitems=31;
phServer=newOPCHANDLE[Numltems];
for(i=0;i{
phServer[i]=m_pItAttr[i].hServer;
r1=m」OPCItemMgt->Querylnterface(IID」OPCSynclO,(void**)&pSynclO);if(FAILED(r1))
{
AfxMessageBox(_T("IOPCAsynclO-Notsupported!
"));
delete[]phServer;
returntrue;
}
else
〃usinglOPCAsynclO:
:
Read(..),lookatOPCspezifikation
r2=pSynclO->Read(OPC_DS_DEVICE,Numitems,phServer,&bdg,&pErrors);
if(FAILED(r2)&&(r2==E_FAIL))
{
AfxMessageBox(_T("Error-Read"));
VariantClear(&bdg[O].vDataValue);
CoTaskMemFree(pErrors);
delete[]phServer;
return-999.0;//赋不可能值
〃exit
(1);
}
}
temp.Format(_T("%.2f"),bdg[O].vDataValue.fltVal);
retData=bdg[1].vDataValue.fltVal;
VariantClear(&bdg[O].vDataValue);
pSynclO->Release();
CoTaskMemFree(pErrors);
delete[]phServer;
return(float)_ttof(temp);
}
boolCOPCComm:
:
RemoveGroup()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
HRESULTr1;
r1=m」OPCServer->RemoveGroup(m_GrpSrvHandle,TRUE);
if(SUCCEEDED(r1))
{
〃AfxMessageBox(_T("Successfullyremovedgroup!
"));
m_GrpSrvHandle=NULL;
m」OPCItemMgt->Release();
m」OPCItemMgt=NULL;
}
else
{
AfxMessageBox(_T("Can'tremovegroup!
"));
returntrue;
}
returnfalse;
}
boolCOPCComm:
:
Removltem(DWORDNumItems)
{
//TODO:
Addyourcontrolnotificationhandlercodehere
HRESULTr1;
HRESULT*pErrors;
OPCHANDLE*phServer;
DWORDi;
phServer=newOPCHANDLE[Numltems];
for(i=0;i{
phServer[i]=m_pItAttr[i].hServer;
}r1=m」OPCItemMgt->Removeltems(Numltems,phServer,&pErrors);
if(FAILED(r1)&&(r1!
=S_FALSE))
AfxMessageBox(_T("Removeltems-failed!
"));
delete]]phServer;
returntrue;
〃exit
(1);
}
delete]]phServer;
returnfalse;
}
主程序调用:
if(m_OPC.ConnectSrv())
{
AfxMessageBox(_T本地服务器没连上!
!
"));return;
}
if(m_OPC.AddGroup())
{
AfxMessageBox(_T添加组失败!
!
"));
return;
if(m_OPC.Addltem((DWORD)31))
{
AfxMessageBox(_T添加项失败!
!
"));return;
}j=0;
for(i=0;i<16;i++)
{
m_OPC.m」temValues[i].vt=VT_R4;
〃m_OPC.m」temValues[i].fltVal=m_PIC.m_Len[i];
}
j=0;
for(i=16;i<31;i++,j++)
{
m_OPC.m」temValues[i].vt=VT_R4;
〃m_OPC.m」temValues[i].fltVal=m_PIC.m_Angle[j];
}
m_OPC.Write((DWORD)31);