Link to home
Start Free TrialLog in
Avatar of Heidi_Lee
Heidi_Lee

asked on

How to find a parameter in multiple line string

vb.net.

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
        Next



        '** 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
        Next

        '** 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
        Next

        '** 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
            Loop
            z = z + 1
        Next
        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

ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial