Example #1
0
        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            // run all background tasks here
            //autoSerialPort = FindPort();
            IProgress <string> progress = null;

            var ports = System.IO.Ports.SerialPort.GetPortNames().OrderBy(s => s);

            if (ports.Count() <= 0)
            {
                MessageBox.Show(
                    "Error: No Serial Ports avaliable.  Make sure the gps reciever is plugged in and installed.  Contact your system administrator for help. \n"
                    , "Error 1: No Serial Ports Found",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error);
                _continue      = false;
                autoSerialPort = null;
                return;
            }
            Application.Current.Dispatcher.Invoke((Action) delegate
            {
                prog = new autoscanProgress();
                prog.Show();
            });


            int i       = 0;
            int portNum = 0;
            int total   = ports.Count() * 3;

            prog.scanPercentLabel.Dispatcher.BeginInvoke((Action)(() => prog.scanPercentLabel.Content = "Beginning Scan..."));
            prog.autoscanProgressBar.Dispatcher.BeginInvoke((Action)(() => prog.autoscanProgressBar.Value = 2));

            foreach (var portName in ports)
            {
                portNum++;
                using (var port = new System.IO.Ports.SerialPort(portName))
                {
                    var        defaultRate     = port.BaudRate;
                    List <int> baudRatesToTest = new List <int>(new[] { 9600, 4800, 19200 }); //Ordered by likelihood // removed: 115200, 57600, 38400, 2400
                                                                                              //Move default rate to first spot
                    if (baudRatesToTest.Contains(defaultRate))
                    {
                        baudRatesToTest.Remove(defaultRate);
                    }
                    baudRatesToTest.Insert(0, defaultRate);

                    foreach (var baud in baudRatesToTest)
                    {
                        i++;
                        double test    = (double)(i) / total;
                        int    percent = (int)(test * 100);
                        Trace.WriteLine("Percent complete: " + percent + "%");

                        Application.Current.Dispatcher.Invoke((Action) delegate
                        {
                            prog.autoscanProgressBar.Value = percent;
                            prog.scanPercentLabel.Content  = "Scanning Port " + portNum + " at Baud " + baud;
                        });

                        if (progress != null)
                        {
                            progress.Report(string.Format("Trying {0} @ {1}baud", portName, port.BaudRate));
                            worker.ReportProgress(percent);
                        }
                        port.BaudRate    = baud;
                        port.ReadTimeout = 2500; //this might not be long enough
                        bool success = false;
                        try
                        {
                            port.Open();
                            if (!port.IsOpen)
                            {
                                continue; //couldn't open port
                            }
                            try
                            {
                                port.ReadTo("$GP");
                                success = true;
                                Trace.WriteLine("GPS Found at: " + port.PortName + " baud " + port.BaudRate);
                            }
                            catch (TimeoutException)
                            {
                                continue;
                            }
                        }
                        catch
                        {
                            //Error reading
                        }
                        finally
                        {
                            port.Close();
                            //prog.Close();
                        }
                        if (success)
                        {
                            Application.Current.Dispatcher.Invoke((Action) delegate
                            {
                                prog.Close();
                            });
                            autoSerialPort = new System.IO.Ports.SerialPort(portName, baud);
                            return;
                        }
                    }
                }
            }
            //serialPort = new System.IO.Ports.SerialPort("COM6", 9600);
            //return serialPort;

            MessageBox.Show(
                "Error: Could not AutoDetect a GPS device.  Make sure the gps reciever is plugged in and installed.  If you believe your device is properly installed, it may not be on a scanned baud rate (9600, 4800, 19200).  " +
                "If this is the case, please go to settings and manually enter the information for your GPS reciever. \n" +
                "Contact your system administrator for help. \n"
                , "Error 1: No Serial Ports Found",
                MessageBoxButton.OK,
                MessageBoxImage.Error);
            autoSerialPort = null;
            Application.Current.Dispatcher.Invoke((Action) delegate
            {
                prog.Close();
            });
            return;
        }
Example #2
0
        private static System.IO.Ports.SerialPort FindPort(IProgress <string> progress = null)
        {
            var ports = System.IO.Ports.SerialPort.GetPortNames().OrderBy(s => s);

            if (ports.Count() <= 0)
            {
                MessageBox.Show(
                    "Error: No Serial Ports avaliable.  Make sure the gps reciever is plugged in and installed.  Contact your system administrator for help. \n"
                    , "Error 1: No Serial Ports Found",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error);
                _continue = false;
                return(null);
            }
            prog = new autoscanProgress();
            prog.Show();

            int i     = 0;
            int total = ports.Count() * 4;

            prog.scanPercentLabel.Dispatcher.BeginInvoke((Action)(() => prog.scanPercentLabel.Content = "Beginning Scan..."));
            prog.autoscanProgressBar.Dispatcher.BeginInvoke((Action)(() => prog.autoscanProgressBar.Value = 2));

            foreach (var portName in ports)
            {
                i++;
                int j = 0;
                using (var port = new System.IO.Ports.SerialPort(portName))
                {
                    j++;
                    var        defaultRate     = port.BaudRate;
                    List <int> baudRatesToTest = new List <int>(new[] { 9600, 4800, 19200 }); //Ordered by likelihood // removed: 115200, 57600, 38400, 2400
                                                                                              //Move default rate to first spot
                    if (baudRatesToTest.Contains(defaultRate))
                    {
                        baudRatesToTest.Remove(defaultRate);
                    }
                    baudRatesToTest.Insert(0, defaultRate);
                    foreach (var baud in baudRatesToTest)
                    {
                        //prog.autoscanProgressBar.Dispatcher.BeginInvoke((Action)(() => prog.autoscanProgressBar.Value = (double)(((i + j) / total) * 100)));
                        prog.SetProgress((int)(double)(((i + j) / total) * 100));
                        prog.scanPercentLabel.Dispatcher.BeginInvoke((Action)(() => prog.scanPercentLabel.Content = "Scanning Port " + i + " at Baud " + baud));

                        if (progress != null)
                        {
                            progress.Report(string.Format("Trying {0} @ {1}baud", portName, port.BaudRate));
                        }
                        port.BaudRate    = baud;
                        port.ReadTimeout = 2000; //this might not be long enough
                        bool success = false;
                        try
                        {
                            port.Open();
                            if (!port.IsOpen)
                            {
                                continue; //couldn't open port
                            }
                            try
                            {
                                port.ReadTo("$GP");
                                success = true;
                            }
                            catch (TimeoutException)
                            {
                                continue;
                            }
                        }
                        catch
                        {
                            //Error reading
                        }
                        finally
                        {
                            port.Close();
                            //prog.Close();
                        }
                        if (success)
                        {
                            prog.Close();
                            return(new System.IO.Ports.SerialPort(portName, baud));
                        }
                    }
                }
            }
            //serialPort = new System.IO.Ports.SerialPort("COM6", 9600);
            //return serialPort;

            MessageBox.Show(
                "Error: Could not AutoDetect a GPS device.  Make sure the gps reciever is plugged in and installed.  If you believe your device is properly installed, it may not be on a scanned baud rate (9600, 4800, 19200).  " +
                "If this is the case, please go to settings and manually enter the information for your GPS reciever. \n" +
                "Contact your system administrator for help. \n"
                , "Error 1: No Serial Ports Found",
                MessageBoxButton.OK,
                MessageBoxImage.Error);
            return(null);
        }