Read and write the Realtime clock
From Digi Developer
Contents |
Python-enabled Digi Products with a hardware RTC
- Digi ConnectPort X4
- Digi ConnectPort X8
- Digi Connect WAN 3G
- Digi ConnectPort WAN VPN
The Realtime Clock is supported by a supercap designed to retain the time for 1 week without power. The hardware RTC only records time in seconds, so no sense of milliseconds are maintained. It also can drift up to 2 seconds per day or 1 minute per month.
Note that the Digi ConnectPort X2 does NOT have a hardware RTC. Although you can set the time and date, it will drift several minutes PER DAY due to the use of spread-spectrum clock signals which constantly vary the CPU speed. So any application expecting to use real time-of-day values on the Digi ConnectPort X2 will require external update, such as NTP or manual rewrite at least once per day.
Reading the RTC
From the telnet/command line
The show time command returns the date and time in the following format. Note that if you have never set the realtime clock, you'll see a warning message instead.
#> show time Current Date and Time: Tue Jun 24 16:27:12 2008
To set the clock, use the set time command. Enter "set time ?" to see the help, then just format the new date and time as required.
#> set time ?
Sets real time clock time/date.
syntax: set time [options...]
options:
time=hh:mm:ss -or- hh:mm
date=mm.dd.yy
#> set time date=03.31.09 time=09:23:17
From Python
- import the time module.
- time.time() returns the UNIX-style time since 1-Jan-1970 as a float, assume the milliseconds are meaningless. This routine might involve serial shifting from the hardware RTC, so might take 5 to 7 msec to complete - or it might take longer. Literally, any call to time.time() might cause blocking hardware access and the return delay will be unpredictable. Moral; don't use this to just calculate durations - how many seconds some function required to run. If the system clock on the device has not been set, this will instead return seconds since the device was powered on. For consistent behavior, ensure that you set the time in the system. See below.
- time.clock() instead just returns a number derived from the system clock and has no basis in "real time". However it is very fast and the milliseconds are valid, so this is the ideal function to use for determining durations - as a stop-watch or timeout.
Writing the RTC
From the telnet/command line
#> set time
Sets real time clock time/date.
syntax: set time [options...]
options:
time=hh:mm:ss -or- hh:mm
date=mm.dd.yy
From Python
- import the time module.
- import the digicli module. This module acts much like a telnet session, so to set the RTC we need to format a text string which looks like: set time date=02.29.08 time=15:23:56. The routine below creates such a string.
def MakeDigiSetTimeString(secsSinceEpoch): """Given secsSinceEpoch, create string required for DigiCli SET TIME such as "set time date=02.29.08 time=15:23:56" """ try: timtup = time.localtime(secsSinceEpoch) # yr4, mon, day, hr, min, sec, wekdy st = 'set time date=%02d.%02d.%02d ' % (timtup[1],timtup[2],(timtup[0]%100)) st += 'time=%02d:%02d:%02d' % (timtup[3],timtup[4],timtup[5]) return st except: return ""
Then this routine feeds the string into the digicli module:
def SetTimeUnix(secsSinceEpoch): """\ Like time.time(), but returns int (not float) plus allows spoofing time """ try: st = MakeDigiSetTimeString(secsSinceEpoch) if sys.platform[:4] == 'digi': success, response = digicli.digicli(st) # print 'digicli.success = ', success # print 'digicli.response = ', response else: print st return True except: return False
Here is a routine which sends the time from the computer running the script
# cpx_settime.py # # use telnet to set CPX8 time/date import sys import time import telnetlib if __name__ == '__main__': bLive = True settings = { 'cpxip':"192.168.1.20",'tcpport':23,'username':"",'password':""} if len(sys.argv) > 1: # see if we have any arguments on command line settings.update( { 'cpxip' : sys.argv[1] } ) else: print 'Copies local time from your PC to a Digi CPX with real-time clock' print 'usage" %s {ip of CPX}' % sys.argv[0] sys.exit( 0) now = time.localtime() # time = HH:MM:SS date=mm.dd.yy setim = "set time time=%02d:%02d:%02d" % (now[3],now[4],now[5]) setim += " date=%02d.%02d.%02d" % (now[1],now[2],(now[0]%100)) if not bLive: print "Would send RTC to <%s>" % setim else: cpxip = settings.get("cpxip","192.168.1.20") tcpport = settings.get("tcpport",23) username = settings.get("username","root") password = settings.get("password","dbps") print "Attempting to set RTC time <%s> on CPX at %s:%d" % (setim,cpxip,tcpport) tn = telnetlib.Telnet(cpxip,tcpport) if len(username) > 0: tn.read_until( "login: ") tn.write( username + "\n") tn.read_until( "password:") tn.write( password + "\n") print tn.read_until( "#> ", 10) tn.write( "show time\n") print tn.read_until( "#> ", 10) tn.write( setim + "\n") print tn.read_until( "#> ", 10) tn.write( "show time\n") print tn.read_until( "#> ", 10) time.sleep( 3.0) tn.close()
