[Mono-bugs] [Bug 321988] System.IO.Ports.SerialPort ReadLine behaviour differs from MS .NET implementation

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Sun Nov 4 21:14:35 EST 2007


--- Comment #2 from Michael Mattess <michaelmattess at rauland.com.au>  2007-11-04 19:14:34 MST ---
Here is a possible replacement for the ReadLine function.  It uses the full
new_line string to detect the end of the line.

Also it still works if NewLine is set to an empty string (which is not
permitted in MS .Net).  In this case it just reads and returns single

One could add some code to more efficiently handle the specific case where
NewLine is a single character.

public string ReadLine (){
    List<byte> bytes_read = new List<byte>();
    byte[] buff = new byte[1];
    int new_line_offset = 0;

    while(true) {
        int n = stream.Read(buff, 0, 1);

        if(n == -1) {


        if(bytes_read.Count >= new_line.Length) {
            bool isNewLine = true;

            //check if we have read a NewLine string.
            for(int i = 0; i < new_line.Length; i++) {
                if(bytes_read[new_line_offset + i] != new_line[i]) {
                    isNewLine = false;

            if(isNewLine) {
                //Remove the NewLine string from the read line.
                bytes_read.RemoveRange(new_line_offset, new_line.Length);


Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.

More information about the mono-bugs mailing list