home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / clocks / atomic / main.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-10-29  |  16.5 KB  |  499 lines

  1. VERSION 4.00
  2. Begin VB.Form MainForm 
  3.    BorderStyle     =   3  'Fixed Dialog
  4.    ClientHeight    =   6105
  5.    ClientLeft      =   1320
  6.    ClientTop       =   1575
  7.    ClientWidth     =   6750
  8.    BeginProperty Font 
  9.       name            =   "System"
  10.       charset         =   0
  11.       weight          =   700
  12.       size            =   9.75
  13.       underline       =   0   'False
  14.       italic          =   0   'False
  15.       strikethrough   =   0   'False
  16.    EndProperty
  17.    Height          =   6510
  18.    Icon            =   "main.frx":0000
  19.    Left            =   1260
  20.    LinkTopic       =   "Form1"
  21.    MaxButton       =   0   'False
  22.    ScaleHeight     =   6105
  23.    ScaleWidth      =   6750
  24.    Top             =   1230
  25.    Width           =   6870
  26.    Begin VB.CommandButton AboutBtn 
  27.       Caption         =   "&About"
  28.       Height          =   375
  29.       Left            =   4080
  30.       TabIndex        =   18
  31.       Top             =   5640
  32.       Width           =   1215
  33.    End
  34.    Begin VB.CommandButton HelpBtn 
  35.       Caption         =   "&Help"
  36.       Height          =   375
  37.       Left            =   2760
  38.       TabIndex        =   17
  39.       Top             =   5640
  40.       Width           =   1215
  41.    End
  42.    Begin VB.Frame Frame1 
  43.       Caption         =   "Settings"
  44.       Height          =   3255
  45.       Left            =   120
  46.       TabIndex        =   2
  47.       Top             =   2280
  48.       Width           =   6495
  49.       Begin VB.CheckBox DaylightSavingsCheckBox 
  50.          Caption         =   "&USA Daylight Savings Time"
  51.          Height          =   255
  52.          Left            =   2520
  53.          TabIndex        =   11
  54.          Top             =   1680
  55.          Width           =   3735
  56.       End
  57.       Begin VB.TextBox PrefixEdit 
  58.          Height          =   360
  59.          Left            =   4440
  60.          TabIndex        =   6
  61.          Top             =   360
  62.          Width           =   1695
  63.       End
  64.       Begin VB.Label AttemptsLabel 
  65.          Height          =   255
  66.          Left            =   2040
  67.          TabIndex        =   14
  68.          Top             =   2760
  69.          Width           =   4095
  70.       End
  71.       Begin VB.Label QuitLabel 
  72.          Height          =   255
  73.          Left            =   120
  74.          TabIndex        =   12
  75.          Top             =   2400
  76.          Width           =   6015
  77.       End
  78.       Begin ComctlLib.Slider QuitSlider 
  79.          Height          =   375
  80.          Left            =   120
  81.          TabIndex        =   13
  82.          Top             =   2760
  83.          Width           =   1575
  84.          _Version        =   65536
  85.          _ExtentX        =   2778
  86.          _ExtentY        =   661
  87.          _StockProps     =   64
  88.          LargeChange     =   10
  89.          Max             =   100
  90.          Min             =   1
  91.          SelStart        =   1
  92.          TickFrequency   =   10
  93.          Value           =   1
  94.       End
  95.       Begin ComctlLib.Slider LocalTimeSlider 
  96.          Height          =   375
  97.          Left            =   120
  98.          TabIndex        =   10
  99.          Top             =   1680
  100.          Width           =   1575
  101.          _Version        =   65536
  102.          _ExtentX        =   2778
  103.          _ExtentY        =   661
  104.          _StockProps     =   64
  105.          SmallChange     =   5
  106.          Max             =   125
  107.          Min             =   -125
  108.          TickFrequency   =   5
  109.       End
  110.       Begin VB.Label LocalTimeLabel 
  111.          Height          =   240
  112.          Left            =   120
  113.          TabIndex        =   9
  114.          Top             =   1320
  115.          Width           =   5895
  116.       End
  117.       Begin VB.Label CommPortLabel 
  118.          Height          =   240
  119.          Left            =   120
  120.          TabIndex        =   3
  121.          Top             =   360
  122.          Width           =   1815
  123.       End
  124.       Begin ComctlLib.Slider CommPortSlider 
  125.          Height          =   375
  126.          Left            =   120
  127.          TabIndex        =   4
  128.          Top             =   720
  129.          Width           =   1215
  130.          _Version        =   65536
  131.          _ExtentX        =   2143
  132.          _ExtentY        =   661
  133.          _StockProps     =   64
  134.          LargeChange     =   1
  135.          Max             =   4
  136.          Min             =   1
  137.          SelStart        =   1
  138.          Value           =   1
  139.       End
  140.       Begin VB.Label Label3 
  141.          AutoSize        =   -1  'True
  142.          Caption         =   "Phone Number:"
  143.          Height          =   240
  144.          Left            =   2040
  145.          TabIndex        =   7
  146.          Top             =   840
  147.          Width           =   1500
  148.       End
  149.       Begin VB.Label PhoneNumberLabel 
  150.          Caption         =   "494-4774"
  151.          Height          =   255
  152.          Left            =   3720
  153.          TabIndex        =   8
  154.          Top             =   840
  155.          Width           =   2415
  156.       End
  157.       Begin VB.Label Label1 
  158.          AutoSize        =   -1  'True
  159.          Caption         =   "Phone &Number Prefix:"
  160.          Height          =   240
  161.          Left            =   2040
  162.          TabIndex        =   5
  163.          Top             =   360
  164.          Width           =   2130
  165.       End
  166.    End
  167.    Begin VB.ListBox TerminalWindowListBox 
  168.       Height          =   1740
  169.       Left            =   120
  170.       TabIndex        =   1
  171.       Top             =   360
  172.       Width           =   6495
  173.    End
  174.    Begin VB.CommandButton HangupBtn 
  175.       Caption         =   "Ha&ngup"
  176.       Height          =   375
  177.       Left            =   1440
  178.       TabIndex        =   16
  179.       Top             =   5640
  180.       Width           =   1215
  181.    End
  182.    Begin VB.CommandButton ExitBtn 
  183.       Caption         =   "E&xit"
  184.       Height          =   375
  185.       Left            =   5400
  186.       TabIndex        =   19
  187.       Top             =   5640
  188.       Width           =   1215
  189.    End
  190.    Begin VB.CommandButton SetClockBtn 
  191.       Caption         =   "&Set Clock"
  192.       Height          =   375
  193.       Left            =   120
  194.       TabIndex        =   15
  195.       Top             =   5640
  196.       Width           =   1215
  197.    End
  198.    Begin VB.Label Label2 
  199.       AutoSize        =   -1  'True
  200.       Caption         =   "&Terminal Window:"
  201.       Height          =   240
  202.       Left            =   120
  203.       TabIndex        =   0
  204.       Top             =   0
  205.       Width           =   1755
  206.    End
  207.    Begin MSCommLib.MSComm Comm 
  208.       Left            =   4800
  209.       Top             =   120
  210.       _Version        =   65536
  211.       _ExtentX        =   847
  212.       _ExtentY        =   847
  213.       _StockProps     =   0
  214.       CDTimeout       =   0
  215.       CommPort        =   1
  216.       CTSTimeout      =   0
  217.       DSRTimeout      =   0
  218.       DTREnable       =   -1  'True
  219.       Handshaking     =   0
  220.       InBufferSize    =   1024
  221.       InputLen        =   0
  222.       Interval        =   1000
  223.       NullDiscard     =   0   'False
  224.       OutBufferSize   =   512
  225.       ParityReplace   =   "?"
  226.       RThreshold      =   0
  227.       RTSEnable       =   0   'False
  228.       Settings        =   "9600,n,8,1"
  229.       SThreshold      =   0
  230.    End
  231. Attribute VB_Name = "MainForm"
  232. Attribute VB_Creatable = False
  233. Attribute VB_Exposed = False
  234. Option Explicit
  235. Const ProgramName = "Atomic"
  236. Const ProgramLongName = "Atomic Clock"
  237. Const Settings = "Settings"
  238. Const CommPort = "CommPort"
  239. Const Prefix = "PhoneNumberPrefix"
  240. Const LocalTimeZone = "LocalTimeZone"
  241. Const USADaylightSavings = "USADaylightSavings"
  242. Const Attempts = "Attempts"
  243. Private Sub SetDateAndTime(ByVal D As Date)
  244.   Dim DateStr As String
  245.   Dim TimeStr As String
  246.   Date = D
  247.   Time = TimeSerial(Hour(D), Minute(D), Second(D))
  248.   DateStr = Trim(Str(Month(D))) + "/" + Trim(Str(Day(D))) + "/" + Trim(Str(Year(D)))
  249.   TimeStr = Format(Hour(D), "00") + ":" + Format(Minute(D), "00") + ":" + Format(Second(D), "00")
  250.   AttemptsLabel = "Set clock to " + DateStr + " " + TimeStr
  251. End Sub
  252. Private Sub HangUp()
  253.   If Comm.PortOpen Then
  254.     UpdateStatus "Hanging Up..."
  255.     Comm.Output = "%"
  256.     Comm.PortOpen = False
  257.     UpdateStatus "Off Hook"
  258.   End If
  259. End Sub
  260. Private Function IsDigit(ByVal Ch As String) As Boolean
  261.   IsDigit = Ch >= "0" And Ch <= "9"
  262. End Function
  263. Private Function SetClock() As Boolean
  264.   Dim Line As String
  265.   Dim Tmp As String
  266.   Dim I As Integer
  267.   Dim Ch As String
  268.   Dim TimeUpdated As Boolean
  269.   Dim InputReceived As Boolean
  270.   Dim Status As Integer
  271.   'Hang up if necessary.
  272.   If Comm.PortOpen Then
  273.     Comm.PortOpen = False
  274.   End If
  275.   Comm.Settings = "9600,N,8,1"
  276.   Comm.InBufferSize = 1024
  277.   Comm.OutBufferSize = 1024
  278.   Comm.InputLen = 0
  279.   Comm.InBufferCount = 0
  280.   Comm.RThreshold = 1
  281.   Comm.SThreshold = 1
  282.   Comm.Handshaking = comRTS
  283.   Comm.CommPort = CommPortSlider.Value
  284.   'Open the port.
  285.   Comm.PortOpen = True
  286.   Comm.Output = "ATDT " + PhoneNumberLabel.Caption + Chr(13) + Chr(10)
  287.     If Comm.InBufferCount > 0 Then
  288.       If Not InputReceived Then
  289.         UpdateStatus "Receiving Time Data..."
  290.       End If
  291.         
  292.       InputReceived = True
  293.       Tmp = Comm.Input
  294.         
  295.       For I = 1 To Len(Tmp)
  296.         Ch = Mid(Tmp, I, 1)
  297.           
  298.         If (Ch = Chr(13)) Then
  299.           Line = Trim(Line)
  300.             
  301.           Line = UCase(Line)
  302.           
  303.           'Loop on error.
  304.           If InStr(Line, "BUSY") <> 0 Or InStr(Line, "NO CARRIER") Then
  305.             TerminalWindowListBox.AddItem (Line)
  306.             HangUp
  307.             SetClock = False
  308.             Exit Function
  309.           End If
  310.             
  311.           TimeUpdated = UpdateTime(Line)
  312.           TerminalWindowListBox.AddItem (Line)
  313.           TerminalWindowListBox.TopIndex = TerminalWindowListBox.ListCount - 1
  314.           Line = ""
  315.         Else
  316.           If (Ch <> Chr(10)) Then
  317.             Line = Line + Ch
  318.           End If
  319.         End If
  320.       Next
  321.     End If
  322.     Status = DoEvents()
  323.   Loop
  324.   HangUp
  325.   SetClock = TimeUpdated
  326. End Function
  327. Private Sub UpdateLocalTimeLabel()
  328.   Dim Sign As String * 1
  329.   Dim USATimeZone As String
  330.   If LocalTimeSlider.Value < 0 Then
  331.     Sign = "-"
  332.   Else
  333.     Sign = "+"
  334.   End If
  335.   Select Case LocalTimeSlider.Value
  336.     Case -7 * 10: USATimeZone = " (USA Mountain Time Zone)"
  337.     Case -6 * 10: USATimeZone = " (USA Central Time Zone)"
  338.     Case -5 * 10: USATimeZone = " (USA Eastern Time Zone)"
  339.   End Select
  340.   LocalTimeLabel.Caption = "&Local Time = UT " + Sign + Format(LocalTimeSlider.Value / 10#, "#0.0") + USATimeZone
  341. End Sub
  342. Private Sub UpdateQuitLabel()
  343.   QuitLabel = "&Quit after " + Trim(Str(QuitSlider.Value)) + " unsuccessful attempts"
  344. End Sub
  345. Sub UpdateStatus(ByVal Text As String)
  346.   Caption = ProgramLongName
  347.   If Len(Text) > 0 Then
  348.     Caption = Caption + " - " + Text
  349.   End If
  350. End Sub
  351. Private Function UpdateTime(ByVal Line As String) As Boolean
  352.   Dim Numbers(11) As Long
  353.   Dim I As Integer
  354.   Dim Number As String
  355.   Dim Ch As String * 1
  356.   Dim Index As Integer
  357.   Dim AllNumbersFound As Boolean
  358.   Dim DST As Boolean
  359.   Dim TimeCorrection As Integer
  360.   Dim AtomicDate As Date
  361.   Dim AtomicTime As Date
  362.   Dim LocalDate As Date
  363.   If IsDigit(Mid(Line, 1, 1)) Then
  364.     For I = 1 To Len(Line)
  365.       Ch = Mid(Line, I, 1)
  366.       
  367.       If IsDigit(Ch) Then
  368.         Number = Number + Ch
  369.       Else
  370.         Numbers(Index) = Val(Number)
  371.         Index = Index + 1
  372.         Number = ""
  373.         
  374.         If Index = 11 Then
  375.           AllNumbersFound = True
  376.           Exit For
  377.         End If
  378.       End If
  379.     Next I
  380.   End If
  381.   If AllNumbersFound Then
  382.     HangUp
  383.     TimeCorrection = LocalTimeSlider.Value \ 10
  384.     AtomicDate = DateSerial(1900 + Numbers(1), Numbers(2), Numbers(3))
  385.     AtomicTime = TimeSerial(Numbers(4), Numbers(5), Numbers(6))
  386.     AtomicDate = AtomicDate + AtomicTime
  387.     LocalDate = AtomicDate + (TimeCorrection / 24#)
  388.     'Adjust for Daylight Savings if necessary.
  389.     If DaylightSavingsCheckBox.Value Then
  390.       
  391.       Select Case Numbers(7)
  392.         Case 0:         DST = False
  393.         Case 1:         DST = Hour(LocalDate) < 2
  394.         Case 2 To 49:   DST = True
  395.         Case 50:        DST = True
  396.         Case 51:        DST = Hour(LocalDate) >= 2
  397.         Case 52 To 99:  DST = False
  398.       End Select
  399.             
  400.       If DST Then
  401.         LocalDate = LocalDate + 1# / 24#
  402.       End If
  403.     End If
  404.     SetDateAndTime LocalDate
  405.   End If
  406.   UpdateTime = AllNumbersFound
  407. End Function
  408. Private Sub UpdateCommPortLabel()
  409.   CommPortLabel.Caption = "&Comm Port (" + Trim(Str(CommPortSlider.Value)) + "):"
  410. End Sub
  411. Private Sub AboutBtn_Click()
  412.   Dim NewLine As String
  413.   Dim Msg As String
  414.   NewLine = Chr(10) + Chr(13)
  415.   Msg = "Atomic Clock sets your computer's clock using the National Institute of Standards and Technology's atomic clock located in Boulder, Colorado, USA" + NewLine + NewLine + "Written by Eric Bergman-Terrell" + NewLine + NewLine + "This program is FREEWARE."
  416.   MsgBox Msg, vbInformation, "About Atomic Clock v. 1.01"
  417. End Sub
  418. Private Sub CommPortSlider_Change()
  419.   UpdateCommPortLabel
  420. End Sub
  421. Private Sub ExitBtn_Click()
  422.   Unload MainForm
  423. End Sub
  424. Private Sub Form_Load()
  425.   Dim Port As Integer
  426.   Dim I As Integer
  427.   Dim Value As Integer
  428.   Dim TimeZone As Integer
  429.   Dim NumAttempts As Integer
  430.   CenterForm Me
  431.   UpdateStatus ""
  432.   Port = GetSetting(ProgramName, Settings, CommPort, 1)
  433.   If Port < CommPortSlider.Min Or Port > CommPortSlider.Max Then
  434.     Port = 1
  435.   End If
  436.   CommPortSlider.Value = Port
  437.   UpdateCommPortLabel
  438.   PrefixEdit.Text = GetSetting(ProgramName, Settings, Prefix, "1-(303)")
  439.   TimeZone = GetSetting(ProgramName, Settings, LocalTimeZone, -7 * 10)
  440.   If TimeZone < LocalTimeSlider.Min Or TimeZone > LocalTimeSlider.Max Then
  441.     TimeZone = -7 * 10
  442.   End If
  443.   LocalTimeSlider.Value = TimeZone
  444.   UpdateLocalTimeLabel
  445.   If GetSetting(ProgramName, Settings, USADaylightSavings, True) Then
  446.     DaylightSavingsCheckBox.Value = 1
  447.   End If
  448.   NumAttempts = Val(GetSetting(ProgramName, Settings, Attempts))
  449.   If NumAttempts < QuitSlider.Min Or NumAttempts > QuitSlider.Max Then
  450.     NumAttempts = 10
  451.   End If
  452.   QuitSlider.Value = NumAttempts
  453.   UpdateQuitLabel
  454. End Sub
  455. Private Sub Form_UnLoad(Cancel As Integer)
  456.   HangUp
  457.   SaveSetting ProgramName, Settings, CommPort, CommPortSlider.Value
  458.   SaveSetting ProgramName, Settings, LocalTimeZone, LocalTimeSlider.Value
  459.   SaveSetting ProgramName, Settings, USADaylightSavings, DaylightSavingsCheckBox.Value
  460.   SaveSetting ProgramName, Settings, Attempts, QuitSlider.Value
  461.   End
  462. End Sub
  463. Private Sub HangupBtn_Click()
  464.   HangUp
  465. End Sub
  466. Private Sub HelpBtn_Click()
  467.   Dim NewLine As String
  468.   Dim Msg As String
  469.   NewLine = Chr(10) + Chr(13)
  470.   Msg = "To set your computer's clock:" + NewLine + NewLine + "1.  Specify your modem's Comm Port." + NewLine + NewLine + "2.  Enter the appropriate Phone Number Prefix.  Users outside of the (303) area code should enter a Phone Number Prefix of 1-(303)." + NewLine + NewLine + "3.  Specify your Local Time (number of hours that your local time differs from UT.)" + NewLine + NewLine + "4.  Specify whether or not to use USA Daylight Savings Time." + NewLine + NewLine + "5.  Specify the number of attempts to make before quitting." + NewLine + NewLine + "6.  Press Set Clock" + NewLine + NewLine + "After setting your computer's clock, Atomic Clock will automitically hang up the connection.  Press Hangup to terminate the connection immediately."
  471.   MsgBox Msg, vbInformation, "Atomic Clock Help"
  472. End Sub
  473. Private Sub LocalTimeSlider_Change()
  474.   Dim NewValue As Integer
  475.   NewValue = (LocalTimeSlider.Value \ 5) * 5
  476.   If NewValue <> CommPortSlider.Value Then
  477.     LocalTimeSlider.Value = NewValue
  478.   End If
  479.   UpdateLocalTimeLabel
  480. End Sub
  481. Private Sub PrefixEdit_Change()
  482.   PhoneNumberLabel = PrefixEdit.Text + "494-4774"
  483.   SaveSetting ProgramName, Settings, Prefix, PrefixEdit.Text
  484. End Sub
  485. Private Sub QuitSlider_Change()
  486.   UpdateQuitLabel
  487. End Sub
  488. Private Sub SetClockBtn_Click()
  489.   Dim NumAttempts As Integer
  490.   Dim Success As Boolean
  491.     NumAttempts = NumAttempts + 1
  492.     AttemptsLabel = "Attempt " + Trim(Val(NumAttempts)) + " of " + Trim(Val(QuitSlider.Value))
  493.     Success = SetClock
  494.   Loop Until Success Or NumAttempts >= QuitSlider.Value
  495.   If Not Success Then
  496.     AttemptsLabel = "Failed to connect in " + Trim(Val(QuitSlider.Value)) + " attempts"
  497.   End If
  498. End Sub
  499.