Solved

Serial Port: ReadLine vs. ReadExisting

Posted on 2008-06-21
5
13,714 Views
Last Modified: 2013-11-07
Hi All,

I am back playing with serial ports and stuff. I would like to know if there is an advantage (programatically) between the .ReadLine() and .ReadExisiting() functions of the SerialPort class.

I have previously used the .ReadExisting() (and built up a string which I then process) in the DataReceived event.

I am able to specify on my device, the "header" and "footer" strings to send. If I send a \r\n (new line) I can use the .ReadLine function.

Recently I have seen that data coming from the device has been missing characters etc, and am wondering if it is because the thread which processes the DataReceived Event is still busy when more data is arriving.

Is it better to use the ReadLine function? Does the ReadLine function clear the port's InBuffer when it is run, or do I still have to clear the buffer?

Oh and by the way, I am using vs2008. I've heard the the 3.x versions of the SerialPort class are riddled with problems, so am using the 2.0 framework for this particular project.

Thanks

Sean

0
Comment
Question by:nzfire
  • 2
5 Comments
 
LVL 14

Expert Comment

by:rachitkohli
ID: 21840121
I had faced problems using .ReadLine() method, even i had some characters missing.
Lately I tried using .ReadExisting() method, which helped me receiving the entire data in a single event. But then i faced a problem with encoding, which i tried changing the encoding type & found the correct results.
Similarly like you had some trouble with the threads as well, I introduced the Delay method (i.e. Thread.Sleep(1000)) which helped me solved the problem.
ReadLine can be used if an end of line is expected, else ReadExisting is best for reading the entire data,..
Try having a Delay, hopefully u would have complete data..
0
 
LVL 1

Author Comment

by:nzfire
ID: 21840615
hi rachitkohli,

Thanks for the reply. Do you have a sample snippet?

My data is a variable length, so I have to have the receive threshold set at 1.

I always expect the new line, so I can use readline, but am thinking if I combine that with a delay like you mention, then I might be cooking!!!!
0
 
LVL 14

Accepted Solution

by:
rachitkohli earned 500 total points
ID: 21843996
Following code i have written for you. Try that.


Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

        'This event will Receive the data from the selected COM port..

        If e.EventType = SerialData.Chars Then

            thRec = New Thread(AddressOf ReceiveData)

            thRec.IsBackground = True

            thRec.Priority = ThreadPriority.Highest

            thRec.Start()

            Thread.Sleep(2)

        End If
 

    End Sub
 
 

Private Sub ReceiveData()

        'Sub to Receive Data from the Serial Port, Will Run in a Thread

        Dim lstItem As ListViewItem

        Dim bRead, nRead As Integer

        Dim returnStr As String = ""

        Dim ascStr As String = ""
 

        bRead = SerialPort1.BytesToRead 'Number of Bytes to read

        Dim cData(bRead - 1) As Byte
 

        SerialPort1.Encoding = Encoding.GetEncoding(65001)
 

        nRead = SerialPort1.Read(cData, 0, bRead)  'Reading the Data

        For Each b As Byte In cData

            ascStr += Chr(b)        'Ascii String

            returnStr += Hex(b).PadLeft(2, "0")     'Hex String (Modified Padding, to intake compulsory 2 chars, mainly in case of 0)

        Next

	Debug.WriteLine (returnStr)

End Sub

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now