Ejemplo n.º 1
0
        public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            if (context != null)
            {
                var workflowBuilder = (WorkflowBuilder)context.GetService(typeof(WorkflowBuilder));
                if (workflowBuilder != null)
                {
                    var portNames = (from builder in workflowBuilder.Workflow.Descendants()
                                     let createPort = ExpressionBuilder.GetWorkflowElement(builder) as CreateSerialPort
                                                      where createPort != null && !string.IsNullOrEmpty(createPort.PortName)
                                                      select !string.IsNullOrEmpty(createPort.Name) ? createPort.Name : createPort.PortName)
                                    .Concat(SerialPortManager.LoadConfiguration().Select(configuration => configuration.PortName))
                                    .Distinct()
                                    .ToList();

                    if (portNames.Count > 0)
                    {
                        return(new StandardValuesCollection(portNames));
                    }
                    else
                    {
                        return(new StandardValuesCollection(SerialPort.GetPortNames()));
                    }
                }
            }

            return(base.GetStandardValues(context));
        }
Ejemplo n.º 2
0
        public override IObservable <TSource> Process <TSource>(IObservable <TSource> source)
        {
            var newLine = ObservableSerialPort.Unescape(NewLine);

            return(Observable.Using(
                       () => SerialPortManager.ReserveConnection(PortName),
                       connection => source.Do(value =>
            {
                lock (connection.SerialPort)
                {
                    connection.SerialPort.Write(value.ToString());
                    connection.SerialPort.Write(newLine);
                }
            })));
        }
Ejemplo n.º 3
0
        public static IObservable <string> ReadLine(string portName, string newLine)
        {
            return(Observable.Create <string>(observer =>
            {
                var data = string.Empty;
                var connection = SerialPortManager.ReserveConnection(portName);
                SerialDataReceivedEventHandler dataReceivedHandler;
                var serialPort = connection.SerialPort;
                dataReceivedHandler = (sender, e) =>
                {
                    switch (e.EventType)
                    {
                    case SerialData.Eof: observer.OnCompleted(); break;

                    case SerialData.Chars:
                    default:
                        if (serialPort.IsOpen && serialPort.BytesToRead > 0)
                        {
                            data += serialPort.ReadExisting();
                            var lines = data.Split(new[] { newLine }, StringSplitOptions.None);
                            for (int i = 0; i < lines.Length; i++)
                            {
                                if (i == lines.Length - 1)
                                {
                                    data = lines[i];
                                }
                                else
                                {
                                    observer.OnNext(lines[i]);
                                }
                            }
                        }
                        break;
                    }
                };

                connection.SerialPort.DataReceived += dataReceivedHandler;
                return Disposable.Create(() =>
                {
                    connection.SerialPort.DataReceived -= dataReceivedHandler;
                    connection.Dispose();
                });
            }));
        }
Ejemplo n.º 4
0
 public override IObservable <SerialPort> Generate()
 {
     return(Observable.Using(
                () => SerialPortManager.ReserveConnection(Name, configuration),
                connection => Observable.Return(connection.SerialPort).Concat(Observable.Never(connection.SerialPort))));
 }