-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSerialTests.cs
More file actions
435 lines (399 loc) · 14.5 KB
/
Copy pathSerialTests.cs
File metadata and controls
435 lines (399 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
using nanoFramework.Hardware.Esp32;
using nanoFramework.TestFramework;
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Ports;
using System.Text;
using System.Threading;
namespace UnitTestsSerialPort
{
[TestClass]
public class SerialTests
{
static SerialPort _serOne;
static SerialPort _serTwo;
[Setup]
public void SetupComPorts()
{
OutputHelper.WriteLine("Setting up tests for an ESP32...");
try
{
OutputHelper.WriteLine("Please adjust for your own usage. If you need another hardware, please add the proper nuget and adjust as well");
Configuration.SetPinFunction(32, DeviceFunction.COM2_RX);
Configuration.SetPinFunction(33, DeviceFunction.COM2_TX);
Configuration.SetPinFunction(12, DeviceFunction.COM2_RTS);
Configuration.SetPinFunction(13, DeviceFunction.COM2_CTS);
Configuration.SetPinFunction(16, DeviceFunction.COM3_RX);
Configuration.SetPinFunction(17, DeviceFunction.COM3_TX);
Configuration.SetPinFunction(27, DeviceFunction.COM3_RTS);
Configuration.SetPinFunction(14, DeviceFunction.COM3_CTS);
OutputHelper.WriteLine("You will need to connect:");
OutputHelper.WriteLine(" COM2 RX <-> COM3 TX");
OutputHelper.WriteLine(" COM2 TX <-> COM3 RX");
OutputHelper.WriteLine(" COM2 RTS <-> COM3 CTS");
OutputHelper.WriteLine(" COM2 CTS <-> COM3 RTS");
_serOne = new SerialPort("COM2");
_serTwo = new SerialPort("COM3");
OutputHelper.WriteLine("SerialPorts created, trying to open them");
_serOne.Open();
OutputHelper.WriteLine("SerialPort One COM2 opened.");
_serTwo.Open();
OutputHelper.WriteLine("SerialPort Two COM3 opened.");
OutputHelper.WriteLine("All SerialPorts opened, will close them");
// Wait a bit just to make sure and close them all
Thread.Sleep(100);
EnsurePortsClosed();
OutputHelper.WriteLine("SerialPorts Closed.");
}
catch
{
Assert.SkipTest("Serial Ports not supported in this platform or not properly configured");
}
}
[TestMethod]
public void GetPortNamesTest()
{
var ports = SerialPort.GetPortNames();
OutputHelper.WriteLine("Available SerialPorts:");
foreach (string port in ports)
{
OutputHelper.WriteLine($" {port}");
}
}
[TestMethod]
public void BasicReadWriteTests()
{
// Arrange
EnsurePortsOpen();
_serOne.WriteTimeout = 1000;
_serOne.ReadTimeout = 1000;
_serTwo.WriteTimeout = 1000;
_serTwo.ReadTimeout = 1000;
byte[] toSend = new byte[] { 0x42, 0xAA, 0x11, 0x00 };
byte[] toReceive = new byte[toSend.Length];
// Act
_serOne.Write(toSend, 0, toSend.Length);
// Give some time for the first com to send the data
Thread.Sleep(100);
_serTwo.Read(toReceive, 0, toReceive.Length);
// Assert
for (int i = 0; i < toSend.Length; i++)
{
Assert.Equal(toSend[i], toReceive[i]);
}
}
[TestMethod]
public void VerifyDefaultReadLineCharacter()
{
// Arrange
EnsurePortsOpen();
_serOne.WriteTimeout = 1000;
_serOne.ReadTimeout = 1000;
_serTwo.WriteTimeout = 1000;
_serTwo.ReadTimeout = 1000;
string toSend = "This is a simple test for verifying the default readline character \r\n \\r";
// Act
_serOne.Write(toSend);
string toReceive = _serTwo.ReadLine();
// Assert
Assert.Equal(toSend, toReceive + "\\r" + _serOne.NewLine);
}
[TestMethod]
public void WriteAndReadStringTests()
{
// Arrange
EnsurePortsOpen();
_serOne.WriteTimeout = 1000;
_serOne.ReadTimeout = 1000;
_serTwo.WriteTimeout = 1000;
_serTwo.ReadTimeout = 1000;
string toSend = "Hi, this is a simple test with string";
// Act
_serOne.WriteLine(toSend);
string toReceive = _serTwo.ReadLine();
// Assert
Assert.Equal(toSend + _serOne.NewLine, toReceive);
}
[TestMethod]
public void ReadMultipleLinesTests()
{
// Arrange
EnsurePortsOpen();
_serOne.WriteTimeout = 1000;
_serOne.ReadTimeout = 1000;
_serTwo.WriteTimeout = 1000;
_serTwo.ReadTimeout = 1000;
string toSend = $"Hi, this is a simple test with string{_serOne.NewLine}And with a second line{_serOne.NewLine}Only line by line should be read{_serOne.NewLine}";
// Act
_serOne.WriteLine(toSend);
string toReceive = _serTwo.ReadLine();
// Assert
Assert.Equal($"Hi, this is a simple test with string{_serOne.NewLine}", toReceive);
toReceive = _serTwo.ReadLine();
Assert.Equal($"And with a second line{_serOne.NewLine}", toReceive);
toReceive = _serTwo.ReadLine();
Assert.Equal($"Only line by line should be read{_serOne.NewLine}", toReceive);
}
[TestMethod]
public void CheckReadByteSize()
{
// Arrange
EnsurePortsOpen();
EnsurePortEmpty(_serTwo);
string toSend = $"I ❤ nanoFramework{_serOne.NewLine}";
int enc = Encoding.UTF8.GetBytes(toSend).Length;
// Act
_serOne.Write(toSend);
// Wait a bit to have data sent
Thread.Sleep(100);
// Assert
Assert.Equal(enc, _serTwo.BytesToRead);
// Read remaining
string sent = _serTwo.ReadExisting();
Assert.Equal(toSend, sent);
}
[TestMethod]
public void CheckReadLineWithoutAnything()
{
long dtOrigine = DateTime.UtcNow.Ticks;
Assert.Throws(typeof(TimeoutException), () =>
{
// Arrange
EnsurePortsOpen();
_serTwo.ReadTimeout = 2000;
EnsurePortEmpty(_serTwo);
// Act
var nothingToRead = _serTwo.ReadLine();
// Assert
});
long dtTimeout = DateTime.UtcNow.Ticks;
Assert.True(dtTimeout >= dtOrigine + _serTwo.ReadTimeout * TimeSpan.TicksPerMillisecond);
}
[TestMethod]
public void CheckReadTimeoutTest()
{
Assert.Throws(typeof(TimeoutException), () =>
{
// Arrange
EnsurePortsOpen();
_serTwo.ReadTimeout = 1000;
// Ensure nothing to read, if yes, read all
EnsurePortEmpty(_serTwo);
byte[] toReadTimeout = new byte[5];
_serTwo.Read(toReadTimeout, 0, toReadTimeout.Length);
});
}
[TestMethod]
public void ReadByteWriteByteTests()
{
// Arrange
EnsurePortsOpen();
_serOne.WriteTimeout = 1000;
_serOne.ReadTimeout = 1000;
_serTwo.WriteTimeout = 1000;
_serTwo.ReadTimeout = 1000;
EnsurePortEmpty(_serOne);
EnsurePortEmpty(_serTwo);
byte[] toWrite = new byte[] { 0, 42, 0 };
byte toRead;
// Act
_serOne.Write(toWrite, 1, 1);
// Wait to make sure it will be send
Thread.Sleep(100);
toRead = (byte)_serTwo.ReadByte();
// Assert
Assert.Equal(toWrite[1], toRead);
}
[TestMethod]
public void CheckBytesAvailableTest()
{
// Arrange
EnsurePortsOpen();
_serOne.WriteTimeout = 1000;
_serOne.ReadTimeout = 1000;
_serTwo.WriteTimeout = 1000;
_serTwo.ReadTimeout = 1000;
EnsurePortEmpty(_serOne);
EnsurePortEmpty(_serTwo);
byte[] lotsOfBytes = new byte[42];
// Act
_serOne.Write(lotsOfBytes, 0, lotsOfBytes.Length);
// Wait to make sure it will be send
Thread.Sleep(100);
var numBytes = _serTwo.BytesToRead;
// Clean
EnsurePortEmpty(_serTwo);
// Assert
Assert.Equal(lotsOfBytes.Length, numBytes);
}
[TestMethod]
public void TryReadWriteWhileClosed()
{
EnsurePortsClosed();
Assert.Throws(typeof(InvalidOperationException), () =>
{
_serOne.Write("Something");
});
Assert.Throws(typeof(InvalidOperationException), () =>
{
byte[] something = new byte[5];
_serOne.Read(something, 0, something.Length);
});
}
[TestMethod]
public void AdjustBaudRateTests()
{
// Arrange
_serOne.BaudRate = 115200;
_serTwo.BaudRate = 115200;
// Act and Assert
SendAndReceiveBasic();
}
[TestMethod]
public void AdjustHandshakeTests()
{
// Arrange
_serOne.Handshake = Handshake.RequestToSend;
_serTwo.Handshake = Handshake.RequestToSend;
// Act and Assert
SendAndReceiveBasic();
_serOne.Handshake = Handshake.None;
_serTwo.Handshake = Handshake.None;
}
[TestMethod]
public void TestStreams()
{
// Arrange
EnsurePortsOpen();
_serOne.WriteTimeout = 1000;
_serOne.ReadTimeout = 1000;
_serTwo.WriteTimeout = 1000;
_serTwo.ReadTimeout = 1000;
EnsurePortEmpty(_serOne);
EnsurePortEmpty(_serTwo);
Stream serOneStream = _serOne.BaseStream;
Stream serTwoStream = _serTwo.BaseStream;
byte[] toSend = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] toReceive = new byte[toSend.Length];
// Act
serOneStream.Write(toSend, 0, toSend.Length);
Thread.Sleep(100);
serTwoStream.Read(toReceive, 0, toReceive.Length);
// Assert
for (int i = 0; i < toSend.Length; i++)
{
Assert.Equal(toSend[i], toReceive[i]);
}
}
[TestMethod]
public void TestEvents()
{
// Arrange
EnsurePortsOpen();
_serOne.WriteTimeout = 1000;
_serOne.ReadTimeout = 1000;
_serTwo.WriteTimeout = 1000;
_serTwo.ReadTimeout = 1000;
EnsurePortEmpty(_serOne);
EnsurePortEmpty(_serTwo);
_serTwo.DataReceived += DataReceivedNormalEventTest;
byte[] toSend = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
// Act
_serOne.Write(toSend, 0, toSend.Length);
// Wait for the data to be received
Thread.Sleep(200);
_serTwo.DataReceived -= DataReceivedNormalEventTest;
}
private void DataReceivedNormalEventTest(object sender, SerialDataReceivedEventArgs e)
{
var ser = (SerialPort)sender;
Debug.WriteLine($"Event fired, number of bytes ready to read: {ser.BytesToRead}");
Assert.Equal(8, ser.BytesToRead);
Assert.True(e.EventType == SerialData.Chars);
}
[TestMethod]
public void TestWatchCharEvents()
{
// Arrange
EnsurePortsOpen();
_serOne.WriteTimeout = 1000;
_serOne.ReadTimeout = 1000;
_serTwo.WriteTimeout = 1000;
_serTwo.ReadTimeout = 1000;
EnsurePortEmpty(_serOne);
EnsurePortEmpty(_serTwo);
_serTwo.DataReceived += DataReceivedWatchChar;
_serTwo.WatchChar = '\r';
string toSendWithWatchChar = "This is a test\r";
// Act
_serOne.Write(toSendWithWatchChar);
Thread.Sleep(200);
_serTwo.DataReceived -= DataReceivedWatchChar;
}
private void DataReceivedWatchChar(object sender, SerialDataReceivedEventArgs e)
{
var ser = (SerialPort)sender;
Debug.WriteLine($"Event fired, number of bytes ready to read: {ser.BytesToRead}");
Assert.True(e.EventType == SerialData.WatchChar);
}
private void SendAndReceiveBasic()
{
EnsurePortsOpen();
EnsurePortEmpty(_serTwo);
byte[] toSend = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] toReceive = new byte[toSend.Length];
// Act
_serOne.Write(toSend, 0, toSend.Length);
// Wait to make sure it will be send
Thread.Sleep(100);
_serTwo.Read(toReceive, 0, toReceive.Length);
// Assert
for (int i = 0; i < toSend.Length; i++)
{
Assert.Equal(toSend[i], toReceive[i]);
}
}
[Cleanup]
public void CleanPorts()
{
EnsurePortsClosed();
_serOne.Dispose();
_serTwo.Dispose();
}
private void EnsurePortsClosed()
{
if (_serOne.IsOpen)
{
_serOne.Close();
}
if (_serTwo.IsOpen)
{
_serTwo.Close();
}
}
private void EnsurePortEmpty(SerialPort port)
{
// Ensure nothing to read, if yes, read all
while (port.BytesToRead > 0)
{
port.ReadByte();
}
}
private void EnsurePortsOpen()
{
if (!_serOne.IsOpen)
{
_serOne.Open();
}
if (!_serTwo.IsOpen)
{
_serTwo.Open();
}
}
}
}