using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.VisualBasic;

namespace LadyBug_TestHarness
{
    public partial class RgrTest_Tabbed : Form
    {
        private const string MSG_4_SPC = "    ";
        private const string MSG_ERROR_START = "Error - ";
        private const string MSG_FAILED = "Failed";
        private const string MSG_SUCCESS = "Success";
        private const string MSG_PLEASE_WAIT = "...please wait...";
        private const string MSG_COUNT_UNKNOWN = "Count = ?";
        private const string MSG_COUNT_KNOWN = "Count = ";
        private const string MSG_NO_SENSOR_CONNECTED = "No sensor connected";
        private const string MSG_SENSOR_IS_NOT_CONNECTED = "Sensor at address {0:d} is not connected";
        private const string MSG_DISCONNECT_RECONNECT_SENSOR = "Please disconnect and reconnect sensor...then click OK";
        private const string MSG_ADDRESS_CHANGE_SUCCESSFUL = "The new address was successfully changed";
        private const string MSG_ADDRESS_CHANGE_UNSUCCESSFUL = "The new address was not successfully changed";
        private const string MSG_INITIALIZING_SENSOR = "The sensor must be initialized...click OK then wait";
        private const string MSG_INITIALIZATION_FAILED = "Sensor initialization failed";
        private const string MSG_ADRESSCONFLICTEXISTS_FAILED = "Unknown error";
        private const string MSG_ADRESSCONFLICTEXISTS_TRUE = "Yes";
        private const string MSG_ADRESSCONFLICTEXISTS_FALSE = "No";
        private const string MSG_SENSOR_TRYING_TO_INITALIZE = "...initializing sensor at addr={0:d}...";
        private const string MSG_GET_TRACE_ERROR = "Unable to take a sweep and get the trace from addr {0:d}";
        private const string MSG_GET_TRACE_SUCCEEDED = "Sweep taken at addr {0:d} array sized to {1:d} points and {2:d} points were retrived";
        private const string MSG_SENSOR_FAILED_INITIALIZATION = "Sensor at address {0:d} had an error during initialization";
        private const string MSG_TRACE_LENGTH_ERROR = "Unable to determine trace len at addr {0:d}";
        private const string MSG_TRACE_LENGTH = "Trace len at addr {0:d} is: {1:d} points";
        private const string FMT_INDEX_POWER = "{0,6:0}{1,10:0.00} dBm";

        LB_API2_Declarations.SDByte[] sensDesc;
        int currAddr = -1;
        int currIdx = -1;
        int currMarkerIdx = 0;
        int currGateIdx = 0;
        string currSN;
        StringBuilder sb;
        private LB_API2_Declarations.PWR_UNITS CURR_UNITS = LB_API2_Declarations.PWR_UNITS.DBM;
        double[] trAry;
        bool inMeasurement = false;
        bool overWriteComplete = false;
        
        private void MeasCW()
        {
            int addr = currAddr;
            LB_API2_Declarations.PASS_FAIL_RESULT pf = LB_API2_Declarations.PASS_FAIL_RESULT.NO_DETERMINATION;
            double cw = 0.0;
            int rslt = 0;
            if (ckCWPF.Checked == true)
            {
                rslt = LB_API2_Declarations.LB_MeasureCW_PF(addr, ref cw, ref pf);
            }
            else
            {
                rslt = LB_API2_Declarations.LB_MeasureCW(addr, ref cw);
            }
            if (rslt > 0)            // if rslt is positive everything is ok
            {
                switch (CURR_UNITS)
                {
                    case LB_API2_Declarations.PWR_UNITS.DBKW:
                        lblLB_MeasureCW.Text = string.Format("{0:F} dBkW", cw);
                        break;
                    case LB_API2_Declarations.PWR_UNITS.DBM:
                        lblLB_MeasureCW.Text = string.Format("{0:F} dBm", cw);
                        break;
                    case LB_API2_Declarations.PWR_UNITS.DBREL:
                        lblLB_MeasureCW.Text = string.Format("{0:F} dB (rel)", cw);
                        break;
                    case LB_API2_Declarations.PWR_UNITS.DBUV:
                        lblLB_MeasureCW.Text = string.Format("{0:F} dBuV", cw);
                        break;
                    case LB_API2_Declarations.PWR_UNITS.DBW:
                        lblLB_MeasureCW.Text = string.Format("{0:F} dBW", cw);
                        break;
                    case LB_API2_Declarations.PWR_UNITS.V:
                        lblLB_MeasureCW.Text = string.Format("{0:E} V", cw);
                        break;
                    case LB_API2_Declarations.PWR_UNITS.W:
                        lblLB_MeasureCW.Text = string.Format("{0:E} W", cw);
                        break;
                }
                if (ckCWPF.Checked == true)
                {
                    lblLB_MeasureCW.Text = lblLB_MeasureCW.Text + " - " + pf.ToString();
                }
            }
        }

        private void MeasPulse()
        {
            int addr = currAddr;
            int rslt = 0;
            LB_API2_Declarations.PASS_FAIL_RESULT pf = LB_API2_Declarations.PASS_FAIL_RESULT.NO_DETERMINATION;
            double pls = 0.0;
            double pk = 0.0;
            double cw = 0.0;
            double dc = 0.0;
            if (ckPulsePF.Checked == true)
            {
                rslt = LB_API2_Declarations.LB_MeasurePulse_PF(addr, ref pls, ref pk, ref cw, ref dc, ref pf);
            }
            else
            {
                rslt = LB_API2_Declarations.LB_MeasurePulse(addr, ref pls, ref pk, ref cw, ref dc);
            }
            if (rslt > 0)            // if rslt is positive everything is ok
            {
                switch (CURR_UNITS)
                {
                    case LB_API2_Declarations.PWR_UNITS.DBKW:
                        lblLB_MeasurePulse.Text = string.Format("Pulse={0:F} dBkW,  Peak={1:F} dBkW, CW={2:F} dBkW, DC={3:F}%,",
                                                                pls, pk, cw, dc * 100.0);
                        break;
                    case LB_API2_Declarations.PWR_UNITS.DBM:
                        lblLB_MeasurePulse.Text = string.Format("Pulse={0:F} dBm,  Peak={1:F} dBm, CW={2:F} dBm, DC={3:F}%,",
                                                                pls, pk, cw, dc * 100.0);
                        break;
                    case LB_API2_Declarations.PWR_UNITS.DBREL:
                        lblLB_MeasurePulse.Text = string.Format("Pulse={0:F} dB,  Peak={1:F} dB, CW={2:F} dB, DC={3:F}%,",
                                                                pls, pk, cw, dc * 100.0);
                        break;
                    case LB_API2_Declarations.PWR_UNITS.DBUV:
                        lblLB_MeasurePulse.Text = string.Format("Pulse={0:F} dBuV,  Peak={1:F} dBuV, CW={2:F} dBuV, DC={3:F}%,",
                                                                pls, pk, cw, dc * 100.0);
                        break;
                    case LB_API2_Declarations.PWR_UNITS.DBW:
                        lblLB_MeasurePulse.Text = string.Format("Pulse={0:F} dBW,  Peak={1:F} dBW, CW={2:F} dBW, DC={3:F}%,",
                                                                pls, pk, cw, dc * 100.0);
                        break;
                    case LB_API2_Declarations.PWR_UNITS.V:
                        lblLB_MeasurePulse.Text = string.Format("Pulse={0:E} V,  Peak={1:E} V, CW={2:E} V, DC={3:F}%,",
                                                                pls, pk, cw, dc * 100.0);
                        break;
                    case LB_API2_Declarations.PWR_UNITS.W:
                        lblLB_MeasurePulse.Text = string.Format("Pulse={0:E} W,  Peak={1:E} W, CW={2:E} WBm, DC={3:F}%,",
                                                                pls, pk, cw, dc * 100.0);
                        break;
                }
                if (ckPulsePF.Checked == true)
                {
                    lblLB_MeasurePulse.Text = lblLB_MeasurePulse.Text + " - " + pf.ToString();
                }
            }
            else
                lblLB_MeasurePulse.Text = MSG_FAILED;
        }

        public RgrTest_Tabbed()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSensorCount_Click(object sender, EventArgs e)
        {
            lblLB_SensorCount.Text = LB_API2_Declarations.LB_SensorCnt().ToString();
        }

        private void btnLB_SensorList_Click(object sender, EventArgs e)
        {
            // get the number of sensors...
            int cnt = LB_API2_Declarations.LB_SensorCnt();
            lstLB_SensorList.Items.Clear();
            if (cnt > 0)
            {
                // declare and array of sensor descriptions
                sensDesc = new LB_API2_Declarations.SDByte[cnt];
                // get the descriptoins
                int rslt = LB_API2_Declarations.LB_SensorList(ref sensDesc[0], cnt);
                // if we got descriptions then put them in the list box for all to see
                if (rslt >= 1)
                {
                    for (int i = 0; i < cnt; i++)
                    {
                        string strSensDesc = string.Concat(string.Format("{0,3:d}    {1,3:d}    ",
                                        sensDesc[i].DeviceIndex, sensDesc[i].DeviceAddress), sensDesc[i].SN());
                        lstLB_SensorList.Items.Add(strSensDesc);
                    }
                }
                else
                {
                    lstLB_SensorList.Items.Add(string.Concat(MSG_ERROR_START, rslt.ToString()));
                }
            }
            else
            {
                lstLB_SensorList.Items.Add(MSG_NO_SENSOR_CONNECTED);
            }
        }

        private void lstLB_SensorList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (sensDesc.Length == lstLB_SensorList.Items.Count)
                {
                    if (lstLB_SensorList.SelectedIndex >= 0)
                    {
                        if (chkAutoFill.CheckState == CheckState.Checked)
                        {
                            int rslt = 0;
                            int yr = 0, mo = 0, da = 0;
                            LB_API2_Declarations.MODEL_NUMBER mn = LB_API2_Declarations.MODEL_NUMBER.GUknwn;
                            LB_API2_Declarations.FEATURE_STATE optVal = LB_API2_Declarations.FEATURE_STATE.ST_OFF;
                            LB_API2_Declarations.CONN_TYPES connType = LB_API2_Declarations.CONN_TYPES.NOT_SET;

                            currAddr = sensDesc[lstLB_SensorList.SelectedIndex].DeviceAddress;
                            currIdx = sensDesc[lstLB_SensorList.SelectedIndex].DeviceIndex;
                            currSN = sensDesc[lstLB_SensorList.SelectedIndex].SN();

                            txtSerNo.Text = currSN;
                            udAddress.Value = currAddr;
                            udIndex.Value = currIdx;
                            rslt = LB_API2_Declarations.LB_GetModelNumber_Addr(currAddr, ref mn);
                            if (rslt <= 0)
                            {
                                MessageBox.Show("Error getting model number");
                                return;
                            }
                            lblCurrMN.Text = mn.ToString();
                            lstOptions.Items.Clear();
                            rslt = LB_API2_Declarations.LB_GetFilterOpt(currSN, ref optVal);
                            if (rslt <= 0)
                            {
                                MessageBox.Show("Error getting filter option");
                                return;
                            }
                            lstOptions.Items.Add("Flt Opt = " + ((LB_API2_Declarations.FEATURE_STATE)optVal).ToString());

                            rslt = LB_API2_Declarations.LB_GetTriggerOpt(currSN, ref optVal);
                            if (rslt <= 0)
                            {
                                MessageBox.Show("Error getting trigger option");
                                return;
                            }
                            lstOptions.Items.Add("Trg Opt = " + ((LB_API2_Declarations.FEATURE_STATE)optVal).ToString());

                            rslt = LB_API2_Declarations.LB_GetRecorderOutOption(currSN, ref optVal);
                            if (rslt <= 0)
                            {
                                MessageBox.Show("Error getting recorder out option");
                                return;
                            }
                            lstOptions.Items.Add("Rec Out Opt = " + ((LB_API2_Declarations.FEATURE_STATE)optVal).ToString());

                            rslt = LB_API2_Declarations.LB_GetConnectorOption(currSN, ref connType);
                            if (rslt <= 0)
                            {
                                MessageBox.Show("Error getting connect type option");
                                return;
                            }
                            lstOptions.Items.Add("Conn type = " + connType.ToString());

                            rslt = LB_API2_Declarations.LB_GetCalAndWtyOption(currSN, ref optVal);
                            if (rslt <= 0)
                            {
                                MessageBox.Show("Error getting cal and warranty option");
                                return;
                            }
                            lstOptions.Items.Add("Cal & Wty Opt = " + ((LB_API2_Declarations.FEATURE_STATE)optVal).ToString());

                            rslt = LB_API2_Declarations.LB_GetBestMatchOpt(currSN, ref optVal);
                            if (rslt <= 0)
                            {
                                MessageBox.Show("Error getting best match option");
                                return;
                            }
                            lstOptions.Items.Add("Best Match Opt = " + ((LB_API2_Declarations.FEATURE_STATE)optVal).ToString());
                            
                            rslt = LB_API2_Declarations.LB_GetWtyOptExpDate(currSN, ref yr, ref mo, ref da); 
                            if (rslt <= 0)
                            {
                                MessageBox.Show("Error getting warranty option expiration date");
                                return;
                            }
                            lstOptions.Items.Add("Wty Opt Exp = " + string.Format("{0:D2}/", mo) + string.Format("{0:D2}/", da) + string.Format("{0:D2}", yr));

                            rslt = LB_API2_Declarations.LB_GetCalOptExpDate(currSN, ref yr, ref mo, ref da);
                            if (rslt <= 0)
                            {
                                MessageBox.Show("Error getting cal option expiration date");
                                return;
                            }
                            lstOptions.Items.Add("Cal Opt Exp = " + string.Format("{0:D2}/", mo) + string.Format("{0:D2}/", da) + string.Format("{0:D2}", yr));
                        }
                    }
                }
            }
            catch (Exception)
            {
                // do nothing    
            } 
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            currSN = "";
            txtSerNo.Text = "";
            currAddr = -1;
            udAddress.Value = -1;
            currIdx = -1;
            udIndex.Value = -1;
            lstOptions.Items.Clear();
        }

        private void btnLB_GetAddress_SN_Click(object sender, EventArgs e)
        {
            try
            {
                int rslt = 0;
                if (currSN.Length < 6)
                {
                    currSN += currSN + new string(' ', 6 - currSN.Length);
                }
                else if (currSN.Length > 6)
                {
                    currSN = currSN.Substring(0, 6);
                }
                rslt = LB_API2_Declarations.LB_GetAddress_SN(currSN);
                if (rslt > 0)
                    lblLB_GetAddress_SN.Text = rslt.ToString();
                else
                    lblLB_GetAddress_SN.Text = MSG_FAILED;
            }
            catch (Exception)
            {
                lblLB_GetAddress_SN.Text = MSG_FAILED;
            }
        }

        private void btnLB_GetAddress_Idx_Click(object sender, EventArgs e)
        {
            int rslt = LB_API2_Declarations.LB_GetAddress_Idx(currIdx);
            if (rslt > 0)
                lblLB_GetAddress_Idx.Text = rslt.ToString();
            else
                lblLB_GetAddress_Idx.Text = MSG_FAILED;
        }

        private void btnLB_SetAddress_Idx_Click(object sender, EventArgs e)
        {
            int addr = Decimal.ToInt32(udLB_SetAddress_Idx_Address.Value);
            if (LB_API2_Declarations.LB_SetAddress_Idx(currIdx, addr) >= 1)
            {
                DialogResult rslt = MessageBox.Show(MSG_DISCONNECT_RECONNECT_SENSOR);
                if (rslt == DialogResult.OK)
                {
                    if (addr==LB_API2_Declarations.LB_GetAddress_Idx(currIdx))
                    {
                        lblLB_SetAddress_Idx.Text = MSG_SUCCESS;
                    }
                    else
                    {
                        lblLB_SetAddress_Idx.Text = MSG_FAILED;
                    }
                }
            }
            else
            {
                lblLB_SetAddress_Idx.Text = MSG_FAILED;
            }
        }

        private void btnLB_SetAddress_SN_Click(object sender, EventArgs e)
        {
            try
            {
                int addr = Decimal.ToInt32(udLB_SetAddress_SN_Addr.Value);
                if (LB_API2_Declarations.LB_SetAddress_SN(currSN, addr) >= 1)
                {
                    DialogResult rslt = MessageBox.Show(MSG_DISCONNECT_RECONNECT_SENSOR);
                    if (rslt == DialogResult.OK)
                    {
                        if (addr == LB_API2_Declarations.LB_GetAddress_SN(currSN))
                        {
                            lblLB_SetAddress_SN.Text = MSG_SUCCESS;
                        }
                        else
                        {
                            lblLB_SetAddress_SN.Text = MSG_FAILED;
                        }
                    }
                }
                else
                {
                    lblLB_SetAddress_SN.Text = MSG_FAILED;
                }
            }
            catch (Exception)
            {
                lblLB_SetAddress_SN.Text = MSG_FAILED;
            }
        }

        private void btnLB_ChangeAddress_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult  rslt = DialogResult.None;
                int newAddress = Decimal.ToInt32(udLB_ChangeAddress_NewAddress.Value);
                if (LB_API2_Declarations.LB_ChangeAddress(currAddr, newAddress) >= 1)
                {
                    // get the user to disconnect and reconnect to reinitialize the sensor
                    rslt = MessageBox.Show(MSG_DISCONNECT_RECONNECT_SENSOR);
                    if (rslt == DialogResult.OK)
                    {
                        // now check address
                        if (newAddress == LB_API2_Declarations.LB_GetAddress_Idx(currIdx))
                        {
                            lblLB_ChangeAddress.Text = MSG_SUCCESS;
                        }
                        else
                        {
                            lblLB_ChangeAddress.Text = MSG_FAILED;
                        }
                    }
                }
                else
                {
                    lblLB_ChangeAddress.Text = MSG_FAILED;
                }
            }
            catch (Exception)
            {
                lblLB_ChangeAddress.Text = MSG_FAILED;
            }
        }

        private void lblLB_ChangeAddress_Click(object sender, EventArgs e)
        {

        }

        private void btnLB_GetIndex_Addr_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int rslt = LB_API2_Declarations.LB_GetIndex_Addr(addr);
            if (rslt < 1)
                lblLB_GetIndex_Addr.Text = MSG_FAILED;
            else
                lblLB_GetIndex_Addr.Text = rslt.ToString();
        }

        private void btnLB_GetIndex_SN_Click(object sender, EventArgs e)
        {
            string sn = currSN;
            int rslt = LB_API2_Declarations.LB_GetIndex_SN(sn);
            if (rslt < 1)
                lblLB_GetIndex_SN.Text = MSG_FAILED;
            else
                lblLB_GetIndex_SN.Text = rslt.ToString();
        }

        private void btnLB_AddressConflictExists_Click(object sender, EventArgs e)
        {
            int rslt = LB_API2_Declarations.LB_AddressConflictExists();
            if (rslt < 0)
            {
                lblLB_AddressConflictExists.Text = MSG_FAILED;
            }
            else if (rslt >= 1)
            {
                lblLB_AddressConflictExists.Text = MSG_ADRESSCONFLICTEXISTS_TRUE;
            }
            else
            {
                lblLB_AddressConflictExists.Text = MSG_ADRESSCONFLICTEXISTS_FALSE;
            }

        }

        private void btnLB_WillAddressConflict_Click(object sender, EventArgs e)
        {
            int addr = (int)udLB_WillAddressConflict.Value;

            int rslt = LB_API2_Declarations.LB_WillAddressConflict(addr);
            if (rslt < 0)
            {
                lblLB_WillAddressConflict.Text = MSG_ADRESSCONFLICTEXISTS_FAILED;
            }
            else if (rslt >= 1)
            {
                lblLB_WillAddressConflict.Text = MSG_ADRESSCONFLICTEXISTS_TRUE;
            }
            else
            {
                lblLB_WillAddressConflict.Text = MSG_ADRESSCONFLICTEXISTS_FALSE;
            }

        }

        private void btnLB_BlinkLED_Idx_Click(object sender, EventArgs e)
        {
            int idx = currIdx;
            int rslt = LB_API2_Declarations.LB_BlinkLED_Idx(idx);
            if (rslt != 1)
                lblLB_BlinkLED_Idx.Text = MSG_FAILED;
            else lblLB_BlinkLED_Idx.Text = MSG_SUCCESS;
        }

        private void btnLB_BlinkLED_Addr_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int rslt = LB_API2_Declarations.LB_BlinkLED_Addr(addr);
            if (rslt != 1)
                lblLB_BlinkLED_Addr.Text = MSG_FAILED;
            else lblLB_BlinkLED_Addr.Text = MSG_SUCCESS;
        }

        private void btnLB_BlinkLED_SN_Click(object sender, EventArgs e)
        {
            try
            {
                string sn = currSN;
                int rslt = LB_API2_Declarations.LB_BlinkLED_SN(sn);
                if (rslt != 1)
                    lblLB_BlinkLED_SN.Text = MSG_FAILED;
                else lblLB_BlinkLED_SN.Text = MSG_SUCCESS;
            }
            catch (Exception)
            {
                lblLB_BlinkLED_SN.Text = MSG_FAILED;
            }
        }

        private void btnLB_GetFirmwareVersion_Click(object sender, EventArgs e)
        {
            byte[] ver = new byte[20];
            int addr = currAddr;
            try
            {
                int rslt = LB_API2_Declarations.LB_GetFirmwareVersion(addr, ref ver[0], 20);
                if (rslt >= 1)
                    lblVersion.Text = Encoding.ASCII.GetString(ver);
                else
                    lblVersion.Text = MSG_FAILED;
            }
            catch (Exception)
            {
                lblVersion.Text = MSG_FAILED;
            }
        }

        private void btnLB_SetCalDueDate_Click(object sender, EventArgs e)
        {
            try
            {
                string sn = currSN;
                int yr = dtLB_SetCalDueDate.Value.Year % 100;
                int mo = dtLB_SetCalDueDate.Value.Month;
                int da = dtLB_SetCalDueDate.Value.Day;
                int rtn = LB_API2_Declarations.LB_SetCalDueDate(sn, yr, mo, da);
                if (rtn > 0)
                {
                    lblLB_SetCalDueDate.Text = MSG_SUCCESS;
                }
                else
                {
                    lblLB_SetCalDueDate.Text = MSG_FAILED;
                }
            }
            catch (Exception)
            {
                lblLB_SetCalDueDate.Text = MSG_FAILED;
            }
        }

        private void btnLB_GetCalDueDate_Click(object sender, EventArgs e)
        {
            try
            {
                string sn = currSN;
                int yr = 0;
                int mo = 0;
                int da = 0;

                int rtn = LB_API2_Declarations.LB_GetCalDueDate(sn, ref yr, ref mo, ref da);
                if (rtn > 0)
                {
                    lblLB_GetCalDueDate.Text = string.Format("{0:D2}/", mo) + string.Format("{0:D2}/", da) + string.Format("{0:D2}", yr);
                }
                else
                {
                    lblLB_GetCalDueDate.Text = MSG_FAILED;
                }
            }
            catch (Exception)
            {
                lblLB_GetCalDueDate.Text = MSG_FAILED;
            }
        }

        private void btnLB_GetModelNumber_SN_Click(object sender, EventArgs e)
        {
            string sn = currSN;
            LB_API2_Declarations.MODEL_NUMBER mn = LB_API2_Declarations.MODEL_NUMBER.MNUknwn;
            try
            {
                int rslt = LB_API2_Declarations.LB_GetModelNumber_SN(sn, ref mn);
                if (rslt >= 1)
                {
                    lblLB_GetModelNumber_SN.Text = mn.ToString();
                }
                else
                {
                    lblLB_GetModelNumber_SN.Text = MSG_FAILED;
                }
            }
            catch (Exception)
            {
                lblLB_GetModelNumber_SN.Text = MSG_FAILED;
            }
        }

        private void btnLB_GetModelNumber_Idx_Click(object sender, EventArgs e)
        {
            int idx = currIdx;
            LB_API2_Declarations.MODEL_NUMBER mn = LB_API2_Declarations.MODEL_NUMBER.MNUknwn;
            int rslt = LB_API2_Declarations.LB_GetModelNumber_Idx(idx, ref mn);
            if (rslt >= 1)
            {
                lblLB_GetModelNumber_Idx.Text = mn.ToString();
            }
            else
            {
                lblLB_GetModelNumber_Idx.Text = MSG_FAILED;
            }
        }

        private void btnLB_GetSerNo_Idx_Click(object sender, EventArgs e)
        {
            try
            {
                ASCIIEncoding AE = new ASCIIEncoding();
                int idx = currIdx;
                byte[] sn = new byte[6];
                int rslt = LB_API2_Declarations.LB_GetSerNo_Idx(idx, ref sn[0]);
                if (rslt >= 1)
                    lblLB_GetSerNo_Idx.Text = AE.GetString(sn);
                else
                    lblLB_GetSerNo_Idx.Text = MSG_FAILED;
            }
            catch (Exception)
            {
                lblLB_GetSerNo_Idx.Text = MSG_FAILED;
            } 
        }

        private void btnLB_GetSerNo_Addr_Click(object sender, EventArgs e)
        {
            try
            {
                ASCIIEncoding AE = new ASCIIEncoding();
                int addr = currAddr;
                byte[] sn = new byte[6];
                int rslt = LB_API2_Declarations.LB_GetSerNo_Addr(addr, ref sn[0]);
                lblLB_GetSerNo_Addr.Text = AE.GetString(sn);
                if (rslt >= 1)
                    lblLB_GetSerNo_Addr.Text = AE.GetString(sn);
                else
                    lblLB_GetSerNo_Addr.Text = MSG_FAILED;
            }
            catch (Exception)
            {
                lblLB_GetSerNo_Addr.Text = MSG_FAILED;
            }

        }

        private void btnLB_GetModelNumber_Addr_Click(object sender, EventArgs e)
        {
            try
            {
                int addr = currAddr;
                LB_API2_Declarations.MODEL_NUMBER mn = LB_API2_Declarations.MODEL_NUMBER.MNUknwn;
                int rslt = LB_API2_Declarations.LB_GetModelNumber_Addr(addr, ref mn);
                if (rslt >= 1)
                {
                    lblLB_GetModelNumber_Addr.Text = mn.ToString();
                }
                else
                {
                    lblLB_GetModelNumber_Addr.Text = MSG_FAILED;
                }
            }
            catch (Exception)
            {
                lblLB_GetModelNumber_Addr.Text = MSG_FAILED;
            }
        }

        private void btnLB_IsSensorConnected_SN_Click(object sender, EventArgs e)
        {
            try
            {
                string sn = txtLB_IsSensorConnected_SN.Text;
                int rslt = LB_API2_Declarations.LB_IsSensorConnected_SN(sn);
                switch (rslt)
                {
                    case 0:
                        lblLB_IsSensorConnected_SN.Text = "No";
                        break;
                    case 1:
                        lblLB_IsSensorConnected_SN.Text = "Yes";
                        break;
                    default:
                        lblLB_IsSensorConnected_SN.Text = MSG_FAILED;
                        break;
                }
            }
            catch (Exception)
            {
                lblLB_IsSensorConnected_SN.Text = MSG_FAILED;
            }
        }

        private void btnLB_IsSensorConnected_Addr_Click(object sender, EventArgs e)
        {
            try
            {
                int addr = (int)udLB_IsSensorConnected_Addr.Value;
                int rslt = LB_API2_Declarations.LB_IsSensorConnected_Addr(addr);
                switch (rslt)
                {
                    case 0:
                        lblLB_IsSensorConnected_Addr.Text = "No";
                        break;
                    case 1:
                        lblLB_IsSensorConnected_Addr.Text = "Yes";
                        break;
                    default:
                        lblLB_IsSensorConnected_Addr.Text = MSG_FAILED;
                        break;
                }
            }
            catch (Exception)
            {
                lblLB_IsSensorConnected_Addr.Text = MSG_FAILED;
            }
        }

        private void btnLB_InitializeSensor_Idx_Click(object sender, EventArgs e)
        {
            lblLB_InitializeSensor_Idx.Text = MSG_PLEASE_WAIT;
            lblLB_InitializeSensor_Idx.Refresh();
            int rslt = LB_API2_Declarations.LB_InitializeSensor_Idx(currIdx);
            if (rslt >= 1)
                lblLB_InitializeSensor_Idx.Text = MSG_SUCCESS;
            else
                lblLB_InitializeSensor_Idx.Text = MSG_FAILED;
        }

        private void btnLB_InitializeSensor_Addr_Click(object sender, EventArgs e)
        {
            lblLB_InitializeSensor_Addr.Text = MSG_PLEASE_WAIT;
            lblLB_InitializeSensor_Addr.Refresh();
            int rslt = LB_API2_Declarations.LB_InitializeSensor_Addr(currAddr);
            if (rslt >= 1)
                lblLB_InitializeSensor_Addr.Text = MSG_SUCCESS;
            else
                lblLB_InitializeSensor_Addr.Text = MSG_FAILED;
        }

        private void btnLB_InitializeSensor_SN_Click(object sender, EventArgs e)
        {
            try
            {
                lblLB_InitializeSensor_SN.Text = MSG_PLEASE_WAIT;
                lblLB_InitializeSensor_SN.Refresh();
                int rslt = LB_API2_Declarations.LB_InitializeSensor_SN(currSN);
                if (rslt >= 1)
                    lblLB_InitializeSensor_SN.Text = MSG_SUCCESS;
                else
                    lblLB_InitializeSensor_SN.Text = MSG_FAILED;
            }
            catch (Exception)
            {
                lblLB_InitializeSensor_SN.Text = MSG_FAILED;
            }
        }

        private void udIndex_ValueChanged(object sender, EventArgs e)
        {
            currIdx = (int)udIndex.Value;
        }

        private void txtSerNo_TextChanged(object sender, EventArgs e)
        {
            currSN = txtSerNo.Text;
        }

        private void udAddress_ValueChanged(object sender, EventArgs e)
        {
            currAddr = (int)udAddress.Value;
        }

        private void btnLB_SetFrequency_Click(object sender, EventArgs e)
        {
            try
            {
                int addr = currAddr;
                double freq = Convert.ToDouble(txtLB_SetFrequency.Text);
                if (LB_API2_Declarations.LB_SetFrequency(addr, freq) > 0)
                {
                    txtLB_SetFrequency.Text = string.Format("{0:f}", freq);
                    lblLB_SetFrequency.Text = MSG_SUCCESS;
                }
                else
                {
                    txtLB_SetFrequency.Text = MSG_FAILED;
                    lblLB_SetFrequency.Text = MSG_FAILED;
                }
            }
            catch (Exception)
            {
                txtLB_SetFrequency.Text = MSG_FAILED;
                lblLB_SetFrequency.Text = MSG_FAILED;
            }

        }

        private void btnLB_GetFrequency_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            double freq = 0.0;

            if (LB_API2_Declarations.LB_GetFrequency(addr, ref freq) > 0)
            {
                txtLB_GetFrequency.Text = string.Format("{0:f}", freq);
                lblLB_GetFrequency.Text = MSG_SUCCESS;
            }
            else
            {
                txtLB_GetFrequency.Text = "-";
                lblLB_GetFrequency.Text = MSG_FAILED;
            }
        }

        private void btnLB_SetMeasurementPowerUnits_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.PWR_UNITS units = (LB_API2_Declarations.PWR_UNITS)cboLB_SetMeasurementPowerUnits.SelectedIndex;

            if (LB_API2_Declarations.LB_SetMeasurementPowerUnits(addr, units) > 0)
            {
                CURR_UNITS = units;
                lblLB_SetMeasurementPowerUnits.Text = MSG_SUCCESS;
            }
            else
            {
                lblLB_SetMeasurementPowerUnits.Text = MSG_FAILED;
            }
        }

        private void btnLB_GetMeasurementPowerUnits_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.PWR_UNITS units = LB_API2_Declarations.PWR_UNITS.DBM;

            if (LB_API2_Declarations.LB_GetMeasurementPowerUnits(addr, ref units) > 0)
            {
                lblLB_GetMeasurementPowerUnits.Text = "Units = " + units.ToString();
                CURR_UNITS = units;
            }
            else
            {
                lblLB_GetMeasurementPowerUnits.Text = MSG_FAILED;
            }
        }

        private void btnLB_SetAverages_Click(object sender, EventArgs e)
        {
            try
            {
                int addr = currAddr;
                int avgs = Convert.ToInt32(txtLB_SetAverages.Text);

                if (LB_API2_Declarations.LB_SetAverages(addr, avgs) > 0)
                {
                    txtLB_SetAverages.Text = string.Format("{0:D}", avgs);
                    lblLB_SetAverages.Text = MSG_SUCCESS;
                }
                else
                {
                    lblLB_SetAverages.Text = MSG_FAILED;
                }
            }
            catch (Exception)
            {
                txtLB_SetAverages.Text = MSG_FAILED;
            }
        }

        private void btnLB_GetAverages_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int avgs = 1;
            if (LB_API2_Declarations.LB_GetAverages(addr, ref avgs) > 0)
            {
                txtLB_GetAverages.Text = string.Format("{0:D}", avgs);
                lblLB_GetAverages.Text = MSG_SUCCESS;
            }
            else
            {
                lblLB_GetAverages.Text = MSG_FAILED;
            }
        }

        private void btnLB_MeasureCW_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            lblLB_MeasureCW.Text = MSG_PLEASE_WAIT;
            lblLB_MeasureCW.Refresh();
            int rslt = LB_API2_Declarations.LB_InitializeSensor_Addr(addr);
            if (rslt >=1)
            {
                MeasCW();
            }
            else
                lblLB_MeasureCW.Text = MSG_FAILED;
        }

        private void btnLB_MeasurePulse_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            lblLB_MeasurePulse.Text = MSG_PLEASE_WAIT;
            lblLB_MeasurePulse.Refresh();
            int rslt = LB_API2_Declarations.LB_InitializeSensor_Addr(addr);
            if (rslt >= 1)
            {
                MeasPulse();
            }
            else
                lblLB_MeasurePulse.Text = MSG_FAILED;
        }

        private void btnLB_SetLimitEnabled_Click(object sender, EventArgs e)
        {
            try
            {
                LB_API2_Declarations.LIMIT_STYLE ls = LB_API2_Declarations.LIMIT_STYLE.LIMITS_OFF;
                ls = (LB_API2_Declarations.LIMIT_STYLE)cboLB_SetLimitEnabled_State.SelectedIndex;
                int rslt = LB_API2_Declarations.LB_SetLimitEnabled(currAddr, ls);
                if (rslt > 0)
                    lblLB_SetLimitEnabled_Rslt.Text = MSG_SUCCESS;
                else
                    lblLB_SetLimitEnabled_Rslt.Text = MSG_FAILED;
            }
            catch (Exception)
            {
                lblLB_SetLimitEnabled_Rslt.Text = MSG_FAILED;
            }
        }

        private void btnLB_GetLimitEnabled_Click(object sender, EventArgs e)
        {
            try
            {
                LB_API2_Declarations.LIMIT_STYLE ls = LB_API2_Declarations.LIMIT_STYLE.LIMITS_OFF;
                int rslt = LB_API2_Declarations.LB_GetLimitEnabled(currAddr, ref ls);
                if (rslt > 0)
                {
                    lblLB_GetLimitEnabled_State.Text = ls.ToString();
                    lblLB_GetLimitEnabled_Rslt.Text = MSG_SUCCESS;
                }
                else
                    lblLB_GetLimitEnabled_Rslt.Text = MSG_FAILED;
            }
            catch (Exception)
            {
                lblLB_GetLimitEnabled_Rslt.Text = MSG_FAILED;
            }
        }

        private void tmrLB_MeasureCW_Tick(object sender, EventArgs e)
        {
            MeasCW();
        }

        private void tmrLB_MeasurePulse_Tick(object sender, EventArgs e)
        {
            MeasPulse();
        }

        private void cbLB_MeasureCW_CheckedChanged(object sender, EventArgs e)
        {
            tmrLB_MeasureCW.Enabled = (cbLB_MeasureCW.Checked == true);
        }

        private void cbLB_MeasurePulse_CheckedChanged(object sender, EventArgs e)
        {
            tmrLB_MeasurePulse.Enabled = (cbLB_MeasurePulse.Checked == true);
        }

        private void btnSetSingleSidedLimit_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            double singleSidedLimit = (double)udSetSingleSidedLimit.Value;
            LB_API2_Declarations.PWR_UNITS uts = LB_API2_Declarations.PWR_UNITS.DBM;
            LB_API2_Declarations.SS_RULE rule = (LB_API2_Declarations.SS_RULE)cboSetSingleSidedLimit_Rule.SelectedIndex;

            int rtn = LB_API2_Declarations.LB_SetSingleSidedLimit(addr, singleSidedLimit, uts, rule);
            if (rtn > 0)
            {
                lblSetSingleSidedLimit.Text = MSG_SUCCESS;
            }
            else
            {
                lblSetSingleSidedLimit.Text = MSG_FAILED;
            }
        }

        private void btnLB_GetSingleSidedLimit_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            double singleSidedLimit = 0.0;
            LB_API2_Declarations.PWR_UNITS uts = LB_API2_Declarations.PWR_UNITS.DBM;
            LB_API2_Declarations.SS_RULE rule = LB_API2_Declarations.SS_RULE.PASS_GT;

            int rtn = LB_API2_Declarations.LB_GetSingleSidedLimit(addr, ref singleSidedLimit, ref uts, ref rule);
            if (rtn > 0)
            {
                lblGetSingleSidedLimit.Text = string.Format("{0:F2} ", singleSidedLimit) + uts.ToString() + ":  " + rule.ToString();
            }
            else
            {
                lblSetSingleSidedLimit.Text = MSG_FAILED;
            }
        }

        private void btnSetDoubleSidedLimit_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            double LL = (double)udLB_SetDoubleSidedLimit_Lower.Value;
            double UL = (double)udLB_SetDoubleSidedLimit_Upper.Value;
            LB_API2_Declarations.PWR_UNITS uts = LB_API2_Declarations.PWR_UNITS.DBM;
            LB_API2_Declarations.DS_RULE rule = (LB_API2_Declarations.DS_RULE)cboLB_SetDoubleSidedLimit_Rule.SelectedIndex;

            int rtn = LB_API2_Declarations.LB_SetDoubleSidedLimit(addr, LL, UL, uts, rule);
            if (rtn > 0)
            {
                lblLB_SetDoubleSidedLimit.Text = MSG_SUCCESS;
            }
            else
            {
                lblLB_SetDoubleSidedLimit.Text = MSG_FAILED;
            }
        }

        private void btnGetDoubleSidedLimit_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            double LL = 0.0;
            double UL = 0.0;
            LB_API2_Declarations.PWR_UNITS uts = LB_API2_Declarations.PWR_UNITS.DBM;
            LB_API2_Declarations.DS_RULE rule = LB_API2_Declarations.DS_RULE.PASS_BETWEEN_EXC;

            int rtn = LB_API2_Declarations.LB_GetDoubleSidedLimit(addr, ref LL, ref UL, ref uts, ref rule);
            if (rtn > 0)
            {
                lblGetDoubleSidedLimit.Text = "LL: " + string.Format("{0:F2}", LL) +
                                            "  UL: " + string.Format("{0:F2}", UL) +
                                            "  " + uts.ToString() + "  : " + rule.ToString();
                lblGetDoubleSidedLimit_Rslt.Text = MSG_SUCCESS;
            }
            else
            {
                lblGetDoubleSidedLimit.Text = "-";
                lblGetDoubleSidedLimit_Rslt.Text = MSG_FAILED;
            }
        }

        private void btnLB_SetAntiAliasing_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.FEATURE_STATE st = (LB_API2_Declarations.FEATURE_STATE)cboLB_SetAntiAliasing.SelectedIndex;
            int rtn = LB_API2_Declarations.LB_SetAntiAliasingEnabled(addr, st);
            if (rtn > 0)
            {
                lblLB_SetAntiAliasing.Text = MSG_SUCCESS;
            }
            else
            {
                lblLB_SetAntiAliasing.Text = MSG_FAILED;
            }
        }

        private void btnLB_GetAntiAliasing_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.FEATURE_STATE st = LB_API2_Declarations.FEATURE_STATE.ST_OFF;
            int rtn = LB_API2_Declarations.LB_GetAntiAliasingEnabled(addr, ref st);
            if (rtn > 0)
            {
                lblLB_GetAntiAliasing.Text = st.ToString() + ": " + MSG_SUCCESS;
            }
            else
            {
                lblLB_GetAntiAliasing.Text = MSG_FAILED;
            }
        }

        private void btnSetAutoPulseEnabled_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.FEATURE_STATE st = (LB_API2_Declarations.FEATURE_STATE)cboSetAutoPulseEnabled.SelectedIndex;
            int rtn = LB_API2_Declarations.LB_SetAutoPulseEnabled(addr, st);
            if (rtn > 0)
            {
                lblSetAutoPulseEnabled.Text = MSG_SUCCESS;
            }
            else
            {
                lblSetAutoPulseEnabled.Text = MSG_FAILED;
            }
        }

        private void btnGetAutoPulseEnabled_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.FEATURE_STATE st = LB_API2_Declarations.FEATURE_STATE.ST_OFF;
            int rtn = LB_API2_Declarations.LB_GetAutoPulseEnabled(addr, ref st);
            if (rtn > 0)
            {
                lblGetAutoPulseEnabled.Text = st.ToString() + ": " + MSG_SUCCESS;
            }
            else
            {
                lblGetAutoPulseEnabled.Text = MSG_FAILED;
            }
        }

        private void btnLB_SetPulseCriteria_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            double pulseCrit = (double)udLB_SetPulseCriteria.Value;
            int rtn = LB_API2_Declarations.LB_SetPulseCriteria(addr, pulseCrit);
            if (rtn > 0)
                lblLB_SetPulseCriteria.Text = MSG_SUCCESS;
            else
                lblLB_SetPulseCriteria.Text = MSG_FAILED;
       }

        private void btnLB_GetPulseCriteria_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            double pulseCrit = 0.0;
            int rtn = LB_API2_Declarations.LB_GetPulseCriteria(addr, ref pulseCrit);
            if (rtn > 0)
                lblLB_GetPulseCriteria.Text = string.Format("{0:F3} : ", pulseCrit) +  MSG_SUCCESS;
            else
                lblLB_GetPulseCriteria.Text = MSG_FAILED;
       }

       private void btnSetDutyCycleEnabled_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.FEATURE_STATE st = (LB_API2_Declarations.FEATURE_STATE)cboSetDutyCycleEnabled.SelectedIndex;
            int rtn = LB_API2_Declarations.LB_SetDutyCycleEnabled(addr, st);
            if (rtn > 0)
            {
                lblSetDutyCycleEnabled.Text = MSG_SUCCESS;
            }
            else
            {
                lblSetDutyCycleEnabled.Text = MSG_FAILED;
            }
        }

        private void btnGetDutyCycleEnabled_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.FEATURE_STATE st = LB_API2_Declarations.FEATURE_STATE.ST_OFF;
            int rtn = LB_API2_Declarations.LB_GetDutyCycleEnabled(addr, ref st);
            if (rtn > 0)
            {
                lblGetDutyCycleEnabled.Text = st.ToString() + ": " + MSG_SUCCESS;
            }
            else
            {
                lblGetDutyCycleEnabled.Text = MSG_FAILED;
            }
        }

        private void btnSetDutyCyclePerCent_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            double dutyCyclePerCent = (double)udSetDutyCyclePerCent.Value;
            int rtn = LB_API2_Declarations.LB_SetDutyCyclePerCent(addr, dutyCyclePerCent);
            if (rtn > 0)
                lblSetDutyCyclePerCent.Text = MSG_SUCCESS;
            else
                lblSetDutyCyclePerCent.Text = MSG_FAILED;
        }

        private void btnGetDutyCyclePerCent_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            double dutyCyclePerCent = 0.0;
            int rtn = LB_API2_Declarations.LB_GetDutyCyclePerCent(addr, ref dutyCyclePerCent);
            if (rtn > 0)
                lblGetDutyCyclePerCent.Text = string.Format("{0:F3}% :", dutyCyclePerCent) + MSG_SUCCESS;
            else
                lblGetDutyCyclePerCent.Text = MSG_FAILED;
        }

        private void btnLB_SetOffsetEnabled_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.FEATURE_STATE st = (LB_API2_Declarations.FEATURE_STATE)cboLB_SetOffsetEnabled.SelectedIndex;
            int rtn = LB_API2_Declarations.LB_SetOffsetEnabled(addr, st);
            if (rtn > 0)
            {
                lblLB_SetOffsetEnabled.Text = MSG_SUCCESS;
            }
            else
            {
                lblLB_SetOffsetEnabled.Text = MSG_FAILED;
            }
        }

        private void btnLB_GetOffsetEnabled_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.FEATURE_STATE st = LB_API2_Declarations.FEATURE_STATE.ST_OFF;
            int rtn = LB_API2_Declarations.LB_GetOffsetEnabled(addr, ref st);
            if (rtn > 0)
            {
                lblLB_GetOffsetEnabled.Text = st.ToString() + ": " + MSG_SUCCESS;
            }
            else
            {
                lblLB_GetOffsetEnabled.Text = MSG_FAILED;
            }
        }

        private void btnLB_SetOffset_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            double offset = (double)udLB_SetOffset.Value;
            int rtn = LB_API2_Declarations.LB_SetOffset(addr, offset);
            if (rtn > 0)
                lblLB_SetOffset.Text = MSG_SUCCESS;
            else
                lblLB_SetOffset.Text = MSG_FAILED;
        }

        private void btnLB_GetOffset_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            double offset = 0.0;
            int rtn = LB_API2_Declarations.LB_GetOffset(addr, ref offset);
            if (rtn > 0)
                lblLB_GetOffset.Text = string.Format("{0:F3} dB :", offset) + MSG_SUCCESS;
            else
                lblLB_GetOffset.Text = MSG_FAILED;
        }

        private void btnLB_SetCWReference_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            double cwRef = (double)udLB_SetCWReference.Value;
            LB_API2_Declarations.PWR_UNITS uts = LB_API2_Declarations.PWR_UNITS.DBM;
            int rtn = LB_API2_Declarations.LB_SetCWReference(addr, cwRef, uts);
            if (rtn > 0)
                lblLB_SetCWReference.Text = MSG_SUCCESS;
            else
                lblLB_SetCWReference.Text = MSG_FAILED;
        }

        private void btnLB_GetCWReference_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            double cwRef = 0.0;
            LB_API2_Declarations.PWR_UNITS uts = LB_API2_Declarations.PWR_UNITS.DBM;
            int rtn = LB_API2_Declarations.LB_GetCWReference(addr, ref cwRef, ref uts);
            if (rtn > 0)
                lblLB_GetCWReference.Text = string.Format("{0:F3} ", cwRef) + uts.ToString() + ": " + MSG_SUCCESS;
            else
                lblLB_GetCWReference.Text = MSG_FAILED;
        }

        private void btnLB_SetPulseReference_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            // get the reference values as they are currently set so we can put them in the list box
            double pulseRef = 0.0, peakRef = 0.0, averageRef = 0.0, dutyCycleRef = 0.0;
            LB_API2_Declarations.PWR_UNITS pwrUnits = LB_API2_Declarations.PWR_UNITS.DBM;
            int rslt = LB_API2_Declarations.LB_GetPulseReference(addr, ref pulseRef, ref peakRef, ref averageRef, ref dutyCycleRef, ref pwrUnits);
            if (rslt < 0)
            {
                lblLB_SetPulseReference.Text = "Error on get...";
                return;
            }
            RgrTst_SetPulseRef frm = new RgrTst_SetPulseRef();
            frm.Addr = addr;
            frm.Pulse = pulseRef;
            frm.Peak = peakRef;
            frm.Avg = averageRef;
            frm.DCRef = dutyCycleRef;
            frm.ShowDialog();
            if (frm.DialogResult == DialogResult.Cancel)
            {
                lblLB_SetPulseReference.Text = "Cancelled";
                return;
            }
            pulseRef = frm.Pulse;
            peakRef = frm.Peak;
            averageRef = frm.Avg;
            dutyCycleRef = frm.DCRef;
            // set the reference values
            rslt = LB_API2_Declarations.LB_SetPulseReference(addr, pulseRef, peakRef, averageRef, dutyCycleRef, pwrUnits);
            if (rslt < 0)
            {
                lblLB_SetPulseReference.Text = "Error on set...";
                return;
            }
            double chkPulseRef = 0.0, chkPeakRef = 0.0, chkAverageRef = 0.0, chkDutyCycleRef = 0.0;
            LB_API2_Declarations.PWR_UNITS chkPwrUnits = LB_API2_Declarations.PWR_UNITS.DBM;
            // get the reference values again and compare to set values for error checking
            rslt = LB_API2_Declarations.LB_GetPulseReference(addr, ref chkPulseRef, ref chkPeakRef, ref chkAverageRef, ref chkDutyCycleRef, ref chkPwrUnits);
            if (rslt < 0)
            {
                lblLB_SetPulseReference.Text = "Error on get check...";
                return;
            }
            if ((chkPwrUnits == pwrUnits) && (chkPulseRef == pulseRef) && (chkPeakRef == peakRef) && (chkDutyCycleRef == dutyCycleRef) && (chkPwrUnits == pwrUnits))
                lblLB_SetPulseReference.Text = MSG_SUCCESS;
            else
                lblLB_SetPulseReference.Text = MSG_FAILED;
        }

        private void btnLB_SetResponse_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.ResponsePoints[] rsp = new LB_API2_Declarations.ResponsePoints[LB_API2_Declarations.MAX_NUM_RESP_PTS];
            int numPts = LB_API2_Declarations.MAX_NUM_RESP_PTS;
            int rslt = LB_API2_Declarations.LB_GetResponse(addr, ref rsp[0], ref numPts);
            // get the current response...
            RgrTst_SetGetResponse frm = new RgrTst_SetGetResponse();
            frm.Response = rsp;
            frm.ShowDialog();
            if (frm.DialogResult == DialogResult.OK)
            {
                rsp = frm.Response;
                numPts = frm.numPts;
            }
            else
            {
                lblLB_SetResponse.Text = "Cancelled";
                return;
            }
            rslt = LB_API2_Declarations.LB_SetResponse(addr, ref rsp[0], numPts);
            if (rslt>0)
                lblLB_SetResponse.Text = MSG_SUCCESS;
            else
                lblLB_SetResponse.Text = MSG_FAILED;
        }

        private void btnLB_SetResponseEnabled_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.FEATURE_STATE st = (LB_API2_Declarations.FEATURE_STATE)cboLB_SetResponseEnabled.SelectedIndex;
            int rtn = LB_API2_Declarations.LB_SetResponseEnabled(addr, st);
            if (rtn > 0)
            {
                lblLB_SetResponseEnabled.Text = MSG_SUCCESS;
            }
            else
            {
                lblLB_SetResponseEnabled.Text = MSG_FAILED;
            }
        }

        private void btnLB_GetResponseEnabled_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.FEATURE_STATE st = LB_API2_Declarations.FEATURE_STATE.ST_OFF;
            int rtn = LB_API2_Declarations.LB_GetResponseEnabled(addr, ref st);
            if (rtn > 0)
            {
                lblLB_GetResponseEnabled.Text = st.ToString() + ": " + MSG_SUCCESS;
            }
            else
            {
                lblLB_GetResponseEnabled.Text = MSG_FAILED;
            }
        }

        private void btnSetTTLTriggerInEnabled_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.FEATURE_STATE st = (LB_API2_Declarations.FEATURE_STATE)cboSetTTLTriggerInEnabled.SelectedIndex;
            int rtn = LB_API2_Declarations.LB_SetTTLTriggerInEnabled(addr, st);
            if (rtn > 0)
            {
                lblSetTTLTriggerInEnabled.Text = MSG_SUCCESS;
            }
            else
            {
                lblSetTTLTriggerInEnabled.Text = MSG_FAILED;
            }
        }

        private void btnGetTTLTriggerInEnabled_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.FEATURE_STATE st = LB_API2_Declarations.FEATURE_STATE.ST_OFF;
            int rtn = LB_API2_Declarations.LB_GetTTLTriggerInEnabled(addr, ref st);
            if (rtn > 0)
            {
                lblGetTTLTriggerInEnabled.Text = st.ToString() + ": " + MSG_SUCCESS;
            }
            else
            {
                lblGetTTLTriggerInEnabled.Text = MSG_FAILED;
            }
        }

        private void btnSetTTLTriggerInInverted_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.FEATURE_STATE st = (LB_API2_Declarations.FEATURE_STATE)cboSetTTLTriggerInInverted.SelectedIndex;
            int rtn = LB_API2_Declarations.LB_SetTTLTriggerInInverted(addr, st);
            if (rtn > 0)
            {
                lblSetTTLTriggerInInverted.Text = MSG_SUCCESS;
            }
            else
            {
                lblSetTTLTriggerInInverted.Text = MSG_FAILED;
            }
        }

        private void btnGetTTLTriggerInInverted_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.FEATURE_STATE st = LB_API2_Declarations.FEATURE_STATE.ST_OFF;
            int rtn = LB_API2_Declarations.LB_GetTTLTriggerInInverted(addr, ref st);
            if (rtn > 0)
            {
                lblGetTTLTriggerInInverted.Text = st.ToString() + ": " + MSG_SUCCESS;
            }
            else
            {
                lblGetTTLTriggerInInverted.Text = MSG_FAILED;
            }
        }

        private void btnSetTTLTriggerOutEnabled_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.FEATURE_STATE st = (LB_API2_Declarations.FEATURE_STATE)cboSetTTLTriggerOutEnabled.SelectedIndex;
            int rtn = LB_API2_Declarations.LB_SetTTLTriggerOutEnabled(addr, st);
            if (rtn > 0)
            {
                lblSetTTLTriggerOutEnabled.Text = MSG_SUCCESS;
            }
            else
            {
                lblSetTTLTriggerOutEnabled.Text = MSG_FAILED;
            }
        }

        private void btnGetTTLTriggerOutEnabled_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.FEATURE_STATE st = LB_API2_Declarations.FEATURE_STATE.ST_OFF;
            int rtn = LB_API2_Declarations.LB_GetTTLTriggerOutEnabled(addr, ref st);
            if (rtn > 0)
            {
                lblGetTTLTriggerOutEnabled.Text = st.ToString() + ": " + MSG_SUCCESS;
            }
            else
            {
                lblGetTTLTriggerOutEnabled.Text = MSG_FAILED;
            }
        }

        private void btnSetTTLTriggerOutInverted_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.FEATURE_STATE st = (LB_API2_Declarations.FEATURE_STATE)cboSetTTLTriggerOutInverted.SelectedIndex;
            int rtn = LB_API2_Declarations.LB_SetTTLTriggerOutInverted(addr, st);
            if (rtn > 0)
            {
                lblSetTTLTriggerOutInverted.Text = MSG_SUCCESS;
            }
            else
            {
                lblSetTTLTriggerOutInverted.Text = MSG_FAILED;
            }
        }

        private void btnGetTTLTriggerOutInverted_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.FEATURE_STATE st = LB_API2_Declarations.FEATURE_STATE.ST_OFF;
            int rtn = LB_API2_Declarations.LB_GetTTLTriggerOutInverted(addr, ref st);
            if (rtn > 0)
            {
                lblGetTTLTriggerOutInverted.Text = st.ToString() + ": " + MSG_SUCCESS;
            }
            else
            {
                lblGetTTLTriggerOutInverted.Text = MSG_FAILED;
            }
        }

        private void btnSetTTLTriggerInTimeOut_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int tmOut = (int)udSetTTLTriggerInTimeOut.Value;
            int rtn = LB_API2_Declarations.LB_SetTTLTriggerInTimeOut(addr, tmOut);
            if (rtn > 0)
                lblSetTTLTriggerInTimeOut.Text = MSG_SUCCESS;
            else
                lblSetTTLTriggerInTimeOut.Text = MSG_FAILED;

        }

        private void btnGetTTLTriggerInTimeOut_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int tmOut = 0;
            int rtn = LB_API2_Declarations.LB_GetTTLTriggerInTimeOut(addr, ref tmOut);
            if (rtn > 0)
                lblGetTTLTriggerInTimeOut.Text = string.Format("{0:D} msec :", tmOut) + MSG_SUCCESS;
            else
                lblGetTTLTriggerInTimeOut.Text = MSG_FAILED;
        }

        private void btnSetRecorderOutSetup_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            double ro0V = (double)udSetRecorderOutSetup_Zero.Value;
            double ro1V = (double)udSetRecorderOutSetup_One.Value;
            int rtn = LB_API2_Declarations.LB_SetRecorderOutSetup(addr, ro0V, ro1V, LB_API2_Declarations.PWR_UNITS.DBM);
            if (rtn > 0)
                lblSetRecorderOutSetup.Text = MSG_SUCCESS;
            else
                lblSetRecorderOutSetup.Text = MSG_FAILED;
        }

        private void btnGetRecorderOutSetup_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            double ro0V = 0.0;
            double ro1V = 0.0;
            LB_API2_Declarations.PWR_UNITS uts = LB_API2_Declarations.PWR_UNITS.DBM;
            int rtn = LB_API2_Declarations.LB_GetRecorderOutSetup(addr, ref ro0V, ref ro1V, ref uts);
            if (rtn > 0)
                lblGetRecorderOutSetup.Text = string.Format("{0:F3} :", ro0V) +
                                                string.Format("{0:F3} :", ro1V) + 
                                                uts.ToString() + ": " + MSG_SUCCESS;
            else
                lblGetRecorderOutSetup.Text = MSG_FAILED;
        }

        private void btnSetRecorderOutEnabled_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.FEATURE_STATE st = (LB_API2_Declarations.FEATURE_STATE)cboSetRecorderOutEnabled.SelectedIndex;
            int rtn = LB_API2_Declarations.LB_SetRecorderOutEnabled(addr, st);
            if (rtn > 0)
            {
                lblSetRecorderOutEnabled.Text = MSG_SUCCESS;
            }
            else
            {
                lblSetRecorderOutEnabled.Text = MSG_FAILED;
            }
        }

        private void btnGetRecorderOutEnabled_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.FEATURE_STATE st = LB_API2_Declarations.FEATURE_STATE.ST_OFF;
            int rtn = LB_API2_Declarations.LB_GetRecorderOutEnabled(addr, ref st);
            if (rtn > 0)
            {
                lblGetRecorderOutEnabled.Text = st.ToString() + " : " + MSG_SUCCESS;
            }
            else
            {
                lblGetRecorderOutEnabled.Text = MSG_FAILED;
            }
        }

        private void btnLB_DriverVersion_Click(object sender, EventArgs e)
        {
            int rtn = LB_API2_Declarations.LB_DriverVersion();
            lblLB_DriverVersion.Text = rtn.ToString();
        }
    }
}