Solved

Serial Port: ReadLine vs. ReadExisting

Posted on 2008-06-21
5
14,463 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 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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

756 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