No announcement yet.

VB6 serial port help

  • Filter
  • Time
  • Show
Clear All
new posts

  • VB6 serial port help

    I'm trying to figure out how to get the proper data off the serial port from my GPS adapter. I'm using xPort to split my GPS into 3 virtual coms, 1 for iGuidance, one for netstumbler, and one for my frontend. All i really want is speed and bearing, so I can have a basic compass in my front end all the time.

    Now I know what part of the NMEA sentence I need, but I'm having trouble getting the sentences into VB in a consistant form. I can see the data that I need in the status bar in xPort, but I'm getting alot of garbage and | characters in my input stream.

    I can zip up my test app that i wrote and attach it if someone wants to take a look at it.

    any help is appreciated.


  • #2
    Hi Trevor,

    are you converting the data properly? ie; if Bytearray is passed, you would either have to deal with it in bytes or convert it into String. If you upload your file, I can have a quick look.

    Rana Hossain
    Author of CarJatra

    CarPC: P3 866MHz, 384MB RAM, 20GB HD, 8" Lilliput, 160W PSU, WiFi + a lot of wires :)


    • #4
      I presume you've checked your code without using xport?

      The ultimate CarPC - Wow!


      • #5
        pulled from RR source

        Public Sub ReadGPS()
        Static Ltry As Double   'Last Time we tried to reconnect GPS
        Static Buff As String   'Buffer of Data
        Dim NB As Long          'Number of Bytes
        Dim b() As Byte         'Array of Bytes
        Dim DT() As String      'GPS Fields
        Dim sData As String     'Temp GPS Data
            On Error Resume Next
            'Prevention -- Issue posted by Arathranar
            If Hibernated Then Exit Sub
            'If MM running
            If MMRunning Then
                Buff = frmMM.MM.Custom("GetNMEA|GGA", "D3Request") + vbCrLf
                Buff = Buff + frmMM.MM.Custom("GetNMEA|RMC", "D3Request") + vbCrLf
                GoTo ProcGPSData
            End If
            'If nothing to do
            If GPSPort = 0 Then Exit Sub
            'Try to read bytes
            NB = CommRead(GPSPort, b, 1024)
            'If Got Data
            If NB > 0 Then
                Buff = Buff + StrConv(b, vbUnicode)
                NB = InStr(1, Buff, Chr(10), vbBinaryCompare)
                'while there are complete lines
                While NB > 0
                    'Get Line of Data
                    sData = Left(Buff, NB - 1)
                    'Adjust Buffer
                    Buff = Mid(Buff, NB + 1)
                    'Process Line of Data
                    If Left(sData, 1) = "$" Then
                        DT = Split(sData, ",")
                        Select Case DT(0)
                            Case "$GPRMC"
                                'Get Lat, Long, Speed and Heading
                                GPS.Lat = CDbl(Left(DT(3), InStr(1, DT(3), ".") - 1))
                                GPS.Lon = CDbl(Left(DT(5), InStr(1, DT(5), ".") - 1))
                                If CommaSys Then
                                    'Prepare for Conversions
                                    If CommaSys Then DT(3) = Replace(DT(3), ".", ",")
                                    If CommaSys Then DT(5) = Replace(DT(5), ".", ",")
                                    If CommaSys Then DT(7) = Replace(DT(7), ".", ",")
                                    If CommaSys Then DT(8) = Replace(DT(8), ".", ",")
                                    'Get Lat/Long
                                    GPS.Lat = GPS.Lat \ 100# + (CDbl(Mid(DT(3), InStr(1, DT(3), ",") - 2)) / 60#)
                                    GPS.Lon = GPS.Lon \ 100# + (CDbl(Mid(DT(5), InStr(1, DT(5), ",") + -2)) / 60#)
                                    GPS.Lat = GPS.Lat \ 100# + (CDbl(Mid(DT(3), InStr(1, DT(3), ".") - 2)) / 60#)
                                    GPS.Lon = GPS.Lon \ 100# + (CDbl(Mid(DT(5), InStr(1, DT(5), ".") + -2)) / 60#)
                                End If
                                If DT(4) = "S" Then GPS.Lat = -GPS.Lat
                                If DT(6) = "W" Then GPS.Lon = -GPS.Lon
                                GPS.speed = CDbl(DT(7))
                                GPS.HDG = CInt(DT(8))
                            Case "$GPGGA"
                                'Get Altitude and Sat Count
                                If CommaSys Then DT(9) = Replace(DT(9), ".", ",")
                                GPS.Alt = CDbl(DT(9))
                                GPS.SATS = CInt(DT(7))
                                GPS.Valid = (Val(DT(6)) > 0)
                        End Select
                    End If
                    'Check for more lines
                    NB = InStr(1, Buff, Chr(10), vbBinaryCompare)
                'Reset Timeout Timer
                Ltry = Timer
                'Didn't get any data in 10 seconds (Problem ?)
                'Try to reconnect GPS
                If Timer - Ltry > 10 And MMRunning = False And CurSCR <> "" Then
                    GPS.Valid = False
                    'Close Port
                    CommClose GPSPort
                    'Give O.S. processing time
                    Sleep 1
                    'Try to Re-open it
                    CommOpen GPSPort, "\\.\COM" + CStr(GPSPort), "baud=4800 parity=N data=8 stop=1"
                    CommSetLine GPSPort, 2, True   'Data Terminal Ready ON (in case)
                    Ltry = Timer
                End If
                If Ltry > Timer Then Ltry = Timer
            End If
        End Sub
        might be of some help :-)

        Follow me on Twitter


        • #7
          hey, thanks for the replies.

          This is the first time I've ever tried anything regarding the serial port, so what I came up with is not very fancy. It was stuff i found with google here and there, and kinda molded into something that i thought would work, so please bear with me.

          I've attached my little test program which basically takes whatever is in the input buffer and using the split function splits it into words() using a comma as the delimiter. If words(0) = "$GPRMC" then it takes words(7) and converts it from knots to km/h and puts it in a textbox, and words(8) (bearing) in another box. I have the MsComm control set to Com10 as that's the com i set aside for it on xPort, it can be changed in the properties of the control. Clicking the start/stop button simply sets the mscomm1.portopen property to true or false.

          I'm pretty sure it's not the right way to go about this, but it's very simple and i came up with it in about an hour. I don't know how else I can test my program, and I know very little about serial programming.

          Thanks for the help
          Attached Files


          • #8
            Originally posted by Enforcer
            Here is where I got the code to dismantle to get the info I want.

            The class file in the zip on the above page was really what I was looking for in the first place, but couldn't find anything. I'd still like to see if what I wrote is going to work with little modification, but if not I can just use the class file


            • #9
              Originally posted by Enforcer View Post
              Laidback, you're still about then?
              Of course, an enforced holiday from broadband, a stuffed mother board and a new house needing plenty of work but I still stick my nose in now & again. :-)

              The ultimate CarPC - Wow!


              • #11
                Why not intercept the $GPVTG sentence, which gives you speed in BOTH Mph and km/h ? (This sentence is optional on certain GPS units)

                You could use AsyncPro components to deal with serial com.
                Now Galileo is real. Muhahahahaha :p