Share |

Synchronize your computer's system time using GPS

Download Eye4Software GPS Toolkit free trial Download the Eye4Software GPS Toolkit fully functional 30 day trial version for free
Browse through the Eye4Software GPS Toolkit for Windows manual Browse through the Eye4Software GPS Toolkit manual

Introduction

Today most computer clocks are synchronized using Internet time servers (Network Time Protocol) or using a central clock on a domain server. In some cases however there is no Internet or other network connection available, for instance, when the computer is situated at a very remote location or the computer may not access the Internet because of security reasons.

In these cases you could use GPS to perform the time synchronization. GPS has the following advantages:

  • GPS units are very cheap these days;
  • GPS is available 24 hours a day, 7 seven days a week;
  • GPS is available at any place, even on very remote locations.

Timing is essential for the GPS system, so it uses a very precise atomic clock. All GPS units that receive the GPS signal are synchronized with this clock. By connecting a GPS unit to your computer, you can retrieve this time information from the GPS.

Eye4Software GPS Toolkit

Using the Eye4Software GPS Toolkit, it is possible to integrate GPS functionality into your programs or scripts within the blink of an eye. You are not supposed to have any knowledge on GPS protocols, serial communication and GPS operation. The product can be used in many programming environments, including Visual Basic, Visual C++, Visual Studio.Net, Borland C++ Builder, Borland Delphi and VBA, but also web oriented applications such as ASP, ASP.NET and PHP, and all other programming environments that support ActiveX. For this time synschronization sample we will use VBScript, it is an easy to learn scripting language, and you can run it directly from the command prompt or Windows Task Scheduler.

Prerequisites

First you must have a VB Script editor and the Eye4Software GPS Component installed on your computer. If you do not have a dedicated VBScript editor, Notepad will do. You can download a free 30 day trial version of the Eye4Software GPS Component here.

Source code

Below you can find the complete source code of the time synchronization script, you can run it from the command prompt or Window Task Scheduler. Using Task Scheduler you can synchronize your computer's system times a few times a day without user intervention.

When using Task Scheduler, execute the command like this: cscript "<path to scriptfile>", and make sure you start the task with the correct user privileges. When using Windows Vista, Windows Seven or Windows Server 2008, and UAC (User Account Control) is enabled, make sure you also check the "Run with highest privileges" checkbox.

Dim objGps, objShell, n, nOffset, strCommand

' Create the GPS object
Set objGps   = CreateObject ( "Eye4Software.Gps" )

' Create the Shell object
Set objShell = CreateObject ( "WScript.Shell" )

' Set LogFile
objGps.LogFile            = "C:\GpsTime.log"

' Set Serial Port ( COM4 )
objGps.DeviceSerialPort = 4

' Set Serial Speed ( 4800 bps )
objGps.DeviceBaudrate	= 4800

' Set your license code here
objGps.RegistrationCode = "XXXXXXXXXXXXXXXX"

' Set GMT offset ( +1 )
nOffset = 1

' Try to open the serial port, quit in case of failure
objGps.Open 

If ( objGps.LastError <> 0 ) Then
	WScript.Echo "Failed to open serial port: " & objGps.LastErrorDescription
	WScript.Quit
End If

' Read Gps data until time is received ( wait for max 10 seconds )
For n = 0 To 100
	If ( objGps.gpsTime <> 0 ) Then
		strCommand = "cmd /k " & Chr ( 34 ) & "time " & EpochTimeToString ( objGps.gpsTime ) & Chr ( 34 )
		Exit For
	End If

	WScript.Sleep ( 100 )
Next

' Execute the DOS time command
If ( strCommand <> "" ) Then
	WScript.Echo "Setting new time..."
	objShell.Exec strCommand
End If

' Close Serial Port
objGps.Close

WScript.Quit

'//////////////////////////////////////////////////////////////////////////////////

Function EpochTimeToString ( Seconds )   
  Dim intHours, intMins, intSecs     
 
  Seconds = Seconds + ( nOffset * 60 * 60 )
   
  intHours = ( Seconds Mod 86400 ) / 3600    
  intMins  = ( Seconds Mod 3600 )  / 60    
  intSecs  = ( Seconds Mod 60 )       
  
  EpochTimeToString = TimeSerial (intHours, intMins, intSecs)
End Function

'//////////////////////////////////////////////////////////////////////////////////