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

📄 opcset.cpp

📁 VC++实现的预测控制
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// OPCSET.cpp : implementation file
//

#include "stdafx.h"
#include "MPCTest.h"
#include "OPCSet.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CONST ULONG NEXT_COUNT = 100; // number of elements to request when calling
const LPCWSTR empty = {L""};
CArray<mytag*,mytag*>* AITagList;
CArray<mytag*,mytag*>* AOTagList;
int COPCSet::ItemCount = 0;
extern double ComputeTime;
CArray<CString,CString> AllItemList;//所有的点;
CString FilePath;//配置文件路径
extern CString DllPathname;
extern CString IniPathName;
extern CString functionname;//被调用函数名
extern CString dialogname;//被调用对话框名
extern CArray<CString,CString> m_Varianame;
mytag* newtag;
extern CString DllPathname;
extern BOOL m_IsStart;
CString m_CurveAIName[50];//最多显示50条曲线
CString m_CurveAOName[50];
/////////////////////////////////////////////////////////////////////////////
// COPCSET dialog

COPCSet::COPCSet(COPCClient* parent, IOPCServer* pServer, CWnd* pParent /*=NULL*/)
   : CDialog(COPCSet::IDD, pParent), pDoc( parent )
{
   browse.Attach( pServer );  // Get a Browse interface
   m_vt = VT_EMPTY;
   nameSpaceType = OPC_NS_HIERARCHIAL;
   m_filter = _T("*");
   m_accessPath = _T("");
   CountItem = 0;
   m_select =0;
   Itemedit = -1;
   Count =0;
   m_CurveAiCount =0;
   m_CurveAoCount =0;
   //{{AFX_DATA_INIT(AddItemDlg)
   m_itemID = _T("");
   m_samename = _T("");
   //}}AFX_DATA_INIT
   
}



void COPCSet::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COPCSet)
	DDX_Control(pDX, IDC_RELIST, m_relist);
	DDX_Control(pDX, IDC_AOLIST, m_aolist);
	DDX_Control(pDX, IDC_AILIST, m_ailist);
	DDX_Control(pDX, IDC_FLAT, m_flat);
	DDX_Control(pDX, IDC_OPC_PROPERTIES_LIST, m_tags);
	DDX_Control(pDX, IDC_OPC_TREE, m_tree);
	DDX_Control(pDX, IDCANCEL, m_bt1);
	DDX_Control(pDX, IDOK,m_btnOK);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(COPCSet, CDialog)
	//{{AFX_MSG_MAP(COPCSet)
	ON_LBN_SELCHANGE(IDC_AILIST, OnSelchangeAilist)
	ON_LBN_DBLCLK(IDC_AILIST, OnDblclkAilist)
	ON_LBN_SELCHANGE(IDC_AOLIST, OnSelchangeAolist)
	ON_LBN_DBLCLK(IDC_AOLIST, OnDblclkAolist)
	ON_LBN_SELCHANGE(IDC_FLAT, OnSelchangeFlat)
	ON_NOTIFY(TVN_SELCHANGED, IDC_OPC_TREE, OnSelchangedOpcTree)
	ON_LBN_SELCHANGE(IDC_OPC_PROPERTIES_LIST, OnSelchangeOpcPropertiesList)
	ON_LBN_DBLCLK(IDC_OPC_PROPERTIES_LIST, OnDblclkOpcPropertiesList)
	ON_LBN_DBLCLK(IDC_FLAT, OnDblclkFlat)
	ON_NOTIFY(TVN_ITEMEXPANDING, IDC_OPC_TREE, OnItemexpandingOpcTree)
	ON_BN_CLICKED(IDC_ITEMDELETE, OnItemdelete)
	ON_LBN_SELCHANGE(IDC_RELIST, OnSelchangeRelist)
	ON_BN_CLICKED(IDC_ITEMMOVEUP, OnItemmoveup)
	ON_BN_CLICKED(IDC_ITEMMOVEDOWN, OnItemmovedown)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COPCSet message handlers
HRESULT COPCSet::BrowseHere( HTREEITEM item )
{
   USES_CONVERSION;
   if( !browse.IsOk() )
      return E_FAIL;

   HRESULT hr = S_OK;
   if( item != NULL )
   {
      HTREEITEM parent = m_tree.GetParentItem( item );
      hr = BrowseHere( parent );
      if( SUCCEEDED( hr ) )
      {
         CString node( m_tree.GetItemText( item ) );
         if( node != _T("Root") )
         {
            hr = browse.ChangeBrowsePosition( OPC_BROWSE_DOWN, T2OLE(node.GetBuffer(0)) );
         }
      }
   }
   return hr;
}
void COPCSet::OnDblclkOpcPropertiesList() 
{
	// TODO: Add your control notification handler code here
	CWaitCursor wait;
	if( nameSpaceType == OPC_NS_HIERARCHIAL )
        OnSelchangedOpcTree( NULL, NULL );
    else
        UpdateFlat();
    UpdateData();
	//*******************************//数据记录
    AllItemList.SetAtGrow(ItemCount,m_itemID);
	m_relist.DeleteString(CountItem-1);
	 newtag = new mytag;
	newtag->name = m_stringname;
	if(!m_select)
	{
       m_CurveAIName[m_CurveAiCount] = m_stringname;
	   m_CurveAiCount++;
	}
	else
	{
       m_CurveAOName[m_CurveAoCount] = m_stringname;
	   m_CurveAoCount++;
	}
	m_stringname +="<->";
    m_stringname += m_itemID;
	
	if(!m_select)
	{
        m_stringname +="I";
	}
	else
	{
		m_stringname +="O";
	}
    if(Count==AITagList->GetSize()+AOTagList->GetSize())
		m_IsStart =TRUE;
	m_relist.InsertString(CountItem-1,m_stringname);
	UpdateData(0);
	
    if( pDoc )
    {
      pDoc->Additem( m_itemID, m_accessPath, m_vt );
	}	
	newtag->pos = ItemCount;
	newtag->name = m_itemID;
	ItemCount++;
	if(!m_select)
	AITagList->Add(newtag);
    else
	AOTagList->Add(newtag);
	
}

void COPCSet::OnDblclkFlat() 
{
	// TODO: Add your control notification handler code here
	CWaitCursor wait;
	if( nameSpaceType == OPC_NS_HIERARCHIAL )
        OnSelchangedOpcTree( NULL, NULL );
    else
        UpdateFlat();
    UpdateData();
	
	//*******************************//数据记录
    AllItemList.SetAtGrow(ItemCount,m_itemID);
	m_relist.DeleteString(CountItem);
	mytag* newtag = new mytag;
	newtag->name = m_stringname;
    m_stringname += m_itemID;
	m_relist.AddString(m_stringname);
	UpdateData(0);
	
    if( pDoc )
    {
      pDoc->Additem( m_itemID, m_accessPath, m_vt );
	}
	newtag->pos = ItemCount;
	ItemCount++;
	if(!m_select)
	AITagList->SetAtGrow(ItemCount-1,newtag);
    else
	AOTagList->SetAtGrow(ItemCount-1,newtag);
}
void COPCSet::UpdateFlat()
{
   USES_CONVERSION;
   if( !browse.IsOk() )
      return;

   CWaitCursor wait;
   // get the tags
   m_flat.ResetContent();
   IEnumString* pEnumString = NULL;
   HRESULT hr = browse.BrowseOPCItemIDs(OPC_FLAT,
                                      T2OLE(m_filter.GetBuffer(0)),
                                      m_vt,
                                      0,  // no filtering (or OPC_READABLE|OPC_WRITEABLE)
                                      &pEnumString);
   if( SUCCEEDED(hr) )
   {
      LPWSTR pName[NEXT_COUNT];
      ULONG count = 0;
      do
      {
         hr = pEnumString->Next(NEXT_COUNT, &pName[0], &count);
         // CString does translation from UNICODE to native type
         // (depends on the way the application is built)
         for( ULONG index=0; index<count; index++ )
         {
            CString name( pName[index] );
            m_flat.AddString( name );
            CoTaskMemFree( pName[index] );
         }
      }
      while( hr == S_OK );
      pEnumString->Release();
   }
   m_flat.SetCurSel( 0 );
}
void COPCSet::OnOK() 
{
	// TODO: Add extra validation here
	CFileDialog SaveFileDlg(true,NULL,NULL,OFN_READONLY|OFN_LONGNAMES|OFN_HIDEREADONLY|OFN_EXTENSIONDIFFERENT,"configure Files (*.ini)|*.ini||All Files (*.*)|*.*||",NULL);	//文件对话框
	SaveFileDlg.m_ofn.lpstrTitle="保存配置好的文件"; 
	CString  FilePath;
	if(SaveFileDlg.DoModal()!=0)
    {
		UpdateData();
	    FilePath = SaveFileDlg.m_ofn.lpstrFile;
		if(FilePath.Right(3)=="ini")
		{}
		else
		{
		FilePath +=".ini";
		}
	}
    
	
	
	CString strSection       = "Section1";
    CString strSectionKey    = "OPC Server Name";
    CString strValue       = pDoc->lastServer;
	WritePrivateProfileString (strSection,strSectionKey,  
        strValue, FilePath); 
	
	strSection       = "Section1";
	strSectionKey    = "explain";
    strValue       = "OPC 服务器名";
	WritePrivateProfileString (strSection,strSectionKey,  
        strValue, FilePath); 

	strSection       = "Section 2";
    strSectionKey    = "OPC Server Node";
    strValue       = "OPC 服务器所在机器的IP";
	WritePrivateProfileString (strSection,strSectionKey,  
        strValue, FilePath); 
	
	strSection       = "Section2";
	strSectionKey    = "explain";
    strValue       = pDoc->lastNode;
	WritePrivateProfileString (strSection,strSectionKey,  
        strValue, FilePath); 
    
	strSection       = "Section3";
    strSectionKey    = "DLL FilePath";
    strValue       = DllPathname;
	WritePrivateProfileString (strSection,strSectionKey,  
        strValue, FilePath); 
	
	strSection       = "Section3";
	strSectionKey    = "explain";
    strValue       = "DLL 算法的路径";
	WritePrivateProfileString (strSection,strSectionKey,  
        strValue, FilePath); 

	strSection       = "Section4";
    strSectionKey    = "INI FilePath";
    strValue       =IniPathName;
	WritePrivateProfileString (strSection,strSectionKey,  
        strValue, FilePath); 
	
	strSection       = "Section4";
	strSectionKey    = "explain";
    strValue       = "DLL 算法的配置的路径";
	WritePrivateProfileString (strSection,strSectionKey,  
        strValue, FilePath); 
	strSection       = "Section5";
    strSectionKey    = "AI的个数";
	CString s;
	s.Format("%d",AITagList->GetSize());
    strValue       =s;
	WritePrivateProfileString (strSection,strSectionKey,  
        strValue, FilePath); 
	
	strSection       = "Section5";
	strSectionKey    = "explain";
    strValue       = "数据输入的点数";
	WritePrivateProfileString (strSection,strSectionKey,  
        strValue, FilePath); 
    strSection       = "Section6";
    strSectionKey    = "AO的个数";
	s.Format("%d",AOTagList->GetSize());
    strValue       =s;
	WritePrivateProfileString (strSection,strSectionKey,  
        strValue, FilePath); 
	
	strSection       = "Section6";
	strSectionKey    = "explain";
    strValue       = "数据输出的点数";
	WritePrivateProfileString (strSection,strSectionKey,  
        strValue, FilePath); 
    POSITION pos = pDoc->items.GetHeadPosition();
	int k = AITagList->GetSize();
	for(int index =0;index<k;index++)
	{
	   strSection.Format("Section%d",index+7);
	   strSectionKey    = "Ai name";
       strValue       = m_CurveAIName[index];//AITagList->GetAt(index)->name;//容易出现问题
	   WritePrivateProfileString (strSection,strSectionKey, 
        strValue, FilePath);
       
	   strSection.Format("Section%d",index+7);
	   strSectionKey    = "OPC Client DataName1";
	   for(int hi=0;hi<AITagList->GetAt(index)->pos;hi++)
	   {
		   pDoc->items.GetNext(pos);
	   }
       strValue = pDoc->items.GetAt(pos)->name;
	
	   WritePrivateProfileString (strSection,strSectionKey, 
            strValue, FilePath); 	
	   pos = pDoc->items.GetHeadPosition();
	}
	k = AOTagList->GetSize();
	pos = pDoc->items.GetHeadPosition();
	for(index =0;index<k;index++)
	{
	   strSection.Format("Section%d",index+AITagList->GetSize()+7);
	   strSectionKey    = "Ao name";
       strValue       = m_CurveAOName[index];//AOTagList->GetAt(index)->name;
	   WritePrivateProfileString (strSection,strSectionKey, 
        strValue, FilePath);
       
	   strSection.Format("Section%d",index+7+AITagList->GetSize());
	   strSectionKey    = "OPC Client DataName1";
	   for(int hi=0;hi<AOTagList->GetAt(index)->pos;hi++)
	   {
		   pDoc->items.GetNext(pos);
	   }
       strValue = pDoc->items.GetAt(pos)->name;
	
	   WritePrivateProfileString (strSection,strSectionKey, 
            strValue, FilePath); 	
	   pos = pDoc->items.GetHeadPosition();
	   }
     
	strSection       = "SectionT";
    strSectionKey    = "Time";
	s.Format("%f",ComputeTime);
    strValue       =s;
	WritePrivateProfileString (strSection,strSectionKey,  
        strValue, FilePath); 
	
	strSection       = "SectionT";
	strSectionKey    = "explain";
    strValue       = "DLL 算法的运算时间";
	WritePrivateProfileString (strSection,strSectionKey,  
        strValue, FilePath); 
}

void COPCSet::OnCancel() 
{
	// TODO: Add extra cleanup here
    
	CDialog::OnCancel();
}

void COPCSet::OnSelchangeAilist() 
{
	// TODO: Add your control notification handler code here
	
    int h = m_ailist.GetCurSel();
	if ( h != LB_ERR )
    {
      m_ailist.GetText(h,m_stringname);
    }
    else
       return;
	if(m_samename.Compare(m_stringname)==0)
		return;
	m_relist.AddString(m_stringname);
	CountItem++;
	m_relist.SetCurSel(CountItem);
	m_samename = m_stringname;
	m_select =0;
}

void COPCSet::OnDblclkAilist() 
{
	// TODO: Add your control notification handler code here
	//选择AI或是AO点

}
void COPCSet::OnSelchangeFlat() 
{
	// TODO: Add your control notification handler code here
	USES_CONVERSION;
   if( !browse.IsOk() )
      return;

   CWaitCursor wait;
   m_itemID.Empty();
   
   int nSel = m_flat.GetCurSel();
   if ( nSel != LB_ERR )
   {
      m_flat.GetText(nSel,m_itemID);
   }
   else
      return;

   // Ask the server for the itemID
   LPWSTR pName = NULL;
   HRESULT hr = browse.GetItemID( T2OLE(m_itemID.GetBuffer(0)), &pName );

⌨️ 快捷键说明

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