⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serverinfoview.cpp

📁 VisualC++通信编程工程实例精解 Chapter 2 Example 1 MSCOMM控件编程实例 Example 2 基于Windows API的虚拟终端实现 Example 3
💻 CPP
字号:
// ServerInfoView.cpp : implementation file
//

#include "stdafx.h"
#include "OpcClientSpy.h"
#include "ServerInfoView.h"
#include "MainFrm.h"
#include "GroupParamsDlg.h"
#include "ServerStatus.h"
#include "TopSplitView.h"
#include "ValueMsgView.h"

#include <winsock2.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern const CLSID CLSID_OPCServerList = {0x13486D51,0x4821,0x11D2,{0xA4,0x94,0x3C,0xB3,0x06,0xC1,0x00,0x00}};
#define __GUID_DEFINED__
#include "OPCDa_Cats.c"

/////////////////////////////////////////////////////////////////////////////
// CServerInfoView

IMPLEMENT_DYNCREATE(CServerInfoView, CFormView)

CServerInfoView::CServerInfoView()
	: CFormView(CServerInfoView::IDD)
{
	//{{AFX_DATA_INIT(CServerInfoView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_pImageList = NULL;

	groupHandle = 0;
	dwConnection1 = 0;
	dwConnection2 = 0;
	testSink = new CAdviseSink;   // create the advise sink for notifications
	testSink->AddRef();

	callbackCP = new OPCCallbackObject;   // create the ConnectionPoint for notifications
	callbackCP->AddRef();
	shutdownCP = new OPCShutdownObject;   // create the ConnectionPoint for notifications
	shutdownCP->AddRef();
	dwShutdownConnection = 0;
	usingCP = FALSE;

	AfxOleLockApp();
	// Everyone can connect back to IAdviseSink
	HRESULT hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
		RPC_C_AUTHN_LEVEL_NONE, RPC_C_IMP_LEVEL_IDENTIFY, NULL, EOAC_NONE, NULL);
	if (FAILED(hr))
		TRACE(_T("CoInitializeSecurity failed, %lx"), hr);
}

CServerInfoView::~CServerInfoView()
{
	if( m_pImageList )
		delete m_pImageList;
	ReleaseAllResource();
	//opc 操作。
	AfxOleUnlockApp();
   if( opcServer.IsOk() )
      DisconnectOPCServer(FALSE);
   testSink->Release(); // OLE should clean this up, but may not have time!
   callbackCP->Release();
   shutdownCP->Release();
   Sleep( 100 );
}

void CServerInfoView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CServerInfoView)
	DDX_Control(pDX, IDC_TREE_SERVER, m_server);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CServerInfoView, CFormView)
	//{{AFX_MSG_MAP(CServerInfoView)
	ON_WM_SIZE()
	ON_WM_CONTEXTMENU()
	ON_COMMAND(IDV_NET_INFO, OnNetInfo)
	ON_COMMAND(IDT_SERVER_CON, OnServerCon)
	ON_UPDATE_COMMAND_UI(IDT_SERVER_CON, OnUpdateServerCon)
	ON_NOTIFY(TVN_SELCHANGED, IDC_TREE_SERVER, OnSelchangedTreeServer)
	ON_COMMAND(IDT_SERVER_INTERUPT, OnServerInterupt)
	ON_UPDATE_COMMAND_UI(IDT_SERVER_INTERUPT, OnUpdateServerInterupt)
	ON_COMMAND(IDT_GROUP_PARA, OnGroupPara)
	ON_UPDATE_COMMAND_UI(IDT_GROUP_PARA, OnUpdateGroupPara)
	ON_COMMAND(IDT_SERVER_STATUS, OnServerStatus)
	ON_UPDATE_COMMAND_UI(IDT_SERVER_STATUS, OnUpdateServerStatus)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CServerInfoView diagnostics

#ifdef _DEBUG
void CServerInfoView::AssertValid() const
{
	CFormView::AssertValid();
}

void CServerInfoView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG



void CServerInfoView::OnInitialUpdate() 
{
	CFormView::OnInitialUpdate();
	//本机节点名。GetComputerName也可以实现
	WORD wVersionRequested = MAKEWORD( 2, 2 );
	WSADATA wsaData;
	if ( WSAStartup( wVersionRequested, &wsaData ) == 0 ) {
		/* Tell the user that we could not find a usable  WinSock DLL. */
 		if( gethostname((char far *)LPCTSTR(m_strLocalName),256)==SOCKET_ERROR )
			MessageBox("无法使用WS2_32.LIB");
		WSACleanup( );
	}
	//找到信息指示列表框
	GetInfoFormView();
	//other
	m_bHaveOPCConnected = FALSE;
	/*//删除所有旧的。为New而做
	m_server.DeleteAllItems();
	//装入图标,并初始化
	InitRootInfo();	*/
	OnNetInfo();
}


//是否已经存在.TRUE=已经存在
BOOL  CServerInfoView::FindThisCLSID(CString node,CLSID clsid)
{
	BOOL bRtn = FALSE;
   POSITION pos = m_vServers.GetHeadPosition();
   while( pos )
   {
      OPCServerInfo* pServerInfo = m_vServers.GetNext(pos);
	  if( (pServerInfo->m_clsid == clsid)&&
		  (pServerInfo->m_nodeName==node) )
	  {
		  bRtn = TRUE;
		  break;
	  }
   }
   return bRtn;
}


//释放所有资源
void  CServerInfoView::ReleaseAllResource(void)
{
   POSITION pos = m_vServers.GetHeadPosition();
   while( pos )
   {
		OPCServerInfo* pServerInfo = m_vServers.GetNext( pos );
		delete pServerInfo;
   }
}

//通过父窗口找到信息显示列表框.
CInfoFormView *CServerInfoView::GetInfoFormView()
{
	CMainFrame *pMainFrame = (CMainFrame *)AfxGetApp()->m_pMainWnd;
	m_pInfoForm = (CInfoFormView *)(pMainFrame->m_wndSplitter.GetPane(1,0));
	return m_pInfoForm;
}


//弹出式菜单。
void CServerInfoView::OnContextMenu(CWnd* pWnd, CPoint point) 
{
   CMenu popMenu;

   if( popMenu.LoadMenu(IDR_MENU_TREE) )
   {
      popMenu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
                                             point.x,point.y,
                                             AfxGetMainWnd());
   }	
}

//列出网络中所有的OPC服务器
void CServerInfoView::OnNetInfo() 
{
	//删除所有旧的。
	m_server.DeleteAllItems();
	//装入图标,并初始化
	InitRootInfo();	
	//清除队列
	ReleaseAllResource();
	m_strNodeList.RemoveAll();
	CWaitCursor wait;
	//GetOPCServers20((char*)LPCTSTR(m_strLocalName),CATID_OPCDAServer10);
	//GetOPCServers20((char*)LPCTSTR(m_strLocalName),CATID_OPCDAServer20);
	GetOPCServers10((char*)LPCTSTR(m_strLocalName));
	//GetOPCServers20("P2",CATID_OPCDAServer10);
	//GetOPCServers20("P2",CATID_OPCDAServer20);
	//GetOPCServers10("P2");
/*	EnumerateNodes(NULL);
	POSITION pos = m_strNodeList.GetHeadPosition();
	while( pos )
	{
		CString strNode = m_strNodeList.GetNext(pos);
		if( strNode.GetLength()>=7 ) //加快调试速度.
			continue;
		if( strNode.CompareNoCase("P3")==0 )//skip p3(以为连接P3时有问题).
			continue;
		GetOPCServers20((char *)LPCTSTR(strNode),CATID_OPCDAServer10);
		GetOPCServers20((char *)LPCTSTR(strNode),CATID_OPCDAServer20);
		GetOPCServers10((char *)LPCTSTR(strNode));
	}*/
	HTREEITEM hItem[] = {m_hRoot1,m_hLocalDA1,m_hLocalDA2,
		m_hRoot2,m_hRemoteDA1,m_hRemoteDA2};
	for( int i=0;i<6;i++ )
		m_server.Expand(hItem[i],TVE_EXPAND);
}

//连接当前的OPC服务器
void CServerInfoView::OnServerCon() 
{
	OPCServerInfo *pServer = GetSelectedServerInfo();
	if( pServer!=NULL )
	{
		if( ConnectOPCServer(pServer)==FALSE )
			return;
		SetConnectPoint();//创建组等
		m_pInfoForm->ShowInfoToList(GANTANHAO,2,pServer->m_ProgID+" 已经启动。");
	}
}

//该菜单是否有效。
void CServerInfoView::OnUpdateServerCon(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(!m_bHaveOPCConnected);	
}

void CServerInfoView::OnSelchangedTreeServer(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
		
	*pResult = 0;
}

//断开服务器
void CServerInfoView::OnServerInterupt() 
{
	DisconnectOPCServer(TRUE);
}

//断开OPC服务器.
void CServerInfoView::DisconnectOPCServer(BOOL bDeleteList) 
{
   CWaitCursor wait;
   HRESULT hr = S_OK;
   if( opcServer.IsOk() && opcGroup.IsOk() )
   {
      if( dwShutdownConnection )
         hr = AtlUnadvise( opcServer,IID_IOPCShutdown, dwShutdownConnection );
      if( usingCP )
      {// OPC 2.0 ConnectionPoints
         hr = AtlUnadvise( opcGroup,IID_IOPCDataCallback, dwConnection1 );
      }
      else
      {
		// call IDataObject::DUnadvise to turn off data notification          
		  DataObject dataObject;
		  HRESULT hr = dataObject.Attach( opcGroup );
          if( SUCCEEDED(hr) )
          {
             if( dwConnection1 )
                hr = dataObject.DUnadvise(dwConnection1);
             if( dwConnection2 )
                hr = dataObject.DUnadvise(dwConnection2);
             dataObject.Detach();
          }
       }
       opcServer.RemoveGroup(groupHandle, FALSE);
   }
   opcGroup.Detach();
   opcServer.Detach();

	Sleep( 100 );
	//删除所有的
	if( bDeleteList==TRUE )
	{
		CMainFrame *pMainFrame = (CMainFrame *)AfxGetApp()->m_pMainWnd;
		CTopSplitView *pView = (CTopSplitView *)pMainFrame->m_wndSplitter.GetPane(0, 0); 
		CValueMsgView *pMsg = (CValueMsgView *)pView->m_leftRightSplitter.GetPane(0,1);
		pMsg->DeleteAllItem();
		m_bHaveOPCConnected = FALSE;
		m_pInfoForm->ShowInfoToList(GANTANHAO,2,"当前服务器已经断开。");
	}
}

void CServerInfoView::OnUpdateServerInterupt(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(m_bHaveOPCConnected);	
}

void CServerInfoView::OnGroupPara() 
{
	if( !opcGroup.IsOk() )
		return;
   CGroupParamsDlg para( opcGroup );
   para.DoModal();
}

void CServerInfoView::OnUpdateGroupPara(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(m_bHaveOPCConnected);	
}

//server status
void CServerInfoView::OnServerStatus() 
{
   ASSERT( opcServer.IsOk() );
   CServerStatus dlg(opcServer);
   dlg.DoModal();
}

void CServerInfoView::OnUpdateServerStatus(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(m_bHaveOPCConnected);	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -