1、S7-200 OPC C+读写 PC Access 服务器 操作系统:Windows 7 旗舰版开发工具:VS2010 开发语言:MFC描述:对 S7-200 的 VD100-VD220 的 31 个双字寄存器进行写;头文件:OPCComm.h#pragma once#include “StdAfx.h“const LPWSTR g_szItemID31 = 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, V
2、D124, 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, rea
3、l“, 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“;class COPCCommpublic:COPCComm(void);COPCComm(void);public:DWORD m_dwAdvise;OPCITEMDEF m_Items31;VARIANT m_ItemValues31;LPWSTR
4、m_ItemID31;OPCITEMATTRIBUTES *m_pItAttr;IUnknown* m_pUnknown;IOPCServer* m_IOPCServer;IOPCItemMgt* m_IOPCItemMgt;IOPCSyncIO* m_pOPCSync;IOPCGroupStateMgt* m_IOPCGroupStateMgt;IOPCAsyncIO2* m_IOPCAsyncIO2;OPCITEMRESULT* m_ItemResult;HRESULT* m_pErrors;OPCHANDLE m_GrpSrvHandle;MULTI_QI m_arrMultiQI6;D
5、WORD m_TransactionID; OPCITEMDEF* m_pItems;OPCITEMSTATE* bdg;public:bool ConnectSrv(void);bool Disconnect(void);bool AddGroup(void);bool AddItem(DWORD NumItems);bool Write(DWORD NumItems);bool RemoveGroup(void);bool RemovItem(DWORD NumItems);float Read(DWORD NumItems);源文件:OPCComm.cpp#include “StdAfx
6、.h“#include “OPCComm.h“COPCComm:COPCComm(void)int i;m_pItAttr = new OPCITEMATTRIBUTES31;for(i = 0; i RemoveGroup(m_GrpSrvHandle,true);m_IOPCItemMgt-Release();m_IOPCServer-Release();return false;bool COPCComm:AddGroup() / TODO: Add your control notification handler code hereLONG TimBias;FLOAT PercDea
7、dband;DWORD dwLCID;DWORD RevUpRate; /outHRESULT r1;TimBias = 0;PercDeadband = 0.0;dwLCID = 0x409;r1 = m_IOPCServer-AddGroup(L“gru1“, TRUE, 500, 1, if(!FAILED(r1)/AfxMessageBox(_T(“Successfully added group to server!“);return false;elseAfxMessageBox(_T(“Cant add group to server!“);m_IOPCItemMgt = NUL
8、L;return true;return false;bool COPCComm:AddItem(DWORD NumItems) / TODO: Add your control notification handler code hereOPCITEMDEF* pItems;OPCITEMRESULT* pItResult;HRESULT* pErrors;HRESULT r1;DWORD i;/DWORD NumItems;/NumItems = 31;pItems = new OPCITEMDEFNumItems;for(i = 0; i AddItems(NumItems, pItem
9、s, if(FAILED(r1) if(m_IOPCServer != NULL)m_IOPCServer-Release();delete pItems;return true;/exit(1);for (i = 0; i QueryInterface(IID_IOPCAsyncIO2, (void*) if (r1 Release(); m_IOPCItemMgt = NULL; m_GrpSrvHandle = NULL; m_IOPCServer-Release(); m_IOPCServer = NULL; CoUninitialize(); return; */delete pIt
10、ems;return false;bool COPCComm:Write(DWORD NumItems) / TODO: Add your control notification handler code here/*m_ItemID0 = g_szItemID0;m_ItemValues0.vt = VT_R4;m_ItemValues0.fltVal = 258.5; /要写入的一 Item 值m_ItemID1 = g_szItemID1; m_ItemValues1.vt = VT_R4;m_ItemValues1.fltVal = 345.2; /要写入的另一 Item 值*/IO
11、PCAsyncIO* pAsyncIO;OPCHANDLE* phServer;HRESULT r1, r2;HRESULT* pErrors;BOOL found = FALSE;DWORD i;/NumItems = 31;phServer = new OPCHANDLENumItems;for(i = 0; i QueryInterface(IID_IOPCAsyncIO, (void *)if(FAILED(r1)AfxMessageBox(_T(“IOPCAsyncIO - Not supported !“);CoTaskMemFree(pErrors);delete phServe
12、r;return true;else/using IOPCAsyncIO:Write(.), look at OPC spezifikationr2 = pAsyncIO-Write(0, NumItems, phServer, m_ItemValues,if(FAILED(r2) delete phServer;return true;/exit(1);pAsyncIO-Release();/CoTaskMemFree(pItemValue);CoTaskMemFree(pErrors);delete phServer;return false;float COPCComm:Read(DWO
13、RD NumItems)/ TODO: Add your control notification handler code hereIOPCSyncIO* pSyncIO;OPCHANDLE* phServer;HRESULT r1, r2;HRESULT* pErrors;BOOL found = FALSE;DWORD i;CString temp;/NumItems = 31;phServer = new OPCHANDLENumItems;for(i = 0; i QueryInterface(IID_IOPCSyncIO, (void *)if(FAILED(r1)AfxMessa
14、geBox(_T(“IOPCAsyncIO - Not supported !“);delete phServer;return true;else/using IOPCAsyncIO:Read(.), look at OPC spezifikationr2 = pSyncIO-Read(OPC_DS_DEVICE, NumItems, phServer,if(FAILED(r2) VariantClear (CoTaskMemFree(pErrors);delete phServer;return -999.0;/赋不可能值/exit(1);temp.Format(_T(“%.2f“), b
15、dg0.vDataValue.fltVal);retData = bdg1.vDataValue.fltVal;VariantClear (pSyncIO-Release();CoTaskMemFree(pErrors);delete phServer;return (float)_ttof(temp);bool COPCComm:RemoveGroup() / TODO: Add your control notification handler code hereHRESULT r1;r1 = m_IOPCServer-RemoveGroup(m_GrpSrvHandle, TRUE);i
16、f(SUCCEEDED(r1)/AfxMessageBox(_T(“Successfully removed group !“);m_GrpSrvHandle = NULL;m_IOPCItemMgt-Release();m_IOPCItemMgt = NULL;elseAfxMessageBox(_T(“Cant remove group ! “);return true;return false;bool COPCComm:RemovItem(DWORD NumItems) / TODO: Add your control notification handler code hereHRESULT r1;HRESULT* pErrors;OPCHANDLE* phServer;DWORD i;phServer = new OPCHANDLENumItems;for(i = 0; i RemoveItems(NumItems, phServer, if(FAILED(r1) delete phServer;return true;/exit(1);