예제 #1
0
        void btnCurrPos_Click(object sender, RoutedEventArgs e)
        {
            bool?res = null;
            CancellationToken cancellationToken = new CancellationToken();

            awaitCoord = true;

            parameters.OnRealtimeStatusProcessed += DataReceived;

            new Thread(() =>
            {
                res = WaitFor.AckResponse <string>(
                    cancellationToken,
                    null,
                    a => GotPosition += a,
                    a => GotPosition -= a,
                    1000, () => RequestStatus());
            }).Start();

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

            parameters.OnRealtimeStatusProcessed -= DataReceived;
        }
예제 #2
0
        public bool WaitForResponse(string command)
        {
            bool?res = null;

            if (Grbl.ResponseLogVerbose)
            {
                Grbl.ResponseLog.Add(command);
            }

            new Thread(() =>
            {
                res = WaitFor.AckResponse <string>(
                    cancellationToken,
                    null,
                    a => Grbl.OnResponseReceived += a,
                    a => Grbl.OnResponseReceived -= a,
                    5000, () => Grbl.ExecuteCommand(command));
            }).Start();

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

            return(res == true);
        }
예제 #3
0
        private void GetDriverStatus(string axis)
        {
            bool?res = null;
            CancellationToken cancellationToken = new CancellationToken();
            var model = DataContext as GrblViewModel;

            DriverStatus = "";

            Comms.com.PurgeQueue();

            model.Poller.SetState(0);
            model.SuspendProcessing = true;

            new Thread(() =>
            {
                res = WaitFor.AckResponse <string>(
                    cancellationToken,
                    response => ProcessStatus(response),
                    a => model.OnResponseReceived += a,
                    a => model.OnResponseReceived -= a,
                    800, () => Comms.com.WriteCommand("M122" + axis));
            }).Start();

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

            model.SuspendProcessing = false;
            model.Poller.SetState(AppConfig.Settings.Base.PollInterval);
        }
예제 #4
0
        public bool Load(GrblViewModel model, string cmd)
        {
            bool?res = null;
            CancellationToken cancellationToken = new CancellationToken();

            Comms.com.PurgeQueue();

            model.Silent = true;

            new Thread(() =>
            {
                res = WaitFor.AckResponse <string>(
                    cancellationToken,
                    response => ProcessDetail(response),
                    a => model.OnResponseReceived += a,
                    a => model.OnResponseReceived -= a,
                    400, () => Comms.com.WriteCommand(cmd));
            }).Start();

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

            return(settings.Count > 0);
        }
예제 #5
0
        public static void Load(GrblViewModel model)
        {
            bool?res = null;
            CancellationToken cancellationToken = new CancellationToken();

            data.Clear();

            if (mounted == null)
            {
                Comms.com.PurgeQueue();

                new Thread(() =>
                {
                    mounted = WaitFor.AckResponse <string>(
                        cancellationToken,
                        null,
                        a => model.OnResponseReceived += a,
                        a => model.OnResponseReceived -= a,
                        500, () => Comms.com.WriteCommand(GrblConstants.CMD_SDCARD_MOUNT));
                }).Start();

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

            if (mounted == true)
            {
                Comms.com.PurgeQueue();

                id           = 0;
                model.Silent = true;

                new Thread(() =>
                {
                    res = WaitFor.AckResponse <string>(
                        cancellationToken,
                        response => Process(response),
                        a => model.OnResponseReceived += a,
                        a => model.OnResponseReceived -= a,
                        2000, () => Comms.com.WriteCommand(GrblConstants.CMD_SDCARD_DIR));
                }).Start();

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

                model.Silent = false;

                data.AcceptChanges();
            }
        }
예제 #6
0
        private void DownloadRun_Click(object sender, RoutedEventArgs e)
        {
            if (currentFile != null && MessageBox.Show(string.Format((string)FindResource("DownloandRun"), (string)currentFile["Name"]), "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes) == MessageBoxResult.Yes)
            {
                var model = DataContext as GrblViewModel;

                using (new UIUtils.WaitCursor())
                {
                    bool?res = null;
                    CancellationToken cancellationToken = new CancellationToken();

                    Comms.com.PurgeQueue();

                    model.SuspendProcessing = true;
                    model.Message           = string.Format((string)FindResource("Downloading"), (string)currentFile["Name"]);

                    GCode.File.AddBlock((string)currentFile["Name"], CNC.Core.Action.New);

                    new Thread(() =>
                    {
                        res = WaitFor.AckResponse <string>(
                            cancellationToken,
                            response => AddBlock(response),
                            a => model.OnResponseReceived += a,
                            a => model.OnResponseReceived -= a,
                            400, () => Comms.com.WriteCommand(GrblConstants.CMD_SDCARD_DUMP + (string)currentFile["Name"]));
                    }).Start();

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

                    model.SuspendProcessing = false;

                    GCode.File.AddBlock(string.Empty, CNC.Core.Action.End);
                }

                model.Message = string.Empty;

                if (Rewind)
                {
                    Comms.com.WriteCommand(GrblConstants.CMD_SDCARD_REWIND);
                }

                FileSelected?.Invoke("SDCard:" + (string)currentFile["Name"], Rewind);
                Comms.com.WriteCommand(GrblConstants.CMD_SDCARD_RUN + (string)currentFile["Name"]);

                Rewind = false;
            }
        }
예제 #7
0
        private bool SetSetting(KeyValuePair <int, string> setting)
        {
            bool?res = null;
            CancellationToken cancellationToken = new CancellationToken();
            var scmd = string.Format("${0}={1}", setting.Key, setting.Value);

            retval = string.Empty;

            new Thread(() =>
            {
                res = WaitFor.AckResponse <string>(
                    cancellationToken,
                    response => Process(response),
                    a => model.OnResponseReceived += a,
                    a => model.OnResponseReceived -= a,
                    400, () => Comms.com.WriteCommand(scmd));
            }).Start();

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

            if (retval != string.Empty)
            {
                if (retval.StartsWith("error:"))
                {
                    var msg = GrblErrors.GetMessage(retval.Substring(6));
                    if (msg != retval)
                    {
                        retval += " - \"" + msg + "\"";
                    }
                }

                var details = GrblSettings.Get((GrblSetting)setting.Key);

                if (MessageBox.Show(string.Format((string)FindResource("SettingsError"), scmd, retval), "ioSender" + (details == null ? "" : " - " + details.Name), MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.No)
                {
                    return(false);
                }
            }
            else if (res == false && MessageBox.Show(string.Format((string)FindResource("SettingsTimeout"), scmd), "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.No)
            {
                return(false);
            }

            return(true);
        }
예제 #8
0
        public bool Init()
        {
            bool?res = null;

            probing.Message = String.Empty;

            Grbl.Poller.SetState(0);  // Disable status polling during probing

            // Clear error status if set
            if (Grbl.GrblError != 0)
            {
                new Thread(() =>
                {
                    res = WaitFor.AckResponse <string>(
                        cancellationToken,
                        null,
                        a => Grbl.OnResponseReceived += a,
                        a => Grbl.OnResponseReceived -= a,
                        1000, () => Grbl.ExecuteCommand(""));
                }).Start();

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

                res = null;
            }

            // Get a status report in order to establish current machine position
            new Thread(() =>
            {
                res = WaitFor.SingleEvent <string>(
                    cancellationToken,
                    null,
                    a => Grbl.OnResponseReceived += a,
                    a => Grbl.OnResponseReceived -= a,
                    1000, () => Comms.com.WriteByte(GrblLegacy.ConvertRTCommand(GrblConstants.CMD_STATUS_REPORT)));
            }).Start();

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

            Grbl.Poller.SetState(AppConfig.Settings.Base.PollInterval);

            if (Grbl.GrblState.State == GrblStates.Alarm)
            {
                probing.Message = GrblAlarms.GetMessage(Grbl.GrblState.Substate.ToString());
                res             = false;
            }

            if (res == true && Grbl.Signals.Value.HasFlag(Signals.ProbeDisconnected))
            {
                probing.Message = "Probing failed, probe is not connected";
                res             = false;
            }

            if (res == true && Grbl.Signals.Value.HasFlag(Signals.Probe))
            {
                probing.Message = "Probing failed, probe signal is asserted";
                res             = false;
            }

            if (res == true && !(Grbl.GrblState.State == GrblStates.Idle || Grbl.GrblState.State == GrblStates.Tool))
            {
                probing.Message = "Probing failed, Grbl is not in idle or tool changing state";
                res             = false;
            }

            if (res == true && !Grbl.IsMachinePositionKnown)
            {
                probing.Message = "Probing failed, could not establish current machine position";
                res             = false;
            }

            _program.Clear();

            if (res != true) // Reenable status polling if init fails
            {
                Grbl.Poller.SetState(AppConfig.Settings.Base.PollInterval);
            }

            return(res == true);
        }
예제 #9
0
        public bool GotoMachinePosition(Position pos, AxisFlags axisflags)
        {
            bool?res  = null;
            bool wait = true;

            string command = "G53G0" + pos.ToString(axisflags);

            Comms.com.PurgeQueue();

            new Thread(() =>
            {
                res = WaitFor.AckResponse <string>(
                    cancellationToken,
                    null,
                    a => Grbl.OnResponseReceived += a,
                    a => Grbl.OnResponseReceived -= a,
                    100, () => Grbl.ExecuteCommand(command));
            }).Start();

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

            if (res == true)
            {
                while (wait && !isCancelled)
                {
                    res = null;

                    new Thread(() =>
                    {
                        res = WaitFor.SingleEvent <string>(
                            cancellationToken,
                            null,
                            a => Grbl.OnRealtimeStatusProcessed += a,
                            a => Grbl.OnRealtimeStatusProcessed -= a,
                            200, () => Comms.com.WriteByte(GrblLegacy.ConvertRTCommand(GrblConstants.CMD_STATUS_REPORT)));
                    }).Start();

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

                    wait = res != true;

                    int i = 0, axes = (int)axisflags;
                    while (axes != 0 && !wait)
                    {
                        if ((axes & 0x01) != 0)
                        {
                            wait = Math.Abs(pos.Values[i] - Grbl.MachinePosition.Values[i]) >= 0.003d; // use step resolution plus some?
                        }
                        i++; axes >>= 1;
                    }

                    if (wait)
                    {
                        Thread.Sleep(200); // needed?
                    }
                }
            }

            return(isCancelled ? false : !wait);
        }
예제 #10
0
        public bool Init(bool check_probe = true)
        {
            bool?res = null;

            IsCancelled     = false;
            probing.Message = string.Empty;

            Grbl.Poller.SetState(0);  // Disable status polling during initialization

            // Clear error status if set
            if (Grbl.GrblError != 0)
            {
                new Thread(() =>
                {
                    res = WaitFor.AckResponse <string>(
                        cancellationToken,
                        null,
                        a => Grbl.OnResponseReceived += a,
                        a => Grbl.OnResponseReceived -= a,
                        1000, () => Grbl.ExecuteCommand(""));
                }).Start();

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

                res = null;
            }

            // Get a status report in order to establish current machine position
            new Thread(() =>
            {
                res = WaitFor.SingleEvent <string>(
                    cancellationToken,
                    null,
                    a => Grbl.OnResponseReceived += a,
                    a => Grbl.OnResponseReceived -= a,
                    AppConfig.Settings.Base.PollInterval * 5, () => Comms.com.WriteByte(GrblInfo.IsGrblHAL ? GrblConstants.CMD_STATUS_REPORT_ALL : GrblLegacy.ConvertRTCommand(GrblConstants.CMD_STATUS_REPORT)));
            }).Start();

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

            Grbl.Poller.SetState(AppConfig.Settings.Base.PollInterval);

            if (Grbl.GrblState.State == GrblStates.Alarm)
            {
                probing.Message = GrblAlarms.GetMessage(Grbl.GrblState.Substate.ToString());
                res             = false;
            }

            if (res == true && check_probe)
            {
                res = IsProbeReady(false);
            }

            if (res == true && !(Grbl.GrblState.State == GrblStates.Idle || Grbl.GrblState.State == GrblStates.Tool))
            {
                probing.Message = LibStrings.FindResource("FailedNotIdle");
                res             = false;
            }

            if (res == true && !Grbl.IsMachinePositionKnown)
            {
                probing.Message = LibStrings.FindResource("FailedNoPos");
                res             = false;
            }

            probing.StartPosition.Set(probing.Grbl.MachinePosition);

            hasPause = probeOnCycleStart = false;
            _program.Clear();

            return(res == true);
        }
예제 #11
0
        public bool LoadFile(string filename)
        {
            int                      pos, id;
            List <string>            lines    = new List <string>();
            List <int>               dep      = new List <int>();
            Dictionary <int, string> settings = new Dictionary <int, string>();
            FileInfo                 file     = new FileInfo(filename);
            StreamReader             sr       = file.OpenText();

            string block = sr.ReadLine();

            while (block != null)
            {
                block = block.Trim();
                try
                {
                    if (lines.Count == 0 && model.IsGrblHAL && block == "%")
                    {
                        lines.Add(block);
                    }
                    else if (block.StartsWith("$") && (pos = block.IndexOf('=')) > 1)
                    {
                        if (int.TryParse(block.Substring(1, pos - 1), out id))
                        {
                            settings.Add(id, block.Substring(pos + 1));
                        }
                        else
                        {
                            lines.Add(block);
                        }
                    }

                    block = sr.ReadLine();
                }
                catch (Exception e)
                {
                    if (MessageBox.Show(((string)FindResource("SettingsFail")).Replace("\\n", "\r\r"), e.Message, MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                    {
                        block = sr.ReadLine();
                    }
                    else
                    {
                        block = null;
                        settings.Clear();
                        lines.Clear();
                    }
                }
            }

            sr.Close();

            if (settings.Count == 0)
            {
                MessageBox.Show((string)FindResource("SettingsInvalid"));
            }
            else
            {
                bool?res = null;
                CancellationToken cancellationToken = new CancellationToken();

                // List of settings that have other dependent settings and have to be set before them
                dep.Add((int)GrblSetting.HomingEnable);

                foreach (var cmd in lines)
                {
                    res    = null;
                    retval = string.Empty;

                    new Thread(() =>
                    {
                        res = WaitFor.AckResponse <string>(
                            cancellationToken,
                            response => Process(response),
                            a => model.OnResponseReceived += a,
                            a => model.OnResponseReceived -= a,
                            400, () => Comms.com.WriteCommand(cmd));
                    }).Start();

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

                    if (retval != string.Empty)
                    {
                        if (MessageBox.Show(string.Format((string)FindResource("SettingsError"), cmd, retval), "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.No)
                        {
                            break;
                        }
                    }
                    else if (res == false && MessageBox.Show(string.Format((string)FindResource("SettingsTimeout"), cmd), "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.No)
                    {
                        break;
                    }
                }

                foreach (var d in dep)
                {
                    if (settings.ContainsKey(d))
                    {
                        if (!SetSetting(new KeyValuePair <int, string>(d, settings[d])))
                        {
                            settings.Clear();
                            break;
                        }
                    }
                }

                foreach (var setting in settings)
                {
                    if (!dep.Contains(setting.Key))
                    {
                        if (!SetSetting(setting))
                        {
                            break;
                        }
                    }
                }

                if (lines[0] == "%")
                {
                    Comms.com.WriteCommand("%");
                }

                using (new UIUtils.WaitCursor())
                {
                    GrblSettings.Load();
                }
            }

            model.Message = string.Empty;

            return(settings.Count > 0);
        }
예제 #12
0
        public bool GotoMachinePosition(Position pos, AxisFlags axisflags)
        {
            bool?  res = null;
            bool   wait = true, running = false;
            double delta, delta_max = 0d;

            string command = "G53" + RapidCommand + pos.ToString(axisflags);

            Comms.com.PurgeQueue();

            Grbl.Poller.SetState(0);

            new Thread(() =>
            {
                res = WaitFor.AckResponse <string>(
                    cancellationToken,
                    null,
                    a => Grbl.OnResponseReceived += a,
                    a => Grbl.OnResponseReceived -= a,
                    1000, () => Grbl.ExecuteCommand(command));
            }).Start();

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

            if (res == true)
            {
                while (wait && !isCancelled)
                {
                    res = null;

                    new Thread(() =>
                    {
                        res = WaitFor.SingleEvent <string>(
                            cancellationToken,
                            null,
                            a => Grbl.OnRealtimeStatusProcessed += a,
                            a => Grbl.OnRealtimeStatusProcessed -= a,
                            400, () => Comms.com.WriteByte(GrblLegacy.ConvertRTCommand(GrblConstants.CMD_STATUS_REPORT)));
                    }).Start();

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

                    wait     = res != true;
                    running |= Grbl.GrblState.State == GrblStates.Run;

                    int i = 0, axes = (int)axisflags;
                    while (axes != 0 && !wait)
                    {
                        if ((axes & 0x01) != 0)
                        {
                            delta     = Math.Abs(pos.Values[i] - Grbl.MachinePosition.Values[i]);
                            wait      = delta > Math.Max(0.003d, GrblInfo.TravelResolution.Values[i] * 2d);
                            delta_max = Math.Max(delta, delta_max);
                            if (wait && Grbl.GrblState.State == GrblStates.Idle && (running || delta_max < 0.01d))
                            {
                                wait        = false;
                                isCancelled = true;
                            }
                        }
                        i++; axes >>= 1;
                    }

                    if (wait)
                    {
                        Thread.Sleep(AppConfig.Settings.Base.PollInterval); // needed?
                    }
                }
            }

            Grbl.Poller.SetState(AppConfig.Settings.Base.PollInterval);

            return(isCancelled ? false : !wait);
        }
        public bool LoadFile(string filename)
        {
            bool          ok    = false;
            List <string> lines = new List <string>();

            FileInfo file = new FileInfo(filename);

            StreamReader sr = file.OpenText();

            string block = sr.ReadLine();

            while (block != null)
            {
                try
                {
                    ok |= block.StartsWith("$");
                    lines.Add(block.Trim());

                    block = sr.ReadLine();
                }
                catch (Exception e)
                {
                    if ((ok = MessageBox.Show("Bummer...\r\rContinue loading?", e.Message, MessageBoxButton.YesNo) == MessageBoxResult.Yes))
                    {
                        block = sr.ReadLine();
                    }
                    else
                    {
                        block = null;
                    }
                }
            }

            sr.Close();

            if (ok)
            {
                bool?res = null;
                CancellationToken cancellationToken = new CancellationToken();

                Comms.com.PurgeQueue();

                foreach (var cmd in lines)
                {
                    if (cmd.StartsWith(";"))
                    {
                        continue;
                    }

                    res    = null;
                    retval = string.Empty;

                    new Thread(() =>
                    {
                        res = WaitFor.AckResponse <string>(
                            cancellationToken,
                            response => Process(response),
                            a => model.OnResponseReceived += a,
                            a => model.OnResponseReceived -= a,
                            400, () => Comms.com.WriteCommand(cmd));
                    }).Start();

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

                    if (retval != string.Empty)
                    {
                        if (MessageBox.Show(string.Format("Setting {0} returned {1}, continue?", cmd, retval), "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.No)
                        {
                            break;
                        }
                    }
                    else if (res == false && MessageBox.Show(string.Format("Timed out while setting {0} , continue?", cmd), "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.No)
                    {
                        break;
                    }
                }

                using (new UIUtils.WaitCursor())
                {
                    GrblSettings.Get();
                }
            }
            else
            {
                MessageBox.Show("The file does not contain any settings.", "ioSender");
            }

            model.Message = string.Empty;

            return(ok);
        }