#!/usr/bin/python3 # Watch PPS counter and write log file # This runs on a Linux system. # It needs something like: # ldattach 18 /dev/ttyS0 # which creates /dev/pps0 and friends # It expects a 60 (50?) Hz signal to be connected to DCD. # That's the normal PPS input pin for NTP. # A 5 volt AC wall wart transformer works OK. # Higher voltages may need a simple resistive voltage divider. import os, time daemon = 1 ppsname = "/sys/class/pps/pps1" + "/assert" logRoot = "/var/log/pps/ppsstats" logFile = "" logName = "" logDay = 0 MJD_1970 = 40587 # MJD for 1 Jan 1970 def GetLogFile(now): global logFile, logName, logDay day = int(now/86400) if logDay == day: return when = time.strftime("%Y%m%d", time.gmtime(now)) logName = "%s.%s" % (logRoot, when) logFile = open(logName, "a") if logDay == 0: logFile.write("\n\n") logDay = day if os.path.exists(logRoot): os.unlink(logRoot) os.link(logName, logRoot) def GetData(): line = open(ppsname).read() hash = line.find("#") when = line[:hash] count = line[hash+1:-1] # print when, count, line return (float(when), int(count)) if (daemon): print("Daemonizing. Bye.") import ctypes ctypes.cdll.LoadLibrary("libc.so.6") libc = ctypes.CDLL("libc.so.6", use_errno=True) xx = libc.daemon(0, 0) (oldWhen, oldCount) = GetData() while 1: time.sleep(10) (when, count) = GetData() deltaWhen = when - oldWhen deltaCount = count - oldCount now = time.time() day = int(now/86400) sec = now - day*86400 mjday = day + MJD_1970 GetLogFile(now) # Format mimics ntpd's log files logFile.write("%d %9.3f" % (mjday, sec)) logFile.write(" %.6f %d %10.6f %4d\n" % (when, count, deltaWhen, deltaCount) ) logFile.flush() oldWhen = when oldCount = count