nzfire
asked on
Serial Port: ReadLine vs. ReadExisting
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
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
ASKER
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!!!!
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!!!!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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..