Пример #1
0
        private void SetHandshake(Handshake handshake)
        {
            Termios termios = GetTermios();

            switch (handshake)
            {
            case Handshake.None:
                termios.ControlFlag &= ~(uint)(ControlFlags.CRTSCTS | ControlFlags.CDTRCTS);
                termios.InputFlag   &= ~(uint)(InputFlags.IXON | InputFlags.IXOFF);
                break;

            case Handshake.XOnXOff:
                termios.ControlFlag &= ~(uint)(ControlFlags.CRTSCTS | ControlFlags.CDTRCTS);
                termios.InputFlag   |= (uint)(InputFlags.IXON | InputFlags.IXOFF);
                break;

            case Handshake.RequestToSend:
                termios.ControlFlag |= (uint)ControlFlags.CRTSCTS;
                termios.ControlFlag &= (uint)ControlFlags.CDTRCTS;
                termios.InputFlag   &= ~(uint)(InputFlags.IXON | InputFlags.IXOFF);
                break;

            case Handshake.RequestToSendXOnXOff:
                termios.ControlFlag |= (uint)ControlFlags.CRTSCTS;
                termios.ControlFlag &= (uint)ControlFlags.CDTRCTS;
                termios.InputFlag   |= (uint)(InputFlags.IXON | InputFlags.IXOFF);
                break;

            default:
                break;
            }
            SetTermios(termios);
        }
Пример #2
0
        private void SetDataBits(int dataBits)
        {
            Termios termios = GetTermios();

            termios.ControlFlag &= ~(uint)ControlFlags.CSIZE;
            switch (dataBits)
            {
            case 5:
                termios.ControlFlag |= (uint)ControlFlags.CS5;
                break;

            case 6:
                termios.ControlFlag |= (uint)ControlFlags.CS6;
                break;

            case 7:
                termios.ControlFlag |= (uint)ControlFlags.CS7;
                break;

            default:
            case 8:
                termios.ControlFlag |= (uint)ControlFlags.CS8;
                break;
            }
            SetTermios(termios);
        }
Пример #3
0
        public static byte[] getBytes(Termios str)
        {
            int size = Marshal.SizeOf(str);

            byte[] arr = new byte[size];

            IntPtr ptr = Marshal.AllocHGlobal(size);

            Marshal.StructureToPtr(str, ptr, true);
            Marshal.Copy(ptr, arr, 0, size);
            Marshal.FreeHGlobal(ptr);
            return(arr);
        }
Пример #4
0
        public static Termios fromBytes(byte[] arr)
        {
            Termios str = new Termios();
            //Debug.WriteLine(Marshal.SizeOf(str));
            int    size = Marshal.SizeOf(str);
            IntPtr ptr  = Marshal.AllocHGlobal(size);

            Marshal.Copy(arr, 0, ptr, size);

            str = (Termios)Marshal.PtrToStructure(ptr, str.GetType());
            Marshal.FreeHGlobal(ptr);

            return(str);
        }
Пример #5
0
        private void SetParity(Parity parity)
        {
            Termios termios = GetTermios();

            switch (parity)
            {
            default:
            case Parity.None:
                termios.ControlFlag &= ~(uint)ControlFlags.PARENB;
                termios.ControlFlag &= ~(uint)ControlFlags.PARODD;
                //termios.InputFlag &= ~(uint)InputFlags.INPCK;
                //termios.InputFlag &= ~(uint)InputFlags.PARMRK;
                break;

            case Parity.Odd:
                termios.ControlFlag |= (uint)ControlFlags.PARENB;
                termios.ControlFlag |= (uint)ControlFlags.PARODD;
                //termios.InputFlag |= (uint)InputFlags.INPCK;
                //termios.InputFlag &= ~(uint)InputFlags.PARMRK;
                break;

            case Parity.Even:
                termios.ControlFlag |= (uint)ControlFlags.PARENB;
                termios.ControlFlag &= ~(uint)ControlFlags.PARODD;
                //termios.InputFlag |= (uint)InputFlags.INPCK;
                //termios.InputFlag &= ~(uint)InputFlags.PARMRK;
                break;

            case Parity.Mark:
                termios.ControlFlag |= (uint)ControlFlags.PARENB;
                termios.ControlFlag |= (uint)ControlFlags.PARODD;
                termios.ControlFlag |= (uint)ControlFlags.CMSPAR;
                //termios.InputFlag |= (uint)InputFlags.INPCK;
                //termios.InputFlag |= (uint)InputFlags.PARMRK;
                break;

            case Parity.Space:
                termios.ControlFlag |= (uint)ControlFlags.PARENB;
                termios.ControlFlag &= ~(uint)ControlFlags.PARODD;
                termios.ControlFlag |= (uint)ControlFlags.CMSPAR;
                //termios.InputFlag |= (uint)InputFlags.INPCK;
                //termios.InputFlag |= (uint)InputFlags.PARMRK;
                break;
            }
            SetTermios(termios);
        }
Пример #6
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);
        }
Пример #7
0
        private void SetStopBits(StopBits stopBits)
        {
            Termios termios = GetTermios();

            switch (stopBits)
            {
            case StopBits.One:
                termios.ControlFlag &= ~(uint)ControlFlags.CSTOPB;
                break;

            case StopBits.Two:
                termios.ControlFlag |= (uint)ControlFlags.CSTOPB;
                break;

            default:
            case StopBits.None:
            case StopBits.OnePointFive:
                throw new ArgumentException($"StopBits only support One or Two");
                break;
            }
            SetTermios(termios);
        }
Пример #8
0
 private void SetTermios(Termios terminos)
 {
     byte[] termiosData = TermiosHelpers.getBytes(terminos);
     Libc.tcsetattr(_FileDescriptor.Value, 0, termiosData);
 }