[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


https://bugzilla.novell.com/show_bug.cgi?id=321988#c2





--- Comment #2 from Michael Mattess <michaelmattess at rauland.com.au>  2007-11-04 19:14:34 MST ---
Hi
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
characters.

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


public string ReadLine (){
    CheckOpen();
    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) {
            break;
        }

        bytes_read.Add(buff[0]);


        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;
                    break;
                }
            }

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

            new_line_offset++;
        }
    }


-- 
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