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

github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlos Alberto Cortez <calberto.cortez@gmail.com>2006-04-21 10:47:26 +0400
committerCarlos Alberto Cortez <calberto.cortez@gmail.com>2006-04-21 10:47:26 +0400
commit6dcd3635ec1b1f1f5db852702b4817b59485d781 (patch)
tree21fcc1caae051bbecc090f72b6910b9504fb7bc0
parenta301e7e1daeff4629d05f94b44e574f1a928fe0d (diff)
2006-04-20 Carlos Alberto Cortez <calberto.cortez@gmail.com>
* WinSerialPort.cs: Inital import of the backend stream for Windows support. svn path=/trunk/mcs/; revision=59729
-rw-r--r--mcs/class/System/System.IO.Ports/ChangeLog5
-rw-r--r--mcs/class/System/System.IO.Ports/WinSerialStream.cs214
2 files changed, 219 insertions, 0 deletions
diff --git a/mcs/class/System/System.IO.Ports/ChangeLog b/mcs/class/System/System.IO.Ports/ChangeLog
index b742a80e8e8..4d6529d10b5 100644
--- a/mcs/class/System/System.IO.Ports/ChangeLog
+++ b/mcs/class/System/System.IO.Ports/ChangeLog
@@ -1,3 +1,8 @@
+2006-04-20 Carlos Alberto Cortez <calberto.cortez@gmail.com>
+
+ * WinSerialPort.cs: Inital import of the backend stream
+ for Windows support.
+
2006-04-06 Carlos Alberto Cortez <calberto.cortez@gmail.com>
* SerialPort.cs:
diff --git a/mcs/class/System/System.IO.Ports/WinSerialStream.cs b/mcs/class/System/System.IO.Ports/WinSerialStream.cs
new file mode 100644
index 00000000000..d333df37ef5
--- /dev/null
+++ b/mcs/class/System/System.IO.Ports/WinSerialStream.cs
@@ -0,0 +1,214 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.IO;
+using System.Runtime.InteropServices;
+using System.Threading;
+using System.ComponentModel;
+
+#if NET_2_0
+
+namespace Mono.System.IO.Ports
+{
+ public class WinSerialStream : Stream, IDisposable
+ {
+ // Windows API Constants
+ const uint GenericRead = 0x80000000;
+ const uint GenericWrite = 0x40000000;
+ const uint OpenExisting = 3;
+ const uint FileFlagOverlapped = 0x40000000;
+ const uint PurgeRxClear = 0x0004;
+ const uint PurgeTxClear = 0x0008;
+
+ int handle;
+ bool disposed;
+
+ [DllImport("kernel32", SetLastError=true)]
+ static extern int CreateFile(string port_name, uint desired_access,
+ uint share_mode, uint security_attrs, uint creation, uint flags,
+ uint template);
+
+ [DllImport("kernel32", SetLastError=true)]
+ static extern bool SetupComm(int handle, int read_buffer_size, int write_buffer_size);
+
+ [DllImport("kernel32", SetLastError = true)]
+ static extern bool PurgeComm(int handle, uint flags);
+
+ public unsafe WinSerialStream(string port_name, int read_buffer_size, int write_buffer_size)
+ {
+ handle = CreateFile(port_name, GenericRead | GenericWrite, 0, 0, OpenExisting,
+ FileFlagOverlapped, 0);
+
+ if (handle == -1)
+ throw new Win32Exception ();
+
+ // Clean buffers
+ if (!PurgeComm(handle, PurgeRxClear | PurgeTxClear))
+ throw new Win32Exception();
+
+ // Set buffers size
+ if (!SetupComm(handle, read_buffer_size, write_buffer_size))
+ throw new Win32Exception();
+ }
+
+ public override bool CanRead
+ {
+ get {
+ return true;
+ }
+ }
+
+ public override bool CanSeek
+ {
+ get {
+ return false;
+ }
+ }
+
+ public override bool CanTimeout
+ {
+ get
+ {
+ return true;
+ }
+ }
+
+ public override bool CanWrite
+ {
+ get {
+ return true;
+ }
+ }
+
+ public override long Length
+ {
+ get
+ {
+ throw new NotSupportedException();
+ }
+ }
+
+ public override long Position
+ {
+ get
+ {
+ throw new NotSupportedException();
+ }
+ set
+ {
+ throw new Exception("The method or operation is not implemented.");
+ }
+ }
+
+ [DllImport("kernel32", SetLastError=true)]
+ static extern bool CloseHandle(int handle);
+
+ protected override void Dispose(bool disposing)
+ {
+ if (disposed)
+ return;
+
+ disposed = true;
+ CloseHandle(handle);
+ }
+
+ void IDisposable.Dispose ()
+ {
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
+
+ public override void Close()
+ {
+ ((IDisposable)this).Dispose();
+ }
+
+ ~WinSerialStream()
+ {
+ Dispose(false);
+ }
+
+ public override void Flush()
+ {
+ CheckDisposed();
+ // No dothing by now
+ }
+
+ public override long Seek(long offset, SeekOrigin origin)
+ {
+ throw new NotSupportedException();
+ }
+
+ public override void SetLength(long value)
+ {
+ throw new NotSupportedException();
+ }
+
+ [DllImport("kernel32", SetLastError=true)]
+ static extern unsafe bool ReadFile(int handle, byte *buffer, int bytes_to_read,
+ int *read_bytes, int overlapped);
+
+ public override unsafe int Read(byte[] buffer, int offset, int count)
+ {
+ CheckDisposed();
+ 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();
+
+ int bytes_read = 0;
+ bool success;
+
+ fixed (byte *ptr = buffer)
+ {
+ success = ReadFile(handle, ptr + offset, count, &bytes_read, 0);
+ }
+
+ if (!success)
+ throw new Win32Exception();
+
+ return bytes_read;
+ }
+
+ [DllImport("kernel32", SetLastError = true)]
+ static extern unsafe bool WriteFile(int handle, byte* buffer, int bytes_to_write,
+ int* bytes_written, int overlapped);
+
+ public override unsafe void Write(byte[] buffer, int offset, int count)
+ {
+ CheckDisposed();
+ 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");
+
+ int bytes_written = 0;
+ bool success;
+ fixed (byte* ptr = buffer)
+ {
+ success = WriteFile(handle, ptr + offset, count, &bytes_written, 0);
+ }
+
+ if (!success)
+ throw new Win32Exception();
+ }
+
+ void CheckDisposed()
+ {
+ if (disposed)
+ throw new ObjectDisposedException(GetType().FullName);
+ }
+
+ }
+}
+
+#endif
+