|
-
August 24th, 2005, 04:28 AM
#1
Detecting line feed and carriage return
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
-
August 24th, 2005, 08:36 AM
#2
Re: Detecting line feed and carriage return
I would check 2 things
1 - Check if your string is in UNICODE or ASCII. Might be different code
2 - Check if you receive any line feed and or Carriage return. Maybe the function you use to read the stream stripe em off.
oh and by the way
DO NOT USE
"On Error GoTo ErrHandler"
use
Try
catch ex as exception
end try
I would change your logic. I mean if $ is starting a packet why you need the CR, LF.. just read the same packet till you get a $.
Last edited by Boumxyz2; August 24th, 2005 at 08:41 AM.
Nicolas Bohemier
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|