/* This program reads the PTSI WWV clock (the rack clock) and synchronizes the system clock to it. It will wake up once every CHECKTIME seconds to check the clock. The sole argument is the current serial port to use. This program is usually called by clock and put in background. */ #include #include #include #include "../track/wirotypes.h" #include "../track/track.h" #include "../track/wiro.h" struct wiro_memory *tinfo; #define CHECKTIME 10 #include #include #include double juliandate(); int openport(char *name, long speed); /******************************************************************/ /* This samples the system clock, the WWV clock, and updates. */ /******************************************************************/ int set_time(arg,theport) int arg; int theport; { struct timeval timeSys,timeClock; struct timezone zoneSys; int i,thousandths; double secClock, secSys, secError; char str[19]="QT"; char str2[19]="QD"; struct tm tmPTSI; /* First, let's see how the two clocks differ */ /* printf("About to write to port\n"); */ write(theport,str,2); /* Get the time from the PSTI clock */ /* printf("Wrote to the port \n"); */ for(i=0; i<15; i++) { read(theport,str+i,1); /* printf("%c", str[i]); fflush(stdout); */ } gettimeofday(&timeSys, &zoneSys); /* Get system time */ write(theport,str2,2); /* Get the date from the PSTI clock */ for(i=0; i<13; i++) { read(theport,str2+i,1); } secSys = timeSys.tv_sec + timeSys.tv_usec / 1000000.0; sscanf(str+1,"%2d",&(tmPTSI.tm_hour)); sscanf(str+4,"%2d",&(tmPTSI.tm_min)); sscanf(str+7,"%2d",&(tmPTSI.tm_sec)); sscanf(str+10,"%3d",&(thousandths)); sscanf(str2+0,"%2d",&(tmPTSI.tm_year)); sscanf(str2+3,"%2d",&(tmPTSI.tm_mon)); sscanf(str2+6,"%2d",&(tmPTSI.tm_mday)); secClock = 3600.0 * (double) tmPTSI.tm_hour + 60.0 * (double) tmPTSI.tm_min + (double) tmPTSI.tm_sec + 0.001 * (double) thousandths + 24 * 3600 * (juliandate(tmPTSI.tm_year, tmPTSI.tm_mon, tmPTSI.tm_mday) - juliandate( 70, 1, 1)); secError = secSys - secClock; if ( (fabs(secError) > 6.0) && (arg == 1) ) { printf(" THE PTSI CLOCK HAS A BIG ERROR! \n"); return(1); } if ( ( secError > 0.005 ) || ( secError < -0.005) ) { { gettimeofday(&timeSys, &zoneSys); /* Get system time */ secSys = (double) timeSys.tv_sec + (double ) timeSys.tv_usec / 1000000.0; timeSys.tv_usec -= 1000000.0 * ((double) secError - floor( (double) secError )); while ( timeSys.tv_usec < 0 ) { timeSys.tv_usec += 1000000.0; timeSys.tv_sec -= 1.0; } while ( timeSys.tv_usec > 1000000.0 ) { timeSys.tv_usec -= 1000000.0; timeSys.tv_sec += 1.0; } timeSys.tv_sec -= floor( secError ); settimeofday(&timeSys, &zoneSys); } return(0); } } main() { int i,j; char c; int theport; char str[30]="QT"; tinfo = get_tinfo(); /* theport = openport("/dev/amp2", 9600); */ theport = openport("/dev/ttyQ1a2", 9600); setraw(theport); set_time(0,theport); sleep(CHECKTIME); set_time(1,theport); tinfo->clockcount = 500; fflush(stdout); }