Ejemplo n.º 1
0
 private void SaveDA()
 {
     //sleep 500 ms, then reset configuration
     Thread.Sleep(600);
     DeviceMgr.Action("daoutput", new byte[] { 0x55, 0x64, 0xfc, 0x00, 0x00, 0x00, 0x04 }); //set control
     Thread.Sleep(100);
 }
Ejemplo n.º 2
0
        internal void pc_cmd(string cmd)
        {
            Logger.SysLog(cmd);
            if (cmd == "resi?")
            {
                if (processor.bOn)
                {
                    DeviceMgr.Report("resi: " + processor.resistance.ToString() + " on");
                }
                else
                {
                    DeviceMgr.Report("resi: " + processor.resistance.ToString() + " off");
                };
                return;
            }
            if (cmd == "current?")
            {
                DeviceMgr.Report("curr: " + led_current.Value);
                return;
            }
            if (cmd == "ZERO")
            {
                Program.mainwnd.processor.ZeroON();
            }
            if (cmd == "H")
            {
                DeviceMgr.Reset();
            }
            Match m;

            m = resi_set_mode.Match(cmd);
            if (m.Success)
            {
                string rvalue   = m.Groups[1].ToString();
                int    newrange = Int32.Parse(m.Groups[3].ToString());
                if (newrange < processor.RangeMin || newrange > processor.RangeMax)
                {
                    return;
                }

                Decimal a;
                if (!Util.TryDecimalParse(m.Groups[1].ToString(), out a))
                {
                    return;
                }
                processor.iRange     = newrange;
                processor.resistance = a;

                processor.bOn = (m.Groups[2].ToString() == "on");
            }
            RefreshDisplay(true);
        }
Ejemplo n.º 3
0
        private bool ToResistance(Decimal resistance)
        {
            if (iRange < RangeMin || iRange > RangeMax) //invalide res case
            {
                ToDAValue(0);
                //DeviceMgr.Action("daoutput", new Byte[] { 0x55, 0x64, 0xff, 0x00, 0x00, 0x00, 0x01 }); //just set to 0
                return(DeviceMgr.success);
            }
            double va;

            if (!CollectVoltage(out va))
            {
                return(false);
            }
            double volt = Convert.ToDouble(resistance);

            if (iRange < RangeMin || iRange > RangeMax)
            {
                return(false);
            }

            volt    = volt * va * FBCurrentScale;
            Current = va * FBCurrentScale;

            if (Current < -0.1) //current switch to negative
            {
                if (bPositive != -1)
                {
                    DeviceMgr.Action("defcurrent", 0);
                    DeviceMgr.Action("negcurrent", 0);
                    DeviceMgr.Action("defcurrent", 0);
                    DeviceMgr.Action("negcurrent", 0);
                    DeviceMgr.Action("defcurrent", 0);
                    bPositive = -1;
                }
            }
            if (Current > 0.1) //current switch to positive
            {
                if (bPositive != 1)
                {
                    DeviceMgr.Action("defcurrent", 0);
                    DeviceMgr.Action("poscurrent", 0);
                    DeviceMgr.Action("defcurrent", 0);
                    DeviceMgr.Action("poscurrent", 0);
                    DeviceMgr.Action("defcurrent", 0);
                    bPositive = 1;
                }
            }
            return(ToDAValue(Math.Abs(volt)));
        }
Ejemplo n.º 4
0
        public void ZeroON()
        {
            int i = 0;

            while (i++ < 5)
            {
                double va;
                if (CollectVoltage(out va))
                {
                    if (Math.Abs(va) < 0.00001) // < 10uV
                    {
                        DeviceMgr.Action("navzero", 0);
                    }
                    break;
                }
                System.Threading.Thread.Sleep(1000);
            }
        }
Ejemplo n.º 5
0
        private bool CollectVoltage(out double reading)
        {
            int badcount = 0; //count of bad communication

            reading = 0;

            while (badcount < 3)
            {
                DeviceMgr.Action("navread", 0);
                if (DeviceMgr.reading < -999)
                {
                    if (DeviceMgr.nav_range != "navto1v")
                    {
                        badcount++;
                        if (badcount < 3)
                        {
                            Thread.Sleep(500);
                            continue;
                        }
                        badcount = 0;
                        DeviceMgr.Action("navto1v", "");
                        Thread.Sleep(3000);
                        continue;
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                }
                if ((DeviceMgr.nav_range == "navto1v") && (Math.Abs(DeviceMgr.reading) < 0.10) && bOn)
                {
                    badcount++;
                    if (badcount < 3)
                    {
                        Thread.Sleep(500);
                        continue;
                    }

                    badcount = 0;
                    DeviceMgr.Action("navto120mv", "");
                    Thread.Sleep(3000);
                    continue;
                }
                if (DeviceMgr.nav_range == "navto120mv")
                {
                    datafilter.Enqueue(DeviceMgr.reading / 1000);
                }
                else
                {
                    datafilter.Enqueue(DeviceMgr.reading);
                }

                /*old method
                 * if (datafilter.Count < 5)
                 *  continue;
                 * double sqr;
                 * sqr = GetSqrt3(10);
                 * datafilter.Dequeue();
                 * if ((DeviceMgr.nav_range == "navto120mv") && (sqr > 0.05)) //1mV
                 * {
                 *  badcount++;
                 *  continue;
                 * }
                 * if ((DeviceMgr.nav_range == "navto1v") && (sqr > 0.05)) //10mv
                 * {
                 *  badcount++;
                 *  continue;
                 * }
                 * badcount = 0;
                 * reading = datafilter.Skip(2).Take(2).Average();
                 */

                if (datafilter.Count < 3)
                {
                    continue;
                }
                if (datafilter.Count > 3)
                {
                    datafilter.Dequeue();
                }

                double sqr;
                sqr = datafilter.Max() - datafilter.Min();

                if ((DeviceMgr.nav_range == "navto120mv") && (sqr > 0.001)) //1mV
                {
                    badcount++;
                    continue;
                }
                if ((DeviceMgr.nav_range == "navto1v") && (sqr > 0.01)) //10mv
                {
                    badcount++;
                    continue;
                }
                badcount = 0;
                reading  = datafilter.Average();
                return(true);
            }
            return(false);
        }
Ejemplo n.º 6
0
        private bool ToDAValue(double voltage)
        {
            voltage = voltage;   // nouse now / 2.0; //divide by 2 because hardware will multiple it with 2
            double vrefp = 20.1; //20.1 volts reference   //11;
            double vrefn = 0;

            // Vout = (Vrefp-Vrefn)*D/(2^20-1)+Vrefn =>  D= (Vout-Vrefn)*(2^20-1)/(Vrefp-Vrefn)
            // when BUF is enabled , Vrefp = 10V;  Vrefn = -10V; D = (Vout+10)*(2^20-1)/(20)
            // If vrefn = 0 and vrefp = 10 then D = Vout*(2^20-1)/10;
            byte[] tosend  = new Byte[] { 0x55, 0x64, 0xff, 0x00, 0x00, 0x00, 0x01 };
            byte[] tosend2 = new Byte[] { 0x55, 0x64, 0x55, 0xff, 0x00, 0x00, 0x00, 0x01 };//special case for 0x55 leading value

            bool changed = false;

            double volt = voltage - Convert.ToDouble(daoffset) + OPCurrentOffset;

            if (Math.Abs(volt) > vrefp) //turn off output or invalid adreading
            {
                bOverLoad = true;
                return(false);
            }
            bOverLoad = false;



            Int32 d = Convert.ToInt32(Math.Round((volt - vrefn) * (1048576 - 1) / (vrefp - vrefn)));

            tosend[5]  = Convert.ToByte(d % 256); d = d / 256;
            tosend2[6] = tosend[5];
            if (tosend[5] != lasttosend[5])
            {
                lasttosend[5] = tosend[5];
                changed       = true;
            }
            tosend[4]  = Convert.ToByte(d % 256);
            tosend2[5] = tosend[4];
            d          = d / 256;
            if (tosend[4] != lasttosend[4])
            {
                lasttosend[4] = tosend[4];
                changed       = true;
            }
            tosend[3]  = Convert.ToByte(d % 256);
            tosend2[4] = tosend[3];
            if (tosend[3] != lasttosend[3])
            {
                lasttosend[3] = tosend[3];
                changed       = true;
            }
            tosend[2]  = Convert.ToByte((256 * 3 - 1 - Convert.ToInt32(tosend[3] + tosend[4] + tosend[5])) % 256);
            tosend2[3] = tosend[2];
            if (changed)
            {
                if (tosend[2] == 0x55)
                {
                    DeviceMgr.Action("daoutput", tosend2);
                }
                else
                {
                    DeviceMgr.Action("daoutput", tosend);
                }
                return(DeviceMgr.success);
            }
            else
            {
                return(true);
            }
        }
Ejemplo n.º 7
0
        public Form1()
        {
            InitializeComponent();
            //ShowCursor(0);
            processor = new Processor();

            digi_upbtns = new RectButton[] { rbtn_1up, rbtn_2up, rbtn_3up, rbtn_4up, rbtn_5up, rbtn_6up };
            digi_dnbtns = new RectButton[] { rbtn_1dn, rbtn_2dn, rbtn_3dn, rbtn_4dn, rbtn_5dn, rbtn_6dn };
            dlg_choice  = new ChoiceWnd();
            dlg_kbd     = new kbdWnd();
            range_btns  = new RectButton[] { btn_range0, btn_range1, btn_range2, btn_range3, btn_range4, btn_range5, btn_range6 };
            string[] range_string = new String[] { "200µΩ/600A", "2mΩ/200A", "20mΩ/100A", "200mΩ/80A", "2Ω/10A", "4Ω/5A", "20Ω/1A" };
            led_ohm.ColorLight      = Color.Red;
            led_ohm.ColorBackground = this.BackColor;
            led_ohm.ElementWidth    = 12;
            led_ohm.RecreateSegments(led_ohm.ArrayCount);
            led_current.ColorLight      = Color.DarkGreen;
            led_current.ColorBackground = this.BackColor;
            led_current.ElementWidth    = 8;
            led_current.RecreateSegments(led_current.ArrayCount);
            led_current.Value = "0.0000";

            btn_zeroon.bgColor = this.BackColor;
            btn_zeroon.SetStyle(Color.Bisque, MyButtonType.roundRectButton);
            btn_zeroon.Text        = "清零";
            btn_zeroon.ValidClick += new EventHandler((o, e) =>
            {
                led_current.Value = "     ";
                processor.ZeroON();
            });

            btn_turnon.bgColor = this.BackColor;
            for (int i = 0; i < digi_upbtns.Length; i++)
            {
                RectButton rb = digi_upbtns[i];
                rb.bgColor = this.BackColor;
                rb.SetStyle(Color.DarkOrange, MyButtonType.triangleupButton);

                rb.ValidClick += new EventHandler((o, e) =>
                {
                    if (processor.iRange >= 0)
                    {
                        TickDigit(digi_upbtns.Length - NameInArray(o, digi_upbtns), true);
                        RefreshDisplay(false);
                    }
                });
            }
            for (int i = 0; i < digi_dnbtns.Length; i++)
            {
                RectButton rb = digi_dnbtns[i];
                rb.bgColor = this.BackColor;
                rb.SetStyle(Color.DarkOrange, MyButtonType.trianglednButton);
                rb.ValidClick += new EventHandler((o, e) =>
                {
                    if (processor.iRange >= 0)
                    {
                        TickDigit(digi_upbtns.Length - NameInArray(o, digi_dnbtns), false);
                        RefreshDisplay(false);
                    }
                });
            }
            btn_turnon.SetStyle(Color.Green, MyButtonType.round2Button);
            btn_turnon.Text        = "OFF";
            btn_turnon.ValidClick += new EventHandler((o, e) =>
            {
                if (!processor.bOn)
                {
                    dt_lastoutput = DateTime.Now.AddSeconds(2);
                }
                processor.bOn = !processor.bOn;
                RefreshDisplay(false);
            });

            for (int i = 0; i < range_btns.Length; i++)
            {
                RectButton rb = range_btns[i];
                rb.bgColor = this.BackColor;
                rb.Text    = range_string[i];
                rb.SetStyle(Color.Green, MyButtonType.roundRectButton);

                rb.ValidClick += new EventHandler((o, e) =>
                {
                    int newrange = NameInArray(o, range_btns);
                    if (newrange == processor.iRange)
                    {
                        return;
                    }

                    try
                    {
                        if (Math.Abs(double.Parse(led_current.Value)) < 0.1)
                        {
                            processor.iRange = newrange;
                            RefreshDisplay(true);
                        }
                        else
                        {
                            Program.MsgShow("请关闭输入电流后,再进行量程切换。");
                        }
                    }
                    catch
                    {
                    }
                });
            }
            tm          = new Timer();
            tm.Interval = 500;
            tm.Tick    += new EventHandler((o, e) =>
            {
                lbl_datetime.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                if (DateTime.Now.Subtract(dt_lastoutput).TotalSeconds < 1)
                {
                    dt_lastoutput = DateTime.Now;
                    return;
                }
                processor.RefreshOutput();
                if (processor.Current > -999)
                {
                    UpdateCurrentDisplay(processor.Current);
                    processor.Current = -9999;
                }
            });
            tm.Enabled = true;
            tm.Start();
            led_ohm.Click += new EventHandler(led_ohm_Click);
            DeviceMgr.Reset();

            RefreshDisplay(true);
        }
Ejemplo n.º 8
0
        private bool ToDAValue(double voltage)
        {
            // Vout = (Vrefp-Vrefn)*D/(2^20-1)+Vrefn =>  D= (Vout-Vrefn)*(2^20-1)/(Vrefp-Vrefn)
            // when BUF is enabled , Vrefp = 10V;  Vrefn = -10V; D = (Vout+10)*(2^20-1)/(20)
            // D = Vout*(2^20-1)/10;
            byte[] tosend  = new Byte[] { 0x55, 0x64, 0xff, 0x00, 0x00, 0x00, 0x01 };
            byte[] tosend2 = new Byte[] { 0x55, 0x64, 0x55, 0xff, 0x00, 0x00, 0x00, 0x01 };//special case for 0x55 leading value

            bool changed = false;

            double volt = voltage - Convert.ToDouble(daoffset) + OPCurrentOffset;

            if (Math.Abs(volt) > 10) //turn off output or invalid adreading
            {
                return(false);
            }



            Int32 d = Convert.ToInt32(Math.Round((volt + 10) * (1048576 - 1) / 20.0));

            tosend[5]  = Convert.ToByte(d % 256); d = d / 256;
            tosend2[6] = tosend[5];
            if (tosend[5] != lasttosend[5])
            {
                lasttosend[5] = tosend[5];
                changed       = true;
            }
            tosend[4]  = Convert.ToByte(d % 256);
            tosend2[5] = tosend[4];
            d          = d / 256;
            if (tosend[4] != lasttosend[4])
            {
                lasttosend[4] = tosend[4];
                changed       = true;
            }
            tosend[3]  = Convert.ToByte(d % 256);
            tosend2[4] = tosend[3];
            if (tosend[3] != lasttosend[3])
            {
                lasttosend[3] = tosend[3];
                changed       = true;
            }
            tosend[2]  = Convert.ToByte((256 * 3 - 1 - Convert.ToInt32(tosend[3] + tosend[4] + tosend[5])) % 256);
            tosend2[3] = tosend[2];
            if (changed)
            {
                if (tosend[2] == 0x55)
                {
                    DeviceMgr.Action("daoutput", tosend2);
                }
                else
                {
                    DeviceMgr.Action("daoutput", tosend);
                }
                return(DeviceMgr.success);
            }
            else
            {
                return(true);
            }
        }