//---------------------------------------------------------------------------
/****************************************************************************
**** Date: 2013-9-11
**** Author: Xu Qun
**** Tel: 15901714590
**** Qq: 519329556
**** Company: Aera
**** Product: NPC5000
**** Chip: W83627
**** Rev: 10.01
****************************************************************************/
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#include "WinIo.h"
#include "watchdog_yelei.h"
TForm1 *Form1;
unsigned char TimeUnit = 0; //时间单位: 0: 秒 1: 分
unsigned char Feed_WDT_Flag = 1; //是否喂狗: 0: 不喂狗 1: 喂狗
DWORD *lpThreadID = NULL;
DWORD CALLBACK ThreadFunc(void *p);
unsigned char T = 0;
unsigned char *ptime; //
HANDLE g_handle = NULL; //
bool L = false;
bool WinIo_Status = false;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Label2->Visible = true;
// 判断时间单位
if (!Time_second->Checked)
TimeUnit = 0x08;
// 判断是否喂狗
if (NoFeed_WDT->Checked)
Feed_WDT_Flag = 0;
// 初始化watchdog
InitWinIo_Message->Visible = true;
if (!WinIo_Status)
{
if (InitializeWinIo())
{
WinIo_Status = true;
// MessageBox(NULL, "加载 WinIo 成功", NULL, MB_OK);
InitWinIo_Message->Font->Color = clLime;
InitWinIo_Message->Caption = "看门狗已开启!";
}
else
{
// MessageBox(NULL, "加载 WinIo 失败", NULL, MB_OK);
InitWinIo_Message->Font->Color = clRed;
InitWinIo_Message->Caption = "看门狗无法正常工作!";
return;
}
}
if(Time_value->Text == "")
{
MessageBox(NULL, "请重新输入看门狗时间初值", NULL, MB_OK);
if (WinIo_Status)
{
ShutdownWinIo();
WinIo_Status = false;
}
InitWinIo_Message->Font->Color = clYellow;
InitWinIo_Message->Caption = "看门狗已停止";
return;
}
if (Feed_WDT->Checked)
{
if(feed_inter_data->Text == "")
{
MessageBox(NULL, "请重新输入喂狗时间间隔", NULL, MB_OK);
if (WinIo_Status)
{
ShutdownWinIo();
WinIo_Status = false;
}
InitWinIo_Message->Font->Color = clYellow;
InitWinIo_Message->Caption = "看门狗已停止";
return;
}
}
Init_WatchDog();
// 设置watchdog unit
WatchDog_SetUnit(TimeUnit);
// 设置watchdog timer
// WatchDog_SetTimer((unsigned char)Time_value->Text.c_str());
T = StrToInt(Time_value->Text);
WatchDog_SetTimer(StrToInt(Time_value->Text));
if (Feed_WDT->Checked)
{
g_handle = CreateThread(0, 0, ThreadFunc, ptime, 0, lpThreadID);
if ( !g_handle )
{
ShowMessage("Thread create fail!");
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Feed_WDTClick(TObject *Sender)
{
// 设置喂狗参数为可见
feed_inter_label->Visible = true;
feed_inter_data->Visible = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NoFeed_WDTClick(TObject *Sender)
{
// 设置喂狗参数为不可见
feed_inter_label->Visible = false;
feed_inter_data->Visible = false;
TerminateThread(g_handle, 0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Time_valueKeyPress(TObject *Sender, char &Key)
{
if (Time_value->Text != "")
{
if (!(((Key >= '0') && (Key <= '9')) || (Key == VK_BACK) || (Key == VK_DELETE)))
{
Key = 0;
}
if ((StrToInt(Time_value->Text)) > 255 || StrToInt(Time_value->Text) < 0)
{
MessageBox(NULL, "输入数值不对(范围是:0-255)", NULL, MB_OK);
Time_value->Text = 0;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::feed_inter_dataKeyPress(TObject *Sender, char &Key)
{
if (feed_inter_data->Text != "")
{
if (!(((Key >= '0') && (Key <= '9')) || (Key == VK_BACK) || (Key == VK_DELETE)))
{
Key = 0;
}
if ((StrToInt(feed_inter_data->Text)) > 255 || StrToInt(feed_inter_data->Text) < 0)
{
MessageBox(NULL, "输入数值不对(范围是:0-255)", NULL, MB_OK);
feed_inter_data->Text = 0;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if (WinIo_Status)
{
WatchDog_Stop();
ShutdownWinIo();
}
WinIo_Status = false;
InitWinIo_Message->Font->Color = clYellow;
InitWinIo_Message->Caption = "看门狗已停止";
InitWinIo_Message->Visible = true;
}
//---------------------------------------------------------------------------
DWORD CALLBACK ThreadFunc(void *p)
{
while(1)
{
WatchDog_Feed(T);
Sleep((unsigned char)&p);
}
}
void __fastcall TForm1::Label2DblClick(TObject *Sender)
{
L = !L;
if (L)
{
Label2->Caption = "Trace";
}
else
Label2->Caption = "I love you";
}
//---------------------------------------------------------------------------