#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.Text;
using System.Threading;
using Testrong.User.Program;

namespace Testrong.User.Program
{
    public class KeysightDMM3458A
    {
        public class Globals
        {
            public static int videfaultRM;

            public static int vi;

            public static int errorStatus;

            public static bool connected;
        }

        public enum KeysightMeasMode
        {
            VOLT,
            CURR,
            RES,
            INIT
        }

        KeysightMeasMode PresentMode;
        double PresentRange;

        public bool InitDMM(string address)
        {
            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;
                }

                Globals.errorStatus = visa32.viSetAttribute(Globals.vi, 1073676312, 13);
                Globals.errorStatus = visa32.viSetAttribute(Globals.vi, 1073676344, 1);
                Globals.errorStatus = visa32.viSetAttribute(Globals.vi, 1073676314, 2000);
                SendCmd("ID?");
                if (GetData().IndexOf("3458A") < 0)
                {
                    Globals.connected = false;
                    return false;
                }

                Globals.connected = true;
                PresentMode = KeysightMeasMode.INIT;
                PresentRange = 0;
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public double GetDmmMeasureData(KeysightMeasMode funcMode, int count = 1, double range = 0)
        {
            if (!Globals.connected)
            {
                return 0.0;
            }

            if (!Setup(count, funcMode, range))
            {
                return 0.0;
            }

            return ReturnReadings(funcMode, range);
        }

        bool GlobalSetup = false;

        private bool Setup(int count, KeysightMeasMode funcMode, double range)
        {
            try
            {
                if (GlobalSetup == false || PresentMode != funcMode || PresentRange != range)
                {
                    string text = "PRESET NORM;";
                    text = text + "NRDGS" + count + ",AUTO;";
                    text += "MEM LIFO;";
                    text += "MFORMAT DREAL;";
                    text += "OFORMAT ASCII;";
                    text += "MATH STAT;";
                    text += "AZERO OFF;";
                    text += "ARANGE ONCE;";
                    switch (funcMode)
                    {
                        case KeysightMeasMode.VOLT:
                            {
                                text += $"DCV {range:F8};";
                            }
                            break;
                        case KeysightMeasMode.CURR:
                            {
                                text += $"DCI {range:F8};";
                            }
                            break;
                        case KeysightMeasMode.RES:
                            {
                                text += $"OHM {range:F8};";
                            }
                            break;
                        default:
                            {
                                return false;
                            }
                    }
                    text += "NPLC 5;";
                    //text += "NPLC 0.05;";
                    text += "TARM AUTO;";
                    text += "TRIG AUTO;";
                    text += "BEEP OFF;";
                    text += "DISP OFF;";
                    GlobalSetup = true;
                    PresentMode = funcMode;
                    PresentRange = range;
                    return SendCmd(text);
                }
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
        private double ReturnReadings(KeysightMeasMode funcMode, double range = 0)
        {
            string text = "";
            try
            {
                Globals.errorStatus = visa32.viSetAttribute(Globals.vi, 1073676314, 2000);                 
                Globals.errorStatus = visa32.viScanf(Globals.vi, "%lf", out double arg);//get dmm vaue
                return arg;

            }
            catch (Exception)
            {
                return 0.0;
            }
        }

        private bool SendCmd(string Cmds)
        {
            try
            {
                Globals.errorStatus = visa32.viPrintf(Globals.vi, Cmds + "\n");
                //Thread.Sleep(5000);
                if (Globals.errorStatus < 0)
                {
                    return false;
                }

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

        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;
            }
        }
    }
}
#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
