Please consider the following naive program which you can start with the different arguments.
On my computer I had COM5 and COM6 to play with, please cahnge according to your setup.
Please connect a cable between the ports (Crossed wiring).
This program is not the answer to your question.
To answer your question: Use e.g. PuTTY and set it up with 9600, 7 databits, none parity, one stopbit (same as program).
When using PuTTY a press on the eneter button generates only a 0x0d (CR), to generate a 0x0a (LF) you must press ctrl-j.
Start the program with e.g. BytesFromStream as an argument (just to check what is really entering the port, byte for byte).
Then start PuTTY and press the following sequence of buttons (commas excluded): A,B,C,<Enter>
You will se the bytes shown in console (last byte 0x0d)
Try also A,B,C,<Enter>,<ctrl-j> and observe that last 2 printouts are 0x0d and 0x0a
Restart the program with argument LinesFromPort and try the same sequences as above.
Lines are printed either they are terminated with 0x0d only or 0x0a only or 0x0d 0x0a
Finally restart the program with argument LinesFromStreamReader and observe that terminating the stream with only 0x0d (CR) does not make the ReadLines returning.
To make the StreamReader.ReadLine to return the line it must be terminated with both 0x0d and 0x0a (CRLF).
This is as far as I can understand not according to the class documentation of StreamReader.
If you modify the program by using a filestream instead of SerialStream (which is the internal class of SerialPort.BaseStream) and the file lacks the 0x0a (LF) a StreamReader on this stream will indeed return the line (according to documentation).
StreamReader based on a MemoryStream behaves good as well, haven't tried a NetworkStream yet.
Src:
using System;
using System.IO;
using System.IO.Ports;
namespace SerialPortTestConsole
{
class Program
{
static void Main(string[] args)
{
SerialPort port = new SerialPort("COM6", 9600, Parity.None, 7, StopBits.One);
port.Open();
port.DiscardInBuffer();
port.DiscardOutBuffer();
Stream stream = port.BaseStream;
if (args.Length == 0 || args[0] == "LinesFromStreamReader")
{
StreamReader reader = new StreamReader(stream);
while (true)
{
Console.WriteLine($"Line received by StreamReader.ReadLine(): {reader.ReadLine()}");
}
}
else if (args[0] == "BytesFromStream")
{
while (true)
{
Console.WriteLine($"Byte read by Stream.ReadByte(): 0x{((byte)stream.ReadByte()):x2}");
}
}
else if (args[0] == "LinesFromPort")
{
port.NewLine = "\r";
while (true)
{
Console.WriteLine($"Line received by Port.ReadLine(): {port.ReadLine()}");
}
}
else Console.WriteLine("Wrong argument");
}
}
}