Beispiel #1
0
        private void CreateVarPanel(ModelInfo mi, string varName, uint addr, int def)
        {
            var panel = new EEPROMMapVariablePanel();

            panel.SetData(mi.Name, varName, (ushort)addr, (short)def);
            panel.Value = null;

            __Panels.Add(panel);

            tblVariables.Controls.Add(panel);
            panel.Dock = DockStyle.Top;
        }
Beispiel #2
0
        private void WriteVariables(IEnumerable <EEPROMMapVariablePanel> panels, Func <EEPROMMapVariablePanel, short> conv, Action onComplete)
        {
            bool   errorSet     = false;
            string errorMessage = "";

            var waiter = new AutoResetEvent(false);

            var PortDataCB = new PortCommunicator.OnDataRecievedEvent((command, data) =>
            {
                if (command != 7)
                {
                    return;
                }

                if (data.Length != 2)
                {
                    errorSet     = true;
                    errorMessage = "Wrong packet structure";
                    waiter.Set();
                    return;
                }

                //                var w1 = (ushort)((data[0] << 8) | data[1]);
                var w2 = data[1];

                if (w2 != 0)
                {
                    errorSet     = true;
                    errorMessage = "EEPRom write failure";
                }

                waiter.Set();
            });

            fmMain.__Port.OnDataRecieved += PortDataCB;

            try
            {
                var readQueue = new Queue <Tuple <EEPROMMapVariablePanel, int> >(
                    panels.Select(pnl => new Tuple <EEPROMMapVariablePanel, int>(pnl, 0)));

                while (readQueue.Count > 0)
                {
                    var tpl = readQueue.Dequeue();
                    EEPROMMapVariablePanel currentReadPanel = tpl.Item1;

                    var b1 = (byte)((currentReadPanel.Addr >> 8) & 0xFF);
                    var b2 = (byte)(currentReadPanel.Addr & 0xFF);

                    var val = conv(currentReadPanel);

                    var b3 = (byte)((val >> 8) & 0xFF);
                    var b4 = (byte)(val & 0xFF);

                    fmMain.__Port.SendCommand(0x7, b1, b2, b3, b4);

                    if (waiter.WaitOne(TIMEOUT) && !errorSet)
                    {
                        Invoke(new Action(() =>
                        {
                            sbProgress.Value++;
                        }));

                        continue;
                    }

                    if (errorSet)
                    {
                        break;
                    }

                    if (tpl.Item2 < 3)
                    {
                        readQueue.Enqueue(new Tuple <EEPROMMapVariablePanel, int>(currentReadPanel, tpl.Item2 + 1));
                        continue;
                    }

                    errorSet     = true;
                    errorMessage = "Request timeout";
                    break;
                }
            }
            finally
            {
                fmMain.__Port.OnDataRecieved -= PortDataCB;
//                waiter.Dispose();
                Invoke(onComplete);
            }

            if (errorSet)
            {
                Invoke(new Action(() => MessageBox.Show(this, errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)));
            }
        }