Solved

Serial Port: ReadLine vs. ReadExisting

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

688 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