Linux应用层系统时间写入RTC时钟的方法

Linux内核版本:linux-3.0.35
开发板:i.MX6S MY-IMX6-EK200
系统:Ubuntu12

    前言:之前写过一篇关于如何通过应用层程序读取系统时间的blog,今天再写一篇如何写入并保存RTC时钟的blog吧。

转载请注明作者和出处:http://blog.csdn.net/c406495762/article/details/51168155

    一、写入时间
    1、预备知识:
    a、mktime
    头文件#include <time.h>
    函数time_t mktime(struct tm *timeptr)
    函数说明:mktime()用来将timeptr所指的tm结构体数据换成从公元1970年1月1日0时0分0 秒算起至今的本地时间所经过的秒数。
    返回值:返回经过的秒数。当发生错误的时候,返回-1。
    b、settimeofday
    头文件#include <sys/time.h>
                  #include <unistd.h>
    函数:int settimeofday(const struct timeval *tv,const struct timezone *tz)
    函数说明:settimeofday()会把目前时间设成由tv所指的结构体信息,当地时区信息则设成tz所指的结构体。
    返回值:只有root权限才能使用此函数修改时间。成功则返回0,失败返回-1,错误代码存于errno。
    2、实践:
    通过mktime和settimeofday配合使用,即可完成时间的写入。
    3、代码如下:

#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#include <bits/types.h>
#include <linux/rtc.h>

struct my_timeval
{
    __time_t tv_sec;
    __suseconds_t tv_usec;
};

/*************************************************
*函数名       :    System_SetTime
*功能         :    写入系统时间
*使用方法      :    char* dt = "2016-04-15 21:00:00";
                   System_SetTime(dt);
**************************************************/
int System_SetTime(char* dt)
{
    struct rtc_time tm;
    struct tm _tm;
    struct my_timeval tv;
    time_t timep;

    sscanf(dt,"%d-%d-%d %d:%d:%d",&tm.tm_year,&tm.tm_mon,&tm.tm_mday,&tm.tm_hour,&tm.tm_min,&tm.tm_sec);
    _tm.tm_sec = tm.tm_sec;
    _tm.tm_min = tm.tm_min;
    _tm.tm_hour = tm.tm_hour;
    _tm.tm_mday = tm.tm_mday;
    _tm.tm_mon = tm.tm_mon - 1;
    _tm.tm_year = tm.tm_year - 1900;

    timep = mktime(&_tm);                               
    tv.tv_sec = timep;
    tv.tv_usec = 0;
    if(settimeofday(&tv, (struct timezone *) 0) < 0)
    {
        printf("Set system datetime error!\n");
        return -1;
    }   
    return 0;
}

void main(void)
{
    char *dt = "2016-4-15 21:00:00";
    System_SetTime(dt);
}

    4、测试结果:
time.c
    二、保存时间
    从上面的测试结果可以看出,可以正常写入系统时间了。我起初也以为这样就可以了,但是我发现,这样是不行的。因为一旦我重新启动开发板,系统时间又会回复到原来的时间。想想也是,我们只是写入了系统时间,没有将系统时间同步到硬件时间,这样系统每次重启读取的硬件时间是没有改变的,启动后得到的系统时间CST = UTC + 8,还是换来的系统时间。那怎样将我们设置的系统时间同步到硬件时间呢?我们知道在终端里,可以通过hwclock –systohc将系统时间同步到硬件时间上去,在应用层怎么实现呢?我不知道有没有其他好的解决办法,我想出来的办法就是在应用层创建子进程,在子进程里调用脚本文件,脚本里的指令就是hwclock –systohc。这样就完成了同步。当然如果有更简单和更合适的方法,欢迎指导、交流。说了这么多,言归正传。
    1、预备知识:
    a、fork创建子进程,代码如下:

/**************************
*功能:创建子进程fork()测试
*时间:2016-4-15
*作者:Jack Cui
***************************/
#include <unistd.h>
#include <stdio.h> 
int main (void) 
{ 
    pid_t fpid;                 //fpid表示fork函数返回的值
    int count=0;
    fpid=fork(); 
    if (fpid < 0)               //创建子进程失败
        printf("error\n"); 
    else if (fpid == 0) {
        printf("I am the child process,my process id is %d\n",getpid()); 
        count++;
    }
    else {
        printf("I am the parent process,my process id is %d\n",getpid()); 
        count++;
    }
    printf("count = %d\n",count);
    return 0;
}

    b、fork测试程序结果显示:
fork
    c、execve()应用层调用脚本文件:
    头文件:#include <unistd.h>
    函数:int execve(const char * filename, char * const argv[], char * const envp[]);
    函数说明: execve()用来执行参数filename 字符串所代表的文件路径, 第二个参数系利用数组指针来传递给执行文件, 最后一个参数则为传递给执行文件的新环境变量数组。
    返回值:如果执行成功则函数不会返回, 执行失败则直接返回-1, 失败原因存于errno 中。
    d、execve()测试代码:

/**************************
*功能:测试execve
*时间:2016-4-15
*作者:Jack Cui
***************************/

#include <stdio.h>         //perror
#include <stdlib.h>        //EXIT_SUCCESS EXIT_FAILURE
#include <unistd.h>        //execve

void main(void)
{
    char * args[] = {"/home/nfsroot/hwclock.sh", NULL};
    if(-1 == (execve("/home/nfsroot/hwclock.sh",args,NULL)))
    {
        perror("execve");
        exit(EXIT_FAILURE);
    }
    exit(EXIT_SUCCESS);
}

    e、脚本内容:
bash
    f、execve测试结果:
bash
    可以看出execve使用正常,我们将脚本内容改为hwclock –systohc就可以实现将系统时间同步到硬件时间了。
    三、整体代码如下:

/******************************************
*功能:Linux应用层系统时间写入RTC时钟的方法
*时间:2016-4-15
*作者:Jack Cui
*******************************************/

#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#include <bits/types.h>
#include <linux/rtc.h>

struct my_timeval
{
    __time_t tv_sec;
    __suseconds_t tv_usec;
};

/*************************************************
*函数名        :   System_SetTime
*功能         :   写入系统时间
*使用方法   :   char* dt = "2016-04-15 21:00:00";
                System_SetTime(dt);
**************************************************/
int System_SetTime(char* dt)
{
    struct rtc_time tm;
    struct tm _tm;
    struct my_timeval tv;
    time_t timep;

    sscanf(dt,"%d-%d-%d %d:%d:%d",&tm.tm_year,&tm.tm_mon,&tm.tm_mday,&tm.tm_hour,&tm.tm_min,&tm.tm_sec);
    _tm.tm_sec = tm.tm_sec;
    _tm.tm_min = tm.tm_min;
    _tm.tm_hour = tm.tm_hour;
    _tm.tm_mday = tm.tm_mday;
    _tm.tm_mon = tm.tm_mon - 1;
    _tm.tm_year = tm.tm_year - 1900;

    timep = mktime(&_tm);                               
    tv.tv_sec = timep;
    tv.tv_usec = 0;
    if(settimeofday(&tv, (struct timezone *) 0) < 0)
    {
        printf("Set system datetime error!\n");
        return -1;
    }   
    return 0;
}

void main(void)
{
    char *dt = "2016-4-15 21:00:00";
    pid_t fpid;                 //fpid表示fork函数返回的值
    fpid=fork(); 
    if (fpid < 0)               //创建子进程失败
        printf("error\n"); 
    else if (fpid == 0) 
    {
        char * args[] = {"/home/nfsroot/hwclock.sh", NULL};
        if(-1 == (execve("/home/nfsroot/hwclock.sh",args,NULL)))
        {
            perror("execve");
            exit(EXIT_FAILURE);
        }
        exit(EXIT_SUCCESS);
    }
    else
    {
        System_SetTime(dt);
    }
    return 0;
}

    四、最终结果显示:
    1、脚本内容:
hwclock
    2、测试结果:
result
    这样我们重新启动开发板,系统时间不会变,设置成功~!

  • 6
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jack-Cui

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值