2014-05-09 54 views
-1

我寫了一個簡單的RTC Test應用程序,編譯爲& ran。但我無法兩件事情從我得到的解碼輸出:Linux RTC應用程序代碼在read()和當前年顯示不正確

輸出:

====== RTC Test ==== 
Open & release 
opened 
get RTC time 
Current RTC date/time is 9-5-114, 18:17:55 
read RTC time 

問題#1)這一年是不正確的,114 &沒有地2014年OR 14如預期。

第二期)的代碼在等待read()方法

這究竟是爲什麼?以及如何解決這個問題? 下面是代碼: -

#include <stdio.h> 
#include <linux/rtc.h> 
#include <sys/ioctl.h> 
#include <sys/time.h> 
#include <sys/types.h> 
#include <fcntl.h> 
#include <unistd.h> 
#include <stdlib.h> 
#include <errno.h> 
#include <string.h> 


int main() 
{ 
     int fd, ret; 
     struct rtc_time rtc_tm; 
     unsigned long data; 

     printf("====== RTC Test ====\n"); 

     printf("Open & release\n"); 
//open function is used to establish the connection between the RTC device with the file descriptor 
     fd = open("/dev/rtc0", O_RDWR, 0);  //open for reading & writing 
     if (fd == -1) 
     { 
       perror("/dev/rtc0"); 
       exit(errno); 
     } 
     else 
     { 
       printf("opened\n"); 
     } 

//ioctl() to configure the RTC device 
     printf("get RTC time\n"); 
     ret = ioctl(fd, RTC_RD_TIME, &rtc_tm); //ioctl command RTC_RD_TIME is used to read the current timer 
     if (ret == -1) 
     { 
       perror("rtc ioctl RTC_RD_TIME error"); 
     } 
     printf("Current RTC date/time is %d-%d-%d, %02d:%02d:%02d\n", 
       rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year, 
rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec); 

     /* Set the RTC time/date */ 
     /* rtc_tm.tm_mday = 31; 
     rtc_tm.tm_mon = 4;  // for example Sep 8 
     rtc_tm.tm_year = 104; 
     rtc_tm.tm_hour = 2; 
     rtc_tm.tm_min = 30; 
     rtc_tm.tm_sec = 0; 
     */ 

// call the read function to wait the Alarm interrupt 
     printf("read RTC time\n"); 
     ret = read(fd, &data, sizeof(unsigned long)); 
     if (ret == -1) 
     { 
       perror("rtc read error"); 
     } 
     printf("rtc read\n"); 

// set RTC time 
     printf("set RTC time\n"); 
     ret == ioctl(fd, RTC_SET_TIME, &rtc_tm); 
     if (ret == -1) 
     { 
       perror("rtc ioctl RTC_SET_TIME error"); 
     } 
     printf("Set Current RTC date/time to %d-%d-%d, %02d:%02d:%02d\n", 
       rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year, 
       rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec); 
     printf("Get RTC time\n"); 
     ret = ioctl(fd, RTC_RD_TIME, &rtc_tm); 
     if (ret == -1) 
     { 
       perror("rtc ioctl RTC_RD_TIME error"); 
     } 
     printf("Current RTC date/time is %d-%d-%d, %02d:%02d:%02d\n", 
       rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year, 
       rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec); 

     rtc_tm.tm_sec += 50; 
     if (rtc_tm.tm_sec >= 60) 
     { 
       rtc_tm.tm_sec %= 60; 
       rtc_tm.tm_min++; 
     } 
     if (rtc_tm.tm_min == 60) 
     { 
       rtc_tm.tm_min = 0; 
       rtc_tm.tm_hour++; 
     } 
     if (rtc_tm.tm_hour == 24) 
     { 
       rtc_tm.tm_hour = 0; 
     } 
     printf("RTC tests done"); 
     close(fd); 

     return 0; 
} 

至於問題#2而言,這可能是因爲在運行RTC設備等待中斷。請確認我的猜測是否正確,因爲如果是的話,那我就不用擔心了。

+0

請不要在相同的帖子中提出無關的問題。這應該作爲兩個單獨的問題發佈。 –

回答

1

關於#1:您可能需要一年解碼:

如果根據RTC的時代和年度註冊年份是 小於1970年它被假定爲100年後,也就是, 2000和2069 http://man7.org/linux/man-pages/man4/rtc.4.html

之間被實現像

 if (wtime.tm_year < 70) 
      wtime.tm_year += 100; 

所以你可以減去100得到14(對於2014)

相關問題