Skip to content

Commit 4824268

Browse files
Align default newline with .NET (#84)
***NO_CI***
1 parent fa420c6 commit 4824268

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ You can as well write and read strings:
9393
```csharp
9494
string toSend = "I ❤ nanoFramework";
9595
port.WriteLine(toSend);
96-
// this will send the string encoded finishing by a new line, by default \r\n
97-
// You can change the new line by anything:
96+
// this will send the string encoded finishing by a new line, by default `\n`
97+
// You can change the new line to be anything:
9898
port.NewLine = "❤❤";
99-
// Now it will send the 2 hearts as the end of line while operating a ReadLine or WriteLine
100-
// You can ad anytime change it back:
101-
port.NewLine = SerialPort.DefaultNewLine; // default is "\r\n"
99+
// Now it will send 2 hearts as the line ending `WriteLine` and will use 2 hearts as the terminator for `ReadLine`.
100+
// You can change it back to the `\n` default at anytime:
101+
port.NewLine = SerialPort.DefaultNewLine; // default is "\n"
102102
// This will read the existing buffer:
103103
string existingString = port.ReadExisting();
104104
// Note that if it can't properly convert the bytes to a string, you'll get an exception

System.IO.Ports/SerialPort.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public sealed class SerialPort : IDisposable
1515
{
1616
// default new line
1717
[System.Diagnostics.DebuggerBrowsable(Diagnostics.DebuggerBrowsableState.Never)]
18-
private const string _defaultNewLine = "\r";
18+
private const string _defaultNewLine = "\n";
1919

2020
[System.Diagnostics.DebuggerBrowsable(Diagnostics.DebuggerBrowsableState.Never)]
2121
private static readonly SerialDeviceEventListener s_eventListener = new();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SerialPort Unit Tests
2+
Note:
3+
The current unit tests only run on real hardware, and are currently setup for ESP32 devices with `COM2` and `COM3` available and interconnected.
4+
Other devices can be added easily by changing the `SetupComPorts` in [SerialTests.cs](SerialTests.cs).
5+

Tests/UnitTestsSerialPort/SerialTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,23 @@ public void BasicReadWriteTests()
9393
}
9494
}
9595

96+
[TestMethod]
97+
public void VerifyDefaultReadLineCharacter()
98+
{
99+
// Arrange
100+
EnsurePortsOpen();
101+
_serOne.WriteTimeout = 1000;
102+
_serOne.ReadTimeout = 1000;
103+
_serTwo.WriteTimeout = 1000;
104+
_serTwo.ReadTimeout = 1000;
105+
string toSend = "This is a simple test for verifying the default readline character \r\n \\r";
106+
// Act
107+
_serOne.Write(toSend);
108+
string toReceive = _serTwo.ReadLine();
109+
// Assert
110+
Assert.Equal(toSend, toReceive + "\\r" + _serOne.NewLine);
111+
}
112+
96113
[TestMethod]
97114
public void WriteAndReadStringTests()
98115
{

0 commit comments

Comments
 (0)