﻿using System.Linq;
using System.Text;

namespace Modbus.IO
{
    internal static class StreamResourceUtility
    {
        internal static string ReadLine(IStreamResource stream)
        {
            var result = new StringBuilder();
            var singleByteBuffer = new byte[1];

            do
            {
                stream.Read(singleByteBuffer, 0, 1);
                result.Append(Encoding.ASCII.GetChars(singleByteBuffer).First());
            } while (!result.ToString().EndsWith(Modbus.NewLine));

            return result.ToString().Substring(0, result.Length - Modbus.NewLine.Length);
        }
    }
}
﻿using System.Linq;
using System.Text;

namespace Modbus.IO
{
    internal static class StreamResourceUtility
    {
        internal static string ReadLine(IStreamResource stream)
        {
            var result = new StringBuilder();
            var singleByteBuffer = new byte[1];

            do
            {
                if (0 == stream.Read(singleByteBuffer, 0, 1))
                    continue;

                result.Append(Encoding.ASCII.GetChars(singleByteBuffer).First());
            } while (!result.ToString().EndsWith(Modbus.NewLine));

            return result.ToString().Substring(0, result.Length - Modbus.NewLine.Length);
        }
    }
}

Add checking of Read() return value.
Add checking of Read() return value.

Without this using of non-initialized (0) value or value from the previous step of loop.
