Code:
GETNETTIME:
NETRETRYCOUNT = 0
GOSUB INTDISABLE
GETNETTIME2:
HSEROUT [13,10,"TIMESERVER ",#TSIP1,".",#TSIP2,".",#TSIP3,".",#TSIP4,13,10]
PAUSE 100
THEADER[0] = $2 ' STX
THEADER[1] = TSIP1 ' DESTINATION IP
THEADER[2] = TSIP2
THEADER[3] = TSIP3
THEADER[4] = TSIP4
THEADER[5] = 0 ; DESTINATION PORT HIGHBYTE
THEADER[6] = 123 ; DESTINATION PORT LOWBYTE
LRC1 = 0
LRC2 = 0
For X = 1 to 6
LRC1 = LRC1 ^ THeader[x]
next X
THEADER[7] = LRC1
THEADER[8] = 0
THEADER[9] = 48 'Data Length lowbyte
;Send the header
HSEROUT [13,10]
For X = 0 to 9
HSEROUT2 [THeader[X]]
Next X
LRC2 = LRC2 ^ THeader[8]
LRC2 = LRC2 ^ THeader[9]
;Send the data
HSEROUT2 [$1B]
LRC2 = LRC2 ^ $1B
For X = 0 To 46
HSEROUT2 [0]
LRC2 = LRC2 ^ 0
NEXT X
;Send the Checksum (LRC)
HSEROUT2 [LRC2]
getresponse:
HSERIN2 1000,NoResponse,[Wait($2)]
hserin2 200,NoResponse,[SKIP 41,TimeNow.Byte3,TimeNow.Byte2,TimeNow.Byte1,TimeNow.Byte0]
' TIME1900_2009 = 3439756800
READ 851,HOFFSET
IF sign = "1" then
if DST = 1 and HOffset = 0 THEN
Sign = "2"
GOTO OtherSide
ENDIF
TimeNow = Timenow - ((HOffset - DST) * 3600)
ENDIF
OtherSide:
IF Sign = "2" THEN
TimeNow = TimeNow + ((Hoffset + DST) * 3600)
ENDIF
SINCEJAN2009 = TIMENOW - 3439756800 ; 3439756800 is the #of sec from Jan1 1900 to Jan1 2009
RUNNINGSECONDS = SINCEjAN2009
YR = 2009
GETYEAR:
LEAP = (YR//4) ; = 0 if leap year
if LEAP = 0 then
SecondsInYear = 31622400 ;366*60*60**24
ELSE
SecondsInYear = 31536000 ;365*60*60*24
endif
If RunningSeconds > SecondsInYear Then
RunningSeconds = RunningSeconds - SecondsInyear
YR = YR + 1
GOTO GetYear
ENDIF
Mo = 1 ; Start with Month = 1
GETMONTH:
IF LEAP > 0 THEN
lookup2 MO,[0,2678400,2419200,2678400,2592000,2678400,2592000,2678400,2678400,2592000,2678400,2592000,2678400],SecondsInMonth
ELSE
LOOKUP2 MO,[0,2678400,2505600,2678400,2592000,2678400,2592000,2678400,2678400,2592000,2678400,2592000,2678400],SecondsInMonth
ENDIF
If RunningSeconds >= SecondsInMonth THEN
RunningSeconds = RunningSeconds - SecondsInMonth
MO = MO + 1
GOTO GETMONTH
ENDIF
FINDDAYS:
DA = RUNNINGSECONDS/86400
RUNNINGSECONDS = RUNNINGSECONDS//86400
HR = RUNNINGSECONDS/3600
RUNNINGSECONDS = RUNNINGSECONDS//3600
MN = RUNNINGSECONDS/60
RUNNINGSECONDS = RUNNINGSECONDS//60
SC = RUNNINGSECONDS
Da = Da + 1 ; Because there is no day zero
HSEROUT [13,10,13,10,DEC2 HR,":",DEC2 MN,":",DEC2 SC," ",DEC2 MO,"/",DEC2 DA,"/",#YR,13,10]
YR8 = YR - 2000 ; clock only takes two digits!
ConverttoBCD:
DT = (DA/10) << 4 + (DA//10)
YR8 = (YR8/10) << 4 + (YR8//10)
MN = (MN/10) << 4 + (MN//10)
SC = (SC/10) << 4 + (SC//10)
HR = (HR/10) << 4 + (HR//10)
MO = (MO/10) << 4 + (MO//10)
gosub CLOCKWRITE
NetFail = 0
HSEROUT ["CLK updated",13,10]
pause 2000
goto DoneClock
NoResponse:
hserout ["RETRYING GetNetTime",13,10]
pause 1000
NETRETRYCOUNT = NETRETRYCOUNT + 1
if NETRETRYCOUNT < 5 THEN
goto getnettime2
ENDIF
IF NETRETRYCOUNT < 10 THEN
GOSUB GETALTERNATESERVER
goto getnettime2
ENDIF
READ 800,Dummy
TSIP1 = Dummy
THEADER[1] = TSIP1 'Set it back to primary
READ 801,Dummy
TSIP2 = Dummy
THEADER[2] = TSIP2
READ 802,Dummy
TSIP3 = Dummy
THEADER[3] = TSIP3
READ 803,Dummy
TSIP4 = Dummy
THEADER[4] = TSIP4
NetFail = 1
HSEROUT [13,10,"FAILED NET TIME",13,10]
DoneClock:
GOSUB INTREENABLE
RETURN
Bookmarks