2 // System.IO.Ports.WinSerialStream.cs
5 // Carlos Alberto Cortez (calberto.cortez@gmail.com)
7 // (c) Copyright 2006 Novell, Inc. (http://www.novell.com)
13 using System.Runtime.InteropServices;
14 using System.Threading;
15 using System.ComponentModel;
17 namespace System.IO.Ports
19 class WinSerialStream : Stream, ISerialStream, IDisposable
21 // Windows API Constants
22 const uint GenericRead = 0x80000000;
23 const uint GenericWrite = 0x40000000;
24 const uint OpenExisting = 3;
25 const uint FileFlagOverlapped = 0x40000000;
26 const uint PurgeRxClear = 0x0004;
27 const uint PurgeTxClear = 0x0008;
28 const uint WinInfiniteTimeout = 0xFFFFFFFF;
29 const uint FileIOPending = 997;
32 const uint SetRts = 3;
33 const uint ClearRts = 4;
34 const uint SetDtr = 5;
35 const uint ClearDtr = 6;
36 const uint SetBreak = 8;
37 const uint ClearBreak = 9;
38 const uint CtsOn = 0x0010;
39 const uint DsrOn = 0x0020;
40 const uint RsldOn = 0x0080;
43 const uint EvRxChar = 0x0001;
44 const uint EvCts = 0x0008;
45 const uint EvDsr = 0x0010;
46 const uint EvRlsd = 0x0020;
47 const uint EvBreak = 0x0040;
48 const uint EvErr = 0x0080;
49 const uint EvRing = 0x0100;
55 IntPtr write_overlapped;
56 IntPtr read_overlapped;
57 ManualResetEvent read_event;
58 ManualResetEvent write_event;
61 [DllImport("kernel32", SetLastError = true)]
62 static extern int CreateFile(string port_name, uint desired_access,
63 uint share_mode, uint security_attrs, uint creation, uint flags,
66 [DllImport("kernel32", SetLastError = true)]
67 static extern bool SetupComm(int handle, int read_buffer_size, int write_buffer_size);
69 [DllImport("kernel32", SetLastError = true)]
70 static extern bool PurgeComm(int handle, uint flags);
72 [DllImport("kernel32", SetLastError = true)]
73 static extern bool SetCommTimeouts(int handle, Timeouts timeouts);
75 public WinSerialStream (string port_name, int baud_rate, int data_bits, Parity parity, StopBits sb,
76 bool dtr_enable, bool rts_enable, Handshake hs, int read_timeout, int write_timeout,
77 int read_buffer_size, int write_buffer_size)
79 handle = CreateFile (port_name != null && !port_name.StartsWith(@"\\.\")
80 ? @"\\.\" + port_name : port_name,
81 GenericRead | GenericWrite, 0, 0, OpenExisting,
82 FileFlagOverlapped, 0);
85 ReportIOError (port_name);
87 // Set port low level attributes
88 SetAttributes (baud_rate, parity, data_bits, sb, hs);
90 // Clean buffers and set sizes
91 if (!PurgeComm (handle, PurgeRxClear | PurgeTxClear) ||
92 !SetupComm (handle, read_buffer_size, write_buffer_size))
96 this.read_timeout = read_timeout;
97 this.write_timeout = write_timeout;
98 timeouts = new Timeouts (read_timeout, write_timeout);
99 if (!SetCommTimeouts(handle, timeouts))
100 ReportIOError (null);
103 SetSignal(SerialSignal.Dtr, dtr_enable);
105 if (hs != Handshake.RequestToSend &&
106 hs != Handshake.RequestToSendXOnXOff)
107 SetSignal(SerialSignal.Rts, rts_enable);
109 // Init overlapped structures
110 NativeOverlapped wo = new NativeOverlapped ();
111 write_event = new ManualResetEvent (false);
112 wo.EventHandle = write_event.Handle;
113 write_overlapped = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (NativeOverlapped)));
114 Marshal.StructureToPtr (wo, write_overlapped, true);
116 NativeOverlapped ro = new NativeOverlapped ();
117 read_event = new ManualResetEvent (false);
118 ro.EventHandle = read_event.Handle;
119 read_overlapped = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (NativeOverlapped)));
120 Marshal.StructureToPtr (ro, read_overlapped, true);
123 public override bool CanRead {
129 public override bool CanSeek {
135 public override bool CanTimeout {
141 public override bool CanWrite {
147 public override int ReadTimeout {
152 if (value < 0 && value != SerialPort.InfiniteTimeout)
153 throw new ArgumentOutOfRangeException ("value");
155 timeouts.SetValues (value, write_timeout);
156 if (!SetCommTimeouts (handle, timeouts))
157 ReportIOError (null);
159 read_timeout = value;
163 public override int WriteTimeout {
165 return write_timeout;
169 if (value < 0 && value != SerialPort.InfiniteTimeout)
170 throw new ArgumentOutOfRangeException ("value");
172 timeouts.SetValues (read_timeout, value);
173 if (!SetCommTimeouts (handle, timeouts))
174 ReportIOError (null);
176 write_timeout = value;
180 public override long Length {
182 throw new NotSupportedException ();
186 public override long Position {
188 throw new NotSupportedException ();
191 throw new NotSupportedException ();
195 [DllImport("kernel32", SetLastError = true)]
196 static extern bool CloseHandle (int handle);
198 protected override void Dispose (bool disposing)
204 CloseHandle (handle);
205 Marshal.FreeHGlobal (write_overlapped);
206 Marshal.FreeHGlobal (read_overlapped);
209 void IDisposable.Dispose ()
212 GC.SuppressFinalize (this);
215 public override void Close ()
217 ((IDisposable)this).Dispose ();
225 public override void Flush ()
231 public override long Seek (long offset, SeekOrigin origin)
233 throw new NotSupportedException();
236 public override void SetLength (long value)
238 throw new NotSupportedException();
242 [DllImport("kernel32", SetLastError = true)]
243 static extern unsafe bool ReadFile (int handle, byte* buffer, int bytes_to_read,
244 out int bytes_read, IntPtr overlapped);
246 [DllImport("kernel32", SetLastError = true)]
247 static extern unsafe bool GetOverlappedResult (int handle, IntPtr overlapped,
248 ref int bytes_transfered, bool wait);
251 public override int Read ([In, Out] byte [] buffer, int offset, int count)
255 throw new ArgumentNullException ("buffer");
256 if (offset < 0 || count < 0)
257 throw new ArgumentOutOfRangeException ("offset or count less than zero.");
259 if (buffer.Length - offset < count )
260 throw new ArgumentException ("offset+count",
261 "The size of the buffer is less than offset + count.");
266 fixed (byte* ptr = buffer) {
267 if (ReadFile (handle, ptr + offset, count, out bytes_read, read_overlapped))
270 // Test for overlapped behavior
271 if (Marshal.GetLastWin32Error () != FileIOPending)
272 ReportIOError (null);
274 if (!GetOverlappedResult (handle, read_overlapped, ref bytes_read, true))
275 ReportIOError (null);
281 throw new TimeoutException (); // We didn't get any byte
287 [DllImport("kernel32", SetLastError = true)]
288 static extern unsafe bool WriteFile (int handle, byte* buffer, int bytes_to_write,
289 out int bytes_written, IntPtr overlapped);
292 public override void Write (byte [] buffer, int offset, int count)
296 throw new ArgumentNullException ("buffer");
298 if (offset < 0 || count < 0)
299 throw new ArgumentOutOfRangeException ();
301 if (buffer.Length - offset < count)
302 throw new ArgumentException ("offset+count",
303 "The size of the buffer is less than offset + count.");
305 int bytes_written = 0;
308 fixed (byte* ptr = buffer) {
309 if (WriteFile (handle, ptr + offset, count, out bytes_written, write_overlapped))
311 if (Marshal.GetLastWin32Error() != FileIOPending)
312 ReportIOError (null);
314 if (!GetOverlappedResult(handle, write_overlapped, ref bytes_written, true))
315 ReportIOError (null);
319 // If the operation timed out, then
320 // we transfered less bytes than the requested ones
321 if (bytes_written < count)
322 throw new TimeoutException ();
325 [DllImport("kernel32", SetLastError = true)]
326 static extern bool GetCommState (int handle, [Out] DCB dcb);
328 [DllImport ("kernel32", SetLastError=true)]
329 static extern bool SetCommState (int handle, DCB dcb);
331 public void SetAttributes (int baud_rate, Parity parity, int data_bits, StopBits bits, Handshake hs)
333 DCB dcb = new DCB ();
334 if (!GetCommState (handle, dcb))
335 ReportIOError (null);
337 dcb.SetValues (baud_rate, parity, data_bits, bits, hs);
338 if (!SetCommState (handle, dcb))
339 ReportIOError (null);
342 void ReportIOError(string optional_arg)
344 int error = Marshal.GetLastWin32Error ();
349 message = "The port `" + optional_arg + "' does not exist.";
352 message = "Parameter is incorrect.";
355 // As fallback, we show the win32 error
356 message = new Win32Exception ().Message;
360 throw new IOException (message);
363 void CheckDisposed ()
366 throw new ObjectDisposedException (GetType ().FullName);
369 // ISerialStream members
370 public void DiscardInBuffer ()
372 if (!PurgeComm (handle, PurgeRxClear))
373 ReportIOError (null);
376 public void DiscardOutBuffer ()
378 if (!PurgeComm (handle, PurgeRxClear))
379 ReportIOError (null);
382 [DllImport ("kernel32", SetLastError=true)]
383 static extern bool ClearCommError (int handle, out uint errors, out CommStat stat);
385 public int BytesToRead {
389 if (!ClearCommError (handle, out errors, out stat))
390 ReportIOError (null);
392 return (int)stat.BytesIn;
396 public int BytesToWrite {
400 if (!ClearCommError (handle, out errors, out stat))
401 ReportIOError (null);
403 return (int)stat.BytesOut;
407 [DllImport ("kernel32", SetLastError=true)]
408 static extern bool GetCommModemStatus (int handle, out uint flags);
410 public SerialSignal GetSignals ()
413 if (!GetCommModemStatus (handle, out flags))
414 ReportIOError (null);
416 SerialSignal signals = SerialSignal.None;
417 if ((flags & RsldOn) != 0)
418 signals |= SerialSignal.Cd;
419 if ((flags & CtsOn) != 0)
420 signals |= SerialSignal.Cts;
421 if ((flags & DsrOn) != 0)
422 signals |= SerialSignal.Dsr;
427 [DllImport ("kernel32", SetLastError=true)]
428 static extern bool EscapeCommFunction (int handle, uint flags);
430 public void SetSignal (SerialSignal signal, bool value)
432 if (signal != SerialSignal.Rts && signal != SerialSignal.Dtr)
433 throw new Exception ("Wrong internal value");
436 if (signal == SerialSignal.Rts)
447 if (!EscapeCommFunction (handle, flag))
448 ReportIOError (null);
451 public void SetBreakState (bool value)
453 if (!EscapeCommFunction (handle, value ? SetBreak : ClearBreak))
454 ReportIOError (null);
459 [StructLayout (LayoutKind.Sequential)]
462 public int dcb_length;
463 public int baud_rate;
465 public short w_reserved;
466 public short xon_lim;
467 public short xoff_lim;
468 public byte byte_size;
470 public byte stop_bits;
471 public byte xon_char;
472 public byte xoff_char;
473 public byte error_char;
474 public byte eof_char;
475 public byte evt_char;
476 public short w_reserved1;
479 //const int fBinary = 0x0001;
480 //const int fParity = 0x0002;
481 const int fOutxCtsFlow = 0x0004;
482 //const int fOutxDsrFlow1 = 0x0008;
483 //const int fOutxDsrFlow2 = 0x0010;
484 //const int fDtrControl = 0x00020;
485 //const int fDsrSensitivity = 0x0040;
486 //const int fTXContinueOnXoff = 0x0080;
487 const int fOutX = 0x0100;
488 const int fInX = 0x0200;
489 //const int fErrorChar = 0x0400;
490 //const int fNull = 0x0800;
491 //const int fRtsControl1 = 0x1000;
492 const int fRtsControl2 = 0x2000;
493 //const int fAbortOnError = 0x4000;
495 public void SetValues (int baud_rate, Parity parity, int byte_size, StopBits sb, Handshake hs)
501 case StopBits.OnePointFive:
507 default: // Shouldn't happen
511 this.baud_rate = baud_rate;
512 this.parity = (byte)parity;
513 this.byte_size = (byte)byte_size;
515 // Clear Handshake flags
516 flags &= ~(fOutxCtsFlow | fOutX | fInX | fRtsControl2);
518 // Set Handshake flags
523 case Handshake.XOnXOff:
524 flags |= fOutX | fInX;
526 case Handshake.RequestToSend:
527 flags |= fOutxCtsFlow | fRtsControl2;
529 case Handshake.RequestToSendXOnXOff:
530 flags |= fOutxCtsFlow | fOutX | fInX | fRtsControl2;
532 default: // Shouldn't happen
538 [StructLayout (LayoutKind.Sequential)]
541 public uint ReadIntervalTimeout;
542 public uint ReadTotalTimeoutMultiplier;
543 public uint ReadTotalTimeoutConstant;
544 public uint WriteTotalTimeoutMultiplier;
545 public uint WriteTotalTimeoutConstant;
547 public const uint MaxDWord = 0xFFFFFFFF;
549 public Timeouts (int read_timeout, int write_timeout)
551 SetValues (read_timeout, write_timeout);
554 public void SetValues (int read_timeout, int write_timeout)
556 // FIXME: The windows api docs are not very clear about read timeouts,
557 // and we have to simulate infinite with a big value (uint.MaxValue - 1)
558 ReadIntervalTimeout = MaxDWord;
559 ReadTotalTimeoutMultiplier = MaxDWord;
560 ReadTotalTimeoutConstant = (read_timeout == -1 ? MaxDWord - 1 : (uint) read_timeout);
562 WriteTotalTimeoutMultiplier = 0;
563 WriteTotalTimeoutConstant = (write_timeout == -1 ? MaxDWord : (uint) write_timeout);
568 [StructLayout (LayoutKind.Sequential)]
573 public uint BytesOut;