Share |

Building GPS applications using Visual Basic

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

The Eye4Software GPS toolkit allows software developers to add GPS functionality to their own programs without the need to have any knowledge on serial communications and GPS protocols like RS-232 and NMEA0183.

The product can be used in many programming environments, such as 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.

Prerequisites

First you must have Visual Basic and the Eye4Software GPS Component installed on your computer. We will use Visual Basic 6.0 in this document, but other versions from 6.0 and up can also be used. You can download the Eye4Software GPS Component here.

Creating the project

Start the Visual Basic IDE, and select the "New Project" option from the "File" menu. The "New Project" dialog now appears, select the "Standard EXE" option to generate a Windows GUI application.

Visual Basic GPS Demo

Add a reference to the ActiveX component

In order to declare and create the objects from your Visual Basic application, you need to add a reference to the ActiveX object, by choosing the "References" option from the "Project" menu. A list of components installed on the system is displayed. Just check the checkbox in front of the "Eye4Software GPS Toolkit 2.2" and click "OK".

Visual Basic GPS Demo

Declare and create the object(s)

After adding the reference to the control, you can declare the objects like this:

Private objGps          As Gps
Private objGpsConstants As GpsConstants

The objects can be created in your code by double clicking on the Form you created, this will open the Form_Load handler. Paste the following code in this function:

Set objGps          = CreateObject("Eye4Software.Gps")
Set objGpsConstants = CreateObject("Eye4Software.GpsConstants")

The source code

Below you can find the sourcecode from the Visual Basic demo as shipped with the project.

Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" 
(ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

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

Private objGps As Gps
Private objGpsConstants As GpsConstants

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

Private bStarted As Boolean

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

Private Sub SetDefaultLogFile()
Dim Buffer As String
Buffer = Space(260)

If GetTempPath(260, Buffer) <> 0 Then
    TextLogFile.Text = Left$(Buffer, InStr(Buffer, vbNullChar) - 1) & "GpsLog.txt"
Else
    TextLogFile.Text = "C:\GpsLog.txt"
End If
End Sub

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

Private Sub CommandStart_Click()
    objGps.DeviceBaudrate = CLng(ComboBaudrate.Text)
    objGps.DeviceSerialPort = ComboDevice.ListIndex + 1
    objGps.EnableChecksum = CheckCRC.Value
    objGps.DeviceTimeout = 2000
    objGps.LogGpsData = CheckLogData.Value
    objGps.LogFile = TextLogFile.Text
    
    objGps.UnitsAltitude = objGpsConstants.GPS_ALTITUDE_FEET
    objGps.UnitsSpeed = objGpsConstants.GPS_SPEED_KNOTS
    objGps.LatLonStringFormat = objGpsConstants.GPS_LATLONFORMAT_DM
        
    objGps.Open
    
    If (DisplayLastResult() = 0) Then
        bStarted = True
    End If
    
    EnableControls
End Sub

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

Private Sub CommandStop_Click()
    objGps.Close
    
    bStarted = False
    
    EnableControls
End Sub

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

Public Function FileExists(sFileName As String) As Boolean
  FileExists = CBool(Len(Dir$(sFileName))) And CBool(Len(sFileName))
End Function

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

Private Sub CommandView_Click()
    If FileExists(TextLogFile.Text) = True Then
        Shell "notepad " + TextLogFile.Text, vbNormalFocus
    End If
End Sub

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

Private Sub Form_Load()

    Set objGps = CreateObject("Eye4Software.Gps")
    Set objGpsConstants = CreateObject("Eye4Software.GpsConstants")
    
    bStarted = False
    
    For i = 1 To 16
        ComboDevice.AddItem ("COM" & i)
    Next
    
    ComboDevice.ListIndex = 0
    
    ComboBaudrate.AddItem ("1200")
    ComboBaudrate.AddItem ("2400")
    ComboBaudrate.AddItem ("4800")
    ComboBaudrate.AddItem ("9600")
    ComboBaudrate.AddItem ("19200")
    ComboBaudrate.AddItem ("38400")
    ComboBaudrate.AddItem ("57600")
    ComboBaudrate.AddItem ("115200")
    
    ComboBaudrate.ListIndex = 2
    
    EnableControls
    
    SetDefaultLogFile
End Sub

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

Private Function DisplayLastResult()
    DisplayLastResult = objGps.LastError
    TextResult = objGps.LastError & " ( " & objGps.LastErrorDescription & " )"
End Function

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

Private Sub EnableControls()
    CommandStart.Enabled = Not bStarted
    CommandStop.Enabled = bStarted
    ComboBaudrate.Enabled = Not bStarted
    ComboDevice.Enabled = Not bStarted
    CheckCRC.Enabled = Not bStarted
    TextLatitude.Enabled = bStarted
    TextLongitude.Enabled = bStarted
    TextAltitude.Enabled = bStarted
    TextCourse.Enabled = bStarted
    TextSats.Enabled = bStarted
    TextTime.Enabled = bStarted
    TextFix.Enabled = bStarted
    TextSpeed.Enabled = bStarted
End Sub

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

Private Sub Form_Unload(Cancel As Integer)
    CommandStop_Click
End Sub

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

Private Sub Frame1_DragDrop(Source As Control, X As Single, Y As Single)

End Sub

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

Private Sub Timer1_Timer()
    If (bStarted = True) Then
       
        TextLatitude.Text = objGps.gpsLatitudeString
        TextLongitude.Text = objGps.gpsLongitudeString
        TextSpeed.Text = objGps.gpsSpeed & " knots"
        TextCourse.Text = objGps.gpsCourse & " degrees"
        TextSats.Text = objGps.gpsSatellites
        TextAltitude.Text = objGps.gpsAltitude & " ft"
        TextTime.Text = objGps.gpsTimeString
        TextFix.Text = objGps.gpsQuality
        
        DisplaySatellites
    End If
End Sub

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

Private Sub DisplaySatellites()
    Dim objSat As GpsSatelliteInfo
    Dim itemSat As ListItem
    
    ListViewSats.Visible = False
    
    ListViewSats.ListItems.Clear
      
    On Error Resume Next
    Set objSat = objGps.GetFirstSatellite
    On Error GoTo 0
    
    While (objGps.LastError = 0)
        If (objSat.UsedForFix = True) Then
            Set itemSat = ListViewSats.ListItems.Add
        
            itemSat.Text = objSat.ID
            itemSat.SubItems(1) = objSat.Elevation
            itemSat.SubItems(2) = objSat.Azimuth
            itemSat.SubItems(3) = objSat.SignalNoiseRatio
        End If
        
        On Error Resume Next
        Set objSat = objGps.GetNextSatellite
        On Error GoTo 0
    Wend
    
    ListViewSats.Visible = True
    
End Sub

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