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

📄 cmdbatview.cpp

📁 CAN__组建现场总线系统设计技术(光盘)
💻 CPP
字号:
// CmdBatView.cpp : implementation file
//

#include "stdafx.h"
#include "VCStyle.h"
#include "CmdBatView.h"

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

#include<Mmsystem.h>

#include "data.h"
#include "sja\\sjaexport.h"
/////////////////////////////////////////////////////////////////////////////
// CCmdBatView

IMPLEMENT_DYNCREATE(CCmdBatView, CFormView)

CCmdBatView::CCmdBatView()
	: CFormView(CCmdBatView::IDD)
{
	//{{AFX_DATA_INIT(CCmdBatView)
	m_LoopCount = 0;
	m_sCmdBat = _T("");
	m_sNote = _T("");
	//}}AFX_DATA_INIT
	m_TimerID = 0;
	m_TimerRes = 0;
}

CCmdBatView::~CCmdBatView()
{
}

void CCmdBatView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCmdBatView)
	DDX_Control(pDX, IDVALID, m_btnvalid);
	DDX_Control(pDX, IDINVALID, m_btninvalid);
	DDX_Control(pDX, IDC_BUTTON_SAVE, m_btnsave);
	DDX_Control(pDX, IDC_BUTTON_LOAD, m_btnload);
	DDX_Control(pDX, IDC_BUTTON_HELP, m_btnhelp);
	DDX_Text(pDX, IDC_EDIT_LOOPCOUNT, m_LoopCount);
	DDX_Text(pDX, IDC_EDIT_CMDBAT, m_sCmdBat);
	DDX_Text(pDX, IDC_EDIT_NOTE, m_sNote);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CCmdBatView, CFormView)
	//{{AFX_MSG_MAP(CCmdBatView)
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(IDC_BUTTON_HELP, OnButtonHelp)
	ON_BN_CLICKED(IDVALID, OnValid)
	ON_BN_CLICKED(IDINVALID, OnInvalid)
	ON_MESSAGE(WM_RECEIVEFRAME,OnReceiveFrame)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON_LOAD, OnButtonLoad)
	ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCmdBatView diagnostics

#ifdef _DEBUG
void CCmdBatView::AssertValid() const
{
	CFormView::AssertValid();
}

void CCmdBatView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
CCmdBatDoc *CCmdBatView::GetDocument ()  // non-debug version is inline
{
	ASSERT (m_pDocument->IsKindOf (RUNTIME_CLASS (CCmdBatDoc)));

	return (CCmdBatDoc *) m_pDocument;
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CCmdBatView message handlers

HBRUSH CCmdBatView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
//	HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	if(nCtlColor==CTLCOLOR_EDIT)
		pDC->SetBkColor(RGB(255,250,255));
	// TODO: Return a different brush if the default is not desired
	return m_bkbrush;
}

extern "C"
void CALLBACK internalTimerProc(UINT id, UINT msg,
	DWORD dwUser, DWORD dw1, DWORD dw2)
{
	CCmdBatView* pcmdbatview = (CCmdBatView*)dwUser;

	CCmdBatDoc* pDoc = pcmdbatview->GetDocument();

	if(pDoc->m_LoopCount == 0) return;//有限次循环
	if(pDoc->m_cmdindex == -1) return;//无效

	//判断是否到发送该命令的时间
	if(pDoc->m_cmd_buf[pDoc->m_cmdindex].remain == 0){
		pDoc->m_cmd_buf[pDoc->m_cmdindex].remain =
			pDoc->m_cmd_buf[pDoc->m_cmdindex].ticks;
		Transmit(pDoc->m_cmd_buf[pDoc->m_cmdindex].cmdframe);
		pDoc->m_cmdindex++;//next command
		if(pDoc->m_cmdindex >= pDoc->m_cmd_buf.GetSize()){
			pDoc->m_cmdindex = 0;//reset
			pDoc->m_LoopCount--;
		}
	}
	else{//否,则等待下一个时间片
		pDoc->m_cmd_buf[pDoc->m_cmdindex].remain--;
	}
}

void CCmdBatView::OnInitialUpdate() 
{
	CFormView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
	m_bkbrush.CreateSolidBrush(RGB(245,240,245));

	// buttons
	m_btnhelp.SubclassDlgItem(IDI_ICON_HELP,this);
	m_btnhelp.SetIcon(IDI_ICON_HELP);

	/////////////
	TIMECAPS tc;
	//通过函数timeGetDevCaps取出系统分辨率的取值范围(对intel系统,1~16毫秒), 
	//如果无错则继续
	if (TIMERR_NOERROR == timeGetDevCaps(&tc,sizeof(TIMECAPS)))
	{
		//分辨率的值不能超出系统的取值范围
		m_TimerRes = min(max(tc.wPeriodMin,1),tc.wPeriodMax);
		//调用timeBeginPeriod函数设置定时器的分辨率,类似于for循环的步长
		timeBeginPeriod(m_TimerRes);
	}

	m_TimerID = timeSetEvent(6,m_TimerRes,internalTimerProc,
		(DWORD)this,TIME_PERIODIC);

}
#include "HelpFrmDlg.h"
void CCmdBatView::OnButtonHelp() 
{
	CHelpFrmDlg dlg;
	dlg.DoModal();
}

void CCmdBatView::OnValid() 
{
	UpdateData(TRUE);
	m_sCmdBat += "\r\n";//必须以回车换行结束

	CCmdBatDoc* pDoc = GetDocument();
	pDoc->m_cmdindex = -1;//先取消当前运行命令队列
	pDoc->m_cmd_buf.RemoveAll();//清空命令队列

	//构造新的命令队列
	BOOL validcmd = FALSE;//判断是否输入合法
	struct Cmd cmd; 

	while(sscanf(m_sCmdBat,"%2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x",
		&cmd.ticks,&cmd.cmdframe[0],&cmd.cmdframe[1],&cmd.cmdframe[2],
		&cmd.cmdframe[3],&cmd.cmdframe[4],&cmd.cmdframe[5],&cmd.cmdframe[6],
		&cmd.cmdframe[7],&cmd.cmdframe[8],&cmd.cmdframe[9],&cmd.cmdframe[10])
		== 12){
		cmd.remain = cmd.ticks;
		pDoc->m_cmd_buf.Add(cmd);
		validcmd = TRUE;//只要有一条就要执行
		m_sCmdBat = m_sCmdBat.Right(m_sCmdBat.GetLength()-m_sCmdBat.Find("\r\n")-2);
	}

	if(validcmd){
		pDoc->m_LoopCount = m_LoopCount;
		pDoc->m_cmdindex = 0;//生效
	}
}

void CCmdBatView::OnInvalid() 
{
	CCmdBatDoc* pDoc = GetDocument();
	pDoc->m_cmdindex = -1;//先取消当前运行命令队列
	pDoc->m_cmd_buf.RemoveAll();//清空命令队列
}



void CCmdBatView::OnDestroy() 
{
	if(m_TimerID){
		timeKillEvent(m_TimerID);
	}
	if (m_TimerRes){
		timeEndPeriod(m_TimerRes);
	}

	CFormView::OnDestroy();	
}

void CCmdBatView::OnButtonLoad() 
{
	CFileDialog dlg(TRUE,"cmd",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
		"批处理文件(*.cmd)|*.cmd||",this);

	if(dlg.DoModal() == IDCANCEL) return;

	CString fname = dlg.GetPathName();

	CFile f;
	if(f.Open(fname,CFile::modeRead) ==0){
		AfxMessageBox("读文件出错!");
		return;
	}

	CArchive ar(&f,CArchive::load);

	ar >> m_sNote;
	ar >> m_LoopCount;
	ar >> m_sCmdBat;

	ar.Close();
	f.Close();
	
	UpdateData(FALSE);
}

void CCmdBatView::OnButtonSave() 
{
	UpdateData();

	CFileDialog dlg(FALSE,"cmd",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
		"批处理文件(*.cmd)|*.cmd||",this);

	if(dlg.DoModal() == IDCANCEL) return;

	CString fname = dlg.GetPathName();

	CFile f;
	if(f.Open(fname,CFile::modeWrite|CFile::modeCreate) ==0){
		AfxMessageBox("写文件出错!");
		return;
	}

	CArchive ar(&f,CArchive::store);

	ar << m_sNote;
	ar << m_LoopCount;
	ar << m_sCmdBat;

	ar.Close();
	f.Close();
}

LRESULT CCmdBatView::OnReceiveFrame(WPARAM w,LPARAM l)
{
	return 0;
}

⌨️ 快捷键说明

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