| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1762 人关注过本帖
标题:[求助]这段C++的程序怎么转成C#啊(DCS系统的OPC客户端)
只看楼主 加入收藏
zorroxiao
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2007-5-9
收藏
 问题点数:0 回复次数:3 
[求助]这段C++的程序怎么转成C#啊(DCS系统的OPC客户端)

头文件:

#ifndef __OPCDCSConnector_H__
#define __OPCDCSConnector_H__
#ifndef __IDCSConnector_H__
#include "DCSConnector.h"
#endif
/*
Author ZDL 2005.2.12
*/
#include "opcda.h"
#include "opc_ae.h"

using namespace std;
class OPCDCSConnector : IDCSConnector{
public :
virtual long __stdcall Initialize() ;
virtual long __stdcall Connect(IN pConnectStr );
virtual long __stdcall Write(IN const Item * );
virtual long __stdcall read(OUT Item * );
virtual long __stdcall Write(IN const Item *,IN const int );
virtual long __stdcall read(OUT Item *,IN const int);
virtual long __stdcall ReleaseResources();
virtual long __stdcall getLastError(OUT char * );
OPCDCSConnector();
protected :

private :
CHAR * m_szOPCServer;
OPCHANDLE m_hGroupHandle;
OPCHANDLE m_hConnectionHandle;
OPCHANDLE m_hServerGroup;
IOPCServer* m_pIOPCServer ; //pointer to IOPServer interface
IOPCItemMgt* m_pIOPCItemMgt; //pointer to IOPCItemMgt interface

};
long AddTheGroup(IOPCServer* pIOPCServer, IOPCItemMgt* &pIOPCItemMgt,OPCHANDLE& hServerGroup);
long AddTheItem(IOPCItemMgt* pIOPCItemMgt, OPCHANDLE& hServerItem,WCHAR * ITEM_ID, unsigned short VT);
long ReadItem(IUnknown* pGroupIUnknown, OPCHANDLE hServerItem, VARIANT& varValue,OUT LPSYSTEMTIME lpSystemTime);
long writeItem(IUnknown* pGroupIUnknown, OPCHANDLE hServerItem, VARIANT& varValue) ;
long RemoveItem(IOPCItemMgt* pIOPCItemMgt, OPCHANDLE hServerItem);
long RemoveGroup (IOPCServer* pIOPCServer, OPCHANDLE hServerGroup);
#endif

源文件:

#include <atlbase.h>
#include <objbase.h>
#include <comdef.h>
#include <IOSTREAM>
#include <MAP>
#include "OPCDCSConnector.h"
#include <crtdbg.h>
#ifdef _DEBUG
#define SET_CRT_DEBUG_FIELD(a) \
_CrtSetDbgFlag((a) | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG))
#define CLEAR_CRT_DEBUG_FIELD(a) \
_CrtSetDbgFlag(~(a) & _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG))
#else
#define SET_CRT_DEBUG_FIELD(a) ((void) 0)
#define CLEAR_CRT_DEBUG_FIELD(a) ((void) 0)
#endif


using namespace std;
const GUID IID_IOPCServer = {0x39c13a4d,0x011e,0x11d0,{0x96,0x75,0x00,0x20,0xaf,0xd8,0xad,0xb3}};//use
const GUID IID_IOPCServerPublicGroups = {0x39c13a4e,0x011e,0x11d0,{0x96,0x75,0x00,0x20,0xaf,0xd8,0xad,0xb3}};
const GUID IID_IOPCBrowseServerAddressSpace = {0x39c13a4f,0x011e,0x11d0,{0x96,0x75,0x00,0x20,0xaf,0xd8,0xad,0xb3}};
const GUID IID_IOPCGroupStateMgt = {0x39c13a50,0x011e,0x11d0,{0x96,0x75,0x00,0x20,0xaf,0xd8,0xad,0xb3}};
const GUID IID_IOPCPublicGroupStateMgt = {0x39c13a51,0x011e,0x11d0,{0x96,0x75,0x00,0x20,0xaf,0xd8,0xad,0xb3}};
const GUID IID_IOPCSyncIO = {0x39c13a52,0x011e,0x11d0,{0x96,0x75,0x00,0x20,0xaf,0xd8,0xad,0xb3}};
const GUID IID_IOPCAsyncIO = {0x39c13a53,0x011e,0x11d0,{0x96,0x75,0x00,0x20,0xaf,0xd8,0xad,0xb3}};
const GUID IID_IOPCItemMgt = {0x39c13a54,0x011e,0x11d0,{0x96,0x75,0x00,0x20,0xaf,0xd8,0xad,0xb3}};//use
const GUID IID_IEnumOPCItemAttributes = {0x39c13a55,0x011e,0x11d0,{0x96,0x75,0x00,0x20,0xaf,0xd8,0xad,0xb3}};
const GUID IID_IOPCDataCallback = {0x39c13a70,0x011e,0x11d0,{0x96,0x75,0x00,0x20,0xaf,0xd8,0xad,0xb3}};
const GUID IID_IOPCAsyncIO2 = {0x39c13a71,0x011e,0x11d0,{0x96,0x75,0x00,0x20,0xaf,0xd8,0xad,0xb3}};
const GUID IID_IOPCServerList = {0x13486D50,0x4821,0x11D2,{0xA4,0x94,0x3C,0xB3,0x06,0xC1,0x00,0x00}};
const GUID IID_IOPCItemProperties = {0x39c13a72, 0x011e, 0x11d0, {0x96, 0x75, 0x00, 0x20, 0xaf, 0xd8, 0xad, 0xb3}};
const GUID CATID_OPCDAServer10 = {0x63d5f430,0xcfe4,0x11d1,{0xb2,0xc8,0x0,0x60,0x8,0x3b,0xa1,0xfb }};
const GUID CATID_OPCDAServer20 = {0x63d5f432,0xcfe4,0x11d1,{0xb2,0xc8,0x0,0x60,0x8,0x3b,0xa1,0xfb }};
const CLSID CLSID_OPCServerList = {0x13486D51,0x4821,0x11D2,{0xA4,0x94,0x3C,0xB3,0x06,0xC1,0x00,0x00}};
const CLSID CLSID_HENHE={0XE6C32641,0XF1CF,0X11D0,{0XB0,0Xe4,0X08,0X00,0X09,0Xcc,0Xd3,0X84}};

#ifndef __ON_ERROR_OPC__
#define __ON_ERROR_OPC__ \
if(result!=S_OK){\
this->ReleaseResources();\
return result;\
}
/*
* USED FOR DETECT MEMORY LEAK
*/
#endif
#ifdef _DEBUG
#define SET_CRT_DEBUG_FIELD(a) \
_CrtSetDbgFlag((a) | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG))
#define CLEAR_CRT_DEBUG_FIELD(a) \
_CrtSetDbgFlag(~(a) & _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG))
#else
#define SET_CRT_DEBUG_FIELD(a) ((void) 0)
#define CLEAR_CRT_DEBUG_FIELD(a) ((void) 0)
#endif
/*
* USED FOR DETECT MEMORY LEAK
*/

long __stdcall OPCDCSConnector::Initialize(){
HRESULT r1;
r1 = CoInitialize(NULL);
if(r1!=S_OK){
return r1;
}
return r1;
}
long __stdcall OPCDCSConnector::Connect(IN ConnectStr * pConnectStr ){
long result=S_OK;
CLSID CLSID_OPCServer;
COSERVERINFO csi, *pcsi=NULL;
WCHAR wsz [MAX_PATH];
if (pConnectStr->param2!=NULL&&strlen(pConnectStr->param2)>0) {
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,pConnectStr->param2 , -1,wsz, MAX_PATH);
//MessageBox(NULL,wsz,wsz,MB_OK);
memset(&csi, 0, sizeof(COSERVERINFO));
csi.pwszName = wsz;
pcsi = &csi;
}

if(pConnectStr==NULL||pConnectStr->param1==NULL){
result=-1;
return result;
}

this->m_szOPCServer=(char *)pConnectStr->param1;
// get the CLSID from the OPC Server Name:
int iBuffLength=MultiByteToWideChar(CP_ACP,0,this->m_szOPCServer,-1,NULL,0);
WCHAR * BUFF=(WCHAR *)HeapAlloc(GetProcessHeap(),0,iBuffLength*sizeof(WCHAR));
MultiByteToWideChar(CP_ACP,0,this->m_szOPCServer,-1,BUFF,iBuffLength);
//MessageBox(NULL,BUFF,NULL,0); @@@
result = CLSIDFromString(BUFF, &CLSID_OPCServer);
//cout<<"result : "<<result<<" CLSIDFromString:"<<BUFF<<endl; @@@
if (result!=S_OK) {
// cout<<"Can not get the remote server CLSID"<<endl;; @@@
}

__ON_ERROR_OPC__;

//queue of the class instances to create
LONG cmq = 1; // nbr of class instance to create.
MULTI_QI queue[1] =
{{&IID_IOPCServer,
NULL,
0}};

// create an instance of the IOPCServer
result = CoCreateInstanceEx(CLSID_OPCServer, NULL, CLSCTX_ALL,
/*&CoServerInfo*/pcsi, cmq, queue);
//printf("CoCreateInstanceEx : %0x \n",result); @@@

if (result!=S_OK) {
//cout<<"Can not CoCreateInstanceEx()"<<endl;; @@@
}

__ON_ERROR_OPC__;
// return a pointer to the IOPCServer interface:
this->m_pIOPCServer=(IOPCServer*) queue[0].pItf;//取得IOPServer接口


//add group
result=AddTheGroup(this->m_pIOPCServer,this->m_pIOPCItemMgt,this->m_hServerGroup);
__ON_ERROR_OPC__;
return result;
}
long __stdcall OPCDCSConnector::read(OUT Item * pItem){
OPCHANDLE hServerItem=0;
long result=S_OK;
//do not try to abstract these codes into one method ,because u should not return thre reference which is allocated in it's self
int iwbuffLength=MultiByteToWideChar(CP_ACP,0,pItem->szItemName,-1,NULL,0);
WCHAR * wbuff= new WCHAR[iwbuffLength];
memset(wbuff,0,sizeof(WCHAR)*iwbuffLength);
MultiByteToWideChar(CP_ACP,0,pItem->szItemName,-1,wbuff,iwbuffLength);
//map<WCHAR *,OPCHANDLE>::iterator it=itemArrary.find(wbuff);
//if(it!=itemArrary.end()){
// hServerItem=(*it).second;
//}else{
result=AddTheItem(this->m_pIOPCItemMgt,hServerItem,wbuff,pItem->value.vt);
// itemArrary.insert( map<WCHAR *,OPCHANDLE>::value_type(wbuff,hServerItem));
//insertCount++;
//this should be never happened
//if(insertCount>10){
// cout<<"error"<<endl;
//}
//}
if(result!=S_OK){
return result;
}
VariantInit(&(pItem->value));
result=ReadItem(this->m_pIOPCItemMgt,hServerItem,pItem->value,&(pItem->timestamp));
// if(result!=S_OK){
// RemoveItem(this->m_pIOPCItemMgt,hServerItem);
// //itemArrary.erase(wbuff);
// }
RemoveItem(this->m_pIOPCItemMgt,hServerItem);
delete [] wbuff;
return result;
}
long __stdcall OPCDCSConnector::ReleaseResources(){
long result=S_OK;
if(this->m_hGroupHandle!=NULL){
RemoveGroup(this->m_pIOPCServer,this->m_hGroupHandle);
}
if(this->m_pIOPCItemMgt!=NULL){
this->m_pIOPCItemMgt->Release();
}
if(this->m_pIOPCServer!=NULL){
this->m_pIOPCServer->Release();
}
CoUninitialize();
return result;
}
long OPCDCSConnector::getLastError(OUT char * buff){

long result=S_OK;
return result;
}
long OPCDCSConnector::read(OUT Item * pItemArray,IN const int iLength){
long result=S_OK;
if(iLength<0||pItemArray==NULL){
return -1;
}
for(int i=0;i<iLength;i++){
pItem _pItem=(pItemArray+i);
result=this->read(_pItem);
if(result!=S_OK){
break;
}
}
return result;

}
long OPCDCSConnector::Write(IN const Item * pItemArray,IN const int iLength){
long result=S_OK;
return result;
}
long OPCDCSConnector::Write(IN const Item *){
long result=S_OK;
return result;
}

long AddTheGroup(IOPCServer* pIOPCServer, IOPCItemMgt* &pIOPCItemMgt,
OPCHANDLE& hServerGroup)
{
DWORD dwUpdateRate = 100;
OPCHANDLE hClientGroup = 0;
// Add an OPC group and get a pointer to the IUnknown I/F:
HRESULT hr = pIOPCServer->AddGroup(/*szName*/ L"test",
/*bActive*/ FALSE,
/*dwRequestedUpdateRate*/ dwUpdateRate,
/*hClientGroup*/ hClientGroup,
/*pTimeBias*/ 0,
/*pPercentDeadband*/ 0,
/*dwLCID*/0,
/*phServerGroup*/&hServerGroup,
&dwUpdateRate,
/*riid*/ IID_IOPCItemMgt,
/*ppUnk*/ (IUnknown**) &pIOPCItemMgt);
return hr;
}

long AddTheItem(IOPCItemMgt* pIOPCItemMgt, OPCHANDLE& hServerItem, WCHAR * ITEM_ID, unsigned short VT)
{
HRESULT hr;
// Array of items to add:
OPCITEMDEF ItemArray[1] =
{{
/*szAccessPath*/ L"",
/*szItemID*/ ITEM_ID,
/*bActive*/ FALSE,
/*hClient*/ 1,
/*dwBlobSize*/ 0,
/*pBlob*/ NULL,
/*vtRequestedDataType*/ VT,
/*wReserved*/0
}};
//Add Result:
OPCITEMRESULT* pAddResult=NULL;
HRESULT* pErrors = NULL;
if (pIOPCItemMgt==NULL) {
return -1;
}
// Add an Item to the previous Group:
hr = pIOPCItemMgt->AddItems(1, ItemArray, &pAddResult, &pErrors);
if (hr!=S_OK) {
return -1;
}
// Server handle for the added item:
hServerItem = pAddResult[0].hServer;

// release memory allocated by the server:
CoTaskMemFree(pAddResult->pBlob);

CoTaskMemFree(pAddResult);
pAddResult = NULL;

CoTaskMemFree(pErrors);
pErrors = NULL;
return hr;
}

long writeItem(IUnknown* pGroupIUnknown, OPCHANDLE hServerItem, VARIANT& varValue){
//get a pointer to the IOPCSyncIOInterface:
IOPCSyncIO* pIOPCSyncIO;
pGroupIUnknown->QueryInterface(__uuidof(pIOPCSyncIO), (void**) &pIOPCSyncIO);
// read the item value from cache:
HRESULT* pErrors = NULL; //to store error code(s)
HRESULT hr = pIOPCSyncIO->Write(1, &hServerItem,&varValue, &pErrors);
if (hr!=S_OK) {
cout<<"write error code : "<<pErrors<<endl;
}
CoTaskMemFree(pErrors);
pErrors = NULL;
// release the reference to the IOPCSyncIO interface:
pIOPCSyncIO->Release();
return hr;
}
long RemoveItem(IOPCItemMgt* pIOPCItemMgt, OPCHANDLE hServerItem)
{
// server handle of items to remove:
OPCHANDLE hServerArray[1];
hServerArray[0] = hServerItem;
//Remove the item:
HRESULT* pErrors; // to store error code(s)
HRESULT hr = pIOPCItemMgt->RemoveItems(1, hServerArray, &pErrors);

//release memory allocated by the server:
CoTaskMemFree(pErrors);
pErrors = NULL;
return hr;
}

long RemoveGroup (IOPCServer* pIOPCServer, OPCHANDLE hServerGroup)
{
// Remove the group:
HRESULT hr = pIOPCServer->RemoveGroup(hServerGroup, FALSE);
return hr;
}
long ReadItem(IUnknown* pGroupIUnknown, OPCHANDLE hServerItem, VARIANT& varValue ,OUT LPSYSTEMTIME lpSystemTime)
{
// value of the item:
OPCITEMSTATE* pValue = NULL;
//get a pointer to the IOPCSyncIOInterface:
IOPCSyncIO* pIOPCSyncIO;
pGroupIUnknown->QueryInterface(__uuidof(pIOPCSyncIO), (void**) &pIOPCSyncIO);
// read the item value from cache:
HRESULT* pErrors = NULL; //to store error code(s)
HRESULT hr = pIOPCSyncIO->Read(OPC_DS_DEVICE, 1, &hServerItem, &pValue, &pErrors);
if (hr!=0) {
}
_ASSERT(pValue!=NULL);
varValue = pValue[0].vDataValue;
FILETIME tempFileTime;
FileTimeToLocalFileTime(&(pValue[0].ftTimeStamp),&tempFileTime);
FileTimeToSystemTime(&tempFileTime,lpSystemTime);
//Release memeory allocated by the OPC server:
CoTaskMemFree(pErrors);
pErrors = NULL;

CoTaskMemFree(pValue);
pValue = NULL;

// release the reference to the IOPCSyncIO interface:
pIOPCSyncIO->Release();
return hr;
}
OPCDCSConnector::OPCDCSConnector(){
SET_CRT_DEBUG_FIELD( _CRTDBG_LEAK_CHECK_DF);
this->m_hConnectionHandle=NULL;
this->m_hGroupHandle=NULL;
this->m_hServerGroup=NULL;
this->m_pIOPCItemMgt=NULL;
this->m_pIOPCServer=NULL;
this->m_szOPCServer=NULL;
}

UsHDuakt.txt (12.38 KB) [求助]这段C++的程序怎么转成C#啊(DCS系统的OPC客户端)


搜索更多相关主题的帖子: DCS OPC 客户端 系统 
2007-05-09 21:35
swc
Rank: 3Rank: 3
等 级:论坛游民
威 望:6
帖 子:394
专家分:83
注 册:2006-4-7
收藏
得分:0 

能不能了解下方法,再自己写?


实践、学习、再实践、再学习......
2007-05-09 21:45
zorroxiao
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2007-5-9
收藏
得分:0 
呵……或者谁有现成的OPC服务器的客户端程序?……
2007-05-09 21:50
tongxiling
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2006-4-12
收藏
得分:0 
我也是初学者,也遇到了同样问题,c++中经常出现指针,而c#中有没有指针,这可怎么办呀?

2007-05-16 16:40
快速回复:[求助]这段C++的程序怎么转成C#啊(DCS系统的OPC客户端)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020091 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved