B4R Tutorial GPS data parsing from raw NMEA format

Hello All,
Here I will show, how to parse the Latitude and Longitude from raw nmea data.
I am using gps module, Arduino Uno and additional softwareSerial.
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private Serial2 As SoftwareSerial 'Declaring Additional Software serial port at Arduino UNO
    Private bc As ByteConverter
    Public astream As AsyncStreams
    Public GPSlat, GPSlon  As Float
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Serial2.Initialize(9600,5,6)  'TX is pin 5 and RX is pin 6
    Log("AppStart")
    astream.Initialize(Serial2.Stream,"astream_newdata",Null)
    'Log(finalLat)
End Sub

Private Sub calculateLat(Received_raw_nmea() As Byte)
    'Log(Received_raw_nmea)
    Dim index As Int=0
    For Each s() As Byte In bc.Split(Received_raw_nmea,",")
    If index=3 Then
        Dim latbeforeparse() As Byte= s
        '====>Separating the latitude with degress And minutes<======'
        Dim index1 As Int=0
        For Each latbeforeparse1() As Byte In bc.Split(s,".")
                Dim degreewithmin() As Byte
                If index1=0 Then
                    degreewithmin=latbeforeparse1
                            'Log("degreewithmin :",degreewithmin)
                    Dim d() As Byte=bc.SubString(latbeforeparse,(degreewithmin.Length-2))
                    'Log("Minute :",d)
                    Dim convertmintodeg As Float =bc.StringFromBytes(d)/60
                            'Log("convertmintodeg :",convertmintodeg)
                    Dim finaldegree() As Byte= bc.SubString2(latbeforeparse1,0,(latbeforeparse1.Length-2)) 'Extraction of First Two digit or Degree
                    Dim finalLat As Float= bc.StringFromBytes(finaldegree)+ convertmintodeg
                    GPSlat=finalLat 'For GSM to send the data over cloud
                    Log("The final Latitude : ",GPSlat)
                End If
                index1=index1+1
        Next
    End If
        index=index+1
    Next
End Sub

Private Sub calculateLon(Received_raw_nmea() As Byte)
    'Log(Received_raw_nmea)
    Dim index As Int=0
    For Each s() As Byte In bc.Split(Received_raw_nmea,",")
    If index=5 Then
        Dim lonbeforeparse() As Byte= s
        '====>Separating the Longitude with degress And minutes<======'
                Dim index1 As Int=0
                    For Each lonbeforeparse1() As Byte In bc.Split(s,".")
                Dim degreewithmin() As Byte
                        If index1=0 Then
                                    degreewithmin=lonbeforeparse1
                                    'Log("degreewithmin :",degreewithmin)
                                    Dim d() As Byte=bc.SubString(lonbeforeparse,(degreewithmin.Length-2))
                            'Log("Minute :",d)
                            Dim convertmintodeg As Float =bc.StringFromBytes(d)/60
                                    'Log("convertmintodeg :",convertmintodeg)
                                    Dim finaldegree() As Byte= bc.SubString2(lonbeforeparse1,0,(lonbeforeparse1.Length-2)) 'Extraction of First Two digit or Degree
                            Dim finalLon As Float= bc.StringFromBytes(finaldegree)+ convertmintodeg
                            GPSlon=finalLon 'For GSM to send the data over cloud
                            Log("The final Longitude : ",GPSlon)
                        End If
                    index1=index1+1
                    Next
        Else If index= 6 And s="W" Or s="S" Then
            'Dim finalLonAfterConversion As Float
                GPSlon=(GPSlon * -1)
            Log("The Final Latitude is :",GPSlon)
    End If
    index=index+1
    Next
End Sub

Sub astream_newdata(rawnmea() As Byte)
    If bc.IndexOf2(rawnmea,"$GPRMC",0)> -1 Then
        calculateLat(rawnmea)
        calculateLon(rawnmea)
    End If
    DelayMicroseconds(100) 'Delay to feed the data to the function
End Sub

Please let me know if there is any problem in my code.

Thanks-
Pokhraj Das
 

Beja

Expert
Licensed User
Longtime User
Hello All,
Here I will show, how to parse the Latitude and Longitude from raw nmea data.
I am using gps module, Arduino Uno and additional softwareSerial.
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private Serial2 As SoftwareSerial 'Declaring Additional Software serial port at Arduino UNO
    Private bc As ByteConverter
    Public astream As AsyncStreams
    Public GPSlat, GPSlon  As Float
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Serial2.Initialize(9600,5,6)  'TX is pin 5 and RX is pin 6
    Log("AppStart")
    astream.Initialize(Serial2.Stream,"astream_newdata",Null)
    'Log(finalLat)
End Sub

Private Sub calculateLat(Received_raw_nmea() As Byte)
    'Log(Received_raw_nmea)
    Dim index As Int=0
    For Each s() As Byte In bc.Split(Received_raw_nmea,",")
    If index=3 Then
        Dim latbeforeparse() As Byte= s
        '====>Separating the latitude with degress And minutes<======'
        Dim index1 As Int=0
        For Each latbeforeparse1() As Byte In bc.Split(s,".")
                Dim degreewithmin() As Byte
                If index1=0 Then
                    degreewithmin=latbeforeparse1
                            'Log("degreewithmin :",degreewithmin)
                    Dim d() As Byte=bc.SubString(latbeforeparse,(degreewithmin.Length-2))
                    'Log("Minute :",d)
                    Dim convertmintodeg As Float =bc.StringFromBytes(d)/60
                            'Log("convertmintodeg :",convertmintodeg)
                    Dim finaldegree() As Byte= bc.SubString2(latbeforeparse1,0,(latbeforeparse1.Length-2)) 'Extraction of First Two digit or Degree
                    Dim finalLat As Float= bc.StringFromBytes(finaldegree)+ convertmintodeg
                    GPSlat=finalLat 'For GSM to send the data over cloud
                    Log("The final Latitude : ",GPSlat)
                End If
                index1=index1+1
        Next
    End If
        index=index+1
    Next
End Sub

Private Sub calculateLon(Received_raw_nmea() As Byte)
    'Log(Received_raw_nmea)
    Dim index As Int=0
    For Each s() As Byte In bc.Split(Received_raw_nmea,",")
    If index=5 Then
        Dim lonbeforeparse() As Byte= s
        '====>Separating the Longitude with degress And minutes<======'
                Dim index1 As Int=0
                    For Each lonbeforeparse1() As Byte In bc.Split(s,".")
                Dim degreewithmin() As Byte
                        If index1=0 Then
                                    degreewithmin=lonbeforeparse1
                                    'Log("degreewithmin :",degreewithmin)
                                    Dim d() As Byte=bc.SubString(lonbeforeparse,(degreewithmin.Length-2))
                            'Log("Minute :",d)
                            Dim convertmintodeg As Float =bc.StringFromBytes(d)/60
                                    'Log("convertmintodeg :",convertmintodeg)
                                    Dim finaldegree() As Byte= bc.SubString2(lonbeforeparse1,0,(lonbeforeparse1.Length-2)) 'Extraction of First Two digit or Degree
                            Dim finalLon As Float= bc.StringFromBytes(finaldegree)+ convertmintodeg
                            GPSlon=finalLon 'For GSM to send the data over cloud
                            Log("The final Longitude : ",GPSlon)
                        End If
                    index1=index1+1
                    Next
        Else If index= 6 And s="W" Or s="S" Then
            'Dim finalLonAfterConversion As Float
                GPSlon=(GPSlon * -1)
            Log("The Final Latitude is :",GPSlon)
    End If
    index=index+1
    Next
End Sub

Sub astream_newdata(rawnmea() As Byte)
    If bc.IndexOf2(rawnmea,"$GPRMC",0)> -1 Then
        calculateLat(rawnmea)
        calculateLon(rawnmea)
    End If
    DelayMicroseconds(100) 'Delay to feed the data to the function
End Sub

Please let me know if there is any problem in my code.

Thanks-
Pokhraj Das

Hi Pokhraj
Thanks for this tutorial.. Didn't try it yet but sure will do. Came at the right time..
It would be good if you also parsed the time information that comes in the nmea data.
 
Top