Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SerialPort.cs « System.IO.Ports « System « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1360e5441338c7e7272c9b464d3c0bc80b2ab8f7 (plain)
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/* -*- Mode: Csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

#if NET_2_0

using System;
using System.Text;
using System.Runtime.InteropServices;

namespace System.IO.Ports
{
	public class SerialPort /* : Component */
	{
		public const int InfiniteTimeout = -1;
		const int DefaultReadBufferSize = 4096;
		const int DefaultWriteBufferSize = 2048;

		bool   isOpen     = false;
		int    baudRate   = 9600;
		Parity parity     = Parity.None;
		StopBits stopBits = StopBits.One;
		Handshake handshake = Handshake.None;
		int    dataBits   = 8;
		bool   breakState = false;
		SerialPortStream stream;
		Encoding encoding = Encoding.ASCII;
		string newLine    = Environment.NewLine;
		string portName;
		int    readTimeout = InfiniteTimeout;
		int    writeTimeout = InfiniteTimeout;
		int readBufferSize = DefaultReadBufferSize;
		int writeBufferSize = DefaultWriteBufferSize;
		int readBufferOffset;
		int readBufferLength;
		int writeBufferOffset;
		int writeBufferLength;
		byte [] readBuffer;
		byte [] writeBuffer;

		public SerialPort ()
		{
			throw new NotImplementedException ();
		}

		/*
		  IContainer is in 2.0?
		  public SerialPort (IContainer container) {
		  }
		*/

		public SerialPort (string portName)
		{
			this.portName = portName;
		}

		public SerialPort (string portName, int baudRate)
		{
			this.portName = portName;
			this.baudRate = baudRate;
		}

		public SerialPort (string portName, int baudRate, Parity parity)
		{
			this.portName = portName;
			this.baudRate = baudRate;
			this.parity = parity;
		}

		public SerialPort (string portName, int baudRate, Parity parity, int dataBits)
		{
			this.portName = portName;
			this.baudRate = baudRate;
			this.parity = parity;
			this.dataBits = dataBits;
		}

		public SerialPort (string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits) 
		{
			this.portName = portName;
			this.baudRate = baudRate;
			this.parity = parity;
			this.dataBits = dataBits;
			this.stopBits = stopBits;
		}

		public Stream BaseStream {
			get {
				if (!isOpen)
					throw new InvalidOperationException ();

				return stream;
			}
		}

		public int BaudRate {
			get {
				return baudRate;
			}
			set {
				if (value <= 0)
					throw new ArgumentOutOfRangeException ("value");
				
				baudRate = value;
				if (isOpen)
					stream.BaudRate = value;
			}
		}

		public bool BreakState {
			get {
				return breakState;
			}
			set {
				CheckOpen ();
				if (value == breakState)
					return; // Do nothing.

				breakState = value;
				// Update the state
			}
		}

		public int BytesToRead {
			get {
				CheckOpen ();
				return readBufferLength + stream.BytesToRead;
			}
		}

		public int BytesToWrite {
			get {
				CheckOpen ();
				return writeBufferLength + stream.BytesToWrite;
			}
		}

		public bool CDHolding {
			get {
				CheckOpen ();
				throw new NotImplementedException ();
			}
		}

		public bool CtsHolding {
			get {
				CheckOpen ();
				throw new NotImplementedException ();
			}
		}

		public int DataBits {
			get {
				return dataBits;
			}
			set {
				if (value < 5 || value > 8)
					throw new ArgumentOutOfRangeException ("value");

				dataBits = value;
				if (isOpen)
					stream.DataBits = value;
			}
		}

		public bool DiscardNull {
			get {
				CheckOpen ();
				throw new NotImplementedException ();
			}
			set {
				CheckOpen ();
				throw new NotImplementedException ();
			}
		}

		public bool DsrHolding {
			get {
				CheckOpen ();
				throw new NotImplementedException ();
			}
		}

		public bool DtrEnable {
			get {
				CheckOpen ();
				throw new NotImplementedException ();
			}
		}

		public Encoding Encoding {
			get {
				return encoding;
			}
			set {
				if (value == null)
					throw new ArgumentNullException ("value");

				encoding = value;
			}
		}

		public Handshake Handshake {
			get {
				return handshake;
			}
			set {
				if (value < Handshake.None || value > Handshake.RequestToSendXOnXOff)
					throw new ArgumentOutOfRangeException ("value");

				handshake = value;
				if (isOpen)
					stream.Handshake = value;
			}
		}

		public bool IsOpen {
			get {
				return isOpen;
			}
		}

		public string NewLine {
			get {
				return newLine;
			}
			set {
				if (value == null)
					throw new ArgumentNullException ("value");
				
				newLine = value;
			}
		}

		public Parity Parity {
			get {
				return parity;
			}
			set {
				if (value < Parity.None || value > Parity.Space)
					throw new ArgumentOutOfRangeException ("value");

				parity = value;
				if (isOpen)
					stream.Parity = value;
			}
		}

		public byte ParityReplace {
			get {
				throw new NotImplementedException ();
			}
			set {
				throw new NotImplementedException ();
			}
		}

		public string PortName {
			get {
				return portName;
			}
			set {
				if (isOpen)
					throw new InvalidOperationException ("Port name cannot be set while port is open.");
				if (value == null)
					throw new ArgumentNullException ("value");
				if (value.Length == 0 || value.StartsWith ("\\\\"))
					throw new ArgumentException ("value");

				portName = value;
			}
		}

		public int ReadBufferSize {
			get {
				return readBufferSize;
			}
			set {
				if (isOpen)
					throw new InvalidOperationException ();
				if (value <= 0)
					throw new ArgumentOutOfRangeException ("value");
				if (value <= DefaultReadBufferSize)
					return;

				readBufferSize = value;
			}
		}

		public int ReadTimeout {
			get {
				return readTimeout;
			}
			set {
				if (value <= 0 && value != InfiniteTimeout)
					throw new ArgumentOutOfRangeException ("value");

				readTimeout = value;
				/*if (isOpen)
					stream.ReadTimeout = value;*/
			}
		}

		public int ReceivedBytesThreshold {
			get {
				throw new NotImplementedException ();
			}
			set {
				if (value <= 0)
					throw new ArgumentOutOfRangeException ("value");

				throw new NotImplementedException ();
			}
		}

		public bool RtsEnable {
			get {
				CheckOpen ();
				throw new NotImplementedException ();
			}
			set {
				CheckOpen ();
				throw new NotImplementedException ();
			}
		}

		public StopBits StopBits {
			get {
				return stopBits;
			}
			set {
				if (value < StopBits.One || value > StopBits.OnePointFive)
					throw new ArgumentOutOfRangeException ("value");
				
				stopBits = value;
				if (isOpen)
					stream.StopBits = value;
			}
		}

		public int WriteBufferSize {
			get {
				return writeBufferSize;
			}
			set {
				if (isOpen)
					throw new InvalidOperationException ();
				if (value <= 0)
					throw new ArgumentOutOfRangeException ("value");
				if (value <= DefaultWriteBufferSize)
					return;

				writeBufferSize = value;
			}
		}

		public int WriteTimeout {
			get {
				return writeTimeout;
			}
			set {
				if (value <= 0 && value != InfiniteTimeout)
					throw new ArgumentOutOfRangeException ("value");

				writeTimeout = value;
				/*if (isOpen)
					stream.WriteTimeout = value;*/
			}
		}

		// methods

		public void Close ()
		{
			isOpen = false;

			if (stream != null)
				stream.Close ();
			
			stream = null;
			readBuffer = null;
			writeBuffer = null;
		}

		public void DiscardInBuffer ()
		{
			CheckOpen ();
			stream.DiscardInputBuffer ();
		}

		public void DiscardOutBuffer ()
		{
			CheckOpen ();
			stream.DiscardOutputBuffer ();
		}

		public static string [] GetPortNames ()
		{
			return new string [0]; // Return empty by now
		}

		public void Open ()
		{
			if (isOpen)
				throw new InvalidOperationException ("Port is already open");
			
			isOpen = true;
			stream = new SerialPortStream (this);
			readBuffer = new byte [readBufferSize];
			writeBuffer = new byte [writeBufferSize];
		}

		public int Read (byte[] buffer, int offset, int count)
		{
			CheckOpen ();
			if (buffer == null)
				throw new ArgumentNullException ("buffer");
			if (offset < 0 || offset >= buffer.Length)
				throw new ArgumentOutOfRangeException ("offset");
			if (count < 0 || count > buffer.Length)
				throw new ArgumentOutOfRangeException ("count");
			if (count > buffer.Length - offset)
				throw new ArgumentException ("count > buffer.Length - offset");
			
			if (readBufferLength <= 0) {
				readBufferOffset = 0;
				readBufferLength = stream.Read (readBuffer, 0, readBuffer.Length);
			}
			
			if (readBufferLength == 0)
				return 0; // No bytes left
			
			if (count > readBufferLength)
				count = readBufferLength; // Update count if needed

			Buffer.BlockCopy (readBuffer, readBufferOffset, buffer, offset, count);
			readBufferOffset += count;
			readBufferLength -= count;

			return count;
		}

		public int Read (char[] buffer, int offset, int count)
		{
			CheckOpen ();
			if (buffer == null)
				throw new ArgumentNullException ("buffer");
			if (offset < 0 || offset >= buffer.Length)
				throw new ArgumentOutOfRangeException ("offset");
			if (count < 0 || count > buffer.Length)
				throw new ArgumentOutOfRangeException ("count");
			if (count > buffer.Length - offset)
				throw new ArgumentException ("count > buffer.Length - offset");

			byte [] bytes = encoding.GetBytes (buffer, offset, count);
			return Read (bytes, 0, bytes.Length);
		}

		public int ReadByte ()
		{
			byte [] buff = new byte [1];
			if (Read (buff, 0, 1) > 0)
				return buff [0];

			return -1;
		}

		public int ReadChar ()
		{
			throw new NotImplementedException ();
		}

		public string ReadExisting ()
		{
			throw new NotImplementedException ();
		}

		public string ReadLine ()
		{
			return ReadTo (newLine);
		}

		public string ReadTo (string value)
		{
			CheckOpen ();
			if (value == null)
				throw new ArgumentNullException ("value");
			if (value.Length == 0)
				throw new ArgumentException ("value");

			throw new NotImplementedException ();
		}

		public void Write (string str)
		{
			CheckOpen ();
			if (str == null)
				throw new ArgumentNullException ("str");
			
			byte [] buffer = encoding.GetBytes (str);
			Write (buffer, 0, buffer.Length);
		}

		public void Write (byte [] buffer, int offset, int count)
		{
			CheckOpen ();
			if (buffer == null)
				throw new ArgumentNullException ("buffer");
			if (offset < 0 || offset >= buffer.Length)
				throw new ArgumentOutOfRangeException ("offset");
			if (count < 0 || count > buffer.Length)
				throw new ArgumentOutOfRangeException ("count");
			if (count > buffer.Length - offset)
				throw new ArgumentException ("count > buffer.Length - offset");

			stream.Write (buffer, offset, count);
		}

		public void Write (char [] buffer, int offset, int count)
		{
			CheckOpen ();
			if (buffer == null)
				throw new ArgumentNullException ("buffer");
			if (offset < 0 || offset >= buffer.Length)
				throw new ArgumentOutOfRangeException ("offset");
			if (count < 0 || count > buffer.Length)
				throw new ArgumentOutOfRangeException ("count");
			if (count > buffer.Length - offset)
				throw new ArgumentException ("count > buffer.Length - offset");

			byte [] bytes = encoding.GetBytes (buffer, offset, count);
			stream.Write (bytes, 0, bytes.Length);
		}

		public void WriteLine (string str)
		{
			Write (str + newLine);
		}

		void CheckOpen ()
		{
			if (!isOpen)
				throw new InvalidOperationException ("Specified port is not open.");
		}

		// events

		public delegate void SerialReceivedEventHandler (object sender, SerialReceivedEventArgs e);
		public delegate void SerialPinChangedEventHandler (object sender, SerialPinChangedEventArgs e);
		public delegate void SerialErrorEventHandler (object sender, SerialErrorEventArgs e);

		public event SerialErrorEventHandler ErrorEvent;
		public event SerialPinChangedEventHandler PinChangedEvent;
		public event SerialReceivedEventHandler ReceivedEvent;
	}
}

#endif