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

📄 watchdogdlg.cpp

📁 基于WINCE 的看门狗程序
💻 CPP
字号:
// WATCHDOGDlg.cpp : implementation file
//

#include "stdafx.h"
#include "WATCHDOG.h"
#include "WATCHDOGDlg.h"

//#include <windows.h>
#include "windev.h"


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

//BOOL WDGwrite(unsigned long rWTCONwrite,unsigned long rWTCNTwrite);

#define IOCTL_RST \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x802, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_DIS \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x803, METHOD_BUFFERED, FILE_ANY_ACCESS)


/////////////////////////////////////////////////////////////////////////////
// CWATCHDOGDlg dialog

CWATCHDOGDlg::CWATCHDOGDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CWATCHDOGDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWATCHDOGDlg)
	m_result = _T("");

	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CWATCHDOGDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWATCHDOGDlg)
	DDX_Control(pDX, IDC_SLIDER, m_slide);
	DDX_Text(pDX, IDC_EDIT1, m_result);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWATCHDOGDlg, CDialog)
	//{{AFX_MSG_MAP(CWATCHDOGDlg)
	ON_BN_CLICKED(IDC_RESET, OnReset)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTONFeedWdt, OnBUTTONFeedWdt)
	ON_BN_CLICKED(IDC_BUTTONUnableWDT, OnBUTTONUnableWDT)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWATCHDOGDlg message handlers

BOOL CWATCHDOGDlg::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
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen
	
	//m_slide.SetLineSize(0x8000);
	m_slide.SetRange(0,0x8000,TRUE);
	m_slide.SetPos(0x8000);


	// TODO: Add extra initialization here
	this->UpdateData(FALSE);
	//this->SetTimer(1,2000,NULL);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

 
/*****************************************************************
*	函数名 	: OnReset
*	功能  	: 打开WatchDog设备按钮,传送控制命令,开始计数
*	返回值	: void
******************************************************************/
void CWATCHDOGDlg::OnReset() 
{   //Printf ("watchdog!!\n");
	//WDGwrite(0xFF39,1689 ); 
    m_Timer = this->SetTimer(1,500,NULL);  //设置一个500ms的定时器
	char devicename[] = {"WDG1:"};

	DWORD *pBuffin ,b = 0x8000;  //0x8000 = 32768

	unsigned long  BytesWritten ;

    //打开一个设备
	pdevice = CreateFile(TEXT("WDG1:"),GENERIC_READ|GENERIC_WRITE, NULL, NULL, OPEN_EXISTING, 0, NULL);
           	if( pdevice == INVALID_HANDLE_VALUE )	
	             {
		            printf("CreateFile is failed Error = %d", GetLastError());
		            //return FALSE;
	             }

       pBuffin = &b;
	   DeviceIoControl(pdevice,IOCTL_RST,(DWORD*)pBuffin,10,NULL,0,NULL,NULL);  //计数寄存器初赋值,并开始计数
	   //MessageBox(_T("reset the system!!!"));
  // }
    m_result.Format(TEXT("WatchDog Count Value: %d"),(int)b);
    this->UpdateData(FALSE);

}

/*****************************************************************
*	函数名 	: ReadCnt
*	功能  	: 读看门狗计数寄存器值函数
*	返回值	: void
******************************************************************/
void CWATCHDOGDlg::ReadCnt()
{   
	int cnt = 0;
	DWORD dwBytesRead;
	bool fReadState;
	DWORD dwLength;
    fReadState=ReadFile(pdevice,&cnt,dwLength,&dwBytesRead,NULL);  //读计数寄存器的值
	if(!fReadState)
	{
    	printf("Read wtd cnt failure!\n");
	}
    m_result.Format(TEXT("WatchDog Count Value: %d"),(int)cnt);
    this->UpdateData(FALSE);
	m_slide.SetPos(cnt);
}


/*****************************************************************
*	函数名 	: OnTimer
*	功能  	: 处理定时器WM_TIMER消息的函数,ClassWizard添加
*	返回值	: void
******************************************************************/
void CWATCHDOGDlg::OnTimer(UINT nIDEvent) 
{
	ReadCnt();
	CDialog::OnTimer(nIDEvent);
}
/*****************************************************************
*	函数名 	: OnBUTTONFeedWdt
*	功能  	: WatchDog 喂狗的操作
*	返回值	: void
******************************************************************/
void CWATCHDOGDlg::OnBUTTONFeedWdt() 
{   
	// TODO: Add your control notification handler code here
	DWORD *pBuffin ,a = 0x8000;   
	pBuffin = &a;
	DWORD BytesWritten = 2;
    if(!WriteFile(pdevice,(DWORD*)pBuffin,sizeof(*pBuffin),&BytesWritten,NULL ))  //将新值写入计数寄存器
		MessageBox(TEXT("FEED ERROR"));
    m_result.Format(TEXT("Feed watch dog"));
    this->UpdateData(FALSE);

}



/*****************************************************************
*	函数名 	: OnBUTTONUnableWDT
*	功能  	: 停止WatchDog 计数寄存器计数
*	返回值	: void
******************************************************************/
void CWATCHDOGDlg::OnBUTTONUnableWDT() 
{
	// TODO: Add your control notification handler code here
    if(!DeviceIoControl(pdevice,IOCTL_DIS,NULL,0,NULL,0,NULL,NULL))  //发送控制命令,停止计数寄存器计数
		MessageBox(TEXT("Unable ERROR"));
	m_result.Format(TEXT("Unable WDT REST"));
	this->UpdateData(FALSE);
	
}

⌨️ 快捷键说明

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