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

📄 dataproviderdlg.cpp

📁 是一个集网络技术与数据库技术于一体的软件
💻 CPP
字号:
// DataProviderDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DataProvider.h"
#include "DataProviderDlg.h"

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

////////////////////////////////////////////////////////////////////////
// CDataProviderDlg dialog

CDataProviderDlg::CDataProviderDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDataProviderDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDataProviderDlg)
	// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	
	ReadSystemIni();
	m_Socket.m_szNetAdr = m_szNetAdr;	
	m_Socket.m_nNetPort = m_nNetPort;	
}

void CDataProviderDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDataProviderDlg)
	// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDataProviderDlg, CDialog)
//{{AFX_MSG_MAP(CDataProviderDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_END, OnEnd)
ON_BN_CLICKED(IDC_EDIT_SYSINI, OnEditSysini)
ON_BN_CLICKED(IDC_REREAD_SYSINI, OnRereadSysini)
ON_BN_CLICKED(IDC_CONNECT, OnConnect)
ON_WM_TIMER()
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////
// CDataProviderDlg message handlers

BOOL CDataProviderDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CDataProviderDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting
		
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
		
		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;
		
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDataProviderDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDataProviderDlg::ReadSystemIni()
{
	char	lpBuffer[301];
	char	pReturnString[4096];
	int		nPointCount,i,j,nSubPos;
	
	LPUNITSTRUCT	lpTempUnit;
	LPPOINTSTRUCT	lpTempPoint;
	CString			strFileName,AlertString,SectionString;
	CString			EntryString,strValue;
	
	//得到系统配置文件的全路径
	::GetCurrentDirectory( 300, (LPTSTR) lpBuffer ); 
	strFileName.Format("%s\\Test.ini",lpBuffer);
	
	//取段名
	GetPrivateProfileString("ControlSegment","Name","",
		pReturnString,4095,(LPCTSTR)strFileName);
	m_Segment.SegmentName = pReturnString;
	if(m_Segment.SegmentName.IsEmpty())
	{
		AfxMessageBox("配置文件\"Test.ini\"不存在 或\r\n段名为空!!");
		return;
	}
	
	//取网络地址和端口号
	GetPrivateProfileString("ControlSegment","ServerAddress","",
		pReturnString,4095,(LPCTSTR)strFileName);
	m_szNetAdr = pReturnString;
	if(m_szNetAdr.IsEmpty())
	{
		AfxMessageBox("服务器地址不能为空!!");
		return;
	}
	m_nNetPort = GetPrivateProfileInt("ControlSegment","ServerCtrlPort",
		8888,(LPCTSTR)strFileName);
	
	GetPrivateProfileString("ControlSegment","Memo","",pReturnString,
		4095,(LPCTSTR)strFileName);
	m_Segment.SegmentMemo = pReturnString;
	
	m_Segment.UnitCount = GetPrivateProfileInt("ControlSegment",
		"UnitCount",0,(LPCTSTR)strFileName);
	if(!m_Segment.UnitCount)
	{
		AfxMessageBox("必须有单元并且数目不能为0!");
		m_Segment.SegmentName.Empty();
		return;
	}
	
	for(i = 1;(UINT)i <= m_Segment.UnitCount;i++)
	{
		lpTempUnit = new UNITSTRUCT;
		SectionString.Format("Unit%d",i);
		GetPrivateProfileString((LPCTSTR)SectionString,"UnitName","",
			pReturnString,4095,(LPCTSTR)strFileName);
		lpTempUnit->UnitName = pReturnString;
		if(lpTempUnit->UnitName.IsEmpty())
		{
			AlertString.Format("第%d个单元信息不全!",i);
			AfxMessageBox(AlertString);
			OnEnd();
			delete lpTempUnit;
			return;
		}
		GetPrivateProfileString((LPCTSTR)SectionString,"UnitMemo","",
			pReturnString,4095,(LPCTSTR)strFileName);
		lpTempUnit->UnitMemo = pReturnString;
		
		nPointCount = GetPrivateProfileInt((LPCTSTR)SectionString,
			"PointCount",0,(LPCTSTR)strFileName);
		if(!nPointCount)
		{
			AlertString.Format("第%d个单元没有点!",i);
			AfxMessageBox(AlertString);
			OnEnd();
			delete lpTempUnit;
			return;
		}
		
		for(j = 1; j <= nPointCount;j++)
		{
			EntryString.Format("Point%d",j);
			GetPrivateProfileString((LPCTSTR)SectionString,
				(LPCTSTR)EntryString,"",pReturnString,4095,
				(LPCTSTR)strFileName);
			strValue = pReturnString;
			
			lpTempPoint = new POINTSTRUCT;
			nSubPos = strValue.Find(',');
			if(nSubPos < 0)
			{
				AlertString.Format("第%d个单元第%d个点信息错误!",i,j);
				AfxMessageBox(AlertString);
				delete lpTempPoint;
				m_Segment.UnitArray.Add(lpTempUnit);
				OnEnd();
				return;
			}
			
			lpTempPoint->PointName = strValue.Left(nSubPos);
			if(lpTempPoint->PointName.IsEmpty())
			{
				AlertString.Format("第%d个单元第%d个点信息错误!",i,j);
				AfxMessageBox(AlertString);
				delete lpTempPoint;
				m_Segment.UnitArray.Add(lpTempUnit);
				OnEnd();
				return;
			}
			strValue = strValue.Right(strValue.GetLength() - nSubPos - 1);
			
			
			nSubPos = strValue.Find(',');
			if(nSubPos < 0)
			{
				AlertString.Format("第%d个单元第%d个点信息错误!",i,j);
				AfxMessageBox(AlertString);
				delete lpTempPoint;
				m_Segment.UnitArray.Add(lpTempUnit);
				OnEnd();
				return;
			}
			
			lpTempPoint->Value = atoi(strValue.Left(nSubPos));
			strValue = strValue.Right(strValue.GetLength() - nSubPos - 1);
			
			lpTempPoint->PointMemo = strValue;
			lpTempUnit->PointArray.Add(lpTempPoint);
		}
		
		m_Segment.UnitArray.Add(lpTempUnit);
	}
}

void CDataProviderDlg::OnEnd() 
{
	// TODO: Add your control notification handler code here
	//去掉定时器。
	KillTimer(1);
	//端开SOCKET,清除内存。
	DestroyIni();
	//关闭对话框
	CDialog::OnOK();
}

void CDataProviderDlg::DestroyIni()
{
	int	UnitCount,PointCount,i,j;
	LPUNITSTRUCT	lpTempUnit;
	LPPOINTSTRUCT	lpTempPoint;
	
	//关闭SOCKET连接。
	if(m_Socket.m_bNetConnected)
	{
		m_Socket.Close();
		m_Socket.m_bNetConnected = FALSE;
		SetDlgItemText(IDC_CONNECT , "连接" );
	}
	if(m_Segment.UnitArray.GetSize() > 0)
	{
		UnitCount = m_Segment.UnitArray.GetSize();
		for(i = 0 ; i < UnitCount ; i++)
		{
			lpTempUnit = (LPUNITSTRUCT)(m_Segment.UnitArray).GetAt(i);
			if(!lpTempUnit)
				continue;
			if(lpTempUnit->PointArray.GetSize() > 0)
			{
				PointCount = lpTempUnit->PointArray.GetSize();
				for(j = 0;j < PointCount;j++)
				{
					lpTempPoint = (LPPOINTSTRUCT)
						lpTempUnit->PointArray.GetAt(j);
					if(lpTempPoint)
					{
						delete lpTempPoint;
					}
				}
				lpTempUnit->PointArray.RemoveAll();
			}
			delete lpTempUnit;
		}
		m_Segment.UnitArray.RemoveAll();
	}
	m_Segment.SegmentName.Empty();
}

void CDataProviderDlg::HandleNetData(LPMODFRAME pFrame)
{
	LPSEGMENTSTRUCT		lpTempSegment;
	LPUNITSTRUCT		lpTempUnit;
	LPPOINTSTRUCT		lpTempPoint;
	CString		TempSysName , TempUnitName , TempPointName , strTemp;
	int					nSubPos,i,j,nUnitCount,nPointCount;
	
	CListBox* m_MessageList=(CListBox*)GetDlgItem(IDC_RECEIVED_DATA);
	if (m_MessageList->GetCount()>30)
		m_MessageList->ResetContent(); 
	m_MessageList->AddString(pFrame->Content);  

	switch(pFrame->nOrder)
	{
	case 1:
		{
			pFrame->nOrder = 2;
			m_Socket.SendFrame(pFrame);
			break;
		}
	case 3:
		{
			if(m_Segment.SegmentName.IsEmpty())
				break;
			pFrame->Content.Format("%s;%s",m_Segment.SegmentName,
				m_Segment.SegmentMemo);
			pFrame->nOrder = 4;
			m_Socket.SendFrame(pFrame);
			break;
		}
	case 5://索要单元信息
		{
			if(pFrame->Content.Right(1) == ";")
				pFrame->Content = pFrame->Content.Left(
						pFrame->Content.GetLength() - 1);
			if(m_Segment.SegmentName != pFrame->Content)
				break;
			pFrame->Content += ";";
			
			lpTempSegment = &m_Segment;
			nUnitCount = lpTempSegment->UnitArray.GetSize();
			for(i = 0;i < nUnitCount; i++)
			{
				lpTempUnit = (LPUNITSTRUCT)
					(lpTempSegment->UnitArray).GetAt(i);
				if(lpTempUnit)
				{
					pFrame->Content = pFrame->Content + 
						lpTempUnit->UnitName + ";";
				}
			}
			pFrame->nOrder = 6;
			m_Socket.SendFrame(pFrame);
			break;
		}
	case 7://取某单元的全部点信息
		{
			BOOL	bIsSend = FALSE;
			if(pFrame->Content.Right(1) == ";")
				pFrame->Content = pFrame->Content.Left(
						pFrame->Content.GetLength() - 1);
			nSubPos = pFrame->Content.Find('.');
			if(nSubPos < 0)
				break;
			TempSysName = pFrame->Content.Left(nSubPos);
			TempUnitName = pFrame->Content.Right(
						pFrame->Content.GetLength() - nSubPos - 1);
			
			if(m_Segment.SegmentName != TempSysName)
				break;
			
			pFrame->Content += ";";
			lpTempSegment = &m_Segment;
			nUnitCount = lpTempSegment->UnitArray.GetSize();
			
			for(i = 0;i < nUnitCount; i++)
			{
				lpTempUnit = (LPUNITSTRUCT)
					(lpTempSegment->UnitArray).GetAt(i);
				if(!lpTempUnit)continue;
				if(lpTempUnit->UnitName == TempUnitName)
				{
					break;
				}
			}
			if(i >= nUnitCount)
				break;
			//找到单元了
			nPointCount = lpTempUnit->PointArray.GetSize();
			for(j = 0; j < nPointCount;j++)
			{
				lpTempPoint = (LPPOINTSTRUCT)
					lpTempUnit->PointArray.GetAt(j);
				if(!lpTempPoint)continue;
				bIsSend = TRUE;
				
				strTemp.Format("%s,%.3f,%s;",lpTempPoint->PointName,
					*(float*)&(lpTempPoint->Value) , 
					(LPCTSTR)lpTempPoint->PointMemo);
				
				pFrame->Content = pFrame->Content + strTemp;
			}
			if(bIsSend)
			{
				pFrame->nOrder = 8;
				m_Socket.SendFrame(pFrame);
				break;
			}
			break;
		}
	}
	delete pFrame;
}

void CDataProviderDlg::OnEditSysini() 
{
	// TODO: Add your control notification handler code here
	WinExec("Notepad.exe Test.ini",SW_SHOW);
}

void CDataProviderDlg::OnRereadSysini() 
{
	// TODO: Add your control notification handler code here
	//断开SOCKET,清除内存
	DestroyIni();
	//重新初始化
	ReadSystemIni();
	m_Socket.m_szNetAdr = m_szNetAdr;	
	m_Socket.m_nNetPort = m_nNetPort;	
	
	//建立SOCKET连接
	if(m_bConnect)	
	{
		m_Socket.Create();
		m_Socket.Init(this);
		if(m_Socket.Connect(m_Socket.m_szNetAdr,m_Socket.m_nNetPort))
		{
			m_Socket.m_bNetConnected = TRUE;
			SetDlgItemText(IDC_CONNECT , "断开" );//将按键改为断开。
		}
		else
		{
			m_Socket.Close();
		}
	}
}

void CDataProviderDlg::OnConnect() 
{
	// TODO: Add your control notification handler code here
	if(m_Socket.m_bNetConnected)
	{
		m_bConnect = FALSE;
		m_Socket.Close();
		m_Socket.m_bNetConnected = FALSE;
		SetDlgItemText(IDC_CONNECT , "连接" );
		KillTimer(1);
	}
	else
	{
		m_bConnect = TRUE;
		m_Socket.Create();
		m_Socket.Init(this);
		if(m_Socket.Connect(m_Socket.m_szNetAdr,m_Socket.m_nNetPort))
		{
			m_Socket.m_bNetConnected = TRUE;
			SetDlgItemText(IDC_CONNECT , "断开" );
			SetTimer(1,5000,NULL);
		}
		else
		{
			m_Socket.Close();
		}
	}
}

void CDataProviderDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	int	UnitCount,PointCount,i,j;
	LPUNITSTRUCT	lpTempUnit;
	LPPOINTSTRUCT	lpTempPoint;
	LPMODFRAME pFrame;
	switch(nIDEvent)
	{
	case 1:
		{
			if(!m_Socket.m_bNetConnected)
				return;
			srand((unsigned)time(NULL));
			if(m_Segment.UnitArray.GetSize() > 0)
			{
				UnitCount = m_Segment.UnitArray.GetSize();
				for(i = 0 ; i < UnitCount ; i++)
				{
					lpTempUnit = 
						(LPUNITSTRUCT)(m_Segment.UnitArray).GetAt(i);
					if(!lpTempUnit)
						continue;
					if(lpTempUnit->PointArray.GetSize() > 0)
					{
						PointCount = lpTempUnit->PointArray.GetSize();
						for(j = 0;j < PointCount;j++)
						{
							lpTempPoint = (LPPOINTSTRUCT)
								lpTempUnit->PointArray.GetAt(j);
							if(lpTempPoint)
							{
								lpTempPoint->Value=rand();
								pFrame=new ModFrame;
								pFrame->Content.Format("%s.%s.%s=%d;",
									m_Segment.SegmentName,
									lpTempUnit->UnitName,
									lpTempPoint->PointName,
									lpTempPoint->Value);
								pFrame->nOrder = 10;
								m_Socket.SendFrame(pFrame);
								delete pFrame;
							}
						}
					}
				}
			}
		}
	}
	CDialog::OnTimer(nIDEvent);
}

void CDataProviderDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	//去掉定时器。
	KillTimer(1);
	//端开SOCKET,清除内存。
	DestroyIni();
	//关闭对话框
	CDialog::OnClose();
}

⌨️ 快捷键说明

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