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 return stream.GetSignal (SerialSignal.Cd);
148 public bool CtsHolding {
151 return stream.GetSignal (SerialSignal.Cts);
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 return stream.GetSignal (SerialSignal.Dsr);
187 public bool DtrEnable {
192 if (value == dtr_enable)
195 stream.SetSignal (SerialSignal.Dtr, value);
201 public Encoding Encoding {
207 throw new ArgumentNullException ("value");
213 public Handshake Handshake {
218 if (value < Handshake.None || value > Handshake.RequestToSendXOnXOff)
219 throw new ArgumentOutOfRangeException ("value");
223 stream.Handshake = value;
233 public string NewLine {
239 throw new ArgumentNullException ("value");
245 public Parity Parity {
250 if (value < Parity.None || value > Parity.Space)
251 throw new ArgumentOutOfRangeException ("value");
255 stream.Parity = value;
259 public byte ParityReplace {
261 throw new NotImplementedException ();
264 throw new NotImplementedException ();
268 public string PortName {
274 throw new InvalidOperationException ("Port name cannot be set while port is open.");
276 throw new ArgumentNullException ("value");
277 if (value.Length == 0 || value.StartsWith ("\\\\"))
278 throw new ArgumentException ("value");
284 public int ReadBufferSize {
286 return readBufferSize;
290 throw new InvalidOperationException ();
292 throw new ArgumentOutOfRangeException ("value");
293 if (value <= DefaultReadBufferSize)
296 readBufferSize = value;
300 public int ReadTimeout {
305 if (value <= 0 && value != InfiniteTimeout)
306 throw new ArgumentOutOfRangeException ("value");
310 stream.ReadTimeout = value;
314 public int ReceivedBytesThreshold {
316 throw new NotImplementedException ();
320 throw new ArgumentOutOfRangeException ("value");
322 throw new NotImplementedException ();
326 public bool RtsEnable {
331 if (value == rts_enable)
334 stream.SetSignal (SerialSignal.Rts, value);
340 public StopBits StopBits {
345 if (value < StopBits.One || value > StopBits.OnePointFive)
346 throw new ArgumentOutOfRangeException ("value");
350 stream.StopBits = value;
354 public int WriteBufferSize {
356 return writeBufferSize;
360 throw new InvalidOperationException ();
362 throw new ArgumentOutOfRangeException ("value");
363 if (value <= DefaultWriteBufferSize)
366 writeBufferSize = value;
370 public int WriteTimeout {
375 if (value <= 0 && value != InfiniteTimeout)
376 throw new ArgumentOutOfRangeException ("value");
378 writeTimeout = value;
380 stream.WriteTimeout = value;
391 protected override void Dispose (bool disposing)
401 public void DiscardInBuffer ()
404 stream.DiscardInputBuffer ();
407 public void DiscardOutBuffer ()
410 stream.DiscardOutputBuffer ();
413 public static string [] GetPortNames ()
415 int p = (int) Environment.OSVersion.Platform;
416 if (p == 4 || p == 128) // Are we on Unix?
417 return Directory.GetFiles ("/dev/", "ttyS*");
419 throw new NotImplementedException ("Detection of ports is not implemented for this platform yet.");
425 throw new InvalidOperationException ("Port is already open");
427 stream = new SerialPortStream (portName, baudRate, dataBits, parity, stopBits, dtr_enable,
428 rts_enable, handshake, readTimeout, writeTimeout, readBufferSize, writeBufferSize);
432 public int Read (byte[] 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 return stream.Read (buffer, offset, count);
447 public int Read (char[] buffer, int offset, int count)
451 throw new ArgumentNullException ("buffer");
452 if (offset < 0 || offset >= buffer.Length)
453 throw new ArgumentOutOfRangeException ("offset");
454 if (count < 0 || count > buffer.Length)
455 throw new ArgumentOutOfRangeException ("count");
456 if (count > buffer.Length - offset)
457 throw new ArgumentException ("count > buffer.Length - offset");
459 byte [] bytes = encoding.GetBytes (buffer, offset, count);
460 return stream.Read (bytes, 0, bytes.Length);
463 public int ReadByte ()
465 byte [] buff = new byte [1];
466 if (Read (buff, 0, 1) > 0)
472 public int ReadChar ()
474 throw new NotImplementedException ();
477 public string ReadExisting ()
479 throw new NotImplementedException ();
482 public string ReadLine ()
484 return ReadTo (newLine);
487 public string ReadTo (string value)
491 throw new ArgumentNullException ("value");
492 if (value.Length == 0)
493 throw new ArgumentException ("value");
495 throw new NotImplementedException ();
498 public void Write (string str)
502 throw new ArgumentNullException ("str");
504 byte [] buffer = encoding.GetBytes (str);
505 Write (buffer, 0, buffer.Length);
508 public void Write (byte [] 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 stream.Write (buffer, offset, count);
523 public void Write (char [] buffer, int offset, int count)
527 throw new ArgumentNullException ("buffer");
528 if (offset < 0 || offset >= buffer.Length)
529 throw new ArgumentOutOfRangeException ("offset");
530 if (count < 0 || count > buffer.Length)
531 throw new ArgumentOutOfRangeException ("count");
532 if (count > buffer.Length - offset)
533 throw new ArgumentException ("count > buffer.Length - offset");
535 byte [] bytes = encoding.GetBytes (buffer, offset, count);
536 stream.Write (bytes, 0, bytes.Length);
539 public void WriteLine (string str)
541 Write (str + newLine);
547 throw new InvalidOperationException ("Specified port is not open.");
550 internal void OnErrorReceived (SerialErrorReceivedEventArgs args)
552 SerialErrorReceivedEventHandler handler =
553 (SerialErrorReceivedEventHandler) Events [error_received];
556 handler (this, args);
559 internal void OnDataReceived (SerialDataReceivedEventArgs args)
561 SerialDataReceivedEventHandler handler =
562 (SerialDataReceivedEventHandler) Events [data_received];
565 handler (this, args);
568 internal void OnDataReceived (SerialPinChangedEventArgs args)
570 SerialPinChangedEventHandler handler =
571 (SerialPinChangedEventHandler) Events [pin_changed];
574 handler (this, args);
578 public event SerialErrorReceivedEventHandler ErrorReceived {
579 add { Events.AddHandler (error_received, value); }
580 remove { Events.RemoveHandler (error_received, value); }
583 public event SerialPinChangedEventHandler PinChanged {
584 add { Events.AddHandler (pin_changed, value); }
585 remove { Events.RemoveHandler (pin_changed, value); }
588 public event SerialDataReceivedEventHandler DataReceived {
589 add { Events.AddHandler (data_received, value); }
590 remove { Events.RemoveHandler (data_received, value); }
594 public delegate void SerialDataReceivedEventHandler (object sender, SerialDataReceivedEventArgs e);
595 public delegate void SerialPinChangedEventHandler (object sender, SerialPinChangedEventArgs e);
596 public delegate void SerialErrorReceivedEventHandler (object sender, SerialErrorReceivedEventArgs e);