Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Capture RS-232 Using MSComm (Variable Length Strings)

Posted on 2005-05-08
5
Medium Priority
?
306 Views
Last Modified: 2008-02-01
Hi All,

Now that I have nutted out the decoding of NMEA sentences thanks to help from EE, I would like to capture NMEA strings on the serial port. I have done it before with other hardware devices, but I can only remember doing it with inputs that have a fixed length. I would like to read the entire "sentence" and then pass that to the function that parses it.

I would prefer event driven communications and don't want to use any timers or such.

Here are some sample sentences that I have received in hyperterminal:

'Begin
$GPGSV,2,1,07,02,65,158,,10,62,013,47,05,52,302,49,30,39,243,*76
$GPGSV,2,2,07,04,27,122,,06,18,234,31,13,09,127,*49
$GPRMC,083428.659,V,3658.4795,S,17454.3913,E,,,040505,,*0E
$GPGGA,083429.659,3658.4795,S,17454.3913,E,0,00,50.0,110.0,M,,,,0000*24
$GPGSA,A,1,,,,,,,,,,,,,50.0,50.0,50.0*05
$GPRMC,083429.659,V,3658.4795,S,17454.3913,E,,,040505,,*0F
'End

What would be the best way to capture the sentences complete?

Sentences are terminated by CRLF

Cheers,
Sean
0
Comment
Question by:nzfire
  • 2
4 Comments
 
LVL 77

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 13957513
Here is some simple event-driven code.

I'm not sure if your 'Begin and 'End are actually received. If not, then I think you'd need a timer to tell when the device has stopped sending.

Dim strLines() As String
Dim i As Integer
Private Sub Command1_Click()
    With MSComm1
        .Port = 1
        .Settings = "9600,N,8,1"
        .RThreshold = 1
        .PortOpen = True
    End With
    ReDim strLines(0)
End Sub

Private Sub MSComm1_OnComm()
    Dim strChar As String
    With MSComm1
        If .CommEvent = comEvReceive Then
            strChar = .Input
            If strChar = vbCrLf Then
                i = i + 1
            Else
                If i > UBound(strLines) Then
                    ReDim Preserve strLines(i)
                End If
                strLines(i) = strLines(i) & strChar
            End If
        End If
    End With
End Sub
0
 
LVL 13

Expert Comment

by:softplus
ID: 13957707
I'd process them one line at a time, i.e. copying GrahamSkan's code:
Private strSerialLine As String
Private Sub Command1_Click()
    With MSComm1
        .Port = 1
        .Settings = "9600,N,8,1"
        .RThreshold = 1
        .PortOpen = True
    End With
    strSerialLine = ""
 End Sub

Private Sub MSComm1_OnComm()
    Dim strChar As String
    With MSComm1
        If .CommEvent = comEvReceive Then
            strChar = .Input
            If strChar = vbCr Then
                Call ProcessNMEALine(strSerialLine)
            Elseif strChar <> vbLf then
                strSerialLine = strSerialLine & strChar
            End If
        End If
    End With
End Sub

Another quick fix - vbCrLf = 2 characters (CR + LF), so you can't really check for that. Just check for vbCr and ignore vbLf's, that'll do the trick. Also, you might add a general check for valid characters (asc()<32, asc()>=asc("a")) and ignore lines that come in with an error (ah the joys of serial communication).
0
 
LVL 1

Author Comment

by:nzfire
ID: 13992498
GrahamSkan,

Nope, there is no beginning and no end. The data is coming from a GPS unit, and is continuous... From that perspective, it makes it hard!!
0
 
LVL 77

Expert Comment

by:GrahamSkan
ID: 13993317
So that would make a timer an optional extra, to monitor for signal loss.
If it is continuous then softplus' point about processing on-the-fly is valid, as is the one about carriage-return and line-feed being two characters.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question