📄 selectserver.cpp
字号:
// SelectServer.cpp : implementation file
//
#include "stdafx.h"
#include "MPCTest.h"
#include "SelectServer.h"
#include "OPCClient.h"
#include <LMCONS.H>
#include <lmserver.h>
#define __GUID_DEFINED__
#include "OPCDa_Cats.c"
#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}};
/////////////////////////////////////////////////////////////////////////////
// CSelectServer dialog
CONST ULONG NEXT_COUNT = 100;
COPCClient* newpDoc;
extern CArray<COPCClient *,COPCClient *> pDocs;//管理多个OPC Server
extern CString m_LoopFileName;
CSelectServer::CSelectServer(CWnd* pParent /*=NULL*/)
: CDialog(CSelectServer::IDD, pParent)
{
m_Enumerated = FALSE;
memset( &m_clsid, 0, sizeof(m_clsid) );
m_catid = CATID_OPCDAServer20;
//{{AFX_DATA_INIT(CSelectServer)
m_Server = _T("MicroCyber.FFServer.1");
m_Node = _T("192.168.2.184");
//}}AFX_DATA_INIT
OpcCount =0;
}
CSelectServer::~CSelectServer()
{
POSITION pos = m_Servers.GetHeadPosition();
while( pos )
{
delete m_Servers.GetNext( pos );
}
}
void CSelectServer::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSelectServer)
DDX_Text(pDX, IDC_OPC_SERVER_NAME, m_Server);
DDX_CBString(pDX, IDC_NODE, m_Node);
DDX_Control(pDX, IDC_OPC_SERVER_ADD, m_btnOK);
DDX_Control(pDX, IDCANCEL, m_bt2);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSelectServer, CDialog)
//{{AFX_MSG_MAP(CSelectServer)
ON_CBN_DROPDOWN(IDC_NODE, OnDropdownNode)
ON_BN_CLICKED(IDC_SERVERS1, OnServers1)
ON_BN_CLICKED(IDC_SERVERS2, OnServers2)
ON_BN_CLICKED(IDCANCEL, OnFileExit)
ON_BN_CLICKED(IDC_OPC_SERVER_ADD, OnOpcServerAdd)
ON_BN_CLICKED(IDC_REFRESH, OnRefresh)
ON_LBN_SELCHANGE(IDC_SERVER_LIST, OnSelchangeList1)
ON_LBN_DBLCLK(IDC_SERVER_LIST, OnDblclkList1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSelectServer message handlers
BOOL CSelectServer::OnInitDialog()
{
CDialog::OnInitDialog();
CheckRadioButton(IDC_SERVERS1, IDC_SERVERS2, IDC_SERVERS2);
// TODO: Add extra initialization here
m_Node = _T("192.168.2.184");
UpdateData(FALSE);
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
//给文件打开按钮添加图标
/*CButton* pFileOpenButton=(CButton*)GetDlgItem(IDC_SELECT_PV_NAME);
pFileOpenButton->SetIcon(AfxGetApp()->LoadIcon(IDI_ICON2));
pFileOpenButton=(CButton*)GetDlgItem(IDC_SELECT_CV_NAME);
pFileOpenButton->SetIcon(AfxGetApp()->LoadIcon(IDI_ICON2));
pFileOpenButton=(CButton*)GetDlgItem(IDC_SELECT_PID_RESET);
pFileOpenButton->SetIcon(AfxGetApp()->LoadIcon(IDI_ICON2));
pFileOpenButton=(CButton*)GetDlgItem(IDC_SELECT_PID_GAIN);
pFileOpenButton->SetIcon(AfxGetApp()->LoadIcon(IDI_ICON2));
pFileOpenButton=(CButton*)GetDlgItem(IDC_SELECT_PID_RATE);
pFileOpenButton->SetIcon(AfxGetApp()->LoadIcon(IDI_ICON2));*/
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
BOOL CSelectServer::EnumerateNodes(LPNETRESOURCE lpnr)//枚举互联网上的点
{
CComboBox* pNodes = (CComboBox*)GetDlgItem(IDC_NODE);
HANDLE hEnum = 0;
//only enumerate nodes in context(in net neighbour)
DWORD dwResult = WNetOpenEnum(RESOURCE_CONTEXT,
RESOURCETYPE_ANY,
RESOURCEUSAGE_CONTAINER, //infact this parameter is ignored
lpnr, // NULL first time this function is called
&hEnum); // handle to resource
if (dwResult != NO_ERROR)
return FALSE;
DWORD cbBuffer = 16384; // 16K buffer
LPNETRESOURCE lpnrLocal = (LPNETRESOURCE) GlobalAlloc(GPTR, cbBuffer);
do
{
DWORD cEntries = 0xFFFFFFFF; // enumerate all possible entries
dwResult = WNetEnumResource(hEnum,
&cEntries,
lpnrLocal,
&cbBuffer);
if (dwResult == NO_ERROR)
{
for( DWORD i = 0; i < cEntries; i++)
{
// If this NETRESOURCE is a container, call the function
// recursively.
if(RESOURCEUSAGE_CONTAINER ==(lpnrLocal[i].dwUsage & RESOURCEUSAGE_CONTAINER))
{
//here we only list node, ignore any container
if(RESOURCEDISPLAYTYPE_SERVER == lpnrLocal[i].dwDisplayType )
{
CString node(lpnrLocal[i].lpRemoteName);
int index = node.Find( _T("\\") );
if( index > -1 )
node = node.Mid(index+2);
if( pNodes )
pNodes->AddString(node);
}
}
}
}
}
while(dwResult != ERROR_NO_MORE_ITEMS);
GlobalFree((HGLOBAL) lpnrLocal);
dwResult = WNetCloseEnum(hEnum);
pNodes->SetWindowText(m_Node);
if (dwResult != NO_ERROR)
return FALSE;
return TRUE;
}
BOOL CSelectServer::GetOPCServers(LPTSTR node)//遍历opc server
{
USES_CONVERSION;
CListBox* pList = (CListBox*)GetDlgItem( IDC_SERVER_LIST );
CWaitCursor wait;
ASSERT_VALID(pList);
pList->ResetContent();
selection = 0;
// New 2.0 method using component categories
IOPCServerList* pServers=NULL;
COSERVERINFO si;
MULTI_QI qi;
si.dwReserved1 = 0;
si.pwszName = NULL;
if( node && node[0] != 0 )
si.pwszName = T2OLE(node);
si.pAuthInfo = NULL;
si.dwReserved2 = 0;
qi.pIID = &IID_IOPCServerList;
qi.pItf = NULL;
qi.hr = 0;
HRESULT hr = CoCreateInstanceEx(CLSID_OPCServerList, NULL, CLSCTX_ALL, &si, 1, &qi);
if (FAILED(hr) || FAILED(qi.hr))
{
CString msg(_T("Please install the OPC 2.0 Components."));
if( !m_Node.IsEmpty() )
msg.Format(_T("Please install the OPC 2.0 Components on %s."), (LPCTSTR)m_Node);
AfxMessageBox(msg);
CDialog::OnCancel();
return FALSE;
}
pServers = (IOPCServerList*)qi.pItf;
IEnumGUID *pEnumGUID=NULL;
hr = pServers->EnumClassesOfCategories(1, &m_catid, 1, &m_catid, &pEnumGUID);
if( SUCCEEDED(hr) )
{
unsigned long count;
CLSID clsid[NEXT_COUNT];
do
{
hr = pEnumGUID->Next(NEXT_COUNT, clsid, &count);
for( ULONG index=0; index<count; index++ )
{
LPOLESTR pszProgID;
LPOLESTR pszUserType;
HRESULT hr2 = pServers->GetClassDetails(clsid[index], &pszProgID, &pszUserType);
if (SUCCEEDED(hr2))
{
OPCServerInfo* pServerInfo = new OPCServerInfo(pszProgID, pszUserType, clsid[index] );
m_Servers.AddTail( pServerInfo );
CString name;
name.Format( _T("%s (%s)"),
(LPCTSTR)pServerInfo->m_ProgID,
(LPCTSTR)pServerInfo->m_Description );
int pos = pList->AddString( name );
CString name1;
name1.Format("%s",(LPCTSTR)pServerInfo->m_ProgID);
if( m_Server == pServerInfo->m_ProgID )
selection = index;
if( m_Server == name|| m_Server == name1)
selection = index;
pList->SetItemData( pos, (DWORD)pServerInfo );
CoTaskMemFree(pszProgID);
CoTaskMemFree(pszUserType);
}
}
}
while ( hr==S_OK );
if( pList->GetCount() > 0 )
pList->SetCurSel(selection);
pEnumGUID->Release();
}
pServers->Release();
OnSelchangeList1();
return TRUE;
}
void CSelectServer::OnDropdownNode() //选择服务器
{
// TODO: Add your control notification handler code here
if( !m_Enumerated )
{
CWaitCursor wait;
// Fill in the list of network nodes
EnumerateNodes(NULL);
m_Enumerated = TRUE;
}
}
void CSelectServer::OnOpcServerAdd()//连接服务器
{
// Look for a matching server name
/*OpcCount++;
if(OpcCount>0)
{
newpDoc = new COPCClient();
pDocs.SetAtGrow(OpcCount,newpDoc);//管理多个OPC Server
ParentDoc = newpDoc;
}*/
POSITION pos = m_Servers.GetHeadPosition();
while( pos )
{
OPCServerInfo* pServerInfo = m_Servers.GetNext( pos );
if(m_Server == pServerInfo->m_ProgID)
{
m_clsid = pServerInfo->m_clsid;
}
}
// If not found, Get CLSID from the ProgID
USES_CONVERSION;
IOPCServerList* pServers=NULL;
COSERVERINFO si;
MULTI_QI qi;
si.dwReserved1 = 0;
si.pwszName = NULL;
if( !m_Node.IsEmpty() )
si.pwszName = T2OLE(m_Node.GetBuffer(0));
si.pAuthInfo = NULL;
si.dwReserved2 = 0;
qi.pIID = &IID_IOPCServerList;
qi.pItf = NULL;
qi.hr = 0;
HRESULT hr = CoCreateInstanceEx(CLSID_OPCServerList, NULL, CLSCTX_ALL, &si, 1, &qi);
if (FAILED(hr) || FAILED(qi.hr))
{
CString msg(_T("Please install the OPC 2.0 Components."));
if( !m_Node.IsEmpty() )
msg.Format(_T("Please install the OPC 2.0 Components on %s."), (LPCTSTR)m_Node);
AfxMessageBox(msg);
CDialog::OnCancel();
return;
}
pServers = (IOPCServerList*)qi.pItf;
hr = pServers->CLSIDFromProgID(T2OLE(m_Server.GetBuffer(0)), &m_clsid);
pServers->Release();
if (FAILED(hr))
{
CString msg;
msg.Format(_T("Error locating '%s'."), (LPCTSTR)m_Server);
if( !m_Node.IsEmpty() )
msg.Format(_T("Error locating '%s' on %s."), (LPCTSTR)m_Server, (LPCTSTR)m_Node);
AfxMessageBox(msg);
CDialog::OnCancel();
return;
}
ParentDoc->m_clsid = m_clsid;
ParentDoc->m_connect = FALSE;
ParentDoc->lastNode = m_Node;
ParentDoc->lastServer = m_Server;
ParentDoc->connect();
//CDialog::OnCancel();
}
void CSelectServer::Addserver(CString m_server)
{
m_Server = m_server;
CWaitCursor wait;
//UpdateData();
// Look for a matching server name
POSITION pos = m_Servers.GetHeadPosition();
while( pos )
{
OPCServerInfo* pServerInfo = m_Servers.GetNext( pos );
if(m_Server == pServerInfo->m_ProgID)
{
m_clsid = pServerInfo->m_clsid;
}
}
// If not found, Get CLSID from the ProgID
USES_CONVERSION;
IOPCServerList* pServers=NULL;
COSERVERINFO si;
MULTI_QI qi;
si.dwReserved1 = 0;
si.pwszName = NULL;
if( !m_Node.IsEmpty() )
si.pwszName = T2OLE(m_Node.GetBuffer(0));
si.pAuthInfo = NULL;
si.dwReserved2 = 0;
qi.pIID = &IID_IOPCServerList;
qi.pItf = NULL;
qi.hr = 0;
HRESULT hr = CoCreateInstanceEx(CLSID_OPCServerList, NULL, CLSCTX_ALL, &si, 1, &qi);
if (FAILED(hr) || FAILED(qi.hr))
{
CString msg(_T("Please install the OPC 2.0 Components."));
if( !m_Node.IsEmpty() )
msg.Format(_T("Please install the OPC 2.0 Components on %s."), (LPCTSTR)m_Node);
AfxMessageBox(msg);
CDialog::OnCancel();
return;
}
pServers = (IOPCServerList*)qi.pItf;
hr = pServers->CLSIDFromProgID(T2OLE(m_Server.GetBuffer(0)), &m_clsid);
pServers->Release();
if (FAILED(hr))
{
CString msg;
msg.Format(_T("Error locating '%s'."), (LPCTSTR)m_Server);
if( !m_Node.IsEmpty() )
msg.Format(_T("Error locating '%s' on %s."), (LPCTSTR)m_Server, (LPCTSTR)m_Node);
AfxMessageBox(msg);
CDialog::OnCancel();
return;
}
ParentDoc->m_clsid = m_clsid;
ParentDoc->m_connect = FALSE;
ParentDoc->lastNode = m_Node;
ParentDoc->lastServer = m_Server;
ParentDoc->connect();
}
void CSelectServer::AddItem(CString m_itemname,CString m_property)
{
if(m_property.Compare("m_PV")==0)
{
ParentDoc->m_PV = TRUE;
ParentDoc->m_CV = FALSE;
ParentDoc->AutoAdditem(m_itemname);
}
if(m_property.Compare("m_CV")==0)
{
ParentDoc->m_CV = TRUE;
ParentDoc->m_PV = FALSE;
ParentDoc->AutoAdditem(m_itemname);
}
if(m_property.Compare("m_PID")==0)
{
ParentDoc->m_PID = TRUE;
ParentDoc->AutoAdditem(m_itemname);
}
}
void CSelectServer::OnServers1() //选择 opc server的版本
{
// TODO: Add your control notification handler code here
CheckRadioButton(IDC_SERVERS1, IDC_SERVERS2, IDC_SERVERS1);
UpdateData();
m_catid = CATID_OPCDAServer10;
//GetOPCServers(m_Node.GetBuffer(0));
}
void CSelectServer::OnServers2() //选择 opc server的版本
{
// TODO: Add your control notification handler code here
CheckRadioButton(IDC_SERVERS1, IDC_SERVERS2, IDC_SERVERS2);
UpdateData();
m_catid = CATID_OPCDAServer20;
//GetOPCServers(m_Node.GetBuffer(0));
}
void CSelectServer::OnOpcFileSave() //保存回路设置
{
// TODO: Add your control notification handler code here
}
void CSelectServer::OnFileExit() //退出
{
// TODO: Add your control notification handler code here
CDialog::OnOK();
}
void CSelectServer::OnOpcServerDelete() //删除要连接的OPC Server Name
{
// TODO: Add your control notification handler code here
POSITION pos = m_Servers.GetHeadPosition();
while( pos )
{
OPCServerInfo* pServerInfo = m_Servers.GetHead();
if(m_Server == pServerInfo->m_ProgID)
{
m_Servers.RemoveAt(pos);
return;
}
pServerInfo = m_Servers.GetNext(pos);
}
}
void CSelectServer::OnRefresh()
{
// TODO: Add your control notification handler code here
UpdateData();
GetOPCServers(m_Node.GetBuffer(0));
}
void CSelectServer::OnSelchangeList1()
{
// TODO: Add your control notification handler code here
CWaitCursor wait;
UpdateData();
CListBox* pList = (CListBox*)GetDlgItem( IDC_SERVER_LIST );
int index = pList->GetCurSel();
OPCServerInfo* pServerInfo = (OPCServerInfo*)pList->GetItemData(index);
if( pServerInfo && (DWORD)pServerInfo != LB_ERR )
{
m_clsid = pServerInfo->m_clsid;
m_Server = pServerInfo->m_ProgID;
}
UpdateData( FALSE );
}
void CSelectServer::OnDblclkList1()
{
// TODO: Add your control notification handler code here
OnOpcServerAdd();
OnOK();
}
void CSelectServer::RemoveServer()
{
ParentDoc->DisConnect();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -