The first step in making an NMEA interpreter is writing a method which does two things: separating each sentence into its individual words and examining the first word to figure out what information is available to extract. Listing 1-1 shows the start of the interpreter class.
(Listing 1-1: The core of an NMEA interpreter is a function which divides NMEA sentences into individual words.)
'** Listing 1-1. The core of an NMEA interpreter
'*******************************************************
Public Class NmeaInterpreter
' Processes information from the GPS receiver
Public Function Parse(ByVal sentence As String) As Boolean
' Divide the sentence into words
Dim Words() As String = GetWords(sentence)
' Look at the first word to decide where to go next
Select Case Words(0)
Case "$GPRMC" ' A "Recommended Minimum" sentence was found!
' Indicate that the sentence was recognized
Return True
Case Else
' Indicate that the sentence was not recognized
Return False
End Select
End Function
' Divides a sentence into individual words
Public Function GetWords(ByVal sentence As String) As String()
Return sentence.Split(","c)
End Function
End Class
The next step is to perform actual extraction of information, starting with latitude and longitude. Latitude and longitude are stored in the form “DDD°MM’SS.S
,” where D
represents hours (also called “degrees”), M
represents minutes and S
represents seconds. Coordinates can be displayed in shorthand, such as “DD°MM.M’
” or even “DD°
.” The fourth word in the sentence, “3939.7
,” shows the current latitude as hours and minutes (39°39.7’
), except the numbers are squished together. The first two characters (39
) represent hours and the remainder of the word (39.7
) represents minutes. Longitude is structured the same way, except that the first three characters represent hours (105°06.6’
). Words five and seven indicate the “hemisphere
,” where “N
” means “North
,” “W
” means “West
” etc. The hemisphere is appended to the end of the numeric portion to make a complete measurement.
I’ve found that NMEA interpreters are much easier to work with they are event-driven. This is because data arrives in no particular order. An event-driven class gives the interpreter the most flexibility and responsiveness to an application. So, I’ll design the interpreter to report information using events. The first event, PositionReceived
, will be raised whenever the current latitude and longitude are received. Listing 1-2 expands the interpreter to report the current position.
Listing 1-2: The interpreter can now report the current latitude and longitude.
'*******************************************************
'** Listing 1-2. Extracting information from a sentence
'*******************************************************
Public Class NmeaInterpreter
' Raised when the current location has changed
Public Event PositionReceived(ByVal latitude As String, _
ByVal longitude As String)
' Processes information from the GPS receiver
Public Function Parse(ByVal sentence As String) As Boolean
' Look at the first word to decide where to go next
Select Case GetWords(sentence)(0)
Case "$GPRMC" ' A "Recommended Minimum" sentence was found!
Return ParseGPRMC(sentence)
Case Else
' Indicate that the sentence was not recognized
Return False
End Select
End Function
' Divides a sentence into individual words
Public Function GetWords(ByVal sentence As String) As String()
Return sentence.Split(","c)
End Function
' Interprets a $GPRMC message
Public Function ParseGPRMC(ByVal sentence As String) As Boolean
' Divide the sentence into words
Dim Words() As String = GetWords(sentence)
' Do we have enough values to describe our location?
If Words(3) <> "" And Words(4) <> "" And Words(5) <> "" And _
Words(6) <> "" Then
' Yes. Extract latitude and longitude
Dim Latitude As String = Words(3).Substring(0, 2) & "°" ' Append hours
Latitude = Latitude & Words(3).Substring(2) & """" ' Append minutes
Latitude = Latitude & Words(4) ' Append the hemisphere
Dim Longitude As String = Words(5).Substring(0, 3) & "°" ' Append hours
Longitude = Longitude & Words(5).Substring(3) & """" ' Append minutes
Longitude = Longitude & Words(6) ' Append the hemisphere
' Notify the calling application of the change
RaiseEvent PositionReceived(Latitude, Longitude)
End If
' Indicate that the sentence was recognized
Return True
End Function
End Class
One thing to watch out for here is that some GPS devices will report blank values when no information is known. Therefore, it’s a good idea to test each word for a value before parsing. If you need to type the degree symbol (°
), hold down the Alt key and type “0176
” on the numeric keypad.
Comments