ioPAC RTU Controllers
C/C++ Sample Code Programming Guide
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Functions
rtc.c File Reference

RTC More...

#include <libmoxa_rtu.h>

Functions

int main (int argc, char *argv[])
 

Detailed Description

RTC

Date
04-10-2013
Author
Eddy Kao
Version
V1.0
Introduction:
Test RTC and System Time.
Example:
./rtc
Help:
root@Moxa:/tmp#./rtc -h
RTC program.

Usage: ./rtc

Library:
RTC APIs

Function Documentation

int main ( int  argc,
char *  argv[] 
)
/*******************************************************************************
* Copyright Moxa Inc.
*
* RTC
*
* Date Author Comment
* 04-10-2013 Eddy Kao Created.
******************************************************************************/
#include <libmoxa_rtu.h>
int main(int argc, char *argv[])
{
int retval = 0;
UINT32 rc = 0;
time_t timep;
struct tm *p;
struct rtc_time timeRTC;
while((retval = getopt(argc, argv, "h")) != -1)
{
switch(retval)
{
case '?':
case 'h':
default:
printf("RTC program.\n\n");
printf("Usage: ./rtc\n\n");
return;
}
}
time(&timep);
p = localtime(&timep);
memset(&timeRTC, 0, sizeof(timeRTC));
timeRTC.tm_sec = p->tm_sec;
timeRTC.tm_min = p->tm_min;
timeRTC.tm_hour = p->tm_hour;
timeRTC.tm_mday = p->tm_mday;
timeRTC.tm_mon = (1 + p->tm_mon);
timeRTC.tm_year = (1900 + p->tm_year);
rc = MX_RTU_RTC_Set(&timeRTC);
if(rc != MISC_ERR_OK)
{
printf("MX_RTU_RTC_Set(&timeRTC), return code = %d\r\n", rc);
exit(1);
}
else
{
printf("Set RTC by System Time successfully.\r\n");
}
memset(&timeRTC, 0, sizeof(timeRTC));
rc = MX_RTU_RTC_Get(&timeRTC);
if(rc != MISC_ERR_OK)
{
printf("MX_RTU_RTC_Get(&timeRTC), return code = %d\r\n", rc);
exit(1);
}
else
{
printf("Get RTC: %d/%d/%d %d:%d:%d\r\n", timeRTC.tm_year, timeRTC.tm_mon, timeRTC.tm_mday, timeRTC.tm_hour, timeRTC.tm_min, timeRTC.tm_sec);
}
printf("\r\n");
printf("Get RTC using \"hwclock -r\"\r\n");
system("hwclock -r");
printf("\r\n");
printf("Set System Time by RTC, using \"hwclock -s\"\r\n");
system("hwclock -s");
printf("Get System Time using \"date\"\r\n");
system("date");
printf("\r\n");
printf("Set RTC by System Time, using \"hwclock -w\"\r\n");
system("hwclock -w");
return 0;
}