#region 程序集 KeysightDMM3458A.Interop, Version=1.0.8622.37453, Culture=neutral, PublicKeyToken=null
// C:\Users\testrong\Desktop\EXT_CAL\SW1.2_55_calibration_RZ_0810\selfCalibration\Testrong.User.Program\Debug\KeysightDMM3458A.Interop.dll
// Decompiled with ICSharpCode.Decompiler 7.1.0.6543
#endregion

using System;
using System.IO;
using System.Text;
using System.Threading;
using Testrong.User.Program;

namespace Testrong.User.Program
{
    /// <summary>
    /// Keysight53220A Related
    /// </summary>
    public class Keysight53220A
    {
        /// <summary>
        /// Global variables
        /// </summary>
        public class Globals
        {
            public static int videfaultRM;

            public static int vi;

            public static int errorStatus;

            public static bool connected;
        }

        /// <summary>
        /// KeysightMeasMode
        /// </summary>
        public enum KeysightMeasMode
        {
            FREQ,
            INIT
        }


        /// <summary>
        /// Initial DMM
        /// </summary>
        /// <param name="address">DMM USB ADDR</param>
        /// <param name="funcMode">KeysightMeasMode</param>
        /// <returns>true if connected, false if not</returns>
        public bool InitDMM53220A(string address, KeysightMeasMode funcMode)
        {
            try
            {
                string text = address.ToUpper();
                if (text.Length < 5)
                {
                    Globals.connected = false;
                    return false;
                }

                if (Globals.connected)
                {
                    Globals.errorStatus = visa32.viClose(Globals.vi);
                }

                Globals.errorStatus = visa32.viOpenDefaultRM(out Globals.videfaultRM);
                Globals.errorStatus = visa32.viOpen(Globals.videfaultRM, text, 0, 0, out Globals.vi);
                if (Globals.errorStatus < 0)
                {
                    Globals.connected = false;
                    return false;
                }

                SendCmd("*IDN?");
                if (GetData().IndexOf("53220A") < 0)
                {
                    Globals.connected = false;
                    return false;
                }

                Globals.connected = true;
                //PresentMode = KeysightMeasMode.INIT;
                return Setup(funcMode);

            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// Get DMM Measured Data
        /// </summary>
        /// <param name="count">count</param>
        /// <returns>ReturnReadings()</returns>
        public double GetDmm53220AMeasureData(int count = 1)
        {
            if (!Globals.connected)
            {
                return 0.0;
            }

            return ReturnReadings();
        }



        /// <summary>
        /// Setup DMM
        /// </summary>
        /// <param name="funcMode">KeysightMeasMode</param>
        /// <returns>SendCmd(text) if Success, false if Failed</returns>
        private bool Setup(KeysightMeasMode funcMode)
        {
            try
            {
                string text = "";
                text += "*RST\n";
                text += "*CLS\n";
                text += "STAT:PRES\n";
                text += "CONF:FREQ\n";
                text += "INP:COUP DC\n";
                text += "INP:LEV 2\n";
                text += "INP:SLOP NEG\n";
                text += "SYST:BEEP:STAT OFF\n";
                text += "FREQ:GATE:SOUR TIME\n";
                text += "FREQ:GATE:TIME .0001\n";
                text += "TRIG:SOUR BUS\n";
                text += "TRIG:COUN 100000\n";
                text += "SAMP:COUN 1\n";
                text += "INIT\n";
                return SendCmd(text);
            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// Return DMM Readings
        /// </summary>
        /// <returns></returns>
        private double ReturnReadings()
        {
            string text = "";
            StringBuilder sb = new StringBuilder(2048);
            int empty_count = 0;
            int sleep_count = 0;
            while (true)
            {
                text = "R?";
                try
                {
                    SendCmd(text);
                    Globals.errorStatus = visa32.viScanf(Globals.vi, "%2048T", sb);
                }
                catch (Exception)
                {
                    return -2;
                }
                if (sb[0] != '#')
                {
                    return -1;
                }
                int meta_count = int.Parse(sb[1].ToString());
                int byte_count = int.Parse(sb.ToString().Substring(2, meta_count));
                if (byte_count > 0)
                {
                    return double.Parse(sb.ToString().Substring(meta_count + 2));
                }
                else
                {
                    empty_count++;
                }
                if (empty_count >= 10)
                {
                    Thread.Sleep(1000);
                    sleep_count++;
                    empty_count = 0;
                }
                if (sleep_count >= 5)
                {
                    return -3;
                }
            }
        }

        /// <summary>
        /// Send CMD to DMM
        /// </summary>
        /// <param name="Cmds">CMD String</param>
        /// <returns></returns>
        private bool SendCmd(string Cmds)
        {
            try
            {
                Globals.errorStatus = visa32.viPrintf(Globals.vi, Cmds + "\n");
                if (Globals.errorStatus < 0)
                {
                    return false;
                }

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Close DMM Connection
        /// </summary>
        /// <returns>true</returns>
        public bool CloseDMM53220A()
        {
            Globals.errorStatus = visa32.viClose(Globals.vi);
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private string GetData()
        {
            StringBuilder stringBuilder = new StringBuilder(2048);
            try
            {
                Globals.errorStatus = visa32.viScanf(Globals.vi, "%2048T", stringBuilder);
                if (Globals.errorStatus < 0)
                {
                    return null;
                }

                return stringBuilder.ToString().Remove(stringBuilder.ToString().Length - 1, 1);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bool InitMeasure()
        {
            return SendCmd("*TRG\n");
        }

        public bool ChangeLevel(string level)
        {
            string text = "";
            text += "*RST\n";
            text += "*CLS\n";
            text += "STAT:PRES\n";
            text += "CONF:FREQ\n";
            //switch (funcMode)
            //{
            //    case KeysightMeasMode.FREQ:
            //        {
            //            //text += "TRIG:COUN 1\n";
            //            //text += "SAMP:COUN 10\n";
            //            //text += "INIT\n";
            //        }
            //        break;
            //    default:
            //        {
            //            return false;
            //        }
            //}
            text += "INP:COUP DC\n";
            //text += "INP:NREJ ON\n";
            text += "INP:LEV "+ level + "\n";
            text += "INP:SLOP NEG\n";
            //text += "INP:LEV 10\n";
            text += "SYST:BEEP:STAT OFF\n";
            text += "FREQ:GATE:SOUR TIME\n";
            text += "FREQ:GATE:TIME .0001\n";
            text += "TRIG:SOUR BUS\n";
            text += "TRIG:COUN 100000\n";
            text += "SAMP:COUN 1\n";
            //text += "INP:LEV:AUTO ON\n";

            //text += "SAMP:COUN 2\n";
            //text += "TRIG:DEL .01\n";   
            text += "INIT\n";
            //GlobalSetup = true;
            //PresentMode = funcMode;
            return SendCmd(text);
            //return SendCmd("INP:LEV " + level + "\n");
        }

    }
}
#if false // 反编译日志
缓存中的 23 项
------------------
解析: "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
找到单个程序集: "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
从以下位置加载: "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\mscorlib.dll"
#endif
