InvalidCastException using GPS

Discussion in 'Questions (Windows Mobile)' started by Scubaticus, Oct 15, 2007.

  1. Scubaticus

    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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Can you post the code under Sub gps_gpsdecoded?
    Try to run the source code rather than the compiled file to see the exact line that rasies the error.
     
  3. Scubaticus

    Scubaticus Active Member Licensed User

    Hi Erel,

    It is the code from the sample on the forum:

    Code:
    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
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    The problem is probably with this line:
    Code:
    ListBox1.add("Speed: "GPS.SpeedOverGround * 1.852)
    SpeedOverGround returns an empty string instead of 0 when there is no data available.
    Try this:
    Code:
    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.
     
  5. Scubaticus

    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:

    Code:
    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: Oct 16, 2007
Loading...