Exemplo n.º 1
0
        /// <summary>
        /// Open the serial port
        /// </summary>
        public void Open()
        {
            if (IsOpen)
            {
                throw new IOException($"Port already open");
            }

            // open serial port
            int fd = Libc.open(_PortName, Libc.OpenFlags.O_RDWR | Libc.OpenFlags.O_NONBLOCK);

            if (fd == -1)
            {
                throw new Exception($"failed to open port ({_PortName})");
            }

            _FileDescriptor = fd;
            // Initilise the serial port
            Termios termios = GetTermios();

            // Make sure we are ok to read
            termios.ControlFlag |= (uint)ControlFlags.CREAD;
            termios.ControlFlag |= (uint)ControlFlags.CLOCAL;
            //Clean the output flags
            termios.OutputFlag &= ~(uint)OutputFlags.OPOST;
            // Clear what can be on the input flags
            termios.InputFlag &= ~(uint)(InputFlags.IGNBRK | InputFlags.BRKINT | InputFlags.ICRNL |
                                         InputFlags.PARMRK | InputFlags.INLCR | InputFlags.ISTRIP | InputFlags.IXON);
            // Set ICANON off
            termios.LocalFalg &= ~(uint)LocalFlags.ICANON;
            // Set ECHO off
            termios.LocalFalg &= ~(uint)LocalFlags.ECHO;
            termios.LocalFalg &= ~(uint)LocalFlags.ECHOE;
            termios.LocalFalg &= ~(uint)LocalFlags.ECHOCTL;
            termios.LocalFalg &= ~(uint)LocalFlags.IEXTEN;
            termios.LocalFalg &= ~(uint)LocalFlags.ISIG;
            SetTermios(termios);
            SetBaudRate(_BaudRate);
            SetDataBits(_DataBits);
            SetParity(_Parity);
            SetStopBits(_StopBits);
            // start reading
            Task.Run((Action)StartReading, _CancellationToken);
        }
Exemplo n.º 2
0
        public void Open()
        {
            // open serial port
            int fd = Libc.open(portName, Libc.OpenFlags.O_RDWR | Libc.OpenFlags.O_NONBLOCK);

            if (fd == -1)
            {
                throw new Exception($"failed to open port ({portName})");
            }

            // set baud rate
            byte[] termiosData = new byte[256];

            Libc.tcgetattr(fd, termiosData);
            Libc.cfsetspeed(termiosData, baudRate);
            Libc.tcsetattr(fd, 0, termiosData);
            // start reading
            Task.Run((Action)StartReading, CancellationToken);
            this.fd = fd;
        }