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

📄 master101_demodlg.cpp

📁 101规约主站库我们用100人年开发的通信规约程序库
💻 CPP
字号:
// Master101_DemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Master101real.h"
#include "Master101_Demo.h"
#include "Master101_DemoDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CMaster101_DemoDlg dialog

CMaster101_DemoDlg::CMaster101_DemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMaster101_DemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMaster101_DemoDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMaster101_DemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMaster101_DemoDlg)
	DDX_Control(pDX, IDC_COMBObaudrate, m_baudrate);
	DDX_Control(pDX, IDC_COMBOport, m_port);
	DDX_Control(pDX, IDC_LIST1, m_list);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMaster101_DemoDlg, CDialog)
	//{{AFX_MSG_MAP(CMaster101_DemoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTONstart, OnBUTTONstart)
	ON_BN_CLICKED(IDC_BUTTONstop, OnBUTTONstop)
	ON_BN_CLICKED(IDC_BUTTONreqcall, OnBUTTONreqcall)
	ON_BN_CLICKED(IDC_BUTTONreqgrp1, OnBUTTONreqgrp1)
	ON_BN_CLICKED(IDC_BUTTONreqdata, OnBUTTONreqdata)
	ON_BN_CLICKED(IDC_BUTTONcontrolselect, OnBUTTONcontrolselect)
	ON_BN_CLICKED(IDC_BUTTONcontroloperate, OnBUTTONcontroloperate)
	ON_BN_CLICKED(IDC_BUTTONcontroldeact, OnBUTTONcontroldeact)
	ON_BN_CLICKED(IDC_BUTTONreqcd, OnBUTTONreqcd)
	ON_BN_CLICKED(IDC_BUTTONdownloadcd, OnBUTTONdownloadcd)
	ON_BN_CLICKED(IDC_BUTTONsynclock, OnBUTTONsynclock)
	ON_BN_CLICKED(IDC_BUTTONfreezeIT, OnBUTTONfreezeIT)
	ON_BN_CLICKED(IDC_BUTTONreqIT, OnBUTTONreqIT)
	ON_BN_CLICKED(IDC_BUTTONreqITgrp1, OnBUTTONreqITgrp1)
	ON_BN_CLICKED(IDC_BUTTONresetproc, OnBUTTONresetproc)
	ON_BN_CLICKED(IDC_BUTTONresetbuff, OnBUTTONresetbuff)
	ON_BN_CLICKED(IDC_BUTTONtest, OnBUTTONtest)
	ON_BN_CLICKED(IDC_BUTTONsetME_NA, OnBUTTONsetMENA)
	ON_BN_CLICKED(IDC_BUTTONsetP_ME_NA, OnBUTTONsetPMENA)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMaster101_DemoDlg message handlers

BOOL CMaster101_DemoDlg::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_port.SetWindowText("COM1");
	m_baudrate.SetWindowText("9600");
	UpdateData(0);
	real=0;
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMaster101_DemoDlg::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 CMaster101_DemoDlg::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 CMaster101_DemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMaster101_DemoDlg::OnBUTTONstart() 
{
	// TODO: Add your control notification handler code here

	UpdateData(1);
	CString port,baudrate;
	m_port.GetWindowText(port);
	m_baudrate.GetWindowText(baudrate);

	if(!real)  
	{
	real=new CMaster101real() ;
	real->dlg=this;
	}
	real->Init_LinkAddr_size(1);
	real->Init_LinkAddr(1);
	real->Init_SerialComm((char*)((LPCSTR)port),atoi((LPCSTR)baudrate));
	real->Init_Reqtimes(3,255);
	if(!real->master_start())
	{
		AfxMessageBox("Can NOT open Serial port !");
		

	}

}

void CMaster101_DemoDlg::OnBUTTONstop() 
{
	// TODO: Add your control notification handler code here
	if(real)
	{
		real->master_close();
		delete real;
		real=NULL;
	}
}
void CMaster101_DemoDlg::SendMsg(BYTE *txbuf,int txlen) 
{
	real->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_list.AddString(str);
	if(m_list.GetCount()>100)
			m_list.DeleteString(0);
	int count=m_list.GetCount();
	m_list.SetCurSel(count-1);
}
void CMaster101_DemoDlg::OnBUTTONreqcall() 
{
	// TODO: Add your control notification handler code here

	BYTE buf[]={100,1,6,1,0,0,20};//Type,Informationbody num,COT,ASDU address
	SendMsg(buf,sizeof(buf));
}

void CMaster101_DemoDlg::OnBUTTONreqgrp1() 
{
	// TODO: Add your control notification handler code here
	BYTE buf[]={100,1,6,1,0,0,21};//Type,Informationbody num,COT,ASDU address
	SendMsg(buf,sizeof(buf));
}

void CMaster101_DemoDlg::OnBUTTONreqdata() 
{
	// TODO: Add your control notification handler code here
	BYTE buf[]={102,1,5,1,0x01,0x00};//Type,Informationbody num,COT,ASDU address
	//BYTE buf[]={102,1,5,1,0x01,0x40};//读遥测量
	SendMsg(buf,sizeof(buf));
}

void CMaster101_DemoDlg::OnBUTTONcontrolselect() 
{
	// TODO: Add your control notification handler code here
	BYTE buf[]={45,1,6,1,0x01,0x60,0x81};//Type,Informationbody num,COT,ASDU address
	SendMsg(buf,sizeof(buf));
}


void CMaster101_DemoDlg::OnBUTTONcontroloperate() 
{
	// TODO: Add your control notification handler code here
	BYTE buf[]={45,1,6,1,0x01,0x60,0x01};//Type,Informationbody num,COT,ASDU address
	SendMsg(buf,sizeof(buf));
	
}

void CMaster101_DemoDlg::OnBUTTONcontroldeact() 
{
	// TODO: Add your control notification handler code here
	BYTE buf[]={45,1,8,1,0x01,0x60,0x81};//Type,Informationbody num,COT,ASDU address
	SendMsg(buf,sizeof(buf));
	
}

void CMaster101_DemoDlg::OnBUTTONreqcd() 
{
	// TODO: Add your control notification handler code here
	BYTE buf[]={106,1,6,1,0x00,0x00,0x00,0x00};//Type,Informationbody num,COT,ASDU address
	SendMsg(buf,sizeof(buf));

}

void CMaster101_DemoDlg::OnBUTTONdownloadcd() 
{
	// TODO: Add your control notification handler code here
	BYTE buf[]={106,1,3,1,0x00,0x00,0x00,0x02};//Type,Informationbody num,COT,ASDU address
									//假设通道延时为0x0200  512ms
	SendMsg(buf,sizeof(buf));
	
}

void CMaster101_DemoDlg::OnBUTTONsynclock() 
{
	// TODO: Add your control notification handler code here
	BYTE Txbuf[256];
	BYTE *buff=Txbuf;
	BYTE *csbuff;
	
	*buff++=103;							//type
	*buff++=1;
	*buff++=6;
	*buff++=1;				//address
	*buff++=0;
	*buff++=0;
	SYSTEMTIME ctim;
	GetLocalTime(&ctim);
	
	*((WORD*)buff)=ctim.wMilliseconds+ctim.wSecond*1000;
	buff++;
	buff++;
	*buff++=ctim.wMinute;
	*buff++=ctim.wHour;
	*buff++=ctim.wDay;//(ctim.wDay&0x1f)|((ctim.wDayOfWeek<<5)&0xe0);
	*buff++=ctim.wMonth;
	*buff++=ctim.wYear-2000;
	int Txlen=buff-Txbuf;
	SendMsg(Txbuf,Txlen);
}

void CMaster101_DemoDlg::OnBUTTONfreezeIT() 
{
	// TODO: Add your control notification handler code here
	BYTE buf[]={101,1,6,1,0x00,0x00,0x45};//Type,Informationbody num,COT,ASDU address
	SendMsg(buf,sizeof(buf));
	
}

void CMaster101_DemoDlg::OnBUTTONreqIT() 
{
	// TODO: Add your control notification handler code here
	BYTE buf[]={101,1,6,1,0x00,0x00,0x05};//Type,Informationbody num,COT,ASDU address
	SendMsg(buf,sizeof(buf));
	
}

void CMaster101_DemoDlg::OnBUTTONreqITgrp1() 
{
	// TODO: Add your control notification handler code here
	BYTE buf[]={101,1,6,1,0x00,0x00,0x01};//Type,Informationbody num,COT,ASDU address
	SendMsg(buf,sizeof(buf));
	
}

void CMaster101_DemoDlg::OnBUTTONresetproc() 
{
	// TODO: Add your control notification handler code here
	BYTE buf[]={105,1,6,1,0x00,0x00,0x01};//Type,Informationbody num,COT,ASDU address
	SendMsg(buf,sizeof(buf));
	
}

void CMaster101_DemoDlg::OnBUTTONresetbuff() 
{
	// TODO: Add your control notification handler code here
	BYTE buf[]={105,1,6,1,0x00,0x00,0x02};//Type,Informationbody num,COT,ASDU address
	SendMsg(buf,sizeof(buf));
	
}

void CMaster101_DemoDlg::OnBUTTONtest() 
{
	// TODO: Add your control notification handler code here
	BYTE buf[]={104,1,6,1,0x00,0x00,0xaa,0x55};//Type,Informationbody num,COT,ASDU address
	SendMsg(buf,sizeof(buf));
}

void CMaster101_DemoDlg::OnBUTTONsetMENA() 
{
	// TODO: Add your control notification handler code here
	BYTE buf[]={48,1,6,1,0x01,0x62,0x01,0x02,0x00};//Type,Informationbody num,COT,ASDU address
	SendMsg(buf,sizeof(buf));
	
}

void CMaster101_DemoDlg::OnBUTTONsetPMENA() 
{
	// TODO: Add your control notification handler code here
	BYTE buf[]={110,1,6,1,0x01,0x50,0x05,0x00,0x01};//Type,Informationbody num,COT,ASDU address
	SendMsg(buf,sizeof(buf));
	
}

⌨️ 快捷键说明

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