?
Solved

Serial Port: ReadLine vs. ReadExisting

Posted on 2008-06-21
5
Medium Priority
?
14,990 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

801 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