How to find a parameter in multiple line string

Posted on 2011-10-25
Medium Priority
Last Modified: 2012-05-12

I have this string from a GPS serial input

bufferGPS = 
"$GPDTM,W86,,00.0000,N,00.0000,E,,W84*41" & vbCrLf
$GPGGA,086610.000,3900.6474,N,12141.4574,E,1,8,1.6,60,M,,M,,*61" & vbCrLf
$GPRMC,086609,00,A,3900,6471,N,12141.4578,E,0.0,270.0,071011,8.2,W,A*2B" & vbCrLf
$GPVTG,276.0,0,T,278.2,M,0.0,N,0.0,K,A*29" & vbCrLf
$GPVDR,,,,,,*57" & vbCrLf
$GPZDA,086609.00,07,10,2011,-08,00*45" & vbCrLf"

Open in new window

If I want to extract the first parameter of $GPGGA which is 086610.000, the quick and easy way I know how to do this is

dim temp as String() = bufferGPS.Split(","c)
Dim ggaTime As String = temp(8)

Open in new window

However, because later the end user might alter the GPS strings' sequence, I would like to have a parsing code that searches the $GPGGA string automatically in a host of other GPS strings.

Being a vb6 gal with no experience with (crap) vb.net, I ported the below vb.net code from vb6.  However after solving a lot of issues like (crap) vb.net no longer support left() and len(), the code still could not work, and I am in a offshore environment with not much time to look into this.
"Public Sub ParseSentence(ByVal InBuff As String, ByVal MyGGA As GGA, ByVal Myvtg As VTG, ByVal MyGSA As GSA, ByVal MyGSV As GSV)

        Dim intcomma As Integer
        Dim aryData(25)
        Dim x, z, st, fn, intst As Integer
        Dim strSentence, strTemp, strDummy As String

        '** Find the start of the sentence...locate $... st stores start position.
        For x = 1 To InBuff.Length()
            If InBuff.Substring(x, 1) = "$" Then
                st = x
                Exit For
            End If

        '** Find the end of the sentence...locate CR... fn stores end position.
        For x = st To InBuff.Length()
            If InBuff.Substring(x, 1) = vbCr Then
                fn = x
                Exit For
            End If

        '** Use st and fn to slice the string to a sentence.
        strSentence = InBuff.Substring(st, fn - st)

        '** How many fields are there in the sentence...Count the comma's
        For x = 1 To strSentence.Length()
            If strSentence.Substring(x, 1) = "," Then
                intcomma = intcomma + 1
            End If

        '** Fill an array with the field data..
        z = 8
        For x = 1 To intComma
            Do While strSentence.Substring(z, 1) <> "," And strSentence.Substring(z, 1) <> "*"
                aryData(x) = aryData(x) & strSentence.Substring(z, 1)
                z = z + 1
            z = z + 1
        Select Case strSentence.Substring(0, 6)
            Case "$GPGGA"
                MyGGA.strUttcTime = aryData(1)
                MyGGA.varLatitude = aryData(2)
                MyGGA.strNSIndicator = aryData(3)
                MyGGA.varLongitude = aryData(4)
                MyGGA.strEWIndicator = aryData(5)
                MyGGA.strPositionFix = aryData(6)
                MyGGA.strSatsUsed = aryData(7)
                MyGGA.strIIDOP = aryData(8)
                MyGGA.strAltitude = aryData(9)
                MyGGA.strAltUnits = aryData(10)
                MyGGA.strGeoid = aryData(11)
                MyGGA.strSepUnits = aryData(12)
                MyGGA.strDgpsAge = aryData(13)
                MyGGA.strDgpsid = aryData(14)
            Case "$GPVTG"
                Myvtg.strCourse1 = aryData(1)
                Myvtg.strReference1 = aryData(2)
                Myvtg.strCourse2 = aryData(3)
                Myvtg.strReference2 = aryData(4)
                Myvtg.strSpeed1 = aryData(5)
                Myvtg.strSpeedUnit1 = aryData(6)
                Myvtg.strSpeed2 = aryData(7)
                Myvtg.strSpeedUnit2 = aryData(8)
            Case "$GPGSA"
                MyGSA.strMode = aryData(1)
                MyGSA.strFixType = aryData(2)
                MyGSA.strSat1 = aryData(3)
                MyGSA.strSat2 = aryData(4)
                MyGSA.strSat3 = aryData(5)
                MyGSA.strSat4 = aryData(6)
                MyGSA.strSat5 = aryData(7)
                MyGSA.strSat6 = aryData(8)
                MyGSA.strSat7 = aryData(9)
                MyGSA.strSat8 = aryData(10)
                MyGSA.strSat9 = aryData(11)
                MyGSA.strSat10 = aryData(12)
                MyGSA.strSat11 = aryData(13)
                MyGSA.strSat12 = aryData(14)
                MyGSA.strPDOP = aryData(15)
                MyGSA.strHDOP = aryData(16)
                MyGSA.strVDOP = aryData(17)
            Case "$GPGSV"
                MyGSV.StrNoMessages = aryData(1)
                MyGSV.StrSeq = aryData(2)
                MyGSV.StrSatsinview = aryData(3)
                MyGSV.StrSatId1 = aryData(4)
                MyGSV.StrElevation1 = aryData(5)
                MyGSV.StrAzimuth1 = aryData(6)
                MyGSV.StrSNR1 = aryData(7)
                MyGSV.StrSatId2 = aryData(8)
                MyGSV.StrElevation2 = aryData(9)
                MyGSV.StrAzimuth2 = aryData(10)
                MyGSV.StrSNR2 = aryData(11)
                MyGSV.StrSatId3 = aryData(12)
                MyGSV.StrElevation3 = aryData(13)
                MyGSV.StrAzimuth3 = aryData(14)
                MyGSV.StrSNR3 = aryData(15)
                MyGSV.StrSatId4 = aryData(16)
                MyGSV.StrElevation4 = aryData(17)
                MyGSV.StrAzimuth4 = aryData(18)
                MyGSV.StrSNR4 = aryData(19)

        End Select

Open in new window

Without knowing the full capability of vb.net functions, my question:  is there a elegant and simple way or function in vb.net that automatically search for "$GPGGA" in multiple lines strings; I dont know, maybe some simple code like below:

If bufferGPS.SuperFunctionSubstring(0, 6) = "$GPGGA" Then
                        Dim temp As String() = bufferGPS.Split(","c)
                       Dim templong As String = temp(2) ' Long
                        Dim tempN As String = temp(3) ' N
                        Dim templat As String = temp(4) ' Lat
                       Dim tempE As String = temp(5) ' E
end if

Open in new window

Question by:Heidi_Lee
LVL 83

Accepted Solution

CodeCruiser earned 668 total points
ID: 37023875
You can try

Dim StartIndex as Integer = bufferGPS.IndexOf("$GPGGA")

Dim gpgga as String = bufferGPS.SubString(bufferGPS.IndexOf(",", StartIndex) + 1, bufferGPS.IndexOf(",", bufferGPS.IndexOf(",", StartIndex) ) -  bufferGPS.IndexOf(",", StartIndex) )

This is untested so you may have to tweak it.

Assisted Solution

vbigham earned 668 total points
ID: 37026226
An extension of your original idea, along with using Array.IndexOf to get at what you need.  This seems to work:
Dim bufferGPS As String

bufferGPS = "$GPDTM,W86,,00.0000,N,00.0000,E,,W84*41" & vbCrLf & _
            "$GPGGA,086610.000,3900.6474,N,12141.4574,E,1,8,1.6,60,M,,M,,*61" & vbCrLf & _
            "$GPRMC,086609,00,A,3900,6471,N,12141.4578,E,0.0,270.0,071011,8.2,W,A*2B" & vbCrLf & _
            "$GPVTG,276.0,0,T,278.2,M,0.0,N,0.0,K,A*29" & vbCrLf & _
            "$GPVDR,,,,,,*57" & vbCrLf & _
            "$GPZDA,086609.00,07,10,2011,-08,00*45" & vbCrLf

Dim temp As String() = bufferGPS.Split(New Char() {","c, CChar(vbCr), CChar(vbLf)})
Dim ggaTime As String

Dim ggaIndex As Integer = Array.IndexOf(temp, "$GPGGA")

If ggaIndex > -1 Then 
    ggaTime = temp(ggaIndex + 1)
End If

' Or if you do not need all of the empty entries try this: '
Dim temp As String() = bufferGPS.Split(New Char() {","c, CChar(vbCr), CChar(vbLf)}, StringSplitOptions.RemoveEmptyEntries)

Open in new window

LVL 13

Assisted Solution

by:Corey Scheich
Corey Scheich earned 664 total points
ID: 37031105
regular expressions are very versatile and work well  in these types of situations.  I use Expresso to build and test them it has served me well over the years
Dim matchString as string = "(?im-snx)(?<GPGGA>[$]GPGGA.*?)\r?$"
        Dim m As System.Text.RegularExpressions.Match
        m = System.Text.RegularExpressions.Regex.Match(bufferGPS, matchString)
        Dim GPGGA As String
        Dim CSV() As String
        Dim GPGGA1 As String
            GPGGA = m.Groups("GPGGA").Value
            CSV = GPGGA.Split(New String() {","}, StringSplitOptions.None)
            GPGGA1 = CSV(1)
        Catch ex As Exception

        End Try

Open in new window


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question