Link to home
Start Free TrialLog in
Avatar of Todd MacPherson
Todd MacPhersonFlag for Canada

asked on

Need to fix my VB.net code to obtain a GPS position.

I am trying to create a click event to obtain my gps position in a click event on a form for a windows device. For sure there is a GPS connected on my device (it's detected on serial port 5) It is also connected to multiple satellites. But my code is not returning a location. I keep getting caught in my error handler "No NMEA message received." Any ideas why this is not working? I thought this would be a rather simple process. The target framework is VB.net 4.7.2


I am a novice at this and have created this code after many hours of 'frigging around' I hope someone can guide me.


Here is the code:; 


Private Sub butGPS_Click(sender As Object, e As EventArgs) Handles butGPS.Click
        ' Clear the GPS status label
        lblGPSStatus.Text = "Searching for GPS..."
        lblGPSStatus.ForeColor = Color.Black


        ' Clear the text boxes
        txtLat.Text = ""
        txtLong.Text = ""
        txtElev.Text = ""
        txtReadings.Text = ""


        ' Set the flag to indicate that the GPS port was not found
        Dim gpsPortFound As Boolean = False


        ' Iterate through each available serial port
        For Each portName As String In SerialPort.GetPortNames()
            Try
                ' Open a serial port connection to the GPS device
                Dim serialPort As New SerialPort(portName, 9600, Parity.None, 8, StopBits.One)
                serialPort.Open()
                ' Check if the port is open
                If serialPort.IsOpen Then
                    ' Set the flag to indicate that the GPS port was found
                    gpsPortFound = True


                    ' Display a message indicating which port is open
                    MessageBox.Show($"Port {portName} is open!", "GPS Port Found", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    ' Read data from the GPS device
                    While serialPort.BytesToRead > 0
                        Dim bytesToRead As Integer = serialPort.BytesToRead
                        Dim buffer(bytesToRead) As Byte
                        serialPort.Read(buffer, 0, bytesToRead)
                        Dim nmeaMessage As String = Encoding.ASCII.GetString(buffer)
                        If nmeaMessage.StartsWith("$") And nmeaMessage.EndsWith("*") Or nmeaMessage.Length = 83 Then
                            txtReadings.Text = nmeaMessage
                            Exit While
                        End If
                    End While


                    If txtReadings.Text.Length > 0 Then
                        ' Parse the NMEA 0183 data
                        Dim nmea As NMEA0183 = NMEA0183.Parse(txtReadings.Text)
                        ' Check if the data is a valid GPS position fix
                        If nmea.IsValid AndAlso nmea.IsPositionFix Then
                            ' Display the latitude, longitude, and elevation in the text boxes
                            txtLat.Text = nmea.Latitude.ToString()
                            txtLong.Text = nmea.Longitude.ToString()
                            txtElev.Text = nmea.Elevation.ToString()
                        End If
                        ' Close the serial port connection
                        serialPort.Close()
                        ' Exit the loop
                        Exit For
                    Else
                        lblGPSStatus.Text = "Error: No NMEA message received"
                        lblGPSStatus.ForeColor = Color.Red
                    End If
                End If
            Catch ex As Exception
                ' Do nothing - just continue to the next port
            End Try
        Next


        If Not gpsPortFound Then
            ' Display a message indicating that no GPS port was found
            MessageBox.Show("No GPS port was found!", "GPS Port Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error)
            ' Update the GPS status label
            lblGPSStatus.Text = "GPS not connected"
            lblGPSStatus.ForeColor = Color.Red
        End If
End Sub

Open in new window


Avatar of zipnotic
zipnotic
Flag of United States of America image

I only skimmed your code, did I miss where you initiated the GPS puck?  

Also, typically only 1 connection to s erial device at a time so if you have a different program connected to that serial port/device your program won't connect to it.  Turn off the other monitoring program but leave the GPS device running.
Avatar of Todd MacPherson

ASKER

I do not have anything else open accessing the GPS and the device is a Mesa 2 ruggedized tablet with Windows 10. The GPS is always on so I did not think it required to be initiated. It's looking for an open serial port, and then opening a connection to it. Once the connection is established, the code is attempting to read data from the GPS device, and parse the received data as an NMEA 0183 message.

It is not explicitly initializing the GPS device, so it is assumed that the GPS device is already powered on and configured to send data over the specified serial port at the time the code runs.
I'd suggest using a different program to test the functionality.   I always initiate the device before reading it.  They usually respond with an acknowledged code.
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada 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
Hello Éric,

Your code is working with target framework 4.8. It is populating the listbox but no map displays. I do not need the map so will work on incorporating your code into my project. Stay tuned.

Thanks,

Todd
Very possible that the maps are not working anymore. Article is almost 10 years old!