1 /* -*- Mode: Csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
6 using System.ComponentModel;
8 using System.Runtime.InteropServices;
10 namespace System.IO.Ports
12 public class SerialPort : Component
14 public const int InfiniteTimeout = -1;
15 const int DefaultReadBufferSize = 4096;
16 const int DefaultWriteBufferSize = 2048;
17 const int DefaultBaudRate = 9600;
18 const int DefaultDataBits = 8;
19 const Parity DefaultParity = Parity.None;
20 const StopBits DefaultStopBits = StopBits.One;
26 Handshake handshake = Handshake.None;
28 bool breakState = false;
29 bool dtr_enable = false;
30 bool rts_enable = false;
31 SerialPortStream stream;
32 Encoding encoding = Encoding.ASCII;
33 string newLine = Environment.NewLine;
35 int readTimeout = InfiniteTimeout;
36 int writeTimeout = InfiniteTimeout;
37 int readBufferSize = DefaultReadBufferSize;
38 int writeBufferSize = DefaultWriteBufferSize;
39 object error_received = new object ();
40 object data_received = new object ();
41 object pin_changed = new object ();
43 static string default_port_name = "ttyS0";
45 public SerialPort () :
46 this (GetDefaultPortName (), DefaultBaudRate, DefaultParity, DefaultDataBits, DefaultStopBits)
52 public SerialPort (IContainer container) {
56 public SerialPort (string portName) :
57 this (portName, DefaultBaudRate, DefaultParity, DefaultDataBits, DefaultStopBits)
61 public SerialPort (string portName, int baudRate) :
62 this (portName, baudRate, DefaultParity, DefaultDataBits, DefaultStopBits)
66 public SerialPort (string portName, int baudRate, Parity parity) :
67 this (portName, baudRate, parity, DefaultDataBits, DefaultStopBits)
71 public SerialPort (string portName, int baudRate, Parity parity, int dataBits) :
72 this (portName, baudRate, parity, dataBits, DefaultStopBits)
76 public SerialPort (string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits)
78 this.portName = portName;
79 this.baudRate = baudRate;
81 this.dataBits = dataBits;
82 this.stopBits = stopBits;
85 static string GetDefaultPortName ()
87 return default_port_name;
90 public Stream BaseStream {
93 throw new InvalidOperationException ();
105 throw new ArgumentOutOfRangeException ("value");
109 stream.BaudRate = value;
113 public bool BreakState {
119 if (value == breakState)
120 return; // Do nothing.
127 public int BytesToRead {
130 return stream.BytesToRead;
134 public int BytesToWrite {
137 return stream.BytesToWrite;
141 public bool CDHolding {
144 throw new NotImplementedException ();
148 public bool CtsHolding {
151 throw new NotImplementedException ();
155 public int DataBits {
160 if (value < 5 || value > 8)
161 throw new ArgumentOutOfRangeException ("value");
165 stream.DataBits = value;
169 public bool DiscardNull {
172 throw new NotImplementedException ();
176 throw new NotImplementedException ();
180 public bool DsrHolding {
183 throw new NotImplementedException ();
187 public bool DtrEnable {
189 throw new NotImplementedException ();
192 throw new NotImplementedException ();
196 public Encoding Encoding {
202 throw new ArgumentNullException ("value");
208 public Handshake Handshake {
213 if (value < Handshake.None || value > Handshake.RequestToSendXOnXOff)
214 throw new ArgumentOutOfRangeException ("value");
218 stream.Handshake = value;
228 public string NewLine {
234 throw new ArgumentNullException ("value");
240 public Parity Parity {
245 if (value < Parity.None || value > Parity.Space)
246 throw new ArgumentOutOfRangeException ("value");
250 stream.Parity = value;
254 public byte ParityReplace {
256 throw new NotImplementedException ();
259 throw new NotImplementedException ();
263 public string PortName {
269 throw new InvalidOperationException ("Port name cannot be set while port is open.");
271 throw new ArgumentNullException ("value");
272 if (value.Length == 0 || value.StartsWith ("\\\\"))
273 throw new ArgumentException ("value");
279 public int ReadBufferSize {
281 return readBufferSize;
285 throw new InvalidOperationException ();
287 throw new ArgumentOutOfRangeException ("value");
288 if (value <= DefaultReadBufferSize)
291 readBufferSize = value;
295 public int ReadTimeout {
300 if (value <= 0 && value != InfiniteTimeout)
301 throw new ArgumentOutOfRangeException ("value");
305 stream.ReadTimeout = value;
309 public int ReceivedBytesThreshold {
311 throw new NotImplementedException ();
315 throw new ArgumentOutOfRangeException ("value");
317 throw new NotImplementedException ();
321 public bool RtsEnable {
323 throw new NotImplementedException ();
326 throw new NotImplementedException ();
330 public StopBits StopBits {
335 if (value < StopBits.One || value > StopBits.OnePointFive)
336 throw new ArgumentOutOfRangeException ("value");
340 stream.StopBits = value;
344 public int WriteBufferSize {
346 return writeBufferSize;
350 throw new InvalidOperationException ();
352 throw new ArgumentOutOfRangeException ("value");
353 if (value <= DefaultWriteBufferSize)
356 writeBufferSize = value;
360 public int WriteTimeout {
365 if (value <= 0 && value != InfiniteTimeout)
366 throw new ArgumentOutOfRangeException ("value");
368 writeTimeout = value;
370 stream.WriteTimeout = value;
386 public void DiscardInBuffer ()
389 stream.DiscardInputBuffer ();
392 public void DiscardOutBuffer ()
395 stream.DiscardOutputBuffer ();
398 public static string [] GetPortNames ()
400 int p = (int) Environment.OSVersion.Platform;
401 if (p == 4 || p == 128) // Are we on Unix?
402 return Directory.GetFiles ("/dev/", "ttyS*");
404 throw new NotImplementedException ("Detection of ports is not implemented for this platform yet.");
410 throw new InvalidOperationException ("Port is already open");
412 stream = new SerialPortStream (portName, baudRate, dataBits, parity, stopBits, dtr_enable,
413 rts_enable, handshake, readTimeout, writeTimeout, readBufferSize, writeBufferSize);
417 public int Read (byte[] buffer, int offset, int count)
421 throw new ArgumentNullException ("buffer");
422 if (offset < 0 || offset >= buffer.Length)
423 throw new ArgumentOutOfRangeException ("offset");
424 if (count < 0 || count > buffer.Length)
425 throw new ArgumentOutOfRangeException ("count");
426 if (count > buffer.Length - offset)
427 throw new ArgumentException ("count > buffer.Length - offset");
429 return stream.Read (buffer, offset, count);
432 public int Read (char[] buffer, int offset, int count)
436 throw new ArgumentNullException ("buffer");
437 if (offset < 0 || offset >= buffer.Length)
438 throw new ArgumentOutOfRangeException ("offset");
439 if (count < 0 || count > buffer.Length)
440 throw new ArgumentOutOfRangeException ("count");
441 if (count > buffer.Length - offset)
442 throw new ArgumentException ("count > buffer.Length - offset");
444 byte [] bytes = encoding.GetBytes (buffer, offset, count);
445 return stream.Read (bytes, 0, bytes.Length);
448 public int ReadByte ()
450 byte [] buff = new byte [1];
451 if (Read (buff, 0, 1) > 0)
457 public int ReadChar ()
459 throw new NotImplementedException ();
462 public string ReadExisting ()
464 throw new NotImplementedException ();
467 public string ReadLine ()
469 return ReadTo (newLine);
472 public string ReadTo (string value)
476 throw new ArgumentNullException ("value");
477 if (value.Length == 0)
478 throw new ArgumentException ("value");
480 throw new NotImplementedException ();
483 public void Write (string str)
487 throw new ArgumentNullException ("str");
489 byte [] buffer = encoding.GetBytes (str);
490 Write (buffer, 0, buffer.Length);
493 public void Write (byte [] buffer, int offset, int count)
497 throw new ArgumentNullException ("buffer");
498 if (offset < 0 || offset >= buffer.Length)
499 throw new ArgumentOutOfRangeException ("offset");
500 if (count < 0 || count > buffer.Length)
501 throw new ArgumentOutOfRangeException ("count");
502 if (count > buffer.Length - offset)
503 throw new ArgumentException ("count > buffer.Length - offset");
505 stream.Write (buffer, offset, count);
508 public void Write (char [] buffer, int offset, int count)
512 throw new ArgumentNullException ("buffer");
513 if (offset < 0 || offset >= buffer.Length)
514 throw new ArgumentOutOfRangeException ("offset");
515 if (count < 0 || count > buffer.Length)
516 throw new ArgumentOutOfRangeException ("count");
517 if (count > buffer.Length - offset)
518 throw new ArgumentException ("count > buffer.Length - offset");
520 byte [] bytes = encoding.GetBytes (buffer, offset, count);
521 stream.Write (bytes, 0, bytes.Length);
524 public void WriteLine (string str)
526 Write (str + newLine);
532 throw new InvalidOperationException ("Specified port is not open.");
535 internal void OnErrorReceived (SerialErrorReceivedEventArgs args)
537 SerialErrorReceivedEventHandler handler =
538 (SerialErrorReceivedEventHandler) Events [error_received];
541 handler (this, args);
544 internal void OnDataReceived (SerialDataReceivedEventArgs args)
546 SerialDataReceivedEventHandler handler =
547 (SerialDataReceivedEventHandler) Events [data_received];
550 handler (this, args);
553 internal void OnDataReceived (SerialPinChangedEventArgs args)
555 SerialPinChangedEventHandler handler =
556 (SerialPinChangedEventHandler) Events [pin_changed];
559 handler (this, args);
563 public event SerialErrorReceivedEventHandler ErrorReceived {
564 add { Events.AddHandler (error_received, value); }
565 remove { Events.RemoveHandler (error_received, value); }
568 public event SerialPinChangedEventHandler PinChanged {
569 add { Events.AddHandler (pin_changed, value); }
570 remove { Events.RemoveHandler (pin_changed, value); }
573 public event SerialDataReceivedEventHandler DataReceived {
574 add { Events.AddHandler (data_received, value); }
575 remove { Events.RemoveHandler (data_received, value); }
579 public delegate void SerialDataReceivedEventHandler (object sender, SerialDataReceivedEventArgs e);
580 public delegate void SerialPinChangedEventHandler (object sender, SerialPinChangedEventArgs e);
581 public delegate void SerialErrorReceivedEventHandler (object sender, SerialErrorReceivedEventArgs e);