InvalidCastException using GPS

Scubaticus

Active Member
Licensed User
I'm currently playing around with the GPSmodule and code the sample in the manual.

After I start the app, I got an:

An error occurred on sub gps_gpsdecoded.
Line number:0
Error description: InvalidCastException

After I choose Continue I see all the data I need, so that's nice, but how to get rid of the InvalidCastException?

Scub
 

Scubaticus

Active Member
Licensed User
Hi Erel,

It is the code from the sample on the forum:

B4X:
Sub GPS_GPSDecoded
   ListBox1.Clear
   ListBox1.add("Status: "& GPS.status)
   ListBox1.Add("Number Of Satellites: " & GPS.NumberOfSatellites)
   ListBox1.Add("Time: " & GPS.UTCTime)
   ListBox1.add("Lat: " & GPS.latitude)
   ListBox1.add("Lon: " & GPS.longitude)
   ListBox1.add("DecLat: "& GPS.DecimalLatitude)
   ListBox1.Add("DecLon: " & GPS.DecimalLongitude)
   ListBox1.add("Speed: "& GPS.SpeedOverGround * 1.852) 'Converts the speed to km/h.
   ListBox1.Add("Course: " & GPS.CourseOverGround)
   ListBox1.Add("Altitude: " & GPS.Altitude)
   utm() = converter.WGS84LatLonToUTM(GPS.DecimalLatitude,GPS.DecimalLongitude) 'Converts the Lat/Lon coordinates to UTM.
   ListBox1.Add("XZone: " & utm(0))
   ListBox1.Add("X: " & utm(1))
   ListBox1.Add("YZone: " & Chr(utm(2)))
   ListBox1.Add("Y: " & utm(3))
End Sub
Debugging on the Ipaq is a little bit difficult for me and I cannot get the CF Card running on the PC. I'll try to figure out how to setup Basic4ppc on the device with the right file locations as I remember the source should be in specific libraries to access them.

Scub
 

Erel

Administrator
Staff member
Licensed User
The problem is probably with this line:
B4X:
ListBox1.add("Speed: "& GPS.SpeedOverGround * 1.852)
SpeedOverGround returns an empty string instead of 0 when there is no data available.
Try this:
B4X:
if StrLength(GPS.SpeedOverGround) > 0 Then
    ListBox1.add("Speed: "& GPS.SpeedOverGround * 1.852)
end if
Another solution will be to use an ErrorLabel to hide the error.
 

Scubaticus

Active Member
Licensed User
That solves the problem. Thanks Erel.

Scub

Edit: I thought it fixed it, but it doesn't. I got in debug on the device and the second line is causing the error:

B4X:
utm() = converter.WGS84LatLonToUTM(GPS.DecimalLatitude,GPS.DecimalLongitude) 'Converts the Lat/Lon coordinates to UTM.
[B]ListBox1.Add("XZone: " & utm(0))[/B]
So its perhaps better to check all the GPS.<attrib> for data?

Edit: I put an errorlabel around the utm part and now it's not bothering me anymore. Perhaps the converter could take care of invalid input sometime.
 
Last edited:
Top