Ejemplo n.º 1
0
        public void Start()
        {
            this.SerialDevices.Clear();

            Task t = new Task(() =>
            {
                int n = 0;
                while (!this.Stopped)
                {
                    bool res = true;
                    for (int i = 0; i < this.SerialDevices.Count; i++)
                    {
                        ExSerialDevice device = this.SerialDevices[i];
                        if (device.IsOpen == false)
                        {
                            try
                            {
                                device.Open();
                                res &= true;
                            }
                            catch (Exception ex)
                            {
                                res &= false;
                                Console.WriteLine(ex.Message);
                                this.SerialDevices.Remove(device);
                                continue;
                            }
                        }
                    }

                    if (MainWindow.App.MainWindowViewModel != null)
                    {
                        //MainWindow.App.MainWindowViewModel.SimConnectViewModel.SerialPortConnected = res;
                        this.Connected = res && this.SerialDevices.Count > 0;
                    }
                    if (n++ % 10 == 0)
                    {
                        this.InitPorts();
                    }

                    Thread.Sleep(1000);
                }
            });

            t.Start();
        }
Ejemplo n.º 2
0
        public void InitPorts()
        {
            foreach (string pname in System.IO.Ports.SerialPort.GetPortNames())
            {
                if (Settings.Default.DisabledComports.Contains(pname) == false)
                {
                    ExSerialDevice sd = this.SerialDevices.Where(k => k.PortName == pname).FirstOrDefault();
                    if (sd == null)
                    {
                        ExSerialDevice sp = new ExSerialDevice(pname, 9600);

                        sp.CallbackProcessHandle += new ExSerialDevice.CallbackProcess(Process);
                        //this.SerialDevices.Add(sp);
                        System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke(() =>
                        {
                            this.SerialDevices.Add(sp);
                        });
                    }
                }
            }
        }