-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
ReadByte.cs
365 lines (296 loc) · 12.4 KB
/
ReadByte.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.IO.PortsTests;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Legacy.Support;
using Xunit;
namespace System.IO.Ports.Tests
{
public class ReadByte : PortsTest
{
//The number of random bytes to receive
private const int numRndByte = 8;
private enum ReadDataFromEnum { NonBuffered, Buffered, BufferedAndNonBuffered };
#region Test Cases
[ConditionalFact(nameof(HasLoopbackOrNullModem))]
public void ASCIIEncoding()
{
Debug.WriteLine("Verifying read with bytes encoded with ASCIIEncoding");
VerifyRead(new ASCIIEncoding());
}
[ConditionalFact(nameof(HasLoopbackOrNullModem))]
public void UTF8Encoding()
{
Debug.WriteLine("Verifying read with bytes encoded with UTF8Encoding");
VerifyRead(new UTF8Encoding());
}
[ConditionalFact(nameof(HasLoopbackOrNullModem))]
public void UTF32Encoding()
{
Debug.WriteLine("Verifying read with bytes encoded with UTF32Encoding");
VerifyRead(new UTF32Encoding());
}
[ConditionalFact(nameof(HasLoopbackOrNullModem))]
public void SerialPort_ReadBufferedData()
{
VerifyRead(Encoding.ASCII, ReadDataFromEnum.Buffered);
}
[ConditionalFact(nameof(HasLoopbackOrNullModem))]
public void SerialPort_IterativeReadBufferedData()
{
VerifyRead(Encoding.ASCII, ReadDataFromEnum.Buffered);
}
[ConditionalFact(nameof(HasLoopbackOrNullModem))]
public void SerialPort_ReadBufferedAndNonBufferedData()
{
VerifyRead(Encoding.ASCII, ReadDataFromEnum.BufferedAndNonBuffered);
}
[ConditionalFact(nameof(HasLoopbackOrNullModem))]
public void SerialPort_IterativeReadBufferedAndNonBufferedData()
{
VerifyRead(Encoding.ASCII, ReadDataFromEnum.BufferedAndNonBuffered);
}
[ConditionalFact(nameof(HasLoopbackOrNullModem))]
public void Read_DataReceivedBeforeTimeout()
{
using (SerialPort com1 = TCSupport.InitFirstSerialPort())
using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
{
byte[] byteXmitBuffer = TCSupport.GetRandomBytes(512);
byte[] byteRcvBuffer = new byte[byteXmitBuffer.Length];
ASyncRead asyncRead = new ASyncRead(com1);
var asyncReadTask = new Task(asyncRead.Read);
Debug.WriteLine(
"Verifying that ReadByte() will read bytes that have been received after the call to Read was made");
com1.Encoding = Encoding.UTF8;
com2.Encoding = Encoding.UTF8;
com1.ReadTimeout = 20000; // 20 seconds
com1.Open();
if (!com2.IsOpen) //This is necessary since com1 and com2 might be the same port if we are using a loopback
com2.Open();
asyncReadTask.Start();
asyncRead.ReadStartedEvent.WaitOne();
//This only tells us that the thread has started to execute code in the method
Thread.Sleep(2000); //We need to wait to guarantee that we are executing code in SerialPort
com2.Write(byteXmitBuffer, 0, byteXmitBuffer.Length);
asyncRead.ReadCompletedEvent.WaitOne();
if (null != asyncRead.Exception)
{
Fail("Err_04448ajhied Unexpected exception thrown from async read:\n{0}", asyncRead.Exception);
}
else if (asyncRead.Result != byteXmitBuffer[0])
{
Fail("Err_0158ahei Expected ReadChar to read {0}({0:X}) actual {1}({1:X})", byteXmitBuffer[0], asyncRead.Result);
}
else
{
Thread.Sleep(1000); //We need to wait for all of the bytes to be received
byteRcvBuffer[0] = (byte)asyncRead.Result;
int readResult = com1.Read(byteRcvBuffer, 1, byteRcvBuffer.Length - 1);
if (1 + readResult != byteXmitBuffer.Length)
{
Fail("Err_051884ajoedo Expected Read to read {0} bytes actually read {1}",
byteXmitBuffer.Length - 1, readResult);
}
else
{
for (int i = 0; i < byteXmitBuffer.Length; ++i)
{
if (byteRcvBuffer[i] != byteXmitBuffer[i])
{
Fail(
"Err_05188ahed Characters differ at {0} expected:{1}({1:X}) actual:{2}({2:X}) asyncRead.Result={3}",
i, byteXmitBuffer[i], byteRcvBuffer[i], asyncRead.Result);
}
}
}
}
TCSupport.WaitForTaskCompletion(asyncReadTask);
}
}
#endregion
#region Verification for Test Cases
private void VerifyRead(Encoding encoding)
{
VerifyRead(encoding, ReadDataFromEnum.NonBuffered);
}
private void VerifyRead(Encoding encoding, ReadDataFromEnum readDataFrom)
{
using (SerialPort com1 = TCSupport.InitFirstSerialPort())
using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
{
Random rndGen = new Random(-55);
int bufferSize = numRndByte;
byte[] byteXmitBuffer = new byte[bufferSize];
// Generate random bytes
for (int i = 0; i < byteXmitBuffer.Length; i++)
{
byteXmitBuffer[i] = (byte)rndGen.Next(0, 256);
}
com1.ReadTimeout = 500;
com1.Encoding = encoding;
TCSupport.SetHighSpeed(com1, com2);
com1.Open();
if (!com2.IsOpen) //This is necessary since com1 and com2 might be the same port if we are using a loopback
com2.Open();
switch (readDataFrom)
{
case ReadDataFromEnum.NonBuffered:
VerifyReadNonBuffered(com1, com2, byteXmitBuffer);
break;
case ReadDataFromEnum.Buffered:
VerifyReadBuffered(com1, com2, byteXmitBuffer);
break;
case ReadDataFromEnum.BufferedAndNonBuffered:
VerifyReadBufferedAndNonBuffered(com1, com2, byteXmitBuffer);
break;
default:
throw new ArgumentOutOfRangeException(nameof(readDataFrom));
}
}
}
private void VerifyReadNonBuffered(SerialPort com1, SerialPort com2, byte[] bytesToWrite)
{
VerifyBytesReadOnCom1FromCom2(com1, com2, bytesToWrite, bytesToWrite);
}
private void VerifyReadBuffered(SerialPort com1, SerialPort com2, byte[] bytesToWrite)
{
BufferData(com1, com2, bytesToWrite);
PerformReadOnCom1FromCom2(com1, com2, bytesToWrite);
}
private void VerifyReadBufferedAndNonBuffered(SerialPort com1, SerialPort com2, byte[] bytesToWrite)
{
byte[] expectedBytes = new byte[(2 * bytesToWrite.Length)];
BufferData(com1, com2, bytesToWrite);
Buffer.BlockCopy(bytesToWrite, 0, expectedBytes, 0, bytesToWrite.Length);
Buffer.BlockCopy(bytesToWrite, 0, expectedBytes, bytesToWrite.Length, bytesToWrite.Length);
VerifyBytesReadOnCom1FromCom2(com1, com2, bytesToWrite, expectedBytes);
}
private void BufferData(SerialPort com1, SerialPort com2, byte[] bytesToWrite)
{
com2.Write(bytesToWrite, 0, 1); // Write one byte at the beginning because we are going to read this to buffer the rest of the data
com2.Write(bytesToWrite, 0, bytesToWrite.Length);
TCSupport.WaitForReadBufferToLoad(com1, bytesToWrite.Length);
com1.Read(new char[1], 0, 1); // This should put the rest of the bytes in SerialPorts own internal buffer
if (com1.BytesToRead != bytesToWrite.Length)
{
Fail("Err_7083zaz Expected com1.BytesToRead={0} actual={1}", bytesToWrite.Length, com1.BytesToRead);
}
}
private void VerifyBytesReadOnCom1FromCom2(SerialPort com1, SerialPort com2, byte[] bytesToWrite, byte[] expectedBytes)
{
com2.Write(bytesToWrite, 0, bytesToWrite.Length);
com1.ReadTimeout = 500;
Thread.Sleep((int)(((bytesToWrite.Length * 10.0) / com1.BaudRate) * 1000) + 250);
PerformReadOnCom1FromCom2(com1, com2, expectedBytes);
}
private void PerformReadOnCom1FromCom2(SerialPort com1, SerialPort com2, byte[] expectedBytes)
{
byte[] byteRcvBuffer = new byte[expectedBytes.Length];
int readInt;
int i;
i = 0;
while (true)
{
try
{
readInt = com1.ReadByte();
}
catch (TimeoutException)
{
break;
}
//While their are more bytes to be read
if (expectedBytes.Length <= i)
{
//If we have read in more bytes then were actually sent
Fail("ERROR!!!: We have received more bytes then were sent");
break;
}
byteRcvBuffer[i] = (byte)readInt;
if (readInt != expectedBytes[i])
{
//If the byte read is not the expected byte
Fail("ERROR!!!: Expected to read {0} actual read byte {1}", (int)expectedBytes[i], readInt);
}
i++;
if (expectedBytes.Length - i != com1.BytesToRead)
{
Fail("ERROR!!!: Expected BytesToRead={0} actual={1}", expectedBytes.Length - i, com1.BytesToRead);
}
}
if (0 != com1.BytesToRead)
{
Fail("ERROR!!!: Expected BytesToRead=0 actual BytesToRead={0}", com1.BytesToRead);
}
if (com1.IsOpen)
com1.Close();
if (com2.IsOpen)
com2.Close();
}
public class ASyncRead
{
private readonly SerialPort _com;
private int _result;
private readonly AutoResetEvent _readCompletedEvent;
private readonly AutoResetEvent _readStartedEvent;
private Exception _exception;
public ASyncRead(SerialPort com)
{
_com = com;
_result = int.MinValue;
_readCompletedEvent = new AutoResetEvent(false);
_readStartedEvent = new AutoResetEvent(false);
_exception = null;
}
public void Read()
{
try
{
_readStartedEvent.Set();
_result = _com.ReadByte();
}
catch (Exception e)
{
_exception = e;
}
finally
{
_readCompletedEvent.Set();
}
}
public AutoResetEvent ReadStartedEvent
{
get
{
return _readStartedEvent;
}
}
public AutoResetEvent ReadCompletedEvent
{
get
{
return _readCompletedEvent;
}
}
public int Result
{
get
{
return _result;
}
}
public Exception Exception
{
get
{
return _exception;
}
}
}
#endregion
}
}