Page 1 of 1

Setting RTC

Posted: Tue Dec 20, 2011 9:10 am
by Vernon
Since I now send email from my Netburners under certain conditions and felt that I should include the time in the message body, I needed a convenient way to set the RTC. I use Terra Term, a free download, and the following code in the Netburner. When I want to set the clock (and they seem to drift a minute or two per month) I turn dip switch 1 ON and press reset. I then start Terra Term and connect to my local network IP (port 23). Below is a screen shot of the transaction and the code that goes in the main loop after initialization.

Image


dip = getdipsw();
putleds( dip );
if (dip == 1)
{


int ListenPort = 23;
// Set up the listening TCP socket
int fdListen = listen(INADDR_ANY, ListenPort, 5);

if (fdListen > 0)
{
dip = 4;
putleds( dip );
IPADDR client_addr;
WORD port;
port = 23;
int fdnet = accept(fdListen, &client_addr, &port, 0);
writestring(fdnet, "Vernon Model RR Time Sync\r\n");
writestring(fdnet, "Because Railroads must run on time.\r\n");
time_t curtime;
struct tm *loctime;
/* Get the current time. */
curtime = time (NULL);
loctime = localtime (&curtime);
siprintf(RXBuffer, (asctime (loctime)));
writestring(fdnet, RXBuffer);
n = 0;
n = read( fdnet, RXBuffer, RX_BUFSIZE );
RXBuffer[n] = '\0';
// above necessary to purge buffer for input below
writestring(fdnet, "\r\n");
writestring(fdnet, "Enter new year value\r\n");
ReplaceStdio( 0, fdnet );
scanf("%d", &newyear);
siprintf(RXBuffer, "new year value %d", newyear);
writestring(fdnet, RXBuffer);
newseconds = 0;
writestring(fdnet, "\r\n");
writestring(fdnet, "Enter new month value\r\n");
scanf("%d", &newmonth);
siprintf(RXBuffer, "new month value %d", newmonth);
newmonth = newmonth - 1;
writestring(fdnet, RXBuffer);
writestring(fdnet, "\r\n");
writestring(fdnet, "Enter new date value\r\n");
scanf("%d", &newdate);
siprintf(RXBuffer, "new date value %d", newdate);
writestring(fdnet, RXBuffer);
writestring(fdnet, "\r\n");
writestring(fdnet, "Enter new hour value\r\n");
scanf("%d", &newhour);
siprintf(RXBuffer, "new hour value %d", newhour);
writestring(fdnet, RXBuffer);
writestring(fdnet, "\r\n");
writestring(fdnet, "Enter new minute value wait for seconds = 0 then enter to sync time\r\n");
scanf("%d", &newminute);
siprintf(RXBuffer, "new minute value %d", newminute);
writestring(fdnet, RXBuffer);
writestring(fdnet, "\r\n");
struct tm bts;
bts.tm_sec = 0;
bts.tm_min = newminute;
bts.tm_hour = newhour;
bts.tm_mday = newdate;
bts.tm_mon = newmonth;
bts.tm_year = newyear;

RTCSetTime( bts);
RTCSetSystemFromRTCTime();
/* Get the current time. */
curtime = time (NULL);
loctime = localtime (&curtime);
siprintf(RXBuffer, (asctime (loctime)));
writestring(fdnet, RXBuffer);
OSTimeDly( TICKS_PER_SECOND * 10 );
close(fdnet);
// sends updated time to terraterm, waits 10 seconds, then closes.
// terraterm will then also close.


}
ENDLESS:
OSTimeDly( TICKS_PER_SECOND * 5 );
goto ENDLESS;
// reset with dip 1 off to resume.
}

Re: Setting RTC

Posted: Wed Dec 21, 2011 5:12 am
by BillC
Hi Vernon, I normally get the time from a network time server as shown below.

Bill

Code: Select all

	NTPupdated = SetNTPTime( AsciiToIp( NTP_SERVER_IP ) );
  	time (&rawtime) ;
	stime = localtime (&rawtime);
    sprintf (reset_time, "\"%4d%.2d%.2d\",\"%.2d%.2d\"", (stime->tm_year+1900), stime->tm_mon+1, stime->tm_mday,stime->tm_hour,stime->tm_min) ;
	if (NTPupdated){
 	  RTCSetRTCfromSystemTime () ;
 	  strcpy (msg1,"NTP") ;
 	  NTPstate = 1 ;
	}
	else   strcpy (msg1,"RTC") ;
 	sprintf (msg,"%s started, using %s time/date\r\n\n",AppName,msg1) ;
    SysLog (msg,1) ;

Re: Setting RTC

Posted: Wed Dec 21, 2011 11:12 am
by Vernon
Interesting !

I didn't know (until now) that accurate time is available on the internet. I thought you needed an NTPT server on your network and I don't have one. I might have to try that out one of these days.

I have an atomic watch and that is why I preferred to just set it.

In addition I wanted to get telnet working so that I can rapidly change other variables. Like say people keep derailing the model train by backing it through switches - well it would be nice to set a variable that blocks switch (turnout) operation.