S7-200 OPC C++读写PC Access服务器
操作系统:Windows 7旗舰版 开发工具:VS2010 开发语言:MFC
描述:对S7-200的VD100-VD220的31个双字寄存器进行写;
头文件:OPCComm.h #pragma once #include \
const LPWSTR g_szItemID[31] = {L\L\
class COPCComm { public:
IUnknown* m_pUnknown; IOPCServer* m_IOPCServer; IOPCItemMgt* m_IOPCItemMgt; OPCITEMATTRIBUTES *m_pItAttr; COPCComm(void); ~COPCComm(void); DWORD m_dwAdvise; OPCITEMDEF m_Items[31]; VARIANT m_ItemValues[31];
L\L\L\L\L\L\
L\L\L\L\L\
public:
LPWSTR m_ItemID[31];
};
IOPCSyncIO* m_pOPCSync;
IOPCGroupStateMgt* m_IOPCGroupStateMgt; IOPCAsyncIO2* m_IOPCAsyncIO2; OPCITEMRESULT* m_ItemResult; HRESULT* m_pErrors;
OPCHANDLE m_GrpSrvHandle; MULTI_QI m_arrMultiQI[6]; DWORD m_TransactionID; OPCITEMDEF* m_pItems; OPCITEMSTATE* bdg; boolConnectSrv(void); bool Disconnect(void); boolAddGroup(void);
boolAddItem(DWORD NumItems); bool Write(DWORD NumItems); boolRemoveGroup(void);
boolRemovItem(DWORD NumItems); float Read(DWORD NumItems);
public:
源文件:OPCComm.cpp #include \#include \
COPCComm::COPCComm(void) { }
m_IOPCServer = NULL; m_IOPCItemMgt = NULL; for(i = 0; i< 31; i++) { }
m_pItAttr[i].szItemID = L\m_pItAttr[i].szAccessPath = L\m_pItAttr[i].hClient = 0; m_pItAttr[i].hServer = 0;
m_pItAttr[i].dwAccessRights = -1; m_pItAttr[i].bActive = FALSE;
m_pItAttr = new OPCITEMATTRIBUTES[31]; inti;
COPCComm::~COPCComm(void) { }
boolCOPCComm::ConnectSrv() { /*
if (r1 != S_OK)
if (r1 == S_FALSE)
{ {
AfxMessageBox(_T(\库已经初始化\
return true;
} else {
AfxMessageBox(_T(\库初始化失败\ return true;; } } */
if(S_OK!=r1) {
if(CO_E_CLASSSTRING == r1) { }
else if(REGDB_E_WRITEREGDB == r1) {
AfxMessageBox(_T(\AfxMessageBox(_T(\return true;
r1 = CLSIDFromProgID(L\r1 = CoInitialize(NULL);
// TODO: Add your control notification handler code here CLSID OPCclsid; HRESULT r1; if(m_pItAttr != NULL) { }
delete[] m_pItAttr;
}
}
return true;
r1 = CoCreateInstance(OPCclsid, NULL, CLSCTX_LOCAL_SERVER, IID_IOPCServer,
(void**)&m_IOPCServer);
if(S_OK != r1) {
AfxMessageBox(_T(\m_IOPCServer = NULL;
//CoUninitialize(); return true;; }
boolCOPCComm::Disconnect() { }
boolCOPCComm::AddGroup() {
TimBias = 0; PercDeadband = 0.0; dwLCID = 0x409;
// TODO: Add your control notification handler code here LONG TimBias; FLOAT PercDeadband; DWORD dwLCID;
DWORD RevUpRate; //out HRESULT r1; return false; if(m_IOPCServer) { }
m_IOPCServer->RemoveGroup(m_GrpSrvHandle,true); m_IOPCItemMgt->Release(); m_IOPCServer->Release();
// TODO: Add your control notification handler code here return false; }
{ }
boolCOPCComm::AddItem(DWORD NumItems) {
//DWORD NumItems; //NumItems = 31;
pItems = new OPCITEMDEF[NumItems];
for(i = 0; i pItems[i].szAccessPath = L\pItems[i].szItemID = g_szItemID[i]; pItems[i].bActive = TRUE; pItems[i].hClient = i + 1; pItems[i].dwBlobSize = 0; pItems[i].pBlob = NULL; pItems[i].vtRequestedDataType = VT_R4; // TODO: Add your control notification handler code here OPCITEMDEF* pItems; OPCITEMRESULT* pItResult; HRESULT* pErrors; DWORD i; return false; //AfxMessageBox(_T(\ return false; } else { } AfxMessageBox(_T(\return true; if(!FAILED(r1)) r1 = m_IOPCServer->AddGroup(L\ &RevUpRate, IID_IOPCItemMgt, (LPUNKNOWN*)&m_IOPCItemMgt); dwLCID, &m_GrpSrvHandle, m_IOPCItemMgt = NULL; HRESULT r1;