Hi,

I have an application in VB.net 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
Else
Select Case (NMEA_state)
Case 1
bytecntr = bytecntr + 1
'chr(13) is carriage return \r
If (ch = Chr(13)) Then
NMEA_state = 2
Else
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
process(NMEA_localbuff)
NMEA_localbuff = ""
End If
NMEA_state = 0
End Select
End If
i = i + 1
ch = Mid(NMEA_buff, i, 1)
End While
ErrHandler:
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.

SASHI