How to read all data across a serial COM

C

cmdolcet69

Im trying to raed data over a com port. The data comes over the serial
port as such reading CRLF reading CRLF and so on.

The code below only sees the first reading CRLF and then only added
that into the DSIDXPort arraylist and never adds readings 2 through
16.....

How can I get it to read those?

Code:
serialPort.ReadTimeout = 50
serialPort.DiscardInBuffer()

Dim strData As String = String.Empty

If Not serialPort.IsOpen Then
strData = String.Empty
Else
strData = serialPort.ReadLine
End If






If strData.Length > 0 Then

Dim tempArray() As String
strData = strData.Replace(vbCrLf,
vbTab).Replace(Chr(26), "").Replace(Chr(12), "").Replace(Chr(13), "")

'parses out the information gathered from the COM port
to a temp location
tempArray = Split(strData, vbTab)


If serialPort.ReadTimeout = 50 Then
Dim intLoop As Integer
For intLoop = 0 To UBound(tempArray)
If IsNumeric(tempArray(intLoop)) Then
DSIMRPort.Add(tempArray(intLoop))
End If

Next
End If
end if


[\CODE]
 
A

Armin Zingler

cmdolcet69 said:
Im trying to raed data over a com port. The data comes over the
serial port as such reading CRLF reading CRLF and so on.

I don't fully understand the meaning of this sentence.
The code below only sees the first reading CRLF and then only added
that into the DSIDXPort arraylist and never adds readings 2 through
16.....

How can I get it to read those?


I only see one "serialPort.ReadLine". When or where do you expect it to read
more?


Armin
 
J

James Hahn

I presume you mean that the format of the data stream is variable length
strings separated by CR and LF.

As VB is an event based language, you need to make use of the event that
indicates that some data has been received from the port. There is no event
available that tells you that a CRLF has been received, but the DataReceived
event will tell you when each character is received. You can use processing
in that event to see if the received character was a CR or LF - if it is,
then there is a data sentence in the buffer that can be retrieved. You can
then use Readline to retrieve the data from the buffer.

You should not use readline unless you know there is a line to be read.
 
D

Dick Grier

I'd have to see the actual code. You call ReadLine each time you want to
read a line (seems clear). Do you have a loop for reading data? Are you
calling this in the DataReceived event (if so, the you certainly cannot call
DiscardInBuffer).


--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top