• 嵌入式编程实现记录开发板启动次数


    我们在有些试验的时候,需要记录开发板的启动次数,看是否启动成功,用于判断是否符合标准,但是我们又不想自行人工的去数启动的次数,这里就使用一个小程序来实现
    代码如下:

    #include <stdio.h>  
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    
    void help()
    {
        printf("Param is incorrect,Please check it \n");
        printf("Usage:   runtimes filename(filename path is absulute path)\n");
        //printf("\nfilename : the filename you want to savefile\r\n");
        return ;
    }
    
    int main(int arc, char *argv[])
    {
        if(arc !=2)
        {
            help();
            exit(0);
        }
        //printf("argv[1]=%s\r\n",argv[1]);
        FILE *fp = NULL;
        char *str="";
        fp = fopen (argv[1], "r");
        if (fp == NULL) //file is not found,then counts=0,create file and write 0 count
        {   
            fp = fopen (argv[1], "w+");
            fprintf(fp,"PowerOn counts is 1\r\n");
            printf("PowerOn counts is %d\n",1);
            fclose(fp);
            exit(0);      
        }
        else
        {
            //calc this file have lines
            int ch;
            int lines=1;       
            while((ch = fgetc(fp)) != EOF)
            {
                if(ch == '\n')
                    lines++;
            }
            fclose(fp);
    
            //reopen file and write lines+1        
            fp = fopen (argv[1], "a");        
            fprintf(fp,"PowerOn counts is %d\r\n",lines);
            fclose(fp);
            printf("PowerOn counts is %d\n",lines);
            exit(0);             
        }   
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    编译成arm系统下的执行文件,拷贝到开发板中。
    执行如下:
    root@imx6ulevk:~# ./runtimes ~
    Segmentation fault
    root@imx6ulevk:~# ./runtimes ~/runtimes.log
    PowerOn counts is 1
    root@imx6ulevk:~# ./runtimes ~/runtimes.log
    PowerOn counts is 2
    root@imx6ulevk:~# ./runtimes ~/runtimes.log
    PowerOn counts is 3
    root@imx6ulevk:~# ./runtimes ~/runtimes.log
    PowerOn counts is 4
    程序执行成功,然后将这个命令放入到启动时执行的脚本中,就可以自动实现了。

    具体如何将文件加入到开发板的启动序列中,查阅自己的开发板的手册或者参考我以前发的文章吧。

    下面我们可能进行嵌入式系统的通讯接口的编程实现,欢迎关注。

    创作不易,点个赞再走呗,期待共同提高。

  • 相关阅读:
    .net core中前端vue HTML5 History 刷新页面404问题
    flume入门案例
    JAX安装
    helm chart详解及常用命令:helm template / package / plugin
    【算法设计与分析】— —实现最优载的贪心算法
    Android Studio 国内镜像代理设置(解释了设置之后项目还是下载镜像失败问题)
    Linux笔记-3
    Spring MVC 五 - Spring MVC的配置和DispatcherServlet初始化过程
    FlinkSql之TableAPI详解
    网页设计中网站的设计与实现
  • 原文地址:https://blog.csdn.net/mainmaster/article/details/125479776