Esempio n. 1
0
 /// <summary>
 /// Open the port using a specified set of options
 /// </summary>
 public bool Open(SerialPortOptions options)
 {
     if (IsOpened)
     {
         return(false);
     }
     if (!File.Exists(device))
     {
         return(false);
     }
     port = open(device, O_RDWR | O_NOCTTY);
     if (IsClosed)
     {
         return(false);
     }
     UpdatePort(options);
     return(true);
 }
Esempio n. 2
0
        /// <summary>
        /// Update the port settings using a set of options
        /// </summary>
        /// <remarks>See https://github.com/pyserial/pyserial/blob/master/serial/serialposix.py</remarks>
        private void UpdatePort(SerialPortOptions options)
        {
            var term = new TermiosStruct();

            tcgetattr(port, ref term);
            term.c_cflag |= CLOCAL | CREAD;
            term.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN);
            term.c_oflag &= ~(OPOST | ONLCR | OCRNL);
            term.c_iflag &= ~(INLCR | IGNCR | ICRNL | IGNBRK | INPCK | ISTRIP);
            term.c_cflag &= ~(CBAUD | CBAUDEX | CSIZE);
            switch (options.Baud)
            {
            case (Baud300):
                term.c_cflag |= B300;
                break;

            case (Baud1200):
                term.c_cflag |= B1200;
                break;

            case (Baud2400):
                term.c_cflag |= B2400;
                break;

            case (Baud4800):
                term.c_cflag |= B4800;
                break;

            case (Baud9600):
                term.c_cflag |= B9600;
                break;

            case (Baud19200):
                term.c_cflag |= B19200;
                break;

            case (Baud38400):
                term.c_cflag |= B38400;
                break;

            case (Baud57600):
                term.c_cflag |= B57600;
                break;

            case (Baud115200):
                term.c_cflag |= B115200;
                break;

            case (Baud230400):
                term.c_cflag |= B230400;
                break;

            default:
                term.c_cflag |= B9600;
                break;
            }
            switch (options.DataBits)
            {
            case (Bits5):
                term.c_cflag |= CS5;
                break;

            case (Bits6):
                term.c_cflag |= CS6;
                break;

            case (Bits7):
                term.c_cflag |= CS7;
                break;

            case (Bits8):
                term.c_cflag |= CS8;
                break;

            default:
                term.c_cflag |= CS8;
                break;
            }
            if (options.Parity == Parity.Odd)
            {
                term.c_cflag |= PARENB | PARODD;
            }
            else if (options.Parity == Parity.Even)
            {
                term.c_cflag &= ~PARODD;
                term.c_cflag |= PARENB;
            }
            else
            {
                term.c_cflag &= ~(PARENB | PARODD);
            }
            if (options.StopBits == StopBits.One)
            {
                term.c_cflag &= ~CSTOPB;
            }
            else
            {
                term.c_cflag |= CSTOPB;
            }
            term.c_iflag &= ~(IXON | IXOFF);
            term.c_cflag &= ~CRTSCTS;
            if (options.FlowControl.HasFlag(FlowControl.XOn))
            {
                term.c_iflag |= IXON;
            }
            if (options.FlowControl.HasFlag(FlowControl.XOff))
            {
                term.c_iflag |= IXOFF;
            }
            if (options.FlowControl.HasFlag(FlowControl.RequestToSend))
            {
                term.c_cflag |= CRTSCTS;
            }
            term.c_cc5 = options.Timeout;
            term.c_cc6 = options.Min;
            tcsetattr(port, TCSANOW, ref term);
        }