Example #1
0
        public termios getOptions()
        {
            termios options;

            Syscalls.tcgetattr(_FileDescriptor, out options);
            return(options);
        }
Example #2
0
        /// <summary>
        /// Flushes input and output buffers
        /// </summary>
        public void Flush(FlushMode mode)
        {
            int queueSelector;

            if (mode == FlushMode.Input)
            {
                queueSelector = Macros.TCIFLUSH;
            }
            else if (mode == FlushMode.Output)
            {
                queueSelector = Macros.TCOFLUSH;
            }
            else
            {
                queueSelector = Macros.TCIOFLUSH;
            }

            Syscalls.tcflush(_FileDescriptor, queueSelector);
        }
Example #3
0
        private void setOptions()
        {
            termios options;

            Syscalls.tcgetattr(_FileDescriptor, out options);

            /* Set baud rate */
            Syscalls.cfsetspeed(ref options, findBaudrate(_baudRate));

            /* These will ensure that the program does not become the 'owner' of the
             * port subject to sporatic job control and hangup signals,
             * and also that the serial interface driver will read incoming data bytes.
             */

            options.c_cflag |= (Macros.CLOCAL | Macros.CREAD);

            /* Set byte size */
            options.c_cflag &= ~Macros.CSIZE;             /* Mask the character size bits */
            options.c_cflag |= findByteSize(_dataBits);   /* Select 8 data bits */

            /* Set parity */
            switch (_parity)
            {
            case Parity.None:
                options.c_cflag &= ~Macros.PARENB;                 /* Mask the parity bits */
                break;

            case Parity.Odd:
                options.c_cflag |= Macros.PARENB;                 /* Mask the parity bits */
                options.c_cflag |= Macros.PARODD;
                break;

            case Parity.Even:
                options.c_cflag |= Macros.PARENB;                 /* Mask the parity bits */
                options.c_cflag &= ~Macros.PARODD;
                break;

            case Parity.Mark:
                throw new NotImplementedException();

            case Parity.Space:
                throw new NotImplementedException();
            }

            /* Set stop bits */
            if (_stopBits == StopBits.One)
            {
                options.c_cflag &= ~Macros.CSTOPB;
            }
            else if (_stopBits == StopBits.Two)
            {
                options.c_cflag |= Macros.CSTOPB;
            }
            else
            {
                throw new ArgumentOutOfRangeException("Only one and two stop bits are supported");
            }

            /* Dtr Control */
            int status = 0;

            if (_dtrControl == DtrControl.Enable)
            {
                Syscalls.ioctl(_FileDescriptor, Macros.TIOCMGET, ref status);
                status &= ~Macros.TIOCM_DTR;
                Syscalls.ioctl(_FileDescriptor, Macros.TIOCMSET, ref status);
            }

            // Hardware flow control

            /*
             *
             * try {
             *      options.c_cflag |= Macros.CNEW_RTSCTS;
             * } catch (ArgumentNullException) {
             *      try {
             *              options.c_cflag |= Macros.CRTSCTS;
             *      } catch (ArgumentNullException) {
             *              throw new HardwareFlowControlNotSupportedException ();
             *      }
             * }
             */
            /* Enables software flow control */

            /*
             * options.c_iflag |= (Macros.IXON | Macros.IXOFF | Macros.IXANY);
             */

            /* Choose raw input (as opposed to canonical) */
            options.c_lflag &= ~(Macros.ICANON | Macros.ECHO | Macros.ECHOE | Macros.ISIG);

            /* Choose raw output (as opposed to canonical) */
            options.c_oflag &= ~Macros.OPOST;


            /* Enable parity */
            options.c_iflag |= (Macros.INPCK | Macros.ISTRIP);

            /* Set read time out and behavior */
            options.c_cc [Macros.VMIN]  = 0;
            options.c_cc [Macros.VTIME] = 2;             // x100 msec

            /* Write out the options */
            Syscalls.tcsetattr(_FileDescriptor, Macros.TCSAFLUSH, ref options);

            /* Read time*/
            Syscalls.fcntl(_FileDescriptor, Macros.F_SETFL, 0);

            termios check = getOptions();

            if (check.c_iflag != options.c_iflag)
            {
                throw new InvalidOperationException();
            }
        }