2 // System.IO.Ports.SerialPortStream.cs
5 // Chris Toshok (toshok@ximian.com)
6 // Carlos Alberto Cortez (calberto.cortez@gmail.com)
8 // (c) Copyright 2006 Novell, Inc. (http://www.novell.com)
16 using System.Runtime.InteropServices;
18 namespace System.IO.Ports
20 class SerialPortStream : Stream, ISerialStream, IDisposable
27 [DllImport ("MonoPosixHelper")]
28 static extern int open_serial (string portName);
30 public SerialPortStream (string portName, int baudRate, int dataBits, Parity parity, StopBits stopBits,
31 bool dtrEnable, bool rtsEnable, Handshake handshake, int readTimeout, int writeTimeout,
32 int readBufferSize, int writeBufferSize)
34 fd = open_serial (portName);
36 throw new IOException ();
38 if (!set_attributes (fd, baudRate, parity, dataBits, stopBits, handshake))
39 throw new IOException (); // Probably Win32Exc for compatibility
41 read_timeout = readTimeout;
42 write_timeout = writeTimeout;
44 SetSignal (SerialSignal.Dtr, dtrEnable);
46 if (handshake != Handshake.RequestToSend &&
47 handshake != Handshake.RequestToSendXOnXOff)
48 SetSignal (SerialSignal.Rts, rtsEnable);
51 public override bool CanRead {
57 public override bool CanSeek {
63 public override bool CanWrite {
69 public override bool CanTimeout {
75 public override int ReadTimeout {
80 if (value < 0 && value != SerialPort.InfiniteTimeout)
81 throw new ArgumentOutOfRangeException ("value");
87 public override int WriteTimeout {
92 if (value < 0 && value != SerialPort.InfiniteTimeout)
93 throw new ArgumentOutOfRangeException ("value");
95 write_timeout = value;
99 public override long Length {
101 throw new NotSupportedException ();
105 public override long Position {
107 throw new NotSupportedException ();
110 throw new NotSupportedException ();
114 public override void Flush ()
116 // If used, this _could_ flush the serial port
117 // buffer (not the SerialPort class buffer)
120 [DllImport ("MonoPosixHelper")]
121 static extern int read_serial (int fd, byte [] buffer, int offset, int count);
124 [DllImport ("MonoPosixHelper")]
125 static extern bool poll_serial (int fd, out int error, int timeout);
127 public override int Read ([In,Out] byte[] buffer, int offset, int count)
131 throw new ArgumentNullException ("buffer");
132 if (offset < 0 || count < 0)
133 throw new ArgumentOutOfRangeException ("offset or count less than zero.");
135 if (buffer.Length - offset < count )
136 throw new ArgumentException ("offset+count",
137 "The size of the buffer is less than offset + count.");
140 bool poll_result = poll_serial (fd, out error, read_timeout);
142 throw new IOException ();
145 // see bug 79735 http://bugzilla.ximian.com/show_bug.cgi?id=79735
146 // should the next line read: return -1;
147 throw new TimeoutException();
150 return read_serial (fd, buffer, offset, count);
153 public override long Seek (long offset, SeekOrigin origin)
155 throw new NotSupportedException ();
158 public override void SetLength (long value)
160 throw new NotSupportedException ();
163 [DllImport ("MonoPosixHelper")]
164 static extern void write_serial (int fd, byte [] buffer, int offset, int count, int timeout);
166 public override void Write (byte[] buffer, int offset, int count)
170 throw new ArgumentNullException ("buffer");
172 if (offset < 0 || count < 0)
173 throw new ArgumentOutOfRangeException ();
175 if (buffer.Length - offset < count)
176 throw new ArgumentException ("offset+count",
177 "The size of the buffer is less than offset + count.");
179 write_serial (fd, buffer, offset, count, write_timeout);
182 protected override void Dispose (bool disposing)
191 [DllImport ("MonoPosixHelper")]
192 static extern void close_serial (int fd);
194 public override void Close ()
196 ((IDisposable) this).Dispose ();
199 void IDisposable.Dispose ()
202 GC.SuppressFinalize (this);
210 void CheckDisposed ()
213 throw new ObjectDisposedException (GetType ().FullName);
216 [DllImport ("MonoPosixHelper")]
217 static extern bool set_attributes (int fd, int baudRate, Parity parity, int dataBits, StopBits stopBits, Handshake handshake);
219 public void SetAttributes (int baud_rate, Parity parity, int data_bits, StopBits sb, Handshake hs)
221 if (!set_attributes (fd, baud_rate, parity, data_bits, sb, hs))
222 throw new IOException ();
225 [DllImport("MonoPosixHelper")]
226 static extern int get_bytes_in_buffer (int fd, int input);
228 public int BytesToRead {
230 return get_bytes_in_buffer (fd, 1);
234 public int BytesToWrite {
236 return get_bytes_in_buffer (fd, 0);
240 [DllImport ("MonoPosixHelper")]
241 static extern void discard_buffer (int fd, bool inputBuffer);
243 public void DiscardInBuffer ()
245 discard_buffer (fd, true);
248 public void DiscardOutBuffer ()
250 discard_buffer (fd, false);
253 [DllImport ("MonoPosixHelper")]
254 static extern SerialSignal get_signals (int fd, out int error);
256 public SerialSignal GetSignals ()
259 SerialSignal signals = get_signals (fd, out error);
261 throw new IOException ();
266 [DllImport ("MonoPosixHelper")]
267 static extern int set_signal (int fd, SerialSignal signal, bool value);
269 public void SetSignal (SerialSignal signal, bool value)
271 if (signal < SerialSignal.Cd || signal > SerialSignal.Rts ||
272 signal == SerialSignal.Cd ||
273 signal == SerialSignal.Cts ||
274 signal == SerialSignal.Dsr)
275 throw new Exception ("Invalid internal value");
277 if (set_signal (fd, signal, value) == -1)
278 throw new IOException ();
281 public void SetBreakState (bool value)
283 throw new NotImplementedException ();