Android Question how to get the lat, long and alt data from a stand alone gps data

Izmirov.Yaminovich

Member
Licensed User
Longtime User
Good day to all of you, I have been searching and go through the forum for 5 hours. I want to ask question. Newbie programmer here

1. how to convert this coding to b4a structure coding?

B4X:
int readserial()
{
//Streaming strings
stringstream ss1,ss2,ss3 (stringstream::in | stringstream::out);
int i;
unsigned char lon_hex[4], lat_hex[4], alt_hex[4]={0};

signed int lon,lat,alt;
float lon_f,lat_f,alt_f;
for (i=1;i<5000;i++)
{
  //Start reading
  read(fd2,buffer,1);
  //Only GPS signal is needed in this part, streaming signal should start with 0xfe1c
  if (buffer[0]==0xfe)
  {
   read(fd2,buffer,1);
   if (buffer[0]==0x1c)
   {
    //Ignore 4 bytes
    for (int o=0;o<4;o++)
    read(fd2,buffer,1);
    if (buffer[0]==0x21)
    {
     //Ignore 4 bytes
     for (int o=0;o<4;o++)
     read(fd2,buffer,1);
     //Read Latitude in hex
     for (int o=0;o<4;o++)
     {
      read(fd2,buffer,1);
      lat_hex[o]=buffer[0];   
     }
     //Read Longitude in hex
     for (int o=0;o<4;o++)
     {
      read(fd2,buffer,1);
      lon_hex[o]=buffer[0];   
     }
     //Ignore 4 bytes
     for (int o=0;o<4;o++)
      read(fd2,buffer,1);
     //Read Altitude in hex
     for (int o=0;o<4;o++)
     {
      read(fd2,buffer,1);
      alt_hex[o]=buffer[0];   
     }
     //Convert hex value into long int
     lat
=lat_hex[0]+lat_hex[1]*256ul+lat_hex[2]*256ul*256ul+lat_hex[3]*256ul*256ul*256ul;
     lon
=lon_hex[0]+lon_hex[1]*256ul+lon_hex[2]*256ul*256ul+lon_hex[3]*256ul*256ul*256ul;
     alt
=alt_hex[0]+alt_hex[1]*256ul+alt_hex[2]*256ul*256ul+alt_hex[3]*256ul*256ul*256ul;
     //Convert into float
     lat_f=(float)lat/10000000;
     lon_f=(float)lon/10000000;
     alt_f=(float)alt/1000;
    
     //Convert three values into string and output

     ss1 << lat_f;
     string test1 = ss1.str();
    
     ss2 << lon_f;
     string test2 = ss2.str();
    
     ss3 << alt_f;
     string test3 = ss3.str();
    
     char output[100]={0};
     strcat(output,"Latitude: ");
     strcat(output,test1.c_str());
     strcat(output," Longitude: ");
     strcat(output,test2.c_str());
     strcat(output," Altitude: ");
     strcat(output,test3.c_str());
     int n2 = write(newsockfd,output,100);  
     if (n2 < 0) error("ERROR writing to socket");
     ss1.str("");
     ss2.str("");
     ss3.str("");
     return 0;
   }
  }

}
}
return 0;
}

Is it like this?

B4X:
 Dim ss1,ss2,ss3  As String
     Dim i As Int
     Dim lon_hex,lat_hex, alt_hex As Char
     lon_hex,lat_hex, alt_hex = Array As Int (0,1,2,3)
     Dim lon,lat,alt As Long
     Dim lon_f,lat_f,alt_f As Float
     Dim o As Int
    
    msg =
     For i = 1 To 4999
      If buffer = 0xfe Then
         If buffer = 0x1c Then
            For o = 0 To 3
                'read lattitude in hex
                lat_hex[o]= buffer
                Next
                For o = 0 To 3
                    'read longitude in hex
                    lon_hex[o] = buffer
                    Next
                    For o = 0 To 3
                        'read altitude in hex
                        alt_hex[o] = buffer
                        Next
                       
                        'convert hex value into long int
                            
     lat =lat_hex[0]+lat_hex[1]*256+lat_hex[2]*256*256+lat_hex[3]*256*256*256;
     lon =lon_hex[0]+lon_hex[1]*256+lon_hex[2]*256*256+lon_hex[3]*256*256*256;
     alt =alt_hex[0]+alt_hex[1]*256+alt_hex[2]*256*256+alt_hex[3]*256*256*256;
   
   
                          'Convert into Float
                        lat_f=(Float)lat/10000000
                        lon_f=(Float)lon/10000000
                        alt_f=(Float)alt/1000
 
   ss1 << lat_f;
Dim test1 As String  = ss1.str();
ss2 << lon_f;
Dim test2 As String  = ss2.str();
ss3 << alt_f;
Dim test3 As String  = ss3.str();
 

Izmirov.Yaminovich

Member
Licensed User
Longtime User
Thank you for reply @NJDude
Yes I have try it, but it use the gps that built in that android phone right? I use the gps receiver that have been connected to radio broadcast.The receiver for the radio broadcast is connected through usb. Thanks to USB serial library.
Even the GPS nmea example can't decode the hex that have been received. I think that I need to keep on researching. If you have any idea workaround this thing, please let me know. :):D

Edited: I forgot to put the example for the hex. It will be like this "FE 16 61 01 01 21 E9 A2 01 E3 ..........."
Cheers
 
Upvote 0
Top