| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607 |
- /* -*- Mode: Csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
- #if NET_2_0
- using System.Text;
- using System.Runtime.InteropServices;
- namespace System.IO.Ports
- {
- public enum Handshake
- {
- None, /* No control is used for the handshake */
- RequestToSend, /* Request-to-Send (RTS) hardware flow
- * control is used. RTS is used to signal
- * that data is available for
- * transmission. */
- RequestToSendXOnXOff, /* Both the Request-to-Send (RTS) hardware
- * control and the XON/XOFF software
- * controls are used. */
- XOnXOff /* The XON/XOFF software control protocol is
- * used. XOFF is a software control sent to
- * stop the transmission of data and the XON
- * control is sent to resume the
- * transmission. These controls are used
- * instead of the Request to Send (RTS) and
- * Clear to Send (CTS) hardware controls. */
- }
- public enum Parity
- {
- Even, /* Sets the parity bit so that the count of bits set is an even number */
- Mark, /* Leaves the parity bit set to 1. */
- None, /* No parity check occurs */
- Odd, /* Sets the parity bit so that the count of bits set is an odd number */
- Space /* Leaves the parity bit set to 0 */
- }
- public enum StopBits
- {
- One, /* One stop bit is used */
- OnePointFive, /* Three stop bits are used. */
- Two /* Two stop bits are used. */
- }
- public class SerialPort /* : Component */
- {
- public const int InfiniteTimeout = -1;
- bool isOpen = false;
- int baudRate = 9600;
- Parity parity = Parity.None;
- StopBits stopBits = StopBits.One;
- Handshake handshake = Handshake.None;
- int dataBits = 8;
- bool breakState = false;
- Stream baseStream;
- string newLine = "\n";
- string portName;
- int unixFd;
- int readTimeout = InfiniteTimeout;
- int writeTimeout = InfiniteTimeout;
- private class SerialPortStream : Stream
- {
- SerialPort port;
- public SerialPortStream (SerialPort port)
- {
- this.port = port;
- }
- public override bool CanRead
- {
- get {
- return true;
- }
- }
- public override bool CanSeek
- {
- get {
- return false;
- }
- }
- public override bool CanWrite
- {
- get {
- return true;
- }
- }
- public override long Length
- {
- get {
- return -1;
- }
- }
- public override long Position
- {
- get {
- return -1;
- }
- set {
- throw new InvalidOperationException ();
- }
- }
- public override void Flush ()
- {
- throw new NotImplementedException ();
- }
- public override int Read ([In,Out] byte[] buffer, int offset, int count)
- {
- return port.Read (buffer, offset, count);
- }
- public override long Seek (long offset, SeekOrigin origin)
- {
- throw new InvalidOperationException ();
- }
- public override void SetLength (long value)
- {
- throw new InvalidOperationException ();
- }
- public override void Write (byte[] buffer, int offset, int count)
- {
- port.Write (buffer, offset, count);
- }
- }
- public SerialPort ()
- {
- throw new NotImplementedException ();
- }
- /*
- IContainer is in 2.0?
- public SerialPort (IContainer container) {
- }
- */
- public SerialPort (string portName)
- {
- this.portName = portName;
- }
- public SerialPort (string portName, int baudRate)
- {
- this.portName = portName;
- this.baudRate = baudRate;
- }
- public SerialPort (string portName, int baudRate, Parity parity)
- {
- this.portName = portName;
- this.baudRate = baudRate;
- this.parity = parity;
- }
- public SerialPort (string portName, int baudRate, Parity parity, int dataBits)
- {
- this.portName = portName;
- this.baudRate = baudRate;
- this.parity = parity;
- this.dataBits = dataBits;
- }
- public SerialPort (string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits) {
- this.portName = portName;
- this.baudRate = baudRate;
- this.parity = parity;
- this.dataBits = dataBits;
- this.stopBits = stopBits;
- }
- public Stream BaseStream
- {
- get {
- if (!isOpen)
- throw new InvalidOperationException ();
- if (baseStream == null)
- baseStream = new SerialPortStream (this);
- return baseStream;
- }
- }
- [DllImport("MonoPosixHelper")]
- private static extern bool set_attributes (int unix_fd, int baud_rate, Parity parity, int dataBits, StopBits stopBits, Handshake handshake);
-
- public int BaudRate
- {
- get {
- return baudRate;
- }
- set {
- this.baudRate = value;
- set_attributes (unixFd, baudRate, parity, dataBits, stopBits, handshake);
- }
- }
- public bool BreakState
- {
- get {
- return breakState;
- }
- set {
- if (!isOpen)
- throw new InvalidOperationException ();
- throw new NotImplementedException ();
- }
- }
- public int BytesToRead
- {
- get {
- if (!isOpen)
- throw new InvalidOperationException ();
- throw new NotImplementedException ();
- }
- }
- public int BytesToWrite
- {
- get {
- if (!isOpen)
- throw new InvalidOperationException ();
- throw new NotImplementedException ();
- }
- }
- public bool CDHolding
- {
- get {
- if (!isOpen)
- throw new InvalidOperationException ();
- throw new NotImplementedException ();
- }
- }
- public bool CtsHolding
- {
- get {
- if (!isOpen)
- throw new InvalidOperationException ();
- throw new NotImplementedException ();
- }
- }
- public int DataBits
- {
- get {
- throw new NotImplementedException ();
- }
- set {
- if (value < 5 || value > 8)
- throw new ArgumentOutOfRangeException ();
- dataBits = value;
- set_attributes (unixFd, baudRate, parity, dataBits, stopBits, handshake);
- }
- }
- public bool DiscardNull
- {
- get {
- if (!isOpen)
- throw new InvalidOperationException ();
- throw new NotImplementedException ();
- }
- set {
- if (!isOpen)
- throw new InvalidOperationException ();
- throw new NotImplementedException ();
- }
- }
- public bool DsrHolding
- {
- get {
- if (!isOpen)
- throw new InvalidOperationException ();
- throw new NotImplementedException ();
- }
- }
- public bool DtrEnable
- {
- get {
- throw new NotImplementedException ();
- }
- }
- public Encoding Encoding
- {
- get {
- throw new NotImplementedException ();
- }
- set {
- throw new NotImplementedException ();
- }
- }
- public Handshake Handshake
- {
- get {
- if (!isOpen)
- throw new InvalidOperationException ();
- return handshake;
- }
- set {
- if (!isOpen)
- throw new InvalidOperationException ();
- handshake = value;
- set_attributes (unixFd, baudRate, parity, dataBits, stopBits, handshake);
- }
- }
- public bool IsOpen
- {
- get {
- return isOpen;
- }
- }
- public string NewLine
- {
- get {
- return newLine;
- }
- set {
- newLine = value;
- }
- }
- public Parity Parity
- {
- get {
- if (!isOpen)
- throw new InvalidOperationException ();
- return parity;
- }
- set {
- if (!isOpen)
- throw new InvalidOperationException ();
- this.parity = value;
- set_attributes (unixFd, baudRate, parity, dataBits, stopBits, handshake);
- }
- }
- public byte ParityReplace
- {
- get {
- throw new NotImplementedException ();
- }
- set {
- throw new NotImplementedException ();
- }
- }
- public string PortName
- {
- get {
- return portName;
- }
- set {
- throw new NotImplementedException ();
- }
- }
- public int ReadBufferSize
- {
- get {
- throw new NotImplementedException ();
- }
- set {
- if (value <= 0)
- throw new ArgumentOutOfRangeException ();
- throw new NotImplementedException ();
- }
- }
- public int ReadTimeout
- {
- get {
- return readTimeout;
- }
- set {
- if (value <= 0 && value != InfiniteTimeout)
- throw new ArgumentOutOfRangeException ();
- readTimeout = value;
- }
- }
- public int ReceivedBytesThreshold
- {
- get {
- throw new NotImplementedException ();
- }
- set {
- if (value <= 0)
- throw new ArgumentOutOfRangeException ();
- throw new NotImplementedException ();
- }
- }
- public bool RtsEnable
- {
- get {
- throw new NotImplementedException ();
- }
- set {
- throw new NotImplementedException ();
- }
- }
- public StopBits StopBits
- {
- get {
- return stopBits;
- }
- set {
- this.stopBits = value;
- set_attributes (unixFd, baudRate, parity, dataBits, stopBits, handshake);
- }
- }
- public int WriteBufferSize
- {
- get {
- throw new NotImplementedException ();
- }
- set {
- if (value <= 0)
- throw new ArgumentOutOfRangeException ();
- throw new NotImplementedException ();
- }
- }
- public int WriteTimeout
- {
- get {
- return writeTimeout;
- }
- set {
- if (value <= 0 && value != InfiniteTimeout)
- throw new ArgumentOutOfRangeException ();
- writeTimeout = value;
- }
- }
- // methods
- [DllImport("MonoPosixHelper")]
- private static extern void close_serial (int unixFd);
- public void Close ()
- {
- isOpen = false;
- close_serial (unixFd);
- }
- [DllImport("MonoPosixHelper")]
- private static extern void discard_buffer (int unixFd, bool input_buffer);
- public void DiscardInBuffer ()
- {
- if (!isOpen)
- throw new InvalidOperationException ();
- discard_buffer (unixFd, true);
- }
- public void DiscardOutBuffer ()
- {
- if (!isOpen)
- throw new InvalidOperationException ();
- discard_buffer (unixFd, false);
- }
- [DllImport("MonoPosixHelper")]
- private static extern string[] list_serial_devices ();
- public static string[] GetPortNames()
- {
- return list_serial_devices ();
- }
- [DllImport("MonoPosixHelper")]
- private static extern int open_serial (string portName);
- public void Open ()
- {
- if (portName == null || portName.StartsWith ("\\\\"))
- throw new ArgumentException ();
- unixFd = open_serial (portName);
- if (unixFd == -1)
- throw new IOException();
- set_attributes (unixFd, baudRate, parity, dataBits, stopBits, handshake);
- isOpen = true;
- }
- [DllImport("MonoPosixHelper")]
- private static extern int read_serial (int unixFd, byte[] buffer, int offset, int count, int timeout);
- public int Read (byte[] buffer, int offset, int count)
- {
- return read_serial (unixFd, buffer, offset, count, readTimeout);
- }
- public int Read (char[] buffer, int offset, int count)
- {
- throw new NotImplementedException ();
- }
- byte[] read_buffer = new byte[4096];
- public int ReadByte ()
- {
- if (Read (read_buffer, 0, 1) == 1)
- return read_buffer [0];
-
- return -1;
- }
- public int ReadChar ()
- {
- throw new NotImplementedException ();
- }
- public string ReadExisting ()
- {
- throw new NotImplementedException ();
- }
- public string ReadLine ()
- {
- return ReadTo (newLine);
- }
- public string ReadTo (string value)
- {
- throw new NotImplementedException ();
- }
- [DllImport("MonoPosixHelper")]
- private static extern void write_serial (int unixFd, byte[] buffer, int offset, int count, int timeout);
- public void Write (string str)
- {
- throw new NotImplementedException ();
- }
- public void Write (byte[] buffer, int offset, int count)
- {
- write_serial (unixFd, buffer, offset, count, writeTimeout);
- }
- public void Write (char[] buffer, int offset, int count)
- {
- throw new NotImplementedException ();
- }
- public void WriteLine (string str)
- {
- Write (str + newLine);
- }
- // events
- public delegate void SerialReceivedEventHandler (object sender, SerialReceivedEventArgs e);
- public delegate void SerialPinChangedEventHandler (object sender, SerialPinChangedEventArgs e);
- public delegate void SerialErrorEventHandler (object sender, SerialErrorEventArgs e);
- public event SerialErrorEventHandler ErrorEvent;
- public event SerialPinChangedEventHandler PinChangedEvent;
- public event SerialReceivedEventHandler ReceivedEvent;
- }
- }
- #endif
|