Exemplo n.º 1
0
        private async Task <object> _openSerialPort(dynamic input)
        {
            ObservableSerialPort serialPort = null;

            try {
                var portName   = input as string;
                var hasOptions = false;
                if (portName == null)
                {
                    portName   = input.portName as string;
                    hasOptions = true;
                }
                serialPort = new ObservableSerialPort(portName);

                if (hasOptions)
                {
                    try { serialPort.BaudRate = (int)input.baudRate; } catch { }
                    try { serialPort.DataBits = (int)input.dataBits; } catch { }
                    try { serialPort.Parity = (Parity)Enum.Parse(typeof(Parity), input.parity as string); } catch { }
                    try { serialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), input.stopBits as string); } catch { }
                }

                var regex = new Regex(@"V\[V\]=,\s{0,2}(\d{1,3}\.\d),I\[A\]=,(\d\.\d{3})", RegexOptions.Compiled | RegexOptions.Singleline);

                var source = serialPort.Where(i => i.Item1 == typeof(SerialData))
                             .Select(i => i.Item2)
                             .Scan(Tuple.Create(string.Empty, Enumerable.Empty <Tuple <double, double> >()), (Sum, New) => {
                    var split  = regex.Split(Sum.Item1 + New);
                    var filter = split.Where((_, i) => i % 3 != 0).Select(double.Parse);
                    var capt   = filter.Where((_, i) => i % 2 == 0).Zip(filter.Where((_, i) => i % 2 != 0), Tuple.Create);
                    return(Tuple.Create(split.Last(), capt));
                })
                             .SelectMany(i => i.Item2)
                             .Select(i => new { V = i.Item1, I = i.Item2, W = i.Item1 * i.Item2 })
                             .Finally(serialPort.Dispose)
                             .Publish().RefCount();

                serialPort.Open();

                return(new {
                    dispose = (EdgeFunc)(async _ => {
                        serialPort.Dispose();
                        return null;
                    }),
                    write = (EdgeFunc)(async str => { serialPort.Write(str); return null; }),
                    subscribe = (EdgeFunc)(async ob => {
                        EdgeFunc onNext, onError = null, onCompleted = null;
                        onNext = ob as EdgeFunc;
                        if (onNext == null)
                        {
                            try { onNext = ob.onNext as EdgeFunc; } catch { }
                            try { onError = ob.onError as EdgeFunc; } catch { }
                            try { onCompleted = ob.onCompleted as EdgeFunc; } catch { }
                        }
                        if (ob == null)
                        {
                            return null;
                        }

                        var dispose = source.Subscribe(
                            i => onNext(i),
                            e => { if (onError != null)
                                   {
                                       onError(e);
                                   }
                            },
                            () => { if (onCompleted != null)
                                    {
                                        onCompleted(null);
                                    }
                            }
                            );

                        return (EdgeFunc)(async _ => {
                            dispose.Dispose();
                            return null;
                        });
                    })
                });
            } catch {
                if (serialPort != null)
                {
                    serialPort.Dispose();
                }
                return(null);
            }
        }
Exemplo n.º 2
0
 public ISerialPort()
 {
     getPortInfo    = _getPortInfo;
     openSerialPort = _openSerialPort;
 }