home *** CD-ROM | disk | FTP | other *** search
Wrap
' ************************************************************************** ' **** ' **** GpsReceiver.NET Global Positioning SDK Example 1 ' **** A demonstration of GpsReceiver.NET's major features. ' **** ' **** For assistance with your GPS device or for troubleshooting: ' **** ' **** http://www.gpsdotnet.com/support ' **** support@gpsdotnet.com ' **** ' **** To view known product issues & updates in the Knowledge Base: ' **** ' **** http://www.gpsdotnet.com/kb ' **** ' **** For pricing information & online ordering: ' **** ' **** http://www.gpsdotnet.com/purchase ' **** ' **** This source code is considered public domain and may be re-used ' **** in your own applications. ' **** ' ************************************************************************* ' **** ' **** NOTE: Since GpsReceiver.NET is multi-threaded, the Invoke() method is ' **** used frequently to make sure that updates to form controls occur ' **** on the form's thread. This prevents the application from locking. ' **** up, especially for Compact Framework apps. For more information, ' **** refer to the Knowledge Base. ' **** ' ************************************************************************* Imports StormSource.Gps Public Class MainForm #Region " Starting the GPS Service " Private Sub StartButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartButton.Click ' Set the COM port to the numeric portion of the selected item in to combo box GpsReceiver.Device.Settings.ComPort = ComPortComboBox.Value GpsReceiver.Device.Settings.BaudRate = BaudRateComboBox.Value ' NOTE: It's important to add error checking around the Start() method and Enabled property Try ' Set the license key (which will suppress the trial "nag" screen) ' GpsReceiver.SetLicenseKey("[insert your license here]") ' Start receiving messages GpsReceiver.Start() Catch ex As UnauthorizedAccessException ' The trial screen was cancelled MessageBox.Show(ex.ToString, "Cannot Continue Trial", MessageBoxButtons.OK, MessageBoxIcon.Error) Catch ex As GpsException ' Notify of the error MessageBox.Show(ex.ToString, "Unable to Begin GPS Communications", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End Try End Sub ' Raised just before a connection attempt is made Private Sub GPS_Connecting(ByVal sender As Object, ByVal e As DeviceSettingsEventArgs) Handles GpsReceiver.Connecting Me.Invoke(New StatusBarEventHandler(AddressOf UpdateStatusBar), "Connecting to GPS device...") End Sub Private Sub GPS_Connected(ByVal sender As Object, ByVal e As System.EventArgs) Handles GpsReceiver.Connected Me.Invoke(New StatusBarEventHandler(AddressOf UpdateStatusBar), "Connected to GPS device, please wait...") ' Enable the Stop button, disable the Start button Me.Invoke(New EventHandler(AddressOf EnableStopButton)) End Sub ' Invoked from the GpsReceiver.NET thread to enable the stop button Private Sub EnableStopButton(ByVal sender As Object, ByVal e As System.EventArgs) StopButton.Enabled = True StartButton.Enabled = False BaudRateComboBox.Enabled = False ComPortComboBox.Enabled = False End Sub Private Sub GPS_Acquiring(ByVal sender As Object, ByVal e As System.EventArgs) Handles GpsReceiver.Acquiring Me.Invoke(New StatusBarEventHandler(AddressOf UpdateStatusBar), "Please wait while satellite information is gathered...") End Sub ' Raised when the receiver has adjusted the baud rate in an attempt to receive GPS information Private Sub GPS_BaudRateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GpsReceiver.BaudRateChanged Select Case GpsReceiver.Device.Settings.BaudRate Case 4800 Me.BaudRateComboBox.SelectedIndex = 0 Case 9600 Me.BaudRateComboBox.SelectedIndex = 1 End Select End Sub Private Sub GPS_Timeout(ByVal sender As Object, ByVal e As System.EventArgs) Handles GpsReceiver.Timeout MessageBox.Show("The GPS device did not respond to commands, even after attempts to detect the correct connection settings. The device may be powered off. Try restarting the Receiver.", "GPS Device Timeout", MessageBoxButtons.OK, MessageBoxIcon.Hand) End Sub #End Region #Region " Stopping the GPS Service " Private Sub StopButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopButton.Click GpsReceiver.Stop() End Sub ' Raised just before an attempt is made to disconnect from the GPS device Private Sub GPS_Disconnecting(ByVal sender As Object, ByVal e As DeviceSettingsEventArgs) Handles GpsReceiver.Disconnecting Me.Invoke(New StatusBarEventHandler(AddressOf UpdateStatusBar), "Disconnecting from GPS device...") End Sub ' Raised when the connection with the device is now closed Private Sub GPS_Disconnected(ByVal sender As Object, ByVal e As System.EventArgs) Handles GpsReceiver.Disconnected Me.Invoke(New StatusBarEventHandler(AddressOf UpdateStatusBar), "Disconnected from GPS device.") ' Reset start & stop buttons Me.Invoke(New EventHandler(AddressOf EnableStartButton)) End Sub ' Invoked from the GpsReceiver.NET thread to enable the stop button Private Sub EnableStartButton(ByVal sender As Object, ByVal e As System.EventArgs) StartButton.Enabled = True PowerOffButton.Enabled = False DownloadWaypointButton.Enabled = False EditWaypointButton.Enabled = False NewWaypointButton.Enabled = False StopButton.Enabled = False BaudRateComboBox.Enabled = True ComPortComboBox.Enabled = True End Sub #End Region #Region " Demonstration of Motion-Related GPS Events " ' Updates the current speed Private Sub UpdateSpeed(ByVal sender As Object, ByVal e As SpeedEventArgs) ' Are we using the Imperial or Metric system? If System.Globalization.RegionInfo.CurrentRegion.IsMetric Then ' Use the Metric system Speed.Text = e.Speed.ToKilometersPerHour.ToString Else ' Use the Imperial system Speed.Text = e.Speed.ToStatuteMilesPerHour.ToString End If End Sub ' Updates the current altitude Private Sub UpdateAltitude(ByVal sender As Object, ByVal e As DistanceEventArgs) ' Are we using the Imperial or Metric system? If System.Globalization.RegionInfo.CurrentRegion.IsMetric Then ' Use the metric system Altitude.Text = e.Distance.ToMeters.ToString Else ' Use the Imperial system Altitude.Text = e.Distance.ToFeet.ToString End If End Sub ' Updates the current bearing Private Sub UpdateBearing(ByVal sender As Object, ByVal e As AzimuthEventArgs) ' Update the bearing Bearing.Text = e.Azimuth.ToString ' And show the current bearing represented as a cardinal (compass) direction Direction.Text = e.Azimuth.CardinalDirection.ToString End Sub ' Updates the currentposition (in UTM) Private Sub UpdateUtmPosition(ByVal sender As Object, ByVal e As UtmPositionEventArgs) ' Update the current position (in UTM) UtmPosition.Text = e.UtmPosition.ToString End Sub ' Updates the current location Private Sub UpdatePosition(ByVal sender As Object, ByVal e As PositionEventArgs) ' Update the current latitude Latitude.Text = e.Position.Latitude.ToString ' Update the longitude Longitude.Text = e.Position.Longitude.ToString End Sub ' Updates the current magnetic variation Private Sub UpdateMagneticVariation(ByVal sender As Object, ByVal e As LongitudeEventArgs) MagneticVariation.Text = e.Longitude.ToString End Sub Private Sub GPS_AltitudeChanged(ByVal sender As Object, ByVal e As DistanceEventArgs) Handles GpsReceiver.AltitudeChanged Me.Invoke(New StatusBarEventHandler(AddressOf UpdateStatusBar), "Altitude has changed.") ' Update the current altitude Me.Invoke(New DistanceEventHandler(AddressOf UpdateAltitude), sender, e) End Sub Private Sub GPS_BearingChanged(ByVal sender As Object, ByVal e As AzimuthEventArgs) Handles GpsReceiver.BearingChanged Me.Invoke(New StatusBarEventHandler(AddressOf UpdateStatusBar), "Bearing has changed.") ' Update the bearing and compass direction Me.Invoke(New AzimuthEventHandler(AddressOf UpdateBearing), sender, e) End Sub Private Sub GPS_SpeedChanged(ByVal sender As Object, ByVal e As SpeedEventArgs) Handles GpsReceiver.SpeedChanged Me.Invoke(New StatusBarEventHandler(AddressOf UpdateStatusBar), "Speed has changed.") ' Update the speed Me.Invoke(New SpeedEventHandler(AddressOf UpdateSpeed), sender, e) End Sub Private Sub GPS_PositionChanged(ByVal sender As Object, ByVal e As PositionEventArgs) Handles GpsReceiver.PositionChanged Me.Invoke(New StatusBarEventHandler(AddressOf UpdateStatusBar), "Position has changed.") ' Update the labels with the new position Me.Invoke(New PositionEventHandler(AddressOf UpdatePosition), sender, e) End Sub ' Raised when UTM coordinates have been reported from the GPS device Private Sub GPS_UtmPositionChanged(ByVal sender As Object, ByVal e As StormSource.Gps.UtmPositionEventArgs) Handles GpsReceiver.UtmPositionChanged Me.Invoke(New StatusBarEventHandler(AddressOf UpdateStatusBar), "UTM position has changed.") ' Update the UTM position Me.Invoke(New UtmPositionEventHandler(AddressOf UpdateUtmPosition), sender, e) End Sub #End Region #Region " Demonstration of Precision-Related Events " ' Updates the form with dilution of precision (accuracy) information Private Sub UpdateMeanDilutionOfPrecision(ByVal sender As Object, ByVal e As DilutionOfPrecisionEventArgs) ' Update the overall (mean) accuracy MeanDilutionOfPrecision.Text = e.DilutionOfPrecision.ToString End Sub ' Updates the form with dilution of precision (accuracy) information Private Sub UpdateHorizontalDilutionOfPrecision(ByVal sender As Object, ByVal e As DilutionOfPrecisionEventArgs) ' Update the accuract of latitude/longitude measurements HorizontalDilutionOfPrecision.Text = e.DilutionOfPrecision.ToString End Sub ' Updates the form with dilution of precision (accuracy) information Private Sub UpdateVerticalDilutionOfPrecision(ByVal sender As Object, ByVal e As DilutionOfPrecisionEventArgs) ' Update the accuracy of altitude measurements VerticalDilutionOfPrecision.Text = e.DilutionOfPrecision.ToString End Sub ' Raised when the accuracy of latitude/longitude measurements has changed Private Sub GPS_HorizontalDilutionOfPrecisionChanged(ByVal sender As Object, ByVal e As DilutionOfPrecisionEventArgs) Handles GpsReceiver.HorizontalDilutionOfPrecisionChanged Me.Invoke(New StatusBarEventHandler(AddressOf UpdateStatusBar), "Lat/long accuracy has changed to " & e.DilutionOfPrecision.ToString) ' Update the accuracy information Invoke(New DilutionOfPrecisionEventHandler(AddressOf UpdateHorizontalDilutionOfPrecision), sender, e) End Sub ' Raised when the mean accuracy in the current latitude/longitude has changed Private Sub GPS_MeanDilutionOfPrecisionChanged(ByVal sender As Object, ByVal e As DilutionOfPrecisionEventArgs) Handles GpsReceiver.MeanDilutionOfPrecisionChanged Me.Invoke(New StatusBarEventHandler(AddressOf UpdateStatusBar), "General accuracy has changed to " & e.DilutionOfPrecision.ToString) ' Update the accuracy information Invoke(New DilutionOfPrecisionEventHandler(AddressOf UpdateMeanDilutionOfPrecision), sender, e) End Sub ' Raised when the confidence level of altitude measurements has changed Private Sub GPS_VerticalDilutionOfPrecisionChanged(ByVal sender As Object, ByVal e As DilutionOfPrecisionEventArgs) Handles GpsReceiver.VerticalDilutionOfPrecisionChanged Me.Invoke(New StatusBarEventHandler(AddressOf UpdateStatusBar), "Altitude accuracy has changed to " & e.DilutionOfPrecision.ToString) ' Update the accuracy information Invoke(New DilutionOfPrecisionEventHandler(AddressOf UpdateVerticalDilutionOfPrecision), sender, e) End Sub Private Sub GPS_MagneticVariationChanged(ByVal sender As Object, ByVal e As LongitudeEventArgs) Handles GpsReceiver.MagneticVariationChanged Me.Invoke(New StatusBarEventHandler(AddressOf UpdateStatusBar), "Magnetic variation has changed") ' Update the magnetic variation Invoke(New LongitudeEventHandler(AddressOf UpdateMagneticVariation), sender, e) End Sub Private Sub GPS_CrossTrackErrorChanged(ByVal sender As Object, ByVal e As StormSource.Gps.CrossTrackErrorEventArgs) Handles GpsReceiver.CrossTrackErrorChanged ' Update the cross-track error Me.Invoke(New CrossTrackErrorEventHandler(AddressOf UpdateCrossTrackError), sender, e) End Sub #End Region #Region " Demonstration of Exception Handling " Public Sub UpdateErrors(ByVal sender As Object, ByVal e As ErrorEventArgs) ' Add the error to the top of the list GpsExceptionTextBox.Text = e.Exception.ToString & vbCrLf & GpsExceptionTextBox.Text End Sub Public Sub UpdateParsingErrors(ByVal sender As Object, ByVal e As ParsingErrorEventArgs) ' Add the error to the top of the list ParsingExceptionTextBox.Text = e.ParsingException.ToString & vbCrLf & ParsingExceptionTextBox.Text End Sub ' NOTE: This event is raised for errors which occur on separate threads within the GpsReceiver.NET engine. ' Use Try..Catch blocks to handle typical GPS errors via the GPSException class. Private Sub GPS_ErrorOccurred(ByVal sender As Object, ByVal e As ErrorEventArgs) Handles GpsReceiver.ErrorOccurred Me.Invoke(New ErrorEventHandler(AddressOf UpdateErrors), sender, e) End Sub ' This event is raised when invalid or corrupt data is received by the device. This event only ' serves as a warning that some data was not 100% understood. These ' errors are not typically forwarded to the user. Private Sub GPS_ParsingErrorOccurred(ByVal sender As Object, ByVal e As ParsingErrorEventArgs) Handles GpsReceiver.ParsingErrorOccurred Me.Invoke(New ParsingErrorEventHandler(AddressOf UpdateParsingErrors), sender, e) End Sub #End Region #Region " Demonstration of Fix-Related Events " Public Sub UpdateFixInformation(ByVal sender As Object, ByVal e As System.EventArgs) If GpsReceiver.IsFixObtained Then FixObtained.Text = "Fix Obtained" Else FixObtained.Text = "No Fix Available" End If FixMethod.Text = GpsReceiver.FixMethod.ToString FixMode.Text = GpsReceiver.FixMode.ToString FixQuality.Text = GpsReceiver.FixQuality.ToString FixLikelihood.Text = GpsReceiver.FixLikelihood.ToString End Sub Private Sub GpsReceiver_FixLikelihoodChanged(ByVal sender As Object, ByVal e As StormSource.Gps.FixLikelihoodEventArgs) Handles GpsReceiver.FixLikelihoodChanged Me.Invoke(New StatusBarEventHandler(AddressOf UpdateStatusBar), "Fix likelihood/stability has changed to " & e.FixLikelihood.ToString) ' Update the magnetic variation Invoke(New EventHandler(AddressOf UpdateFixInformation)) End Sub Private Sub GPS_FixLost(ByVal sender As Object, ByVal e As System.EventArgs) Handles GpsReceiver.FixLost Me.Invoke(New StatusBarEventHandler(AddressOf UpdateStatusBar), "Satellite fix has been lost.") ' Update the magnetic variation Invoke(New EventHandler(AddressOf UpdateFixInformation)) End Sub Private Sub GPS_FixMethodChanged(ByVal sender As Object, ByVal e As FixMethodEventArgs) Handles GpsReceiver.FixMethodChanged Me.Invoke(New StatusBarEventHandler(AddressOf UpdateStatusBar), "Satellite fix method has changed.") ' Update the magnetic variation Invoke(New EventHandler(AddressOf UpdateFixInformation)) End Sub Private Sub GPS_FixModeChanged(ByVal sender As Object, ByVal e As FixModeEventArgs) Handles GpsReceiver.FixModeChanged Me.Invoke(New StatusBarEventHandler(AddressOf UpdateStatusBar), "Satellite fix mode has changed.") ' Update the magnetic variation Invoke(New EventHandler(AddressOf UpdateFixInformation)) End Sub Private Sub GPS_FixObtained(ByVal sender As Object, ByVal e As System.EventArgs) Handles GpsReceiver.FixObtained Me.Invoke(New StatusBarEventHandler(AddressOf UpdateStatusBar), "Satellite fix has been obtained!") ' Update the magnetic variation Invoke(New EventHandler(AddressOf UpdateFixInformation)) End Sub Private Sub GPS_FixQualityChanged(ByVal sender As Object, ByVal e As FixQualityEventArgs) Handles GpsReceiver.FixQualityChanged Me.Invoke(New StatusBarEventHandler(AddressOf UpdateStatusBar), "Satellite fix quality has changed") ' Update the magnetic variation Invoke(New EventHandler(AddressOf UpdateFixInformation)) End Sub #End Region #Region " Demonstration of Satellite-Related GPS Events " Private Sub UpdateSatelliteCounts(ByVal sender As Object, ByVal e As SatelliteCountEventArgs) Select Case e.Type Case SatelliteCountType.ActiveCount ActiveSatellites.Text = e.Count Case SatelliteCountType.FixedCount FixedSatellites.Text = e.Count Case SatelliteCountType.TrackedCount TrackedSatellites.Text = e.Count End Select End Sub Private Sub UpdateSatellite(ByVal sender As Object, ByVal e As SatelliteEventArgs) With SatelliteList.Items(e.Satellite.Index) .Text = e.Satellite.PseudoRandomCode .SubItems(1).Text = e.Satellite.Elevation.ToString .SubItems(2).Text = e.Satellite.Azimuth.ToString .SubItems(3).Text = e.Satellite.SignalToNoiseRatio.ToString & " (" & e.Satellite.SignalToNoiseRatio.Rating.ToString & ")" If e.Satellite.IsFixObtained Then .BackColor = Color.FromArgb(60, 0, 255, 0) Else .BackColor = Color.FromArgb(60, 255, 0, 0) End If End With End Sub Private Sub GPS_SatelliteAzimuthChanged(ByVal sender As Object, ByVal e As SatelliteEventArgs) Handles GpsReceiver.SatelliteAzimuthChanged Invoke(New SatelliteEventHandler(AddressOf UpdateSatellite), sender, e) End Sub Private Sub GPS_SatelliteElevationChanged(ByVal sender As Object, ByVal e As SatelliteEventArgs) Handles GpsReceiver.SatelliteElevationChanged Invoke(New SatelliteEventHandler(AddressOf UpdateSatellite), sender, e) End Sub Private Sub GPS_SatelliteFixLost(ByVal sender As Object, ByVal e As SatelliteEventArgs) Handles GpsReceiver.SatelliteFixLost Invoke(New SatelliteEventHandler(AddressOf UpdateSatellite), sender, e) End Sub Private Sub GPS_SatelliteFixObtained(ByVal sender As Object, ByVal e As SatelliteEventArgs) Handles GpsReceiver.SatelliteFixObtained Invoke(New SatelliteEventHandler(AddressOf UpdateSatellite), sender, e) End Sub Private Sub GPS_SatellitePRCChanged(ByVal sender As Object, ByVal e As SatelliteEventArgs) Handles GpsReceiver.SatellitePseudoRandomCodeChanged Invoke(New SatelliteEventHandler(AddressOf UpdateSatellite), sender, e) End Sub Private Sub GPS_SatelliteSignalToNoiseRatioChanged(ByVal sender As Object, ByVal e As SatelliteEventArgs) Handles GpsReceiver.SatelliteSignalToNoiseRatioChanged Invoke(New SatelliteEventHandler(AddressOf UpdateSatellite), sender, e) End Sub ' Handles three events: SatelliteActiveCountChanged, SatelliteFixCountChanged, SatelliteTrackedCountChanged Private Sub GPS_SatelliteCountChanged(ByVal sender As Object, ByVal e As SatelliteCountEventArgs) Handles GpsReceiver.SatelliteFixedCountChanged, GpsReceiver.SatelliteActiveCountChanged, GpsReceiver.SatelliteTrackedCountChanged Invoke(New SatelliteCountEventHandler(AddressOf UpdateSatelliteCounts), sender, e) End Sub #End Region #Region " Demonstration of Raw Sentence Processing " ' Stores information about the current sentence Private CurrentSentence As String ' Returns True if the sentence was fully understood Private CurrentSentenceType As SentenceType ' Adds the specified sentence to the list Private Sub AddSentence(ByVal sender As Object, ByVal e As EventArgs) Select Case CurrentSentenceType Case SentenceType.Recognized ' The sentence was processed successfully RecognizedSentences.Items.Add(CurrentSentence) RecognizedSentences.SelectedIndex = RecognizedSentences.Items.Count - 1 ' Prevent too many lines from being in the list box If RecognizedSentences.Items.Count > 50 Then RecognizedSentences.Items.RemoveAt(0) Case SentenceType.Unrecognized ' The sentence contained some corrupt data, or was proprietary to a specific device UnrecognizedSentences.Items.Add(CurrentSentence) UnrecognizedSentences.SelectedIndex = UnrecognizedSentences.Items.Count - 1 ' Prevent too many lines from being in the list box If UnrecognizedSentences.Items.Count > 50 Then UnrecognizedSentences.Items.RemoveAt(0) End Select End Sub ' Handles two events: SentenceReceived and UnrecognizedSentenceReceived Private Sub GPS_SentenceReceived(ByVal sender As Object, ByVal e As SentenceEventArgs) Handles GpsReceiver.SentenceReceived, GpsReceiver.UnrecognizedSentenceReceived CurrentSentence = e.Sentence CurrentSentenceType = e.Type ' Now add the sentence Invoke(New EventHandler(AddressOf AddSentence)) End Sub ' Raised when someone clicks on the "Parse" button. Private Sub ParseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ParseButton.Click ' Parse the data which was entered into the text box Try GpsReceiver.Parse(ParsingText.Text) Catch ex As Exception Stop End Try End Sub #End Region #Region " Demonstration of GPS device date/time information " ' Updates the form with local date/time information Public Sub UpdateLocalTime(ByVal sender As Object, ByVal e As EventArgs) LocalTime.Text = Now.ToString End Sub ' Updates the form with GPS device's date/time information Public Sub UpdateGpsTime(ByVal sender As Object, ByVal e As DateTimeEventArgs) ' NOTE: GPS devices send time in UTC. We must convert it to local time UTCDateTime.Text = e.DateTime.ToLocalTime.ToString End Sub ' Update the local time to show the comparison between local time and the time obtained from the GPS receiver Private Sub LocalTimeTimer_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles LocalTimeTimer.Elapsed Invoke(New EventHandler(AddressOf UpdateLocalTime)) End Sub ' Raised whenever the GPS device reports a new date/time ' NOTE: Several devices stop sending this information as soon as a fix is obtained! Private Sub GPS_UTCDateTimeChanged(ByVal sender As Object, ByVal e As DateTimeEventArgs) Handles GpsReceiver.UtcDateTimeChanged Me.Invoke(New StatusBarEventHandler(AddressOf UpdateStatusBar), "GPS date/time has changed.") ' Update the magnetic variation Invoke(New DateTimeEventHandler(AddressOf UpdateGpsTime), sender, e) End Sub #End Region #Region " Demonstration of Waypoints " Private CurrentWaypoint As Waypoint ' Raised when the "Refresh" button is clicked on the Waypoints tab Private Sub DownloadWaypointButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DownloadWaypointButton.Click ' Retrieve the latest waypoints from the device (asynchronously) Try GpsReceiver.Waypoints.Refresh(True) Catch ex As GpsException MessageBox.Show(ex.ToString, "Unable to Load Waypoints", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1) End Try End Sub ' Raised when a new waypoint is selected Private Sub WaypointList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WaypointList.SelectedIndexChanged ' Enable the Edit button EditWaypointButton.Enabled = True End Sub ' Raised when the "New..." button is clicked on the Waypoints tab Private Sub NewWaypointButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewWaypointButton.Click ' Make a new waypoint form Dim WaypointForm As New NewWaypointForm ' Tell the form what kind of GPS device we're using WaypointForm.Device = GpsReceiver.Device ' Show the form WaypointForm.Show() End Sub ' Raised when the "Edit..." button is clicked on the Waypoints tab Private Sub EditWaypointButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditWaypointButton.Click ' Get the waypoint that was clicked Dim SelectedWaypoint As Waypoint = GpsReceiver.Waypoints(WaypointList.SelectedItems(0).Text) ' Make a new editor form Dim EditForm As New EditWaypointForm ' Tell this form to edit the selected waypoint EditForm.Waypoint = SelectedWaypoint ' Show the form EditForm.Show() End Sub ' Raised when a waypoint is double-clicked Private Sub WaypointList_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles WaypointList.DoubleClick ' Is anything selected? If WaypointList.SelectedItems.Count = 0 Then Exit Sub ' Open the selected item EditWaypointButton_Click(sender, e) End Sub Private Sub UpdateCrossTrackError(ByVal sender As Object, ByVal e As CrossTrackErrorEventArgs) ' Update the cross-track error Me.CrossTrackError.Text = e.CrossTrackError.ToString ' Update the recommended steering direction to correct the error Me.RecommendedSteeringDirection.Text = e.CrossTrackError.RecommendedSteeringDirection.ToString End Sub Private Sub UpdateOriginWaypoint(ByVal sender As Object, ByVal e As WaypointEventArgs) Me.OriginWaypoint.Text = e.Waypoint.ToString End Sub Private Sub UpdateDestinationWaypoint(ByVal sender As Object, ByVal e As WaypointEventArgs) Me.DestinationWaypoint.Text = e.Waypoint.ToString End Sub Private Sub UpdateClearWaypointList(ByVal sender As Object, ByVal e As EventArgs) DownloadWaypointButton.Enabled = False WaypointList.Items.Clear() End Sub Private Sub UpdateCompleteWaypointList(ByVal sender As Object, ByVal e As EventArgs) DownloadWaypointButton.Enabled = True End Sub ' Adds the specified sentence to the list Private Sub AddWaypoint(ByVal sender As Object, ByVal e As EventArgs) Dim WaypointListItem As ListViewItem ' Does this waypoint already exist in the list view? If WaypointItemExists(CurrentWaypoint.Name) Then ' Yes. Update the existing list view item WaypointListItem = GetWaypointItem(CurrentWaypoint.Name) WaypointListItem.SubItems(0).Text = CurrentWaypoint.Latitude.ToString WaypointListItem.SubItems(1).Text = CurrentWaypoint.Longitude.ToString WaypointListItem.SubItems(2).Text = CurrentWaypoint.Symbol.ToString WaypointListItem.SubItems(3).Text = CurrentWaypoint.Description Else ' No. Make a new list view item WaypointListItem = WaypointList.Items.Add(CurrentWaypoint.Name) WaypointListItem.SubItems.Add(CurrentWaypoint.Latitude.ToString) WaypointListItem.SubItems.Add(CurrentWaypoint.Longitude.ToString) WaypointListItem.SubItems.Add(CurrentWaypoint.Symbol.ToString) WaypointListItem.SubItems.Add(CurrentWaypoint.Description) End If End Sub ' Raised just before the collection of waypoints is re-downloaded Private Sub GPS_WaypointDownloadBegins(ByVal sender As Object, ByVal e As System.EventArgs) Handles GpsReceiver.WaypointDownloadBegins Invoke(New EventHandler(AddressOf UpdateClearWaypointList)) End Sub ' Returns True if the waypoint name currently exists in the WaypointList list view Private Function WaypointItemExists(ByVal name As String) As Boolean Dim WaypointListViewItem As ListViewItem For Each WaypointListViewItem In WaypointList.Items If WaypointListViewItem.Text = name Then Return True Next End Function ' Returns the waypoint list item associated with the specified waypoint name Private Function GetWaypointItem(ByVal name As String) As ListViewItem Dim WaypointListViewItem As ListViewItem For Each WaypointListViewItem In WaypointList.Items If WaypointListViewItem.Text = name Then Return WaypointListViewItem Next Return Nothing End Function ' Raised when one waypoint has been downloaded from the GPS device Private Sub GPS_WaypointDownloaded(ByVal sender As Object, ByVal e As StormSource.Gps.WaypointEventArgs) Handles GpsReceiver.WaypointDownloaded ' Set the current waypoint CurrentWaypoint = e.Waypoint ' And add it to the List View Invoke(New EventHandler(AddressOf AddWaypoint)) End Sub ' Raised when the last waypoint has been downloaded from the device Private Sub GPS_WaypointDownloadEnds(ByVal sender As Object, ByVal e As System.EventArgs) Handles GpsReceiver.WaypointDownloadEnds ' Re-enable the Refresh button Invoke(New EventHandler(AddressOf UpdateCompleteWaypointList)) End Sub Private Sub GPS_DestinationWaypointArrivalAlarm(ByVal sender As Object, ByVal e As StormSource.Gps.WaypointEventArgs) Handles GpsReceiver.DestinationWaypointArrivalAlarm Stop End Sub Private Sub GPS_DestinationWaypointChanged(ByVal sender As Object, ByVal e As StormSource.Gps.WaypointEventArgs) Handles GpsReceiver.DestinationWaypointChanged ' Update the destination waypoint Me.Invoke(New WaypointEventHandler(AddressOf UpdateDestinationWaypoint)) End Sub Private Sub GPS_OriginWaypointChanged(ByVal sender As Object, ByVal e As StormSource.Gps.WaypointEventArgs) Handles GpsReceiver.OriginWaypointChanged ' Update the origin waypoint Me.Invoke(New WaypointEventHandler(AddressOf UpdateOriginWaypoint)) End Sub #End Region #Region " Demonstration of GPS Device Identification " ' Raised when GpsReceiver.NET is able to identify this device uniquely from any other device Private Sub GPS_DeviceIdentified(ByVal sender As Object, ByVal e As StormSource.Gps.DeviceEventArgs) Handles GpsReceiver.DeviceIdentified Invoke(New EventHandler(AddressOf UpdateDeviceInformation)) End Sub Private Sub GPS_ProtocolChanged(ByVal sender As Object, ByVal e As StormSource.Gps.DeviceEventArgs) Handles GpsReceiver.ProtocolChanged Invoke(New EventHandler(AddressOf UpdateDeviceInformation)) End Sub Private Sub GPS_ManufacturerChanged(ByVal sender As Object, ByVal e As StormSource.Gps.DeviceEventArgs) Handles GpsReceiver.ManufacturerChanged Invoke(New EventHandler(AddressOf UpdateDeviceInformation)) End Sub Private Sub GPS_ProductNameChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GpsReceiver.ProductNameChanged Invoke(New EventHandler(AddressOf UpdateDeviceInformation)) End Sub Private Sub GPS_BatteryLifeChanged(ByVal sender As Object, ByVal e As StormSource.Gps.TimeSpanEventArgs) Handles GpsReceiver.BatteryLifeChanged Invoke(New EventHandler(AddressOf UpdateDeviceInformation)) End Sub ' Updates the form on the form's thread Private Sub UpdateDeviceInformation(ByVal sender As Object, ByVal e As EventArgs) Try ' Update device indentification information Me.Manufacturer.Text = GpsReceiver.Device.Manufacturer.ToString Me.DeviceName.Text = GpsReceiver.Device.Name Me.SoftwareVersion.Text = GpsReceiver.Device.SoftwareVersion Me.Protocol.Text = GpsReceiver.Device.Protocol.ToString Me.BatteryLife.Text = GpsReceiver.Device.BatteryLife.ToString ' Update waypoint capabilities Me.WaypointAddressCheckbox.Checked = GpsReceiver.Device.SupportsWaypointAddress Me.WaypointAltitudeCheckbox.Checked = GpsReceiver.Device.SupportsWaypointAltitude Me.WaypointCityCheckbox.Checked = GpsReceiver.Device.SupportsWaypointCity Me.WaypointColorCheckbox.Checked = GpsReceiver.Device.SupportsWaypointColor Me.WaypointCommentCheckbox.Checked = GpsReceiver.Device.SupportsWaypointComment Me.WaypointCountryCheckbox.Checked = GpsReceiver.Device.SupportsWaypointCountry Me.WaypointIntersectingRoadCheckbox.Checked = GpsReceiver.Device.SupportsWaypointIntersectingRoad Me.WaypointDepthCheckbox.Checked = GpsReceiver.Device.SupportsWaypointDepth Me.WaypointDisplayModeCheckbox.Checked = GpsReceiver.Device.SupportsWaypointDisplayMode Me.WaypointFacilityNameCheckbox.Checked = GpsReceiver.Device.SupportsWaypointFacility Me.WaypointStateCheckbox.Checked = GpsReceiver.Device.SupportsWaypointState Me.WaypointSymbolCheckbox.Checked = GpsReceiver.Device.SupportsWaypointSymbol ' To make features stand out more, enable/disable the checkbox Me.WaypointAddressCheckbox.Enabled = Me.WaypointAddressCheckbox.Checked Me.WaypointAltitudeCheckbox.Enabled = Me.WaypointAltitudeCheckbox.Checked Me.WaypointCityCheckbox.Enabled = Me.WaypointCityCheckbox.Checked Me.WaypointColorCheckbox.Enabled = Me.WaypointColorCheckbox.Checked Me.WaypointColorComboBox.Enabled = Me.WaypointColorCheckbox.Checked Me.WaypointCommentCheckbox.Enabled = Me.WaypointCommentCheckbox.Checked Me.WaypointCountryCheckbox.Enabled = Me.WaypointCountryCheckbox.Checked Me.WaypointIntersectingRoadCheckbox.Enabled = Me.WaypointIntersectingRoadCheckbox.Checked Me.WaypointDepthCheckbox.Enabled = Me.WaypointDepthCheckbox.Checked Me.WaypointDisplayModeCheckbox.Enabled = Me.WaypointDisplayModeCheckbox.Checked Me.WaypointDisplayModeComboBox.Enabled = Me.WaypointDisplayModeCheckbox.Checked Me.WaypointFacilityNameCheckbox.Enabled = Me.WaypointFacilityNameCheckbox.Checked Me.WaypointStateCheckbox.Enabled = Me.WaypointStateCheckbox.Checked Me.WaypointSymbolCheckbox.Enabled = Me.WaypointSymbolCheckbox.Checked Me.WaypointSymbolComboBox.Enabled = Me.WaypointSymbolCheckbox.Checked ' Update available waypoint symbols WaypointSymbolComboBox.Items.Clear() WaypointSymbolComboBox.Items.AddRange(GpsReceiver.Device.GetSymbols) WaypointSymbolComboBox.SelectedIndex = 0 ' Update available waypoint colors WaypointColorComboBox.Items.Clear() WaypointColorComboBox.Items.AddRange(GpsReceiver.Device.GetColors) WaypointColorComboBox.SelectedIndex = 0 ' Update available waypoint display modes WaypointDisplayModeComboBox.Items.Clear() WaypointDisplayModeComboBox.Items.AddRange(GpsReceiver.Device.GetDisplayModes) WaypointDisplayModeComboBox.SelectedIndex = 0 ' Enable/disable features based on the protocol Me.PowerOffButton.Enabled = (GpsReceiver.Device.Protocol = StormSource.Gps.Protocol.GarminBinary) Me.NewWaypointButton.Enabled = (GpsReceiver.Device.Protocol = StormSource.Gps.Protocol.GarminBinary) Me.DownloadWaypointButton.Enabled = (GpsReceiver.Device.Protocol = StormSource.Gps.Protocol.GarminBinary) Catch ex As Exception End Try End Sub #End Region #Region " Demonstration of powering off a GPS device " ' NOTE: Currently, the PowerOff() method is only supported when using the Garmin® ' binary protocol. Set your device to use the Garmin protocol before using this method. Private Sub PowerOffButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PowerOffButton.Click ' Confirm that the device should be powered off Select Case MessageBox.Show("When you turn off the GPS device, you will need to turn it back on manually. Do you still want to power off the device?", "GPS Device Power-Off", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) Case DialogResult.Yes Try ' Try to power off the device. If the device is not using the Garmin® protocol, this will fail GpsReceiver.Device.PowerOff() Catch ex As GpsException ' The power-off failed MessageBox.Show("The device must be set to the Garmin® protocol in order for it to be shut off. Please change the protocol and try again.", "Unable to Power Off", MessageBoxButtons.OK, MessageBoxIcon.Warning) End Try End Select End Sub #End Region #Region " Demonstration of Viewer Rotation " Private RotateThread As System.Threading.Thread Private Sub UpdateRotation() Do While True ' Increase the rotation angle by half a degree SatelliteViewer.Bearing += 0.4 ' Sleep for 50ms System.Threading.Thread.Sleep(30) Loop End Sub Private Sub chkEnableRotation_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkEnableRotation.CheckedChanged If chkEnableRotation.Checked Then ' Enable the rotation RotateThread = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf UpdateRotation)) RotateThread.Name = "Demo of Rotating Satellite Viewer" RotateThread.IsBackground = True RotateThread.Start() Else ' Disable the rotation If Not RotateThread Is Nothing Then RotateThread.Abort() RotateThread = Nothing End If ' Reset the rotation to zero SatelliteViewer.Bearing = 0 End If End Sub #End Region #Region " Demonstration of Viewer Customization " Private Sub chkShowPRCNumbers_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkShowPRCNumbers.CheckedChanged SatelliteViewer.IsSatellitePrcVisible = chkShowPRCNumbers.Checked End Sub Private Sub ShowCompassCheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShowCompassCheckBox.CheckedChanged SatelliteViewer.IsCompassVisible = ShowCompassCheckBox.Checked End Sub #End Region #Region " Miscellaneous Form Events " Public Delegate Sub StatusBarEventHandler(ByVal message As String) ' Used to update the status bar Public Sub UpdateStatusBar(ByVal message As String) StatusBar.Text = message End Sub Private Sub MainForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing ' Stop GPS services GpsReceiver.Stop() ' Clean up by disposing of resources LocalTimeTimer.Dispose() GpsReceiver.Dispose() End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Bind the SatelliteViewer to our GPS receiver SatelliteViewer.Receiver = GpsReceiver ' Start updating the local time LocalTimeTimer.Enabled = True End Sub #End Region End Class