public int SetupAndOpen(string appname, System.Windows.Threading.Dispatcher dispatcher)
        {
            int    status     = 0;
            bool   selectPort = false;
            string port       = string.Empty;

            CNC.Core.Resources.Path = AppDomain.CurrentDomain.BaseDirectory;

            string[] args = Environment.GetCommandLineArgs();

            int p = 0;

            while (p < args.GetLength(0))
            {
                switch (args[p++])
                {
                case "-inifile":
                    CNC.Core.Resources.IniName = GetArg(args, p++);
                    break;

                case "-configmapping":
                    CNC.Core.Resources.ConfigName = GetArg(args, p++);
                    break;

                case "-language":
                    CNC.Core.Resources.Language = GetArg(args, p++);
                    break;

                case "-port":
                    port = GetArg(args, p++);
                    break;

                case "-selectport":
                    selectPort = true;
                    break;
                }
            }

            if (!Load(CNC.Core.Resources.IniFile))
            {
                if (MessageBox.Show("Config file not found or invalid, create new?", appname, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    if (!Save(CNC.Core.Resources.IniFile))
                    {
                        MessageBox.Show("Could not save config file.", appname);
                        status = 1;
                    }
                }
                else
                {
                    status = 1;
                }
            }

            if (!string.IsNullOrEmpty(port))
            {
                selectPort = false;
            }

            if (!selectPort)
            {
                if (!string.IsNullOrEmpty(port))
                {
                    setPort(port);
                }
#if USEWEBSOCKET
                if (Config.PortParams.ToLower().StartsWith("ws://"))
                {
                    new WebsocketStream(Config.PortParams, dispatcher);
                }
                else
#endif
                if (char.IsDigit(Config.PortParams[0])) // We have an IP address
                {
                    new TelnetStream(Config.PortParams, dispatcher);
                }
                else
                {
                    new SerialStream(Config.PortParams, Comms.ResetMode.None, dispatcher);
                }
            }

            if ((Comms.com == null || !Comms.com.IsOpen) && string.IsNullOrEmpty(port))
            {
                PortDialog portsel = new PortDialog();

                port = portsel.ShowDialog(Config.PortParams);
                if (string.IsNullOrEmpty(port))
                {
                    status = 2;
                }

                else
                {
                    setPort(port);
#if USEWEBSOCKET
                    if (port.ToLower().StartsWith("ws://"))
                    {
                        new WebsocketStream(Config.PortParams, dispatcher);
                    }
                    else
#endif
                    if (char.IsDigit(port[0])) // We have an IP address
                    {
                        new TelnetStream(Config.PortParams, dispatcher);
                    }
                    else
                    {
                        new SerialStream(Config.PortParams, Comms.ResetMode.None, dispatcher);
                    }

                    Save(CNC.Core.Resources.IniFile);
                }
            }

            if (Comms.com != null && Comms.com.IsOpen)
            {
                int delay = 40; // 400 ms

                // Wait to see if a MPG is polling Grbl...
                while (delay-- != 0 && Comms.com.Reply == "")
                {
                    System.Threading.Thread.Sleep(10);
                }

                // ...if so show dialog for wait for it to stop polling and relinquish control.
                if (!(Comms.com.Reply == "" || Comms.com.Reply.StartsWith("Grbl") || Comms.com.Reply.StartsWith("[MSG:")))
                {
                    MPGPending await = new MPGPending();
                    await.ShowDialog();
                    if (await.Cancelled)
                    {
                        Comms.com.Close();
                        status = 2;
                    }
                }
            }
            else if (status != 2)
            {
                MessageBox.Show(string.Format("Unable to open connection ({0})", Config.PortParams), appname, MessageBoxButton.OK, MessageBoxImage.Error);
                status = 2;
            }

            return(status);
        }
Exemple #2
0
        public int SetupAndOpen(string appname, GrblViewModel model, System.Windows.Threading.Dispatcher dispatcher)
        {
            int    status     = 0;
            bool   selectPort = false;
            string port       = string.Empty;

            CNC.Core.Resources.Path = AppDomain.CurrentDomain.BaseDirectory;

            string[] args = Environment.GetCommandLineArgs();

            int p = 0;

            while (p < args.GetLength(0))
            {
                switch (args[p++])
                {
                case "-inifile":
                    CNC.Core.Resources.IniName = GetArg(args, p++);
                    break;

                case "-configmapping":
                    CNC.Core.Resources.ConfigName = GetArg(args, p++);
                    break;

                case "-language":
                    CNC.Core.Resources.Language = GetArg(args, p++);
                    break;

                case "-port":
                    port = GetArg(args, p++);
                    break;

                case "-selectport":
                    selectPort = true;
                    break;

                default:
                    if (!args[p - 1].EndsWith(".exe") && File.Exists(args[p - 1]))
                    {
                        FileName = args[p - 1];
                    }
                    break;
                }
            }

            if (!Load(CNC.Core.Resources.IniFile))
            {
                if (MessageBox.Show("Config file not found or invalid, create new?", appname, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    if (!Save(CNC.Core.Resources.IniFile))
                    {
                        MessageBox.Show("Could not save config file.", appname);
                        status = 1;
                    }
                }
                else
                {
                    return(1);
                }
            }

            if (!string.IsNullOrEmpty(port))
            {
                selectPort = false;
            }

            if (!selectPort)
            {
                if (!string.IsNullOrEmpty(port))
                {
                    setPort(port);
                }
#if USEWEBSOCKET
                if (Config.PortParams.ToLower().StartsWith("ws://"))
                {
                    new WebsocketStream(Config.PortParams, dispatcher);
                }
                else
#endif
                if (char.IsDigit(Config.PortParams[0])) // We have an IP address
                {
                    new TelnetStream(Config.PortParams, dispatcher);
                }
                else
#if USEELTIMA
                { new EltimaStream(Config.PortParams, Config.ResetDelay, dispatcher); }
#else
                { new SerialStream(Config.PortParams, Config.ResetDelay, dispatcher); }
#endif
            }

            if ((Comms.com == null || !Comms.com.IsOpen) && string.IsNullOrEmpty(port))
            {
                PortDialog portsel = new PortDialog();

                port = portsel.ShowDialog(Config.PortParams);
                if (string.IsNullOrEmpty(port))
                {
                    status = 2;
                }

                else
                {
                    setPort(port);
#if USEWEBSOCKET
                    if (port.ToLower().StartsWith("ws://"))
                    {
                        new WebsocketStream(Config.PortParams, dispatcher);
                    }
                    else
#endif
                    if (char.IsDigit(port[0])) // We have an IP address
                    {
                        new TelnetStream(Config.PortParams, dispatcher);
                    }
                    else
#if USEELTIMA
                    { new EltimaStream(Config.PortParams, Config.ResetDelay, dispatcher); }
#else
                    { new SerialStream(Config.PortParams, Config.ResetDelay, dispatcher); }
#endif
                    Save(CNC.Core.Resources.IniFile);
                }
            }

            if (Comms.com != null && Comms.com.IsOpen)
            {
                Comms.com.DataReceived += model.DataReceived;

                CancellationToken cancellationToken = new CancellationToken();

                // Wait 400ms to see if a MPG is polling Grbl...

                new Thread(() =>
                {
                    MPGactive = WaitFor.SingleEvent <string>(
                        cancellationToken,
                        null,
                        a => model.OnRealtimeStatusProcessed += a,
                        a => model.OnRealtimeStatusProcessed -= a,
                        500);
                }).Start();

                while (MPGactive == null)
                {
                    EventUtils.DoEvents();
                }

                // ...if so show dialog for wait for it to stop polling and relinquish control.
                if (MPGactive == true)
                {
                    MPGPending await = new MPGPending(model);
                    await.ShowDialog();
                    if (await.Cancelled)
                    {
                        Comms.com.Close(); //!!
                        status = 2;
                    }
                }

                model.IsReady      = true;
                model.PollInterval = Config.PollInterval;
            }
            else if (status != 2)
            {
                MessageBox.Show(string.Format("Unable to open connection ({0})", Config.PortParams), appname, MessageBoxButton.OK, MessageBoxImage.Error);
                status = 2;
            }

            return(status);
        }
Exemple #3
0
        public int SetupAndOpen(string appname, GrblViewModel model, System.Windows.Threading.Dispatcher dispatcher)
        {
            int    status     = 0;
            bool   selectPort = false;
            int    jogMode    = -1;
            string port       = string.Empty;

            CNC.Core.Resources.Path = AppDomain.CurrentDomain.BaseDirectory;

            string[] args = Environment.GetCommandLineArgs();

            int p = 0;

            while (p < args.GetLength(0))
            {
                switch (args[p++].ToLowerInvariant())
                {
                case "-inifile":
                    CNC.Core.Resources.IniName = GetArg(args, p++);
                    break;

                case "-debugfile":
                    CNC.Core.Resources.DebugFile = GetArg(args, p++);
                    break;

                case "-configmapping":
                    CNC.Core.Resources.ConfigName = GetArg(args, p++);
                    break;

                case "-locale":
                case "-language":     // deprecated
                    CNC.Core.Resources.Locale = GetArg(args, p++);
                    break;

                case "-port":
                    port = GetArg(args, p++);
                    break;

                case "-selectport":
                    selectPort = true;
                    break;

                case "-islegacy":
                    CNC.Core.Resources.IsLegacyController = true;
                    break;

                case "-jogmode":
                    if (int.TryParse(GetArg(args, p++), out jogMode))
                    {
                        jogMode = Math.Min(Math.Max(jogMode, 0), (int)JogConfig.JogMode.KeypadAndUI);
                    }
                    break;

                default:
                    if (!args[p - 1].EndsWith(".exe") && File.Exists(args[p - 1]))
                    {
                        FileName = args[p - 1];
                    }
                    break;
                }
            }

            if (!Load(CNC.Core.Resources.IniFile))
            {
                if (MessageBox.Show(LibStrings.FindResource("CreateConfig"), appname, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    if (!Save(CNC.Core.Resources.IniFile))
                    {
                        MessageBox.Show(LibStrings.FindResource("CreateConfigFail"), appname);
                        status = 1;
                    }
                }
                else
                {
                    return(1);
                }
            }

            if (jogMode != -1)
            {
                Base.Jog.Mode = (JogConfig.JogMode)jogMode;
            }

            if (!string.IsNullOrEmpty(port))
            {
                selectPort = false;
            }

            if (!selectPort)
            {
                if (!string.IsNullOrEmpty(port))
                {
                    setPort(port);
                }
#if USEWEBSOCKET
                if (Base.PortParams.ToLower().StartsWith("ws://"))
                {
                    new WebsocketStream(Base.PortParams, dispatcher);
                }
                else
#endif
                if (char.IsDigit(Base.PortParams[0])) // We have an IP address
                {
                    new TelnetStream(Base.PortParams, dispatcher);
                }
                else
#if USEELTIMA
                { new EltimaStream(Config.PortParams, Config.ResetDelay, dispatcher); }
#else
                { new SerialStream(Base.PortParams, Base.ResetDelay, dispatcher); }
#endif
            }

            if ((Comms.com == null || !Comms.com.IsOpen) && string.IsNullOrEmpty(port))
            {
                PortDialog portsel = new PortDialog();

                port = portsel.ShowDialog(Base.PortParams);
                if (string.IsNullOrEmpty(port))
                {
                    status = 2;
                }

                else
                {
                    setPort(port);
#if USEWEBSOCKET
                    if (port.ToLower().StartsWith("ws://"))
                    {
                        new WebsocketStream(Base.PortParams, dispatcher);
                    }
                    else
#endif
                    if (char.IsDigit(port[0])) // We have an IP address
                    {
                        new TelnetStream(Base.PortParams, dispatcher);
                    }
                    else
#if USEELTIMA
                    { new EltimaStream(Config.PortParams, Config.ResetDelay, dispatcher); }
#else
                    { new SerialStream(Base.PortParams, Base.ResetDelay, dispatcher); }
#endif
                    Save(CNC.Core.Resources.IniFile);
                }
            }

            if (Comms.com != null && Comms.com.IsOpen)
            {
                Comms.com.DataReceived += model.DataReceived;

                CancellationToken cancellationToken = new CancellationToken();

                // Wait 400ms to see if a MPG is polling Grbl...

                new Thread(() =>
                {
                    MPGactive = WaitFor.SingleEvent <string>(
                        cancellationToken,
                        null,
                        a => model.OnRealtimeStatusProcessed += a,
                        a => model.OnRealtimeStatusProcessed -= a,
                        500);
                }).Start();

                while (MPGactive == null)
                {
                    EventUtils.DoEvents();
                }

                // ...if so show dialog for wait for it to stop polling and relinquish control.
                if (MPGactive == true)
                {
                    MPGPending await = new MPGPending(model);
                    await.ShowDialog();
                    if (await.Cancelled)
                    {
                        Comms.com.Close(); //!!
                        status = 2;
                    }
                }

                model.IsReady = true;
            }
            else if (status != 2)
            {
                MessageBox.Show(string.Format(LibStrings.FindResource("ConnectFailed"), Base.PortParams), appname, MessageBoxButton.OK, MessageBoxImage.Error);
                status = 2;
            }

            return(status);
        }