Ejemplo n.º 1
0
        public static bool Get(GrblViewModel model)
        {
            bool?res = null;
            CancellationToken cancellationToken = new CancellationToken();

            settings.Clear();
            Comms.com.PurgeQueue();

            model.Silent = true;

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

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

            model.Silent = false;

            if (IsGrblHAL && !Resources.ConfigName.StartsWith("hal_"))
            {
                Resources.ConfigName = "hal_" + Resources.ConfigName;
            }

            try
            {
                StreamReader file = new StreamReader(string.Format("{0}{1}", Resources.Path, Resources.ConfigName));

                if (file != null)
                {
                    string line = file.ReadLine();

                    line = file.ReadLine(); // Skip header

                    while (line != null)
                    {
                        string[] columns = line.Split('\t');

                        if (columns.Length >= 6)
                        {
                            DataRow[] rows = settings.Select("Id=" + columns[0]);
                            if (rows.Count() == 1)
                            {
                                rows[0]["Name"]        = columns[1];
                                rows[0]["Unit"]        = columns[2];
                                rows[0]["DataType"]    = columns[3];
                                rows[0]["DataFormat"]  = columns[4];
                                rows[0]["Description"] = columns[5];
                                if (columns.Length >= 7)
                                {
                                    rows[0]["Min"] = dbl.Parse(columns[6]);
                                }
                                if (columns.Length >= 8)
                                {
                                    rows[0]["Max"] = dbl.Parse(columns[7]);
                                }
                                if ((string)rows[0]["DataType"] == "float")
                                {
                                    rows[0]["Value"] = GrblSettings.FormatFloat((string)rows[0]["Value"], (string)rows[0]["DataFormat"]);
                                }
                            }
                        }
                        line = file.ReadLine();
                    }
                    file.Close();
                    file.Dispose();
                }
            }
            catch
            {
            }

            settings.AcceptChanges();

            return(Loaded);
        }
Ejemplo n.º 2
0
        public static bool Get()
#endif
        {
            settings.Clear();

            Comms.com.DataReceived += Process;
#if USE_ASYNC
            var task = Task.Run(() => Comms.com.AwaitAck(GrblConstants.CMD_GETSETTINGS));
            await await Task.WhenAny(task, Task.Delay(2500));
#else
            Comms.com.PurgeQueue();
            Comms.com.WriteCommand(GrblConstants.CMD_GETSETTINGS);
            Comms.com.AwaitAck();
#endif
            Comms.com.DataReceived -= Process;

            if (IsGrblHAL && !Resources.ConfigName.StartsWith("hal_"))
            {
                Resources.ConfigName = "hal_" + Resources.ConfigName;
            }

            try
            {
                StreamReader file = new StreamReader(string.Format("{0}\\{1}", Resources.Path, Resources.ConfigName));

                if (file != null)
                {
                    string line = file.ReadLine();

                    line = file.ReadLine(); // Skip header

                    while (line != null)
                    {
                        string[] columns = line.Split('\t');

                        if (columns.Length >= 6)
                        {
                            DataRow[] rows = settings.Select("Id=" + columns[0]);
                            if (rows.Count() == 1)
                            {
                                rows[0]["Name"]        = columns[1];
                                rows[0]["Unit"]        = columns[2];
                                rows[0]["DataType"]    = columns[3];
                                rows[0]["DataFormat"]  = columns[4];
                                rows[0]["Description"] = columns[5];
                                if (columns.Length >= 7)
                                {
                                    rows[0]["Min"] = dbl.Parse(columns[6]);
                                }
                                if (columns.Length >= 8)
                                {
                                    rows[0]["Max"] = dbl.Parse(columns[7]);
                                }
                                if ((string)rows[0]["DataType"] == "float")
                                {
                                    rows[0]["Value"] = GrblSettings.FormatFloat((string)rows[0]["Value"], (string)rows[0]["DataFormat"]);
                                }
                            }
                        }
                        line = file.ReadLine();
                    }
                    file.Close();
                    file.Dispose();
                }
            }
            catch
            {
            }

            settings.AcceptChanges();

            return(Loaded);
        }