コード例 #1
0
 public SerialControl(Serial port)
 {
     this.port        = port;
     close            = new CancellationTokenSource();
     readCt           = close.Token;
     port.ReadTimeout = 5;
 }
コード例 #2
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Write the portName as argument.");
                return;
            }

            Console.WriteLine("Starting on port " + args[0]);
            var serialPort = new RJCP.IO.Ports.SerialPortStream(args[0])
            {
                BaudRate     = 19200,
                DataBits     = 8,
                StopBits     = StopBits.One,
                Parity       = Parity.None,
                ReadTimeout  = 1000,
                WriteTimeout = 500,
                Handshake    = Handshake.None
            };

            if (!serialPort.IsOpen)
            {
                Console.WriteLine("Opening port.");
                serialPort.Open();
            }

            Console.WriteLine("Press ESCAPE to read a line.");
            Console.WriteLine("Otherwise, type and perss enter to send a message.");
            Console.WriteLine("Type exit to quit.");
            var buffer = new StringBuilder();

            do
            {
                var key = Console.ReadKey();
                if (key.Key == ConsoleKey.Escape)
                {
                    string read = null;
                    try
                    {
                        read = serialPort.ReadLine();
                    }
                    catch (TimeoutException)
                    {
                        read = "TIMEOUT";
                    }
                    Console.WriteLine("R> " + read);
                }
                else if (key.Key == ConsoleKey.Enter)
                {
                    var final = buffer.ToString();
                    if (final.ToUpper() == "EXIT")
                    {
                        break;
                    }

                    serialPort.WriteLine(final);
                    Console.WriteLine("");
                    buffer.Clear();
                }
                else
                {
                    buffer.Append(key.KeyChar);
                }
            } while (true);

            serialPort.Close();

            Console.WriteLine("Ending");
        }
コード例 #3
0
        private static IObservable <T> CreateListener <T>(string portName, BaudRate baudRate, Parity parity, int dataBits, StopBits stopBits, Encoding encoding, int bufferSize, Func <RJCPIO.SerialPortStream, T[], Task <int> > readFromPort)
        {
            if (string.IsNullOrEmpty(portName))
            {
                throw new ArgumentNullException("portName");
            }
            if (encoding == null)
            {
                throw new ArgumentNullException("encoding");
            }
            if (bufferSize < 1)
            {
                throw new ArgumentOutOfRangeException("bufferSize", bufferSize, string.Empty);
            }

            var buffer = new T[bufferSize];

            return(Observable.Using(
                       () =>
            {
                RJCPIO.Parity rjcpParity;
                switch (parity)
                {
                case Parity.Even: rjcpParity = RJCPIO.Parity.Even; break;

                case Parity.Mark: rjcpParity = RJCPIO.Parity.Mark; break;

                case Parity.None: rjcpParity = RJCPIO.Parity.None; break;

                case Parity.Odd: rjcpParity = RJCPIO.Parity.Odd; break;

                case Parity.Space: rjcpParity = RJCPIO.Parity.Space; break;

                default: throw new NotSupportedException(string.Format("'{0}' is not supported.", parity));
                }

                RJCPIO.StopBits rjcpStopBits;
                switch (stopBits)
                {
                case StopBits.One: rjcpStopBits = RJCPIO.StopBits.One; break;

                case StopBits.OnePointFive: rjcpStopBits = RJCPIO.StopBits.One5; break;

                case StopBits.Two: rjcpStopBits = RJCPIO.StopBits.Two; break;

                default: throw new NotSupportedException(string.Format("'{0}' is not supported.", stopBits));
                }

                var port = new RJCPIO.SerialPortStream(portName, (int)baudRate, dataBits, rjcpParity, rjcpStopBits)
                {
                    NewLine = "\r\n", Encoding = encoding
                };
                port.Open();

                return port;
            },
                       port =>
            {
                int?count = null;

                return Observable.While(() => port.IsOpen && (count == null || count > 0),
                                        Observable.Defer(
                                            async() =>
                {
                    try
                    {
                        count = await readFromPort(port, buffer).ConfigureAwait(false);

                        if (count <= 0)
                        {
                            return Notification.CreateOnCompleted <T>().ToObservable();
                        }
                        else
                        {
                            var received = new T[count.Value];
                            Array.Copy(buffer, 0, received, 0, count.Value);
                            return received.ToObservable();
                        }
                    }
                    catch (Exception ex)
                    {
                        return Notification.CreateOnError <T>(ex).ToObservable();
                    }
                }));
            }));
        }