linux看门狗程序

    #include <stdio.h>  
    #include <stdlib.h>  
    #include <string.h>  
    #include <sys/types.h>  
    #include <sys/stat.h>  
    #include <unistd.h>  
    #include <fcntl.h>  
    #include <sys/ioctl.h>  
    #include <errno.h>  
    #include <sys/time.h>  
    #include <unistd.h>  
    #include <time.h>  
    #include <getopt.h>  
    #include <sys/signal.h>  
    #include <termios.h>  
      
    struct watchdog_info{  
        unsigned int options;   //options the card/driver supprots 19           
        unsigned int firmware_version;  //firmcard version of the card  
        unsigned char identity[32];     //identity of the board 21  
     };  
      
    #define WATCHDOG_IOCTL_BASE 'W'  
    #define WDIOC_GETSUPPORT _IOR(WATCHDOG_IOCTL_BASE, 0, struct watchdog_info)  
    #define WDIOC_SETTIMEOUT _IOWR(WATCHDOG_IOCTL_BASE, 6, int)  
    #define WDIOC_GETTIMEOUT _IOR(WATCHDOG_IOCTL_BASE, 7, int) 27   
    #define WDIOS_DISABLECARD 0x0001        /* Turn off the watchdog timer */  
    #define WDIOS_ENABLECARD 0x0002 /* Turn on the watchdog timer */  
    #define WDIOC_SETOPTIONS _IOR(WATCHDOG_IOCTL_BASE, 4, int)  
    #define WDIOC_KEEPALIVE _IOR(WATCHDOG_IOCTL_BASE, 5, int)  
      
    int Getch (void)   //无回显的从屏幕输入字符,来达到喂狗的目的  
      
    {  
      
         int ch;  
         struct termios oldt, newt;   //终端设备结构体  
         tcgetattr(STDIN_FILENO, &oldt);   //获得终端属性  
         newt = oldt;  
         newt.c_lflag &= ~(ECHO|ICANON);   //设置无回显属性  
         tcsetattr(STDIN_FILENO, TCSANOW, &newt);  //设置新的终端属性  
         ch = getchar();   //从键盘输入一个数据  
         tcsetattr(STDIN_FILENO, TCSANOW, &oldt);  //恢复终端设备初始设置  
         return ch;  
      
    }  
     //suspend some seconds  
    int zsleep(int millisecond)  
      
    {  
         unsigned long usec;  
         usec=1000*millisecond;  
         usleep(usec); //睡眠usec秒  
    }  
    int Init()  
    {   
         int fd;  
         //open device file  
         fd = open("/dev/watchdog",O_RDWR);   //打开看门狗设备  
          if(fd < 0)  
         {  
             printf("device open fail\n");  
             return -1;  
         }  
         return fd;  
    }  
      
    int main(int argc,char **argv)  
    {  
         int fd,ch;  
         int i,j;  
         char c;  
         struct watchdog_info wi;  
         fd=Init();  //打开终端看门狗设备  
         //读板卡信息,但不常用  
      
          ioctl(fd,WDIOC_GETSUPPORT,&wi);  
         printf("%d,%s\n",wi.options,wi.identity);  
         //读看门狗溢出时间,默认是5s  
      
         //重新设置时间为10s  
      
         i=5;  
         printf("%d\n",ioctl(fd,WDIOC_SETTIMEOUT,&i));  
         //读新的设置时间  
      
          printf("%d\n",ioctl(fd,WDIOC_GETTIMEOUT,&i));  
         printf("%d\n",i);   
         //看门狗开始和停止工作,打开和关闭设备具有同样的功能  
      
         //关闭  
          i=WDIOS_DISABLECARD;  
         printf("%d\n",ioctl(fd,WDIOC_SETOPTIONS,&i));  
         //打开  
          i=WDIOS_ENABLECARD;  
         printf("%d\n",ioctl(fd,WDIOC_SETOPTIONS,&i));  
         while(1)  
         {  
               zsleep(100);  
               if((c=Getch())!=27){  
                    //输入如果不是ESC,就喂狗,否则不喂狗,到时间后系统重启  
      
                    ioctl(fd,WDIOC_KEEPALIVE,NULL);  
                    //write(fd,NULL,1);     //同样是喂狗  
      
               }  
         }  
        close(fd);   //关闭设备  
         return 0;  
    }  

Linux下,可以通过使用QT编写看门狗程序。以下是一个简单的示例: ``` c++ #include <QtCore/QCoreApplication> #include <QtCore/QTimer> #include <QtCore/QDebug> #include <unistd.h> #include <signal.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <errno.h> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // 打开看门狗设备 int fd = open("/dev/watchdog", O_WRONLY); if (fd < 0) { qDebug() << "open watchdog device failed"; return -1; } // 设置看门狗定时器时间为5秒 int timeout = 5; if (ioctl(fd, WDIOC_SETTIMEOUT, &timeout) != 0) { qDebug() << "set watchdog timeout failed"; return -1; } // 启动看门狗 if (ioctl(fd, WDIOC_SETOPTIONS, WDIOS_ENABLECARD) != 0) { qDebug() << "start watchdog failed"; return -1; } // 定时器定时5秒 QTimer timer; timer.setInterval(5000); timer.start(); // 定时器超时后喂狗 QObject::connect(&timer, &QTimer::timeout, [](){ qDebug() << "feed dog"; write(fd, "V", 1); }); // 退出程序时关闭看门狗设备 QObject::connect(&a, &QCoreApplication::aboutToQuit, [&](){ qDebug() << "stop watchdog"; ioctl(fd, WDIOC_SETOPTIONS, WDIOS_DISABLECARD); close(fd); }); return a.exec(); } ``` 该程序使用了Qt的定时器来实现看门狗的喂狗功能,定时器超时后会向看门狗设备写入一个字节来喂狗。程序通过connect函数将定时器超时信号连接到喂狗的槽函数上,同时也连接了程序退出信号到关闭看门狗设备的槽函数上。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值