?
Solved

Serial Port: ReadLine vs. ReadExisting

Posted on 2008-06-21
5
Medium Priority
?
15,617 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
3 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

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

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

840 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