Beispiel #1
0
        //设定喂狗时间
        private void Feed_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //WDT的可编程超时范围为1~65535秒/分
            ushort time = 60;

            try
            {
                time = Convert.ToUInt16(time_val.Text);
                if (time < 10 || time > 65535)
                {
                    MessageBox.Show("请输入10~65536之间的整数!");
                    return;
                }
            }
            catch (Exception)
            {
                MessageBox.Show("请输入10~65536之间的整数!");
                return;
            }

            if (watchdog == null)
            {
                MessageBox.Show("未选择迷你电脑型号!");
                return;
            }
            interval.Text = "喂狗间隔:" + time + "s";
            feedTime      = time;
            wtimer        = Convert.ToUInt16(Math.Ceiling(time * 0.8));//定时器必须提前喂狗
            Console.WriteLine("feedTime:{0} wtimer:{1}", feedTime, wtimer);

            watchdog.InitSuperIO();
            watchdog.InitLogicDevice();//LDN 07

            if (watchdog.MinipcType.Equals("Q500"))
            {
                //SMI# Control Register 2 (Index=F1h, Default=00h)
                //bit 6:触发方式 0边沿触发/1电平触发
                //bit 2 SMI# WDT功能开关 1开/0关
                if (timeUint == 0)
                {
                    watchdog.EnableWatchDog(0xf1, 0x44);//0100 0100
                }
            }
            else
            {
                //Watch Dog Timer Configuration Register (Index=72h, Default=001s0000b)
                //bit 7:喂狗时间单位 1秒/0分
                //bit 4:WDT功能开关 1开/0关
                if (timeUint == 0)
                {
                    watchdog.EnableWatchDog(0x72, 0x90);//1001 0000
                }
            }
            watchdog.ExitSuperIo();

            //看门狗管理器
            //先清除已存在的看门狗,如果存在
            WatchDogManager tm = WatchDogManager.Instance;

            if (tm.Timer != null)
            {
                Console.WriteLine("关闭之前的定时器!");
                tm.Timer.Stop();
                tm.Timer    = null;
                tm.WatchDog = null;
            }

            //设定喂狗时间,即在设定时间内必须喂狗,否则会自动重启
            watchdog.FeedDog(feedTime);
            if (!wdttimer.IsEnabled)
            {
                wdttimer.Interval = TimeSpan.FromMilliseconds(wtimer * 1000);
                wdttimer.Start();
            }

            tm.Timer    = wdttimer;
            tm.WatchDog = watchdog;
        }