www.pudn.com > IEC104MasterSlave.rar > NAIEC104MasterDlg.cpp, change:2013-04-18,size:14557b


// NAIEC104MasterDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "NAIEC104Master.h" 
#include "NAIEC104MasterDlg.h" 
#include "Master104.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	//}}AFX_DATA 
 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(CAboutDlg) 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
	//}}AFX_VIRTUAL 
 
// Implementation 
protected: 
	//{{AFX_MSG(CAboutDlg) 
	//}}AFX_MSG 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
	//{{AFX_DATA_INIT(CAboutDlg) 
	//}}AFX_DATA_INIT 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAboutDlg) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CNAIEC104MasterDlg dialog 
 
CNAIEC104MasterDlg::CNAIEC104MasterDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CNAIEC104MasterDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CNAIEC104MasterDlg) 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	master = NULL; 
	isResponseUTest = FALSE; 
	isResponseIFrame = FALSE; 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
CNAIEC104MasterDlg::~CNAIEC104MasterDlg() 
{ 
	if(master != NULL); 
	delete master; 
} 
 
 
void CNAIEC104MasterDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CNAIEC104MasterDlg) 
	DDX_Control(pDX, IDC_BUTTON_START, m_start); 
	DDX_Control(pDX, IDC_BUTTON_STOP, m_stop); 
	DDX_Control(pDX, IDC_LIST_RECEIVEMSG, m_revList); 
	DDX_Control(pDX, IDC_IPADDRESS_SLAVE, m_slaveIP); 
	DDX_Control(pDX, IDC_LIST_SEND_MSG, m_sendList); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CNAIEC104MasterDlg, CDialog) 
	//{{AFX_MSG_MAP(CNAIEC104MasterDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart) 
	ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop) 
	ON_BN_CLICKED(IDC_BUTTON_USTART, OnButtonUstart) 
	ON_BN_CLICKED(IDC_BUTTON_USTOP, OnButtonUstop) 
	ON_BN_CLICKED(IDC_BUTTON_CALL, OnButtonCall) 
	ON_BN_CLICKED(IDC_BUTTON_TEST, OnButtonTest) 
	ON_BN_CLICKED(IDC_BUTTON_CLOCKSYN, OnButtonClocksyn) 
	ON_BN_CLICKED(IDC_BUTTON_ITCALL, OnButtonItcall) 
	ON_BN_CLICKED(IDC_BUTTON_YK, OnButtonYk) 
	ON_BN_CLICKED(IDC_BUTTON_YT, OnButtonYt) 
	ON_BN_CLICKED(IDC_BUTTON_READYX, OnButtonReadyx) 
	ON_BN_CLICKED(IDC_BUTTON_READYC, OnButtonReadyc) 
	ON_BN_CLICKED(IDC_BUTTON_CLEANSEND, OnButtonCleansend) 
	ON_BN_CLICKED(IDC_BUTTON_CLEANRECEIVE, OnButtonCleanreceive) 
	ON_BN_CLICKED(IDC_BUTTON_READGTDATA, OnButtonReadgtdata) 
	ON_BN_CLICKED(IDC_BUTTON_READHISTORYDATA, OnButtonReadhistorydata) 
	ON_BN_CLICKED(IDC_BUTTON_CTEST, OnButtonCtest) 
	ON_BN_CLICKED(IDC_BUTTON_S, OnButtonS) 
	ON_BN_CLICKED(IDC_BUTTON_I, OnButtonI) 
	ON_BN_CLICKED(IDC_BUTTON_SAVEMSG, OnButtonSavemsg) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CNAIEC104MasterDlg message handlers 
 
BOOL CNAIEC104MasterDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// Add "About..." menu item to system menu. 
 
	// IDM_ABOUTBOX must be in the system command range. 
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
	ASSERT(IDM_ABOUTBOX < 0xF000); 
 
	CMenu* pSysMenu = GetSystemMenu(FALSE); 
	if (pSysMenu != NULL) 
	{ 
		CString strAboutMenu; 
		strAboutMenu.LoadString(IDS_ABOUTBOX); 
		if (!strAboutMenu.IsEmpty()) 
		{ 
			pSysMenu->AppendMenu(MF_SEPARATOR); 
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
		} 
	} 
 
	//  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 
	m_slaveIP.SetAddress(192,168,1,123); 
	UpdateData(0); 
	//	m_stop.EnableWindow(false); 
	SetButtonState(FALSE); 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CNAIEC104MasterDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} 
 
// 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 CNAIEC104MasterDlg::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 CNAIEC104MasterDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CNAIEC104MasterDlg::OnButtonStart()  
{ 
	// TODO: Add your control notification handler code here 
	master = new Master104(); 
	master->dlg = this; 
	int index = 0; 
	BYTE nf1,nf2,nf3,nf4; 
	m_slaveIP.GetAddress(nf1,nf2,nf3,nf4); 
	CString str; 
	str.Format("%d.%d.%d.%d",nf1,nf2,nf3,nf4);//这里的nf得到的值是IP值了. 
//	MessageBox(str); 
	index = master->OpenLink(str.GetBuffer(0), 2404); 
 
//	index = master->OpenLink("192.168.1.121", 2404); 
	if(!index) // 创建socket, 建立连接connect 
	{ 
		MessageBox("启动成功"); 
		master->Send_StartDT(); 
		// 连接建立后立即启动总召唤 
		BYTE buf[]={100,1,6,0,1,0,0,0,0,20};//Type,Informationbody num,COT,Source ADDR,ASDU addressLow,ASDU addressHigh 
		// SendMsg(buf,sizeof(buf)); 
 
		m_stop.EnableWindow(true); 
		m_start.EnableWindow(false); 
		SetButtonState(TRUE);	 
	} 
	else if(index == 3) 
	{ 
		MessageBox("连接已存在"); 
	} 
	else 
	{ 
		MessageBox("启动失败"); 
	}		 
} 
 
void CNAIEC104MasterDlg::OnButtonStop()  
{ 
	// TODO: Add your control notification handler code here 
	// master->Send_StopDT(); 
	stopLink(); 
	// 处理界面 
	SetButtonState(FALSE); 
} 
 
void CNAIEC104MasterDlg::OnButtonUstart()  
{ 
	// TODO: Add your control notification handler code here 
	master->Send_StartDT(); 
} 
 
void CNAIEC104MasterDlg::OnButtonUstop()  
{ 
	// TODO: Add your control notification handler code here 
	master->Send_StopDT();	 
} 
 
/************************** 总召唤****************************/ 
void CNAIEC104MasterDlg::OnButtonCall()  
{ 
	// TODO: Add your control notification handler code here 
	BYTE buf[]={100,1,6,0,1,0,0,0,0,20};//Type,Informationbody num,COT,Source ADDR,ASDU addressLow,ASDU addressHigh 
	SendMsg(buf,sizeof(buf)); 
} 
 
void CNAIEC104MasterDlg::SendMsg(BYTE *txbuf, int txlen) 
{ 
	master->Send_ASDU_Msg(txbuf,txlen); 
	CString ch, str; 
	str="Tx:"; 
	for(int i=0;i<txlen;i++) 
	{ 
		ch.Format(" %02x",txbuf[i]); 
		str+=ch; 
	} 
//	m_sendList.AddString(str);  
//	m_revList.AddString(str); 
} 
 
void CNAIEC104MasterDlg::OnButtonTest()  
{ 
	// TODO: Add your control notification handler code here 
	master->Send_TestFR();	 
} 
 
void CNAIEC104MasterDlg::OnButtonClocksyn()  
{ 
	// TODO: Add your control notification handler code here 
	BYTE Txbuf[256]; 
	BYTE *buff = Txbuf; 
	BYTE *csbuff; 
 
	int Index = 0; 
 
    buff[Index++] = 0x67; 
	buff[Index++] = 0x01; 
	buff[Index++] = 0x06; 
	buff[Index++] = 0x00; 
	buff[Index++] = 0x01; 
	buff[Index++] = 0x00; 
	buff[Index++] = 0x00; 
	buff[Index++] = 0x00; 
	buff[Index++] = 0x00; 
	SYSTEMTIME ctim; 
	GetLocalTime(&ctim); 
	WORD second = ctim.wMilliseconds + ctim.wSecond*1000; 
	// WORD second =  ctim.wSecond*1000; 
	buff[Index++] = second; 
	buff[Index++] = second>>8; 
	buff[Index++] = ctim.wMinute; 
	buff[Index++] = ctim.wHour; 
	buff[Index++]  = ((ctim.wDayOfWeek%7)&0x03)<<5 | (ctim.wDay&0x1F); 
	// buff[Index++] = (ctim.wDay&0x1f)|((ctim.wDayOfWeek<<4)&0x70); 
	buff[Index++] = ctim.wMonth; 
	buff[Index++] = ctim.wYear-2000; 
	 
	int Txlen = buff-Txbuf; 
	// 显示出来 
	CString ch, str; 
	for(int i = 0; i<Index; i++) 
	{ 
		ch.Format(" %02x ", buff[i]); 
	    str += ch; 
	} 
 
	SendMsg(Txbuf,Index);	 
} 
 
/*********************电度总召唤******************************/ 
void CNAIEC104MasterDlg::OnButtonItcall()  
{ 
	// TODO: Add your control notification handler code here 
	BYTE buf[]={101,1,6,0,1,0,0x00,0x00,0x00,0x45};//Type,Informationbody num,COT,Source ADDR,ASDU addressLow,ASDU addressHigh 
	SendMsg(buf,sizeof(buf)); 
} 
 
void CNAIEC104MasterDlg::OnButtonYk()  
{ 
	// TODO: Add your control notification handler code here 
	// 想遥控谁就传谁的地址 
	// 遥控预置 
	BYTE buf[]={45,1,6,0,1,0,0x04,0x60,0x00,0x81};//Type,Informationbody num,COT,Source ADDR,ASDU addressLow,ASDU addressHigh 
	SendMsg(buf,sizeof(buf));	 
} 
 
void CNAIEC104MasterDlg::OnButtonYt()  
{ 
	// TODO: Add your control notification handler code here 
	BYTE buf[]={0x31,0x82,0x05,0x00,0x01,0x00,0x01,0x62,0x00,0xA1, 0x10, 0x89, 0x15};//Type,Informationbody num,COT,Source ADDR,ASDU addressLow,ASDU addressHigh 
	SendMsg(buf,sizeof(buf));	 
} 
 
void CNAIEC104MasterDlg::OnButtonReadyx()  
{ 
	// TODO: Add your control notification handler code here 
	// 单点遥信, 连续的两个数据, 请求, 公共地址,信息体起始地址 
	BYTE buf[]={0x01,0x82,0x05,0x00,0x01,0x00,0x0a,0x00,0x00};//Type,Informationbody num,COT,Source ADDR,ASDU addressLow,ASDU addressHigh 
	SendMsg(buf,sizeof(buf)); 
	// 收到之后解析, 可变结构限定词,起始地址, 组合相应的包返回 
} 
 
void CNAIEC104MasterDlg::OnButtonReadyc()  
{ 
	// TODO: Add your control notification handler code here 
	BYTE buf[]={0x0b,0x82,0x05,0x00,0x01,0x00,0x01,0x40,0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00};//Type,Informationbody num,COT,Source ADDR,ASDU addressLow,ASDU addressHigh 
	SendMsg(buf,sizeof(buf));	 
} 
 
void CNAIEC104MasterDlg::OnButtonCleansend()  
{ 
	// TODO: Add your control notification handler code here 
	m_sendList.ResetContent();	 
} 
 
void CNAIEC104MasterDlg::OnButtonCleanreceive()  
{ 
	// TODO: Add your control notification handler code here 
	m_revList.ResetContent();	 
} 
 
void CNAIEC104MasterDlg::OnButtonReadgtdata()  
{ 
	// TODO: Add your control notification handler code here 
	BYTE buf[]={0x35,0x01,0x06,0x00,0x01,0x00,0x00,0x00,0x00, 0x14}; 
	SendMsg(buf,sizeof(buf)); 
} 
 
void CNAIEC104MasterDlg::OnButtonReadhistorydata()  
{ 
	// 读取历史数据 
	BYTE buf[]={0x37, 0x01, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xfb, 0xe5, 0x21, 0x0d, 0x0e, 0x03, 0x0d, 0x14};  
	SendMsg(buf,sizeof(buf));	 
} 
 
void CNAIEC104MasterDlg::SetButtonState(BOOL state) 
{ 
	if(!state) 
		(CWnd *)GetDlgItem(IDC_BUTTON_START)->EnableWindow(!state); 
 
	(CWnd *)GetDlgItem(IDC_BUTTON_STOP)->EnableWindow(state); 
	(CWnd *)GetDlgItem(IDC_BUTTON_CALL)->EnableWindow(state); 
	(CWnd *)GetDlgItem(IDC_BUTTON_CLOCKSYN)->EnableWindow(state); 
	(CWnd *)GetDlgItem(IDC_BUTTON_ITCALL)->EnableWindow(state); 
	(CWnd *)GetDlgItem(IDC_BUTTON_USTART)->EnableWindow(state); 
	(CWnd *)GetDlgItem(IDC_BUTTON_USTOP)->EnableWindow(state); 
	(CWnd *)GetDlgItem(IDC_BUTTON_TEST)->EnableWindow(state); 
	(CWnd *)GetDlgItem(IDC_BUTTON_READYX)->EnableWindow(state); 
	(CWnd *)GetDlgItem(IDC_BUTTON_READYC)->EnableWindow(state); 
	(CWnd *)GetDlgItem(IDC_BUTTON_YK)->EnableWindow(state); 
	(CWnd *)GetDlgItem(IDC_BUTTON_YT)->EnableWindow(state); 
	(CWnd *)GetDlgItem(IDC_BUTTON_READGTDATA)->EnableWindow(state); 
	(CWnd *)GetDlgItem(IDC_BUTTON_READHISTORYDATA)->EnableWindow(state);	 
} 
 
void CNAIEC104MasterDlg::stopLink() 
{ 
	master->CloseLink(false);	 
	if(master != NULL) 
	{ 
		delete master; 
		master = NULL; 
	} 
	 
	m_stop.EnableWindow(false); 
	m_start.EnableWindow(true); 
	SetButtonState(FALSE); 
} 
 
void CNAIEC104MasterDlg::OnButtonCtest()  
{ 
	// TODO: Add your control notification handler code here 
	master->Send_U_Msg(CMD_TESTC); 
} 
 
void CNAIEC104MasterDlg::OnButtonS()  
{ 
	// TODO: Add your control notification handler code here 
	master->Send_S_Msg(); 
	 
} 
 
void CNAIEC104MasterDlg::OnButtonI()  
{ 
	// TODO: Add your control notification handler code here 
	BYTE buf[]={0x11,1,6,0,1,0,0,0,0,20};//Type,Informationbody num,COT,Source ADDR,ASDU addressLow,ASDU addressHigh 
	SendMsg(buf,sizeof(buf));	 
} 
 
BOOL CNAIEC104MasterDlg::GetUTestIsResponse() 
{ 
	return ((CButton *)GetDlgItem(IDC_CHECK_NORESPONSEUTEST))->GetCheck(); 
} 
 
BOOL CNAIEC104MasterDlg::GetIFrameIsResponse() 
{ 
	return ((CButton *)GetDlgItem(IDC_CHECK_NOIFRAMERESPONSE))->GetCheck(); 
 
} 
 
BOOL CNAIEC104MasterDlg::GetRevIisResponseS() 
{ 
	return ((CButton *)GetDlgItem(IDC_CHECK_REVIISRESPONSES))->GetCheck(); 
	 
} 
 
void CNAIEC104MasterDlg::OnButtonSavemsg()  
{ 
 
	CString strSource; 
	CString tempStr; 
	int nCount = m_revList.GetSelCount(); 
	if(nCount == -1) 
	{ 
		AfxMessageBox("没有行被选中"); 
		return; 
	} 
	int *SelIndex = new int[nCount]; 
	 
	m_revList.GetSelItems(nCount, SelIndex);//现在selindex数组里面就是你要的行的索引 
	for(int i = 0; i<nCount; i++) 
	{ 
		m_revList.GetText(SelIndex[i], tempStr); 
		tempStr = tempStr.Mid(tempStr.Find(":")+1); 
		strSource += tempStr; 
		tempStr = ""; 
	} 
	delete[] SelIndex;  
	 
	int size = strSource.GetLength(); 
	TCHAR* str = new TCHAR[size+1]; 
	memset(str, 0, size+1); 
	//其参数为CString字符串的长度 
    //	str = strSource.GetBuffer(0); 
	char * xx = strSource.GetBuffer(0); 
	strcpy(str, xx); 
	str[size] = '\0'; 
 
	HGLOBAL hMem; 
	LPTSTR pStr; 
	// 分配内存空间 
	hMem = GlobalAlloc(GHND | GMEM_SHARE, size+1);//  
	pStr = (LPTSTR)GlobalLock(hMem); 
	// lstrcpy(pStr, str); 
	strcpy(pStr, str); 
	GlobalUnlock(hMem); 
	OpenClipboard(); 
	EmptyClipboard(); 
	// 设置剪贴板文本 
	SetClipboardData(CF_TEXT, hMem); 
	CloseClipboard(); 
	// 释放内存空间 
	GlobalFree(hMem); 
 
 
	strSource.ReleaseBuffer(); 
	if(str != NULL) 
	{ 
		delete []str; 
		str = NULL; 
	} 
	AfxMessageBox("复制完成"); 
}