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;
        long diagSweepPassCntr = 0;
        long diagSweepFailCntr = 0;
        bool increaseSweepTime = true;

        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 btnPPInitializeSensor_Addr_Click(object sender, EventArgs e)
        {
            lblPPInitializeSensor_Addr.Text = MSG_PLEASE_WAIT;
            lblPPInitializeSensor_Addr.Refresh();
            int rslt = LB_API2_Declarations.LB_InitializeSensor_Addr(currAddr);
            if (rslt >= 1)
                lblPPInitializeSensor_Addr.Text = MSG_SUCCESS;
            else
                lblPPInitializeSensor_Addr.Text = MSG_FAILED;
        }

        private void btnPP_GetTraceLength_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int rslt = LB_API2_Declarations.PP_GetTraceLength(addr);
            if (rslt < 1)
            {
                lblPP_GetTraceLength.Text = MSG_FAILED;
                lblPP_GetTraceLength_Tr.Text = MSG_FAILED;
            }
            else
            {
                lblPP_GetTraceLength.Text = rslt.ToString() + " : " + MSG_SUCCESS;
                lblPP_GetTraceLength_Tr.Text = rslt.ToString() + " : " + MSG_SUCCESS;
            }
        }

        private void btnPP_GetTraceAvgs_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int avgs = 0;
            int rslt = LB_API2_Declarations.PP_GetTraceAvgs(addr, ref avgs);
            if (rslt<1)
                lblPP_GetTraceAvgs.Text = MSG_FAILED;
            else
                lblPP_GetTraceAvgs.Text = avgs.ToString() + " : " + MSG_SUCCESS;
        }

        private void btnPP_GetAvgMode_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.AVG_MODE avgMode = LB_API2_Declarations.AVG_MODE.AVG_OFF;
            int rslt = LB_API2_Declarations.PP_GetAvgMode(addr, ref avgMode);
            if (rslt < 1)
                lblPP_GetAvgMode.Text = MSG_FAILED;
            else
                lblPP_GetAvgMode.Text = avgMode.ToString() + " : " + MSG_SUCCESS;
        }

        private void btnPP_GetTriggerLevel_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            double trgLvl = 0.0;
            int rslt = LB_API2_Declarations.PP_GetTriggerLevel(addr, ref trgLvl);
            if (rslt < 1)
                lblPP_GetTriggerLevel.Text = MSG_FAILED;
            else
                lblPP_GetTriggerLevel.Text = string.Format("{0:F3} dBm : ", trgLvl) + MSG_SUCCESS;
        }

        private void bntPP_GetTriggerSoure_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.TRIGGER_SOURCE trgSrc = LB_API2_Declarations.TRIGGER_SOURCE.INT_AUTO_LEVEL;
            int rslt = LB_API2_Declarations.PP_GetTriggerSoure(addr, ref trgSrc);
            if (rslt < 1)
                lblPP_GetTriggerSoure.Text = MSG_FAILED;
            else
                lblPP_GetTriggerSoure.Text = trgSrc.ToString() + " : " + MSG_SUCCESS;
        }

        private void btnPP_GetFilter_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.FLT_CO_FREQ fltCOFreq = LB_API2_Declarations.FLT_CO_FREQ.FLT_UNK;
            int rslt = LB_API2_Declarations.PP_GetFilter(addr, ref fltCOFreq);
            if (rslt < 1)
                lblPP_GetFilter.Text = MSG_FAILED;
            else
                lblPP_GetFilter.Text = fltCOFreq.ToString() + " : " + MSG_SUCCESS;
        }

        private void btnPP_GetPoles_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.FLT_POLES fltPoles = LB_API2_Declarations.FLT_POLES.ONE_POLE;
            int rslt = LB_API2_Declarations.PP_GetPoles(addr, ref fltPoles);
            if (rslt < 1)
                lblPP_GetPoles.Text = MSG_FAILED;
            else
                lblPP_GetPoles.Text = fltPoles.ToString() + " : " + MSG_SUCCESS;
        }

        private void btnPP_GetSweepDelay_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int swpDelay = 0;
            int rslt = LB_API2_Declarations.PP_GetSweepDelay(addr, ref swpDelay);
            if (rslt < 1)
                lblPP_GetSweepDelay.Text = MSG_FAILED;
            else
                lblPP_GetSweepDelay.Text = swpDelay.ToString() + " : " + MSG_SUCCESS;
        }

        private void btnGetSweepDelayMode_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int swpDelayMode = 0;
            int rslt = LB_API2_Declarations.PP_GetSweepDelayMode(addr, ref swpDelayMode);
            if (rslt < 1)
                lblGetSweepDelayMode.Text = MSG_FAILED;
            else
            {
                if (swpDelayMode == 0)
                    lblGetSweepDelayMode.Text = "OFF : " + MSG_SUCCESS;
                else
                    lblGetSweepDelayMode.Text = "ON : " + MSG_SUCCESS;
            }
        }

        private void btnGetSweepTime_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int swpTm = 0;
            int rslt = LB_API2_Declarations.PP_GetSweepTime(addr, ref swpTm);
            if (rslt < 1)
                lblGetSweepTime.Text = MSG_FAILED;
            else
                lblGetSweepTime.Text = swpTm.ToString() + " usec : " + MSG_SUCCESS;
        }

        private void btnPP_SetTraceAvgs_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int trcAvgs = (int)udPP_SetTraceAvgs_Avgs.Value;
            int rslt = LB_API2_Declarations.PP_SetTraceAvgs(addr, trcAvgs);
            if (rslt < 1)
                lblPP_SetTraceAvgs.Text = MSG_FAILED;
            else
                lblPP_SetTraceAvgs.Text = MSG_SUCCESS;
        }

        private void btnPP_SetAvgMode_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.AVG_MODE avgMode = (LB_API2_Declarations.AVG_MODE)cboPP_SetAvgMode.SelectedIndex;
            int rslt = LB_API2_Declarations.PP_SetAvgMode(addr, avgMode);
            if (rslt < 1)
                lblPP_SetAvgMode.Text = MSG_FAILED;
            else
                lblPP_SetAvgMode.Text = MSG_SUCCESS;
        }

        private void btnPP_SetTriggerLevel_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            double trgLvl = (double)udPP_SetTriggerLevel_Level.Value;
            int rslt = LB_API2_Declarations.PP_SetTriggerLevel(addr, trgLvl);
            if (rslt < 1)
                lblPP_SetTriggerLevel.Text = MSG_FAILED;
            else
                lblPP_SetTriggerLevel.Text = MSG_SUCCESS;
        }

        private void btnPP_SetTriggerSoure_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.TRIGGER_SOURCE trgScr = (LB_API2_Declarations.TRIGGER_SOURCE)cboPP_SetTriggerSoure.SelectedIndex;
            int rslt = LB_API2_Declarations.PP_SetTriggerSoure(addr, trgScr);
            if (rslt < 1)
                lblPP_SetTriggerSoure.Text = MSG_FAILED;
            else
                lblPP_SetTriggerSoure.Text = MSG_SUCCESS;
        }

        private void btnPP_SetSweepTime_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            if (cboSetSweepTime.SelectedIndex<0)
            {
                MessageBox.Show("Select a sweep time before attempting to set the sweep time");
                return;
            }
            int swpTm = Convert.ToInt32(cboSetSweepTime.Items[cboSetSweepTime.SelectedIndex]);
            int rslt = LB_API2_Declarations.PP_SetSweepTime(addr, swpTm);
            if (rslt < 1)
                lblSetSweepTime.Text = MSG_FAILED;
            else
                lblSetSweepTime.Text = MSG_SUCCESS;
        }

        private void btnPP_SetFilter_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.FLT_CO_FREQ fltrIdx = (LB_API2_Declarations.FLT_CO_FREQ)(cboPP_SetFilter.SelectedIndex+1);
            int rslt = LB_API2_Declarations.PP_SetFilter(addr, fltrIdx);
            if (rslt < 1)
                lblPP_SetFilter.Text = MSG_FAILED;
            else
                lblPP_SetFilter.Text = MSG_SUCCESS;
        }

        private void btnPP_SetPoles_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.FLT_POLES fltrPoles = (LB_API2_Declarations.FLT_POLES)cboPP_SetPoles.SelectedIndex;
            int rslt = LB_API2_Declarations.PP_SetPoles(addr, fltrPoles);
            if (rslt < 1)
                lblPP_SetPoles.Text = MSG_FAILED;
            else
                lblPP_SetPoles.Text = MSG_SUCCESS;
        }

        private void btnPP_SetSweepDelay_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int swpDly = (int)udPP_SetSweepDelay_Delay.Value;
            int rslt = LB_API2_Declarations.PP_SetSweepDelay(addr,swpDly);
            if (rslt < 1)
                lblPP_SetSweepDelay.Text = MSG_FAILED;
            else
                lblPP_SetSweepDelay.Text = MSG_SUCCESS;
        }

        private void btnSetSweepDelayMode_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int swpDlyMode = (int)cboSetSweepDelayMode.SelectedIndex;
            int rslt = LB_API2_Declarations.PP_SetSweepDelayMode(addr, swpDlyMode);
            if (rslt < 1)
                lblSetSweepDelayMode.Text = MSG_FAILED;
            else
                lblSetSweepDelayMode.Text = MSG_SUCCESS;
        }

        private void btnPP_GetTrace_Click(object sender, EventArgs e)
        {
            int addr = currAddr;

            sb = new StringBuilder();
            lblPP_GetTrace.Text = string.Format(MSG_SENSOR_TRYING_TO_INITALIZE, addr);
            lblPP_GetTrace.Refresh();
            if (LB_API2_Declarations.LB_IsSensorConnected_Addr(addr) >= 1)
            {
                if (LB_API2_Declarations.LB_InitializeSensor_Addr(addr) >= 1)
                {
                    int trLen = LB_API2_Declarations.PP_GetTraceLength(addr);
                    if (trLen > 0)
                    {
                        int trUsed = 0;
                        int rslt;
                        
                        trAry = new double[trLen];
                        rslt = LB_API2_Declarations.PP_GetTrace(addr, ref trAry[0], trLen, ref trUsed);
                        if (rslt >= 1)
                        {
                            lblPP_GetTrace.Text = string.Format(MSG_GET_TRACE_SUCCEEDED, addr, trLen, trUsed);
                            lstPP_GetTrace.Items.Clear();
                            lstPP_GetTrace.Items.Add("  Idx      Amp(dBm)");
                            for (int i = 0; i < trUsed; i++)
                            {
                                lstPP_GetTrace.Items.Add(string.Format("{0,6:d}{1,12:F3}", i, trAry[i]));
                                sb.Append(i);
                                sb.Append('\t');
                                sb.Append(trAry[i]);
                                sb.Append('\n');
                            }
                        }
                        else
                        {
                            lblPP_GetTrace.Text = string.Format(MSG_GET_TRACE_ERROR, addr);
                        }
                    }
                    else
                    {
                        lblPP_GetTrace.Text = string.Format(MSG_TRACE_LENGTH_ERROR, addr);
                    }
                }
                else
                {
                    lblPP_GetTrace.Text = string.Format(MSG_SENSOR_FAILED_INITIALIZATION, addr);
                }
            }
            else
            {
                lblPP_GetTrace.Text = string.Format(MSG_SENSOR_IS_NOT_CONNECTED, addr);
            }
        }

        private void btnCopy_Click_1(object sender, EventArgs e)
        {
            try
            {
                Clipboard.Clear();
                if (!Clipboard.ContainsText())
                    Clipboard.SetText(sb.ToString());
            }
            catch (Exception)
            {
                MessageBox.Show("Unable to copy to clipboard", "Copy", MessageBoxButtons.OK);
            }
        }

        private void btnPP_CurrTrace2AnalysisTrace_Click(object sender, EventArgs e)
        {
            double[] tr = new double[10001]; // By setting a = NULL and trLen = -1 the
                                            // trace would not be handed back. We are
                                            // sending an array and the lenght of the 
                                            // array...so we'll get the data back. This
                                            // is for demonstration purposes since we
                                            // are clearly not using the trace data 
                                            // returned in the array.
            int trLen = 10000;
            int addr = currAddr;
            int rslt = LB_API2_Declarations.PP_CurrTrace2AnalysisTrace(addr, ref tr[0], trLen);
            if (rslt > 0)
                lblPP_CurrTrace2AnalysisTrace.Text = MSG_SUCCESS;
            else
                lblPP_CurrTrace2AnalysisTrace.Text = MSG_FAILED;
        }

        private void btnPP_CurrTrace2AnalysisTrace_Mrks_Click(object sender, EventArgs e)
        {
            double a = 0;
            int addr = currAddr;
            int rslt = LB_API2_Declarations.PP_CurrTrace2AnalysisTrace(addr, ref a, -1);
            if (rslt > 0)
            {
                double pkExCrit = (double)udPP_GetPeaks_Idx_PkCrit_Mkrs.Value;
                double measThresh = (double)udPP_GetPeaks_Idx_MeasThresh_Mkrs.Value;
                rslt = LB_API2_Declarations.LB_SetPulseCriteria(addr, pkExCrit);
                if (rslt < 0)
                    lblPP_CurrTrace2AnalysisTrace_Mrks.Text = MSG_FAILED;
                rslt = LB_API2_Declarations.PP_SetMeasurementThreshold(addr, measThresh);
                if (rslt < 0)
                    lblPP_CurrTrace2AnalysisTrace_Mrks.Text = MSG_FAILED;
                else
                    lblPP_CurrTrace2AnalysisTrace_Mrks.Text = MSG_SUCCESS;
            }
            else
                lblPP_CurrTrace2AnalysisTrace_Mrks.Text = MSG_FAILED;
        }

        private void btnPP_GetPeaks_Idx_Click(object sender, EventArgs e)
        {
            int pksUsed = 0;
            int maxPks = 3000;
            int addr = currAddr;
            double orgPulseCrit = 0.0;
            double orgMeasThresh = 0.0;
            double newPulseCrit = (double)udPP_GetPeaks_Idx_PkCrit_Mkrs.Value;
            double newMeasThresh = (double)udPP_GetPeaks_Idx_MeasThresh_Mkrs.Value;
            lstPP_GetPeaks_Idx.Items.Clear();
            lblPP_GetPeaks_Idx_Count.Text = MSG_COUNT_UNKNOWN;
            LB_API2_Declarations.Peak[] pks = new LB_API2_Declarations.Peak[maxPks];
            int rslt = LB_API2_Declarations.LB_GetPulseCriteria(addr, ref orgPulseCrit);
            rslt = LB_API2_Declarations.LB_SetPulseCriteria(addr, newPulseCrit);
            rslt = LB_API2_Declarations.PP_GetMeasurementThreshold(addr, ref orgMeasThresh);
            rslt = LB_API2_Declarations.PP_SetMeasurementThreshold(addr, newMeasThresh);
            rslt = LB_API2_Declarations.PP_GetPeaks_Idx(addr, ref pks[0], maxPks, ref pksUsed);
            if (pksUsed != 0)
            {
                for (int j = 0; j < pksUsed; j++) lstPP_GetPeaks_Idx.Items.Add(string.Format(FMT_INDEX_POWER, pks[j].trIdx, pks[j].value));
                lblPP_GetPeaks_Idx_Count.Text = string.Concat(MSG_COUNT_KNOWN, pksUsed.ToString());
            }
            else lblPP_GetPeaks_Idx_Count.Text = MSG_COUNT_UNKNOWN;
        }

        private void btnPP_GetPeaksFromTr_Val_Click(object sender, EventArgs e)
        {
            int pksUsed = 0;
            int maxPks = 3000;
            int addr = currAddr;
            double orgPulseCrit = 0.0;
            double orgMeasThresh = 0.0;
            double chkMeasThresh = 0.0;
            double newPulseCrit = (double)udPP_GetPeaks_Idx_PkCrit_Mkrs.Value;
            double newMeasThresh = (double)udPP_GetPeaks_Idx_MeasThresh_Mkrs.Value;
            lstPP_GetPeaks_Val.Items.Clear();
            lblPP_GetPeaks_Val_Count.Text = MSG_COUNT_UNKNOWN;
            LB_API2_Declarations.Peak[] pks = new LB_API2_Declarations.Peak[maxPks];
            int rslt = LB_API2_Declarations.LB_GetPulseCriteria(addr, ref orgPulseCrit);
            rslt = LB_API2_Declarations.LB_SetPulseCriteria(addr, newPulseCrit);

            rslt = LB_API2_Declarations.PP_GetMeasurementThreshold(addr, ref orgMeasThresh);
            rslt = LB_API2_Declarations.PP_SetMeasurementThreshold(addr, newMeasThresh);
            rslt = LB_API2_Declarations.PP_GetMeasurementThreshold(addr, ref chkMeasThresh);
            if (chkMeasThresh != newMeasThresh)
            {
                MessageBox.Show("Error in setting measurement threshhold");
            }

            rslt = LB_API2_Declarations.PP_GetPeaks_Val(addr, ref pks[0], maxPks, ref pksUsed);
            if (pksUsed != 0)
            {
                for (int j = 0; j < pksUsed; j++) lstPP_GetPeaks_Val.Items.Add(string.Format(FMT_INDEX_POWER, pks[j].trIdx, pks[j].value));
                lblPP_GetPeaks_Val_Count.Text = string.Concat(MSG_COUNT_KNOWN, pksUsed.ToString());
            }
            else lblPP_GetPeaks_Val_Count.Text = MSG_COUNT_UNKNOWN;
        }

        private void btnPP_GetAnalysisTraceLength_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int rslt = LB_API2_Declarations.PP_GetAnalysisTraceLength(addr);
            if (rslt < 1)
            {
                lblPP_GetAnalysisTraceLength.Text = MSG_FAILED;
            }
            else
            {
                lblPP_GetAnalysisTraceLength.Text = rslt.ToString() + " : " + MSG_SUCCESS;
            }
        }

        private void btnPP_SetMarkerMode_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int idx = currMarkerIdx;
            LB_API2_Declarations.MARKER_MODE mode = (LB_API2_Declarations.MARKER_MODE)cboPP_SetMarkerMode.SelectedIndex;
            int rslt = LB_API2_Declarations.PP_SetMarkerMode(addr, idx, mode);
            if (rslt >= 1)
                lblPP_SetMarkerMode.Text = MSG_SUCCESS;
            else lblPP_SetMarkerMode.Text = MSG_FAILED;
        }

        private void udMkrIdx_ValueChanged(object sender, EventArgs e)
        {
            currMarkerIdx = (int)udMkrIdx.Value;
        }

        private void btnPP_GetMarkerMode_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int idx = currMarkerIdx;
            LB_API2_Declarations.MARKER_MODE mode = LB_API2_Declarations.MARKER_MODE.MKR_OFF;
            int rslt = LB_API2_Declarations.PP_GetMarkerMode(addr, idx, ref mode);
            if (rslt >= 1)
                lblPP_GetMarkerMode.Text = cboPP_SetMarkerMode.Items[(int)mode].ToString();
            else lblPP_GetMarkerMode.Text = MSG_FAILED;
        }

        private void btnPP_MarkerToPk_Click(object sender, EventArgs e)
        {
            long pkIdx = 0;
            double amp = 0.0, time = 0.0;
            int addr = currAddr;
            int idx = currMarkerIdx;
            int rslt = LB_API2_Declarations.PP_MarkerToPk(addr, idx);
            if (rslt > 0)
            {
                pkIdx = rslt;
                rslt = LB_API2_Declarations.PP_GetMarkerAmp(addr, idx, ref amp);
                if (rslt > 0)
                {
                    rslt = LB_API2_Declarations.PP_GetMarkerPositionTime(addr, idx, ref time);
                    if (rslt > 0)
                    {
                        lblPP_MarkerToPk.Text = "Idx = " + pkIdx.ToString() + "  " + amp.ToString("##0.0## dBm") + "  " + time.ToString("##0.0##E+00");
                        tt.SetToolTip(lblPP_MarkerToPk, 
                            "Idx = " + pkIdx.ToString() + "\nAmp = " + amp.ToString("##0.0## dBm") + " \nTime = " + time.ToString("##0.0##E+00 sec"));
                    }
                    else lblPP_MarkerToPk.Text = MSG_FAILED;
                }
                else lblPP_MarkerToPk.Text = MSG_FAILED;
            }
            else lblPP_MarkerToPk.Text = MSG_FAILED;
        }

        private void btnPP_MarkerToLowestPk_Click(object sender, EventArgs e)
        {
            long pkIdx = 0;
            double amp = 0.0, time = 0.0;
            int addr = currAddr;
            int idx = currMarkerIdx;
            int rslt = LB_API2_Declarations.PP_MarkerToLowestPk(addr, idx);
            if (rslt > 0)
            {
                pkIdx = rslt;
                rslt = LB_API2_Declarations.PP_GetMarkerAmp(addr, idx, ref amp);
                if (rslt > 0)
                {
                    rslt = LB_API2_Declarations.PP_GetMarkerPositionTime(addr, idx, ref time);
                    if (rslt > 0)
                    {
                        lblPP_MarkerToLowestPk.Text = "Idx = " + pkIdx.ToString() + "  " + amp.ToString("##0.0## dBm") + "  " + time.ToString("##0.0##E+00");
                        tt.SetToolTip(lblPP_MarkerToLowestPk,
                            "Idx = " + pkIdx.ToString() + "\nAmp = " + amp.ToString("##0.0## dBm") + " \nTime = " + time.ToString("##0.0##E+00 sec"));
                    }
                    else lblPP_MarkerToLowestPk.Text = MSG_FAILED;
                }
                else lblPP_MarkerToLowestPk.Text = MSG_FAILED;
            }
            else lblPP_MarkerToLowestPk.Text = MSG_FAILED;
        }

        private void btnPP_MarkerPkLower_Click(object sender, EventArgs e)
        {
            long pkIdx = 0;
            double amp = 0.0, time = 0.0;
            int addr = currAddr;
            int idx = currMarkerIdx;
            int rslt = LB_API2_Declarations.PP_MarkerPkLower(addr, idx);
            if (rslt > 0)
            {
                pkIdx = rslt;
                rslt = LB_API2_Declarations.PP_GetMarkerAmp(addr, idx, ref amp);
                if (rslt > 0)
                {
                    rslt = LB_API2_Declarations.PP_GetMarkerPositionTime(addr, idx, ref time);
                    if (rslt > 0)
                    {
                        lblPP_MarkerPkLower.Text = "Idx = " + pkIdx.ToString() + "  " + amp.ToString("##0.0## dBm") + "  " + time.ToString("##0.0##E+00");
                        tt.SetToolTip(lblPP_MarkerPkLower,
                            "Idx = " + pkIdx.ToString() + "\nAmp = " + amp.ToString("##0.0## dBm") + " \nTime = " + time.ToString("##0.0##E+00 sec"));
                    }
                    else lblPP_MarkerPkLower.Text = MSG_FAILED;
                }
                else lblPP_MarkerPkLower.Text = MSG_FAILED;
            }
            else lblPP_MarkerPkLower.Text = MSG_FAILED;
        }

        private void btnPP_MarkerPkHigher_Click(object sender, EventArgs e)
        {
            long pkIdx = 0;
            double amp = 0.0, time = 0.0;
            int addr = currAddr;
            int idx = currMarkerIdx;
            int rslt = LB_API2_Declarations.PP_MarkerPkHigher(addr, idx);
            if (rslt > 0)
            {
                pkIdx = rslt;
                rslt = LB_API2_Declarations.PP_GetMarkerAmp(addr, idx, ref amp);
                if (rslt > 0)
                {
                    rslt = LB_API2_Declarations.PP_GetMarkerPositionTime(addr, idx, ref time);
                    if (rslt > 0)
                    {
                        lblPP_MarkerPkHigher.Text = "Idx = " + pkIdx.ToString() + "  " + amp.ToString("##0.0## dBm") + "  " + time.ToString("##0.0##E+00");
                        tt.SetToolTip(lblPP_MarkerPkHigher,
                            "Idx = " + pkIdx.ToString() + "\nAmp = " + amp.ToString("##0.0## dBm") + " \nTime = " + time.ToString("##0.0##E+00 sec"));
                    }
                    else lblPP_MarkerPkHigher.Text = MSG_FAILED;
                }
                else lblPP_MarkerPkHigher.Text = MSG_FAILED;
            }
            else lblPP_MarkerPkHigher.Text = MSG_FAILED;
        }

        private void btnPP_MarkerToFirstPk_Click(object sender, EventArgs e)
        {
            long pkIdx = 0;
            double amp = 0.0, time = 0.0;
            int addr = currAddr;
            int idx = currMarkerIdx;
            int rslt = LB_API2_Declarations.PP_MarkerToFirstPk(addr, idx);
            if (rslt > 0)
            {
                pkIdx = rslt;
                rslt = LB_API2_Declarations.PP_GetMarkerAmp(addr, idx, ref amp);
                if (rslt > 0)
                {
                    rslt = LB_API2_Declarations.PP_GetMarkerPositionTime(addr, idx, ref time);
                    if (rslt > 0)
                    {
                        lblPP_MarkerToFirstPk.Text = "Idx = " + pkIdx.ToString() + "  " + amp.ToString("##0.0## dBm") + "  " + time.ToString("##0.0##E+00");
                        tt.SetToolTip(lblPP_MarkerToFirstPk,
                            "Idx = " + pkIdx.ToString() + "\nAmp = " + amp.ToString("##0.0## dBm") + " \nTime = " + time.ToString("##0.0##E+00 sec"));
                    }
                    else lblPP_MarkerToFirstPk.Text = MSG_FAILED;
                }
                else lblPP_MarkerToFirstPk.Text = MSG_FAILED;
            }
            else lblPP_MarkerToFirstPk.Text = MSG_FAILED;
        }

        private void btnPP_MarkerToLastPk_Click(object sender, EventArgs e)
        {
            long pkIdx = 0;
            double amp = 0.0, time = 0.0;
            int addr = currAddr;
            int idx = currMarkerIdx;
            int rslt = LB_API2_Declarations.PP_MarkerToLastPk(addr, idx);
            if (rslt > 0)
            {
                pkIdx = rslt;
                rslt = LB_API2_Declarations.PP_GetMarkerAmp(addr, idx, ref amp);
                if (rslt > 0)
                {
                    rslt = LB_API2_Declarations.PP_GetMarkerPositionTime(addr, idx, ref time);
                    if (rslt > 0)
                    {
                        lblPP_MarkerToLastPk.Text = "Idx = " + pkIdx.ToString() + "  " + amp.ToString("##0.0## dBm") + "  " + time.ToString("##0.0##E+00");
                        tt.SetToolTip(lblPP_MarkerToLastPk,
                            "Idx = " + pkIdx.ToString() + "\nAmp = " + amp.ToString("##0.0## dBm") + " \nTime = " + time.ToString("##0.0##E+00 sec"));
                    }
                    else lblPP_MarkerToLastPk.Text = MSG_FAILED;
                }
                else lblPP_MarkerToLastPk.Text = MSG_FAILED;
            }
            else lblPP_MarkerToLastPk.Text = MSG_FAILED;
        }

        private void btnPP_MarkerPrevPk_Click(object sender, EventArgs e)
        {
            long pkIdx = 0;
            double amp = 0.0, time = 0.0;
            int addr = currAddr;
            int idx = currMarkerIdx;
            int rslt = LB_API2_Declarations.PP_MarkerPrevPk(addr, idx);
            if (rslt > 0)
            {
                pkIdx = rslt;
                rslt = LB_API2_Declarations.PP_GetMarkerAmp(addr, idx, ref amp);
                if (rslt > 0)
                {
                    rslt = LB_API2_Declarations.PP_GetMarkerPositionTime(addr, idx, ref time);
                    if (rslt > 0)
                    {
                        lblPP_MarkerPrevPk.Text = "Idx = " + pkIdx.ToString() + "  " + amp.ToString("##0.0## dBm") + "  " + time.ToString("##0.0##E+00");
                        tt.SetToolTip(lblPP_MarkerPrevPk,
                            "Idx = " + pkIdx.ToString() + "\nAmp = " + amp.ToString("##0.0## dBm") + " \nTime = " + time.ToString("##0.0##E+00 sec"));
                    }
                    else lblPP_MarkerPrevPk.Text = MSG_FAILED;
                }
                else lblPP_MarkerPrevPk.Text = MSG_FAILED;
            }
            else lblPP_MarkerPrevPk.Text = MSG_FAILED;
        }

        private void btnPP_MarkerNextPk_Click(object sender, EventArgs e)
        {
            long pkIdx = 0;
            double amp = 0.0, time = 0.0;
            int addr = currAddr;
            int idx = currMarkerIdx;
            int rslt = LB_API2_Declarations.PP_MarkerNextPk(addr, idx);
            if (rslt > 0)
            {
                pkIdx = rslt;
                rslt = LB_API2_Declarations.PP_GetMarkerAmp(addr, idx, ref amp);
                if (rslt > 0)
                {
                    rslt = LB_API2_Declarations.PP_GetMarkerPositionTime(addr, idx, ref time);
                    if (rslt > 0)
                    {
                        lblPP_MarkerNextPk.Text = "Idx = " + pkIdx.ToString() + "  " + amp.ToString("##0.0## dBm") + "  " + time.ToString("##0.0##E+00");
                        tt.SetToolTip(lblPP_MarkerNextPk,
                            "Idx = " + pkIdx.ToString() + "\nAmp = " + amp.ToString("##0.0## dBm") + " \nTime = " + time.ToString("##0.0##E+00 sec"));
                    }
                    else lblPP_MarkerNextPk.Text = MSG_FAILED;
                }
                else lblPP_MarkerNextPk.Text = MSG_FAILED;
            }
            else lblPP_MarkerNextPk.Text = MSG_FAILED;
        }

        private void btnGetMarkerAmp_Click(object sender, EventArgs e)
        {
            double amp = 0.0;
            int addr = currAddr;
            int idx = currMarkerIdx;
            int rslt = LB_API2_Declarations.PP_GetMarkerAmp(addr, idx, ref amp);
            lblGetMarkerAmp.Text = amp.ToString("##0.0## dBm");
            tt.SetToolTip(lblGetMarkerAmp,"Amp = " + amp.ToString("##0.0## dBm"));
        }

        private void btnGetMarkerDeltaAmp_Click(object sender, EventArgs e)
        {
            double amp = 0.0;
            int addr = currAddr;
            int idx = currMarkerIdx;
            int rslt = LB_API2_Declarations.PP_GetMarkerDeltaAmp(addr, idx, ref amp);
            lblGetMarkerDeltaAmp.Text = amp.ToString("##0.0## dBm");
            tt.SetToolTip(lblGetMarkerDeltaAmp, "Amp = " + amp.ToString("##0.0## dBm"));
        }

        private void btnSetGetMarkerPosition_Click(object sender, EventArgs e)
        {
            double amp = 0.0, time = 0.0;
            int addr = currAddr;
            int mkrIdx = currMarkerIdx;
            int trIdx = (int)udSetGetMarkerPosition.Value;
            int rslt = LB_API2_Declarations.PP_SetMarkerPosition(addr, mkrIdx, trIdx);
            if (rslt > 0)
            {
                int chkTrIdx = 0;
                rslt = LB_API2_Declarations.PP_GetMarkerPosition(addr, mkrIdx, ref chkTrIdx);
                if (rslt <= 0)
                {
                    lblSetGetMarkerPosition.Text = MSG_FAILED;
                    return;
                }
                if (chkTrIdx != trIdx)
                {
                    lblSetGetMarkerPosition.Text = MSG_FAILED + ": Set != Get";
                    return;
                }
                lblSetGetMarkerPosition.Text = MSG_SUCCESS;
            }
            else lblSetGetMarkerPosition.Text = MSG_FAILED;
        }

        private void btnSetGetMarkerTime_Click(object sender, EventArgs e)
        {
            double amp = 0.0, time = 0.0;
            int addr = currAddr;
            int mkrIdx = currMarkerIdx;
            double mkrTime = ((double)udSetGetMarkerTime.Value); //  / 1e6;
            int rslt = LB_API2_Declarations.PP_SetMarkerPositionTime(addr, mkrIdx, mkrTime);
            if (rslt > 0)
            {
                double chkMkrTime = 0.0;
                rslt = LB_API2_Declarations.PP_GetMarkerPositionTime(addr, mkrIdx, ref chkMkrTime);
                if (rslt <= 0)
                {
                    lblSetGetMarkerTime.Text = MSG_FAILED;
                    return;
                }
                rslt = LB_API2_Declarations.PP_GetMarkerAmp(addr, mkrIdx, ref amp);
                if (rslt > 0)
                {
                    int pos = 0;
                    rslt = LB_API2_Declarations.PP_GetMarkerPosition(addr, mkrIdx, ref pos);
                    if (rslt > 0)
                    {
                        lblSetGetMarkerTime.Text = pos.ToString() +
                                                "  " + amp.ToString("##0.0##") +
                                                "  " + mkrTime.ToString("##0.0##E+00");
                        tt.SetToolTip(lblSetGetMarkerTime,
                                                pos.ToString() +
                                                "\nAmp = " + amp.ToString("##0.0##") +
                                                "\nTime =  " + mkrTime.ToString("##0.0##E+00"));
                    }
                    else lblSetGetMarkerTime.Text = MSG_FAILED;
                }
                else lblSetGetMarkerTime.Text = MSG_FAILED;
            }
            else lblSetGetMarkerTime.Text = MSG_FAILED;
        }

        private void btnSetGetDeltaMarkerTime_Click(object sender, EventArgs e)
        {
            double amp = 0.0, time = 0.0;
            int addr = currAddr;
            int mkrIdx = currMarkerIdx;
            LB_API2_Declarations.MARKER_MODE mode = LB_API2_Declarations.MARKER_MODE.MKR_OFF;
            // check the marker mode first!!!
            int rslt = LB_API2_Declarations.PP_GetMarkerMode(addr, mkrIdx, ref mode);
            if (rslt <= 0)
            {
                MessageBox.Show("Error in getting the marker mode, check address and marker...\n...did you forget to initialize the sensor?\n...take a sweep?\n...move the trace to analysis?");
                return;
            }
            if (mode != LB_API2_Declarations.MARKER_MODE.DELTA_MKR)
            {
                MessageBox.Show("Marker " + mkrIdx.ToString() + " mode must be: " +
                            LB_API2_Declarations.MARKER_MODE.DELTA_MKR.ToString() + "\n" +
                            "Marker " + mkrIdx.ToString() + " mode is: " +
                            mode.ToString());
                return;
            }
            double mkrTime = ((double)udSetGetDeltaMarkerTime.Value);
            rslt = LB_API2_Declarations.PP_SetMarkerDeltaTime(addr, mkrIdx, mkrTime);
            if (rslt > 0)
            {
                double chkMkrTime = 0.0;
                rslt = LB_API2_Declarations.PP_GetMarkerDeltaTime(addr, mkrIdx, ref chkMkrTime);
                if (rslt <= 0)
                {
                    lblSetGetDeltaMarkerTime.Text = MSG_FAILED;
                    return;
                }
                rslt = LB_API2_Declarations.PP_GetMarkerDeltaAmp(addr, mkrIdx, ref amp);
                if (rslt > 0)
                {
                    int pos = 0;
                    rslt = LB_API2_Declarations.PP_GetMarkerPosition(addr, mkrIdx, ref pos);
                    if (rslt > 0)
                    {
                        lblSetGetDeltaMarkerTime.Text = pos.ToString() +
                                                            "  " + amp.ToString("##0.0##") +
                                                            "  " + mkrTime.ToString("##0.0##E+00");
                        tt.SetToolTip(lblSetGetDeltaMarkerTime, 
                                                            "Idx = " + pos.ToString() +
                                                            "\nAmp = " + amp.ToString("##0.0##") +
                                                            "\nTime = " + mkrTime.ToString("##0.0##E+00"));
                    }
                    else lblSetGetDeltaMarkerTime.Text = MSG_FAILED;
                }
                else lblSetGetDeltaMarkerTime.Text = MSG_FAILED;
            }
            else lblSetGetDeltaMarkerTime.Text = MSG_FAILED;
        }

        private void btnPP_MarkerPosIsValid_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int mkrIdx = currMarkerIdx;
            int valid = 0;
            int rslt = LB_API2_Declarations.PP_MarkerPosIsValid(addr, mkrIdx, ref valid);
            if (valid==0)
                lblPP_MarkerPosIsValid.Text = "Pos = Invalid";
            else
                lblPP_MarkerPosIsValid.Text = "Pos = Valid";
        }

        private void btnDraw_Click(object sender, EventArgs e)
        {
            try
            {
                Graphics gr = pbTrace.CreateGraphics();
                Pen pn = new Pen(Color.Black);
                Pen pn_gray = new Pen(Color.Gray);
                pn_gray.DashStyle = DashStyle.Dot;
                
                gr.Clear(pbTrace.BackColor);
                gr.TranslateTransform((float)0, (float)-20, MatrixOrder.Append);
                gr.ScaleTransform((float)pbTrace.Width / (float)trAry.GetLength(0), (float)pbTrace.Height / (float)-100.0, MatrixOrder.Append);
                PointF[] t = new PointF[trAry.GetLength(0)];
                for (int i = 0; i <trAry.GetLength(0); i++)
                {
                    t[i].X = (float)i;
                    t[i].Y = (float)trAry[i];
                }
                gr.DrawLines(pn, t);
                pn.DashStyle = DashStyle.Dot;
                // draw horizontal lines
                float lvl = (float)0.0;
                for (int i = 1; i <10; i++)
                {
                    lvl = (float)20.0 - (float)i * (float)10.0;
                    gr.DrawLine(pn_gray, (float)0.0, lvl, (float)trAry.GetLength(0), lvl);
                }
                // draw vertial lines
                float incr = (float)trAry.GetLength(0) / (float)10.0;
                for (int i = 1; i < 10; i++)
                {
                    gr.DrawLine(pn_gray, incr * (float)i, (float)20.0, incr * (float)i, (float)-80.0);
                }
                pn.Dispose();
                pn_gray.Dispose();
                gr.Dispose();
            }
            catch (Exception)
            {
                MessageBox.Show("Error occurred while drawing trace...did you take a trace?");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            double avgPwr = 0.0;
            int rslt = LB_API2_Declarations.PP_GetTraceAvgPower(addr, ref avgPwr);
            if (rslt != 0)
                lblPP_GetTraceAvgPower.Text = string.Format("{0:F3} dBm",avgPwr);
            else
                lblPP_MarkerPosIsValid.Text = MSG_FAILED;
        }

        private void btnPP_GetTracePulsePower_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            double plsPwr = 0.0;
            int rslt = LB_API2_Declarations.PP_GetTracePulsePower(addr, ref plsPwr);
            if (rslt != 0)
                lblPP_GetTracePulsePower.Text = string.Format("{0:F3} dBm", plsPwr);
            else
                lblPP_GetTracePulsePower.Text = MSG_FAILED;
        }

        private void btnPP_GetTraceCrestFactor_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            double crFactor = 0.0;
            int rslt = LB_API2_Declarations.PP_GetTraceCrestFactor(addr, ref crFactor);
            if (rslt != 0)
                lblPP_GetTraceCrestFactor.Text = string.Format("{0:F3} dB", crFactor);
            else
                lblPP_GetTraceCrestFactor.Text = MSG_FAILED;
        }

        private void btnPP_GetTracePkPwr_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            double pkPwr = 0.0;
            int rslt = LB_API2_Declarations.PP_GetTracePkPwr(addr, ref pkPwr);
            if (rslt != 0)
                lblPP_GetTracePkPwr.Text = string.Format("{0:F3} dBm", pkPwr);
            else
                lblPP_GetTracePkPwr.Text = MSG_FAILED;
        }

        private void btnPP_GetTraceDC_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            double DC = 0.0;
            int rslt = LB_API2_Declarations.PP_GetTraceDC(addr, ref DC);
            if (rslt != 0)
                lblPP_GetTraceDC.Text = string.Format("{0:F3} %", DC * 100.0);
            else
                lblPP_GetTraceDC.Text = MSG_FAILED;
        }

        private void btnPP_SetTriggerEdge_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.TRIGGER_EDGE trEdge = (LB_API2_Declarations.TRIGGER_EDGE)cboPP_SetTriggerEdge.SelectedIndex;
            int rslt = LB_API2_Declarations.PP_SetTriggerEdge(addr, trEdge);
            if (rslt < 1)
                lblPP_SetTriggerEdge.Text = MSG_FAILED;
            else
                lblPP_SetTriggerEdge.Text = MSG_SUCCESS;
        }

        private void btnPP_GetTriggerEdge_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.TRIGGER_EDGE trEdge = LB_API2_Declarations.TRIGGER_EDGE.POSITIVE;
            int rslt = LB_API2_Declarations.PP_GetTriggerEdge(addr, ref trEdge);
            if (rslt < 1)
                lblPP_GetTriggerEdge.Text = MSG_FAILED;
            else
                lblPP_GetTriggerEdge.Text = trEdge.ToString();
        }

        private void udGateIdx_ValueChanged(object sender, EventArgs e)
        {
            currGateIdx = (int)udGateIdx.Value;
        }

        private void btnPP_CurrTrace2AnalysisTrace_Gates_Click(object sender, EventArgs e)
        {
            double a = 0;
            int addr = currAddr;
            int rslt = LB_API2_Declarations.PP_CurrTrace2AnalysisTrace(addr, ref a, -1);
            if (rslt > 0)
            {
                double pkExCrit = (double)udPP_GetPeaks_Idx_PkCrit_Gates.Value;
                double measThresh = (double)udPP_GetPeaks_Idx_MeasThresh_Gates.Value;
                rslt = LB_API2_Declarations.LB_SetPulseCriteria(addr, pkExCrit);
                if (rslt < 0)
                    lblPP_CurrTrace2AnalysisTrace_Gates.Text = MSG_FAILED;
                rslt = LB_API2_Declarations.PP_SetMeasurementThreshold(addr, measThresh);
                if (rslt < 0)
                    lblPP_CurrTrace2AnalysisTrace_Gates.Text = MSG_FAILED;
                else
                    lblPP_CurrTrace2AnalysisTrace_Gates.Text = MSG_SUCCESS;
            }
            else
                lblPP_CurrTrace2AnalysisTrace_Gates.Text = MSG_FAILED;
        }

        private void btnPP_SetGateMode_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int gateIdx = currGateIdx;
            // do a set
            LB_API2_Declarations.GATE_MODE gateMode = (LB_API2_Declarations.GATE_MODE)cboPP_SetGateMode_Mode.SelectedIndex;
            int rslt = LB_API2_Declarations.PP_SetGateMode(addr, gateIdx, gateMode);
            if (rslt <= 0)
            {
                lblPP_SetGateMode.Text = MSG_FAILED;
                return;
            }
            // do a get
            LB_API2_Declarations.GATE_MODE chkGateMode = LB_API2_Declarations.GATE_MODE.GATE_OFF;
            rslt = LB_API2_Declarations.PP_GetGateMode(addr, gateIdx, ref chkGateMode);
            if (rslt <= 0)
            {
                lblPP_SetGateMode.Text = MSG_FAILED;
                return;
            }
            // compare the values and report the result
            if (gateMode == chkGateMode)
                lblPP_SetGateMode.Text = MSG_SUCCESS + ": " + chkGateMode.ToString();
            else
                lblPP_SetGateMode.Text = MSG_FAILED;
            
        }

        private void btnGatePositionIsValid_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int gateIdx = currGateIdx;
            int valid = 0;
            int rslt = LB_API2_Declarations.PP_GatePositionIsValid(addr, gateIdx, ref valid);
            if (rslt <= 0)
            {
                lblGatePositionIsValid.Text = MSG_FAILED;
                return;
            }
            if (valid == 0)
                lblGatePositionIsValid.Text = "Success: Invalid";
            else
                lblGatePositionIsValid.Text = "Success: Valid";
        }

        private void btnEndPos_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int gateIdx = currGateIdx;
            int trIdx = -1;
            int rslt = LB_API2_Declarations.PP_GetGateEndPosition(addr, gateIdx, ref trIdx);
            if (rslt <= 0)
            {
                lblEndPos.Text = MSG_FAILED;
                return;
            }
            else
                lblEndPos.Text = MSG_SUCCESS + string.Format(": Pos = {0:D}", trIdx);
        }

        private void btnGateEndPosition_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int gateIdx = currGateIdx;
            int trIdx = (int)udGateEndPosition_Pos.Value;
            int rslt = LB_API2_Declarations.PP_SetGateEndPosition(addr, gateIdx, trIdx);
            if (rslt <= 0)
            {
                lblGateEndPosition.Text = MSG_FAILED;
                return;
            }
            else
                lblGateEndPosition.Text = MSG_SUCCESS;
        }

        private void btnStartPos_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int gateIdx = currGateIdx;
            int trIdx = -1;
            int rslt = LB_API2_Declarations.PP_GetGateStartPosition(addr, gateIdx, ref trIdx);
            if (rslt <= 0)
            {
                lblStartPos.Text = MSG_FAILED;
                return;
            }
            else
                lblStartPos.Text = MSG_SUCCESS + string.Format(": Pos = {0:D}", trIdx);
        }

        private void btnGateStartPosition_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int gateIdx = currGateIdx;
            int trIdx = (int)udGateStartPosition_Pos.Value;
            int rslt = LB_API2_Declarations.PP_SetGateStartPosition(addr, gateIdx, trIdx);
            if (rslt <= 0)
            {
                lblGateStartPosition.Text = MSG_FAILED;
                return;
            }
            else
                lblGateStartPosition.Text = MSG_SUCCESS;
        }

        private void btnStartEndPos_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int gateIdx = currGateIdx;
            int trSttIdx = (int)udStartEndPos_Start.Value;
            int trStpIdx = (int)udStartEndPos_End.Value;
            int rslt = LB_API2_Declarations.PP_SetGateStartEndPosition(addr, gateIdx, trSttIdx, trStpIdx);
            if (rslt <= 0)
            {
                lblStartEndPos.Text = MSG_FAILED;
                return;
            }
            int chkTrSttIdx = -1;
            int chkTrStpIdx = -1;
            rslt = LB_API2_Declarations.PP_GetGateStartEndPosition(addr, gateIdx, ref chkTrSttIdx, ref chkTrStpIdx);
            if (rslt <= 0)
            {
                lblStartEndPos.Text = MSG_FAILED;
                return;
            }
            else
                lblStartEndPos.Text = MSG_SUCCESS + string.Format(": {0:D} {1:D}", chkTrSttIdx, chkTrStpIdx);
        }

        private void btnPP_GetGatePeakPower_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int gateIdx = currGateIdx;
            double pkPwr = 0.0;
            int rslt = LB_API2_Declarations.PP_GetGatePeakPower(addr, gateIdx, ref pkPwr);
            if (rslt <= 0)
            {
                lblPP_GetGatePeakPower.Text = MSG_FAILED;
                return;
            }
            else
                lblPP_GetGatePeakPower.Text = MSG_SUCCESS + string.Format(": {0:F3} dBm", pkPwr);
        }

        private void btnPP_GetGateAveragePower_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int gateIdx = currGateIdx;
            double avgPwr = 0.0;
            int rslt = LB_API2_Declarations.PP_GetGateAveragePower(addr, gateIdx, ref avgPwr);
            if (rslt <= 0)
            {
                lblPP_GetGateAveragePower.Text = MSG_FAILED;
                return;
            }
            else
                lblPP_GetGateAveragePower.Text = MSG_SUCCESS + string.Format(": {0:F3} dBm", avgPwr);
        }

        private void btnPP_GetGateCrestFactor_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int gateIdx = currGateIdx;
            double crF = 0.0;
            int rslt = LB_API2_Declarations.PP_GetGateCrestFactor(addr, gateIdx, ref crF);
            if (rslt <= 0)
            {
                lblPP_GetGateCrestFactor.Text = MSG_FAILED;
                return;
            }
            else
                lblPP_GetGateCrestFactor.Text = MSG_SUCCESS + string.Format(": {0:F3} dB", crF);
        }

        private void btnPP_GetGateRiseTime_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int gateIdx = currGateIdx;
            double riseTm = 0.0;
            int rslt = LB_API2_Declarations.PP_GetGateRiseTime(addr, gateIdx, ref riseTm);
            if (rslt <= 0)
            {
                lblPP_GetGateRiseTime.Text = MSG_FAILED;
                return;
            }
            else
                lblPP_GetGateRiseTime.Text = MSG_SUCCESS + string.Format(": {0:F6} usec", riseTm);
        }

        private void btnPP_GetGateFallTime_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int gateIdx = currGateIdx;
            double fallTm = 0.0;
            int rslt = LB_API2_Declarations.PP_GetGateFallTime(addr, gateIdx, ref fallTm);
            if (rslt <= 0)
            {
                lblPP_GetGateFallTime.Text = MSG_FAILED;
                return;
            }
            else
                lblPP_GetGateFallTime.Text = MSG_SUCCESS + string.Format(": {0:F6} usec", fallTm);
        }

        private void btnPP_GetGatePulseWidth_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int gateIdx = currGateIdx;
            double plsWidth = 0.0;
            int rslt = LB_API2_Declarations.PP_GetGatePulseWidth(addr, gateIdx, ref plsWidth);
            if (rslt <= 0)
            {
                lblPP_GetGatePulseWidth.Text = MSG_FAILED;
                return;
            }
            else
                lblPP_GetGatePulseWidth.Text = MSG_SUCCESS + string.Format(": {0:F6} usec", plsWidth);
        }

        private void btnPP_GetGatePRT_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int gateIdx = currGateIdx;
            double PRT = 0.0;
            int rslt = LB_API2_Declarations.PP_GetGatePRT(addr, gateIdx, ref PRT);
            if (rslt <= 0)
            {
                lblPP_GetGatePRT.Text = MSG_FAILED;
                return;
            }
            else
                lblPP_GetGatePRT.Text = MSG_SUCCESS + string.Format(": {0:F3} usec", PRT);
        }

        private void btnPP_GetGatePRF_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int gateIdx = currGateIdx;
            double PRF = 0.0;
            int rslt = LB_API2_Declarations.PP_GetGatePRF(addr, gateIdx, ref PRF);
            if (rslt <= 0)
            {
                lblPP_GetGatePRF.Text = MSG_FAILED;
                return;
            }
            else
                lblPP_GetGatePRF.Text = MSG_SUCCESS + string.Format(": {0:F3} kz", PRF/1000.0);
        }

        private void btnPP_GetGateDutyCycle_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int gateIdx = currGateIdx;
            double DC = 0.0;
            int rslt = LB_API2_Declarations.PP_GetGateDutyCycle(addr, gateIdx, ref DC);
            if (rslt <= 0)
            {
                lblPP_GetGateDutyCycle.Text = MSG_FAILED;
                return;
            }
            else
                lblPP_GetGateDutyCycle.Text = MSG_SUCCESS + string.Format(": {0:F3} %", DC * 100.0);
        }

        private void btnPP_GetGateDroop_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int gateIdx = currGateIdx;
            double droop = 0.0;
            int rslt = LB_API2_Declarations.PP_GetGateDroop(addr, gateIdx, ref droop);
            if (rslt <= 0)
            {
                lblPP_GetGateDroop.Text = MSG_FAILED;
                return;
            }
            else
                lblPP_GetGateDroop.Text = MSG_SUCCESS + string.Format(": {0:F3} dB", droop);
        }

        private void btnGateStartTime_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int gateIdx = currGateIdx;
            double gateTm = (double)udGateStartTime.Value;
            int rslt = LB_API2_Declarations.PP_SetGateStartTime(addr, gateIdx, gateTm);
            if (rslt <= 0)
            {
                lblGateStartTime.Text = MSG_FAILED;
                return;
            }
            else
                lblGateStartTime.Text = MSG_SUCCESS;
        }

        private void btnStartTime_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int gateIdx = currGateIdx;
            double gateTm = (double)udGateStartTime.Value;
            int rslt = LB_API2_Declarations.PP_GetGateStartTime(addr, gateIdx, ref gateTm);
            if (rslt <= 0)
            {
                lblStartTime.Text = MSG_FAILED;
                return;
            }
            else
                lblStartTime.Text = MSG_SUCCESS + string.Format(": {0:F3} usec", gateTm);
        }

        private void btnStartEndTime_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int gateIdx = currGateIdx;
            double gateTm = (double)udGateStartTime.Value;
            int rslt = LB_API2_Declarations.PP_GetGateStartTime(addr, gateIdx, ref gateTm);
            if (rslt <= 0)
            {
                lblGateStartTime.Text = MSG_FAILED;
                return;
            }
            else
                lblStartEndTime .Text = MSG_SUCCESS + string.Format(": {0:F3} usec", gateTm); ;
            double gateSttTm = (double)udStartEndTime_Start.Value;
            double gateStpTm = (double)udStartEndTime_End.Value;
            rslt = LB_API2_Declarations.PP_SetGateStartEndTime(addr, gateIdx, gateSttTm, gateStpTm);
            if (rslt <= 0)
            {
                lblStartEndTime.Text = MSG_FAILED;
                return;
            }
            else
            {
                double chkGateSttTm = 0.0;
                double chkGateStpTm = 0.0;
                rslt = LB_API2_Declarations.PP_GetGateStartEndTime(addr, gateIdx, ref chkGateSttTm, ref chkGateStpTm);
                if (rslt <= 0)
                {
                    lblStartEndTime.Text = MSG_FAILED;
                    return;
                }
                if ((gateStpTm==chkGateStpTm) && (gateSttTm==gateSttTm))
                {
                    lblStartEndTime.Text = MSG_SUCCESS + string.Format(": {0:F3} {1:F3} ", chkGateSttTm, chkGateStpTm);
                }
                else lblStartEndTime.Text = MSG_FAILED + string.Format(": {0:F3} {1:F3} ", chkGateSttTm, chkGateStpTm);
            }
        }

        private void btnEndTime_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int gateIdx = currGateIdx;
            double gateTm = 0.0;
            int rslt = LB_API2_Declarations.PP_GetGateEndTime(addr, gateIdx, ref gateTm);
            if (rslt <= 0)
            {
                lblEndTime.Text = MSG_FAILED;
                return;
            }
            else
                lblEndTime.Text = MSG_SUCCESS + string.Format(": {0:F3}usec", gateTm);

        }

        private void btnGateEndTime_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int gateIdx = currGateIdx;
            double gateTm = (double)udGateEndTime.Value;
            int rslt = LB_API2_Declarations.PP_SetGateEndTime(addr, gateIdx, gateTm);
            if (rslt <= 0)
            {
                lblGateEndTime.Text = MSG_FAILED;
                return;
            }
            else
                lblGateEndTime.Text = MSG_SUCCESS;
        }

        private void btnPP_SetTriggerOut_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.TRIGGER_OUT_MODE trgOutMode = (LB_API2_Declarations.TRIGGER_OUT_MODE)cboPP_SetTriggerOut.SelectedIndex;
            int rslt = LB_API2_Declarations.PP_SetTriggerOut(addr, trgOutMode);
            if (rslt < 1)
                lblPP_SetTriggerOut.Text = MSG_FAILED;
            else
                lblPP_SetTriggerOut.Text = MSG_SUCCESS;
        }

        private void btnPP_GetTriggerOut_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            LB_API2_Declarations.TRIGGER_OUT_MODE trgOutMode = LB_API2_Declarations.TRIGGER_OUT_MODE.TRG_OUT_DISABLED;
            int rslt = LB_API2_Declarations.PP_GetTriggerOut(addr, ref trgOutMode);
            if (rslt < 1)
                lblPP_GetTriggerOut.Text = MSG_FAILED;
            else
                lblPP_GetTriggerOut.Text = MSG_SUCCESS + ": " + trgOutMode.ToString();
        }

        private void btnPP_GetGateOverShoot_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int gateIdx = currGateIdx;
            double overShoot = 0.0;
            int rslt = LB_API2_Declarations.PP_GetGateOverShoot(addr, gateIdx, ref overShoot);
            if (rslt <= 0)
            {
                lblPP_GetGateOverShoot.Text = MSG_FAILED;
                return;
            }
            else
                lblPP_GetGateOverShoot.Text = MSG_SUCCESS + string.Format(": {0:F3} dB", overShoot);
        }

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

        private void btnPP_GetGatePulsePower_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int gateIdx = currGateIdx;
            double plsPwr = 0.0;
            int rslt = LB_API2_Declarations.PP_GetGatePulsePower(addr, gateIdx, ref plsPwr);
            if (rslt <= 0)
            {
                lblPP_GetGatePulsePower.Text = MSG_FAILED;
                return;
            }
            else
                lblPP_GetGatePulsePower.Text = MSG_SUCCESS + string.Format(": {0:F3} dB", plsPwr);
        }

        double DBM2WATTS(double DBM)
        {
            return Math.Pow(10.0, DBM/10.0) * 0.001;
        }

        double WATTS2DBM(double WATTS)
        {
            return 10.0 * Math.Log10(WATTS / 0.001);
        }

        private void btnPP_GetPulseEdgesPosition_Click(object sender, EventArgs e)
        {
            int rslt = 0;
            int addr = currAddr;
            int pkIdx = (int)udPulseEdgesPos.Value;
            int leftSide = 0, rightSide = 0;
            string strRslt;
            rslt = LB_API2_Declarations.PP_GetPulseEdgesPosition(addr, pkIdx, ref leftSide, ref rightSide);
            if (rslt > 0)
            {
                strRslt = "Left: " + leftSide.ToString("#####0") +
                          "    Right: " + rightSide.ToString("#####0");
            }
            else
            {
                strRslt = "Error - PP_GetPulseEdgesPosition";
                return;
            }
            lblPulseEdgesPos.Text = strRslt;
            return;
        }

        private void btnPP_GetPulseEdgesTime_Click(object sender, EventArgs e)
        {
            int rslt = 0;
            int addr = currAddr;
            double pkTime = (double)udPulseEdgesTime.Value;
            double leftSide = 0, rightSide = 0;
            string strRslt;
            rslt = LB_API2_Declarations.PP_GetPulseEdgesTime(addr, pkTime, ref leftSide, ref rightSide);
            if (rslt > 0)
            {
                strRslt = "Left: " + leftSide.ToString("#####0.000") + "usec    " +
                          "Right: " + rightSide.ToString("#####0.000") + "usec";
            }
            else
            {
                strRslt = "Error - PP_GetPulseEdgesTime";
                return; 
            }
            lblPulseEdgesTime.Text = strRslt;
            return;
        }

        private void btnGetMarkerPosition_Click(object sender, EventArgs e)
        {
            int rslt;
            double amp = 0.0, time = 0.0;
            int addr = currAddr;
            int mkrIdx = currMarkerIdx;
            int trIdx = 0;
            rslt = LB_API2_Declarations.PP_GetMarkerPosition(addr, mkrIdx, ref trIdx);
            if (rslt > 0)
            {
                rslt = LB_API2_Declarations.PP_GetMarkerAmp(addr, mkrIdx, ref amp);
                if (rslt>0)
                    lblGetMarkerPosition.Text = trIdx.ToString() + "  " + amp.ToString("##0.0##");
                else lblGetMarkerPosition.Text = MSG_FAILED;
                return;
            }
            else lblGetMarkerPosition.Text = MSG_FAILED;
       }

        private void button2_Click_2(object sender, EventArgs e)
        {
            int addr = currAddr;
            int mkrIdx = currMarkerIdx;
            double mkrTime = 0.0;
            int rslt = LB_API2_Declarations.PP_GetMarkerPositionTime(addr, mkrIdx, ref mkrTime);
            if (rslt > 0)
            {
                double amp = 0.0;
                rslt = LB_API2_Declarations.PP_GetMarkerAmp(addr, mkrIdx, ref amp);
                if (rslt > 0)
                {
                    lblGetMarkerTime.Text = mkrTime.ToString("##0.00### usec") + ":   " + amp.ToString("##0.0##");
                }
                else lblGetMarkerTime.Text = MSG_FAILED;
            }
            else lblGetMarkerTime.Text = MSG_FAILED;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            int addr = currAddr;
            int rslt = 0;

            rslt = LB_API2_Declarations.LB_DiagStoreCoeff(addr);
        }

        private void btnGetDeltaMarkerTime_Click(object sender, EventArgs e)
        {
            int addr = currAddr;
            int mkrIdx = currMarkerIdx;
            double mkrDeltaTime = 0.0;
            int rslt = LB_API2_Declarations.PP_GetMarkerDeltaTime(addr, mkrIdx, ref mkrDeltaTime);
            if (rslt > 0)
            {
                double deltaAmp = 0.0;
                rslt = LB_API2_Declarations.PP_GetMarkerDeltaAmp(addr, mkrIdx, ref deltaAmp);
                if (rslt > 0)
                {
                    lblGetDeltaMarkerTime.Text = mkrDeltaTime.ToString("##0.00### usec") + ":   " + deltaAmp.ToString("##0.0##");
                }
                else lblGetDeltaMarkerTime.Text = MSG_FAILED;
            }
            else lblGetDeltaMarkerTime.Text = MSG_FAILED;
        }

        private void cmdSweep_Click(object sender, EventArgs e)
        {
            int addr = currAddr;

            sb = new StringBuilder();
            lblPP_GetTrace.Text = string.Format(MSG_SENSOR_TRYING_TO_INITALIZE, addr);
            lblPP_GetTrace.Refresh();
            if (LB_API2_Declarations.LB_IsSensorConnected_Addr(addr) >= 1)
            {
                if (LB_API2_Declarations.LB_InitializeSensor_Addr(addr) >= 1)
                {
                    int trLen = LB_API2_Declarations.PP_GetTraceLength(addr);
                    if (trLen > 0)
                    {
                        int trUsed = 0;
                        int rslt;

                        trAry = new double[trLen];
                        rslt = LB_API2_Declarations.PP_GetTrace(addr, ref trAry[0], trLen, ref trUsed);
                        if (rslt >= 1)
                        {
                            lblPP_GetTrace.Text = string.Format(MSG_GET_TRACE_SUCCEEDED, addr, trLen, trUsed);
                            lstPP_GetTrace.Items.Clear();
                            lstPP_GetTrace.Items.Add("  Idx      Amp(dBm)");
                            for (int i = 0; i < trUsed; i++)
                            {
                                lstPP_GetTrace.Items.Add(string.Format("{0,6:d}{1,12:F3}", i, trAry[i]));
                                sb.Append(i);
                                sb.Append('\t');
                                sb.Append(trAry[i]);
                                sb.Append('\n');
                            }
                        }
                        else
                        {
                            lblPP_GetTrace.Text = string.Format(MSG_GET_TRACE_ERROR, addr);
                        }
                    }
                    else
                    {
                        lblPP_GetTrace.Text = string.Format(MSG_TRACE_LENGTH_ERROR, addr);
                    }
                }
                else
                {
                    lblPP_GetTrace.Text = string.Format(MSG_SENSOR_FAILED_INITIALIZATION, addr);
                }
            }
            else
            {
                lblPP_GetTrace.Text = string.Format(MSG_SENSOR_IS_NOT_CONNECTED, addr);
            }
        }

    }
}