DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Detecting line feed and carriage return

  1. #1
    Join Date
    Aug 2005

    Detecting line feed and carriage return


    I have an application in that reads a stream of GPS NMEA data from a serial port and converts it to string and then sends the string to a parser subroutine.

    The parser subroutine looks for an identifier that signals the start of one NMEA data packet i.e. "$".

    When it finds this, the subroutine than copies the characters following the "$" char one by one into a local buffer until it reaches a carriage return. When it finds a carriage return it then goes on look for a line feed, at which point it then sends the local buffer to another function that processes the particular string.

    My problem is simply this - my subroutine has difficulty in differentiating between a line feed character and a carriage return character.

    This is my code:

    Private Sub ParseText(ByVal NMEA_buff As String, ByVal MAX_PACKET As Integer)
    'MAX_PACKET is 100
    Dim ch As String
    Dim i As Integer
    Static NMEA_localbuff As String
    Static NMEA_state As Integer

    On Error GoTo ErrHandler

    i = 1
    ch = Mid(NMEA_buff, i, 1)
    While (ch <> "")
    If (ch = "$") Then
    NMEA_state = 1
    NMEA_localbuff = ch
    bytecntr = 0
    Select Case (NMEA_state)
    Case 1
    bytecntr = bytecntr + 1
    'chr(13) is carriage return \r
    If (ch = Chr(13)) Then
    NMEA_state = 2
    NMEA_localbuff = NMEA_localbuff + ch
    End If
    If (bytecntr >= MAX_PACKET) Then
    NMEA_state = 0
    End If

    Case 2
    ' chr(10) is line feed \n
    If (ch = Chr(10)) Then
    NMEA_localbuff = ""
    End If
    NMEA_state = 0
    End Select
    End If
    i = i + 1
    ch = Mid(NMEA_buff, i, 1)
    End While
    MsgBox("Error " & Err.Number & " " & Err.Description)

    End Sub

    It keeps looping around the same Select Case 1 without evenr going into Select Case 2 as it should.

    I've tried using vbLf and vbCr for Chr(10) and Chr(13) respectively, but still no dice.

    Is there another way I can use to detect line feeds and carriage returns?

    Any help will be greatly appreciated...

    Thanks and regards.


  2. #2
    Join Date
    Dec 2003
    Is there a reason why you can't look for vbCrLf instead of each each individually?
    Microsoft MVP (Visual Basic)

Similar Threads

  1. carriage return problem
    By kim in forum Web
    Replies: 2
    Last Post: 09-29-2001, 12:10 PM
  2. Carriage return and newline
    By Charles in forum Database
    Replies: 1
    Last Post: 04-30-2001, 12:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center