10楼#
发布于:2004-09-17 15:58
The Core of An Interpreter (Continued)
<P>The next step is to perform actual extraction of information, starting with latitude and longitude. Latitude and longitude are stored in the form "DD°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 (105006.6'). Words five and seven indicate the "hemisphere," where "N" means "North," "W" means "West," and so forth. The hemisphere is appended to the end of the numeric portion to make a complete measurement.</P> (continued) <P> <P>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.</P> <P>Listing 1-2: The interpreter can now report the current latitude and longitude.</P>'********************************************************'** 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) ; "0" ' 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) ; "0" ' 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 FunctionEnd Class <P>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.</P> [此贴子已经被作者于2004-9-17 15:59:30编辑过]
|
|
|
上一页
下一页