using CgpibCs;

namespace Slave
{
    public partial class Form1 : Form
    {
        Cgpib gpib = new Cgpib();
        short m_Id;

        public Form1()
        {
            InitializeComponent();
        }

        //***************************************************************************
        //   Initializing
        //***************************************************************************
        private void Slave_Load(object sender, System.EventArgs e)
        {
            txtDevName.Text = "GPIB000";
            cboDelim.SelectedIndex = 1;                     //Set "CR+LF" as default
            cboEoi.SelectedIndex = 1;                       //Set "Use" as default
            txtSend.Text = "CONTEC Slave Sample";           //dafault send data
            txtSPollStb.Text = "0";
        }
        //***************************************************************************
        //   Display transmitted string to list box of "Return code"(sub routine)
        //***************************************************************************
        private void funcSetlstRet(string strRet)
        {
            //Add the item to the end of list box
            //Clear all of the items if the number of items more than 256
            if (lstRet.Items.Count >= 256)
            {
                lstRet.Items.RemoveAt(0);
                lstRet.Items.Add(strRet);
            }
            else
            {
                lstRet.Items.Add(strRet);
            }
            lstRet.SelectedIndex = lstRet.Items.Count - 1;
        }

        //**************************************************************************************************
        //   Set the target (sub routine)
        //   Function: Set Equipment ID, Delimiter and EOI
        //   Return value: true(normally), false(abnormally)
        //**************************************************************************************************
        private bool funcSetParam()
        {
            short Delim;
            short Eoi;
            short Eos;
            int Ret;
            string strRet;

            //Set delimiter and EOI line
            Delim = (short)cboDelim.SelectedIndex;
            Eoi = (short)cboEoi.SelectedIndex;
            Eos = 0;
            Ret = gpib.SetDelim(
                                    m_Id,
                                    Delim,  //Set delimiter
                                    Eoi,    //Set EOI line
                                    Eos     //Set EOS(reserved)
                                    );
            if (Ret != 0)
            {
                //error
                gpib.GetErrorString(Ret, out strRet);
                funcSetlstRet(strRet);      //Display in list
                return false;
            }
            else
            {
                funcSetlstRet("[Partner apparatus setting] completed normally");    //Display in list
            }

            return true;
        }

        //***************************************************************************
        //   Initialization
        //   Function: Get device ID from device name
        //***************************************************************************
        private void cmdInit_Click(object sender, System.EventArgs e)
        {
            int Ret;
            int Data;
            string strRet;

            Ret = gpib.Init(txtDevName.Text, out m_Id);
            if (Ret != 0)
            {
                //error
                gpib.GetErrorString(Ret, out strRet);
                funcSetlstRet(strRet);      //Display in list
            }
            else
            {
                //If get device ID normally
                strRet = "GpibInit : DeviceID = " + m_Id;
                funcSetlstRet(strRet);  //Display in list
                                        //Get device address
                Ret = gpib.GetStatus(m_Id, 0x08, out Data);
                if (Ret == 0)
                {
                    txtMyAddr.Text = Data.ToString();
                }
                //Confirm whether it is set to slave
                Ret = gpib.GetStatus(m_Id, 0x0A, out Data);
                if ((Ret == 0) && (Data == 0))
                {
                    MessageBox.Show("It is not a slave. Please set it to slave in property page.");
                }
                else
                {
                    funcSetlstRet("[Initialize] completed normally");   //Display in list
                }
            }

            gpib.SendIFC(m_Id, 1);  //Send IFC
        }

        //***************************************************************************
        //   Exit
        //   Function: Disable device ID
        //***************************************************************************
        private void cmdExit_Click(object sender, System.EventArgs e)
        {
            int Ret;
            string strRet;

            Ret = gpib.Exit(m_Id);
            if (Ret != 0)
            {
                //error
                gpib.GetErrorString(Ret, out strRet);
                funcSetlstRet(strRet);      //Display in list
            }
            this.Close();
        }

        //***************************************************************************
        //   Send Data
        //   Function: Send data to master device
        //***************************************************************************
        private void cmdSend_Click(object sender, System.EventArgs e)
        {
            int SendLen;
            int Ret;
            string strRet;
            string SendBuf;

            //Set equipment ID, delimiter and EOI
            if (funcSetParam() == false) return;

            //Get sending data from edit box
            SendBuf = txtSend.Text;
            SendLen = SendBuf.Length;
            if (SendLen == 0)
            {
                funcSetlstRet("GpibSendData : sending data does not exist");    //Display in list
                return;
            }

            funcSetlstRet("GpibSendData : sending data...");        //Display in list
                                                                    //Transmit data
            Ret = gpib.SendData(m_Id, ref SendLen, SendBuf);
            if (Ret != 0)
            {
                //error
                gpib.GetErrorString(Ret, out strRet);
                funcSetlstRet(strRet);      //Display in list
            }
            else
            {
                //Sent data normally
                strRet = "GpibSendData : the number of sent data= " + SendLen.ToString();
                funcSetlstRet(strRet);                              //Display in list
                funcSetlstRet("[Send data] completed normally");    //Display in list
            }
        }

        //***************************************************************************
        //   Receive data
        //   Function: Receive data from master device
        //***************************************************************************
        private void cmdRec_Click(object sender, System.EventArgs e)
        {
            int RecLen;
            int Ret;
            string strRet;
            string RecBuf;

            //Set equipment ID, delimiter and EOI
            if (funcSetParam() == false) return;

            RecLen = 256;   //Set recieving buffer size
            funcSetlstRet("GpibRecData :  receiving data...");  //Display in list
                                                                //Receive data
            Ret = gpib.RecData(m_Id, ref RecLen, out RecBuf);
            if (Ret != 0)
            {
                //error
                gpib.GetErrorString(Ret, out strRet);
                funcSetlstRet(strRet);                          //Display in list
            }
            else
            {
                //Finish receiving data normally
                txtRec.Text = RecBuf.Substring(0, RecLen);
                strRet = "GpibRecData : the number of received data = " + RecLen.ToString();
                funcSetlstRet(strRet);                              //Display in list
                funcSetlstRet("[Receive data] completed normally"); //Display in list
            }
        }

        //***************************************************************************
        //  Set status byte
        //   Function: Transmit SRQ and set status byte
        //***************************************************************************
        private void cmdSendSrq_Click(object sender, System.EventArgs e)
        {
            int Ret;
            string strRet;
            short intSrqSend;

            //The SRQ setting value is acquired. 
            if (optSrqNotSend.Checked == true)
            {
                intSrqSend = 0;
            }
            else
            {
                intSrqSend = 1;
            }
            //Transmit SRQ and perform the functin of seting status byte
            Ret = gpib.SendSRQ(m_Id, intSrqSend, short.Parse(txtSPollStb.Text));
            if (Ret != 0)
            {
                //error
                gpib.GetErrorString(Ret, out strRet);
                funcSetlstRet(strRet);                      //Display in list
            }
            else
            {
                funcSetlstRet("[Transmit SRQ ans set status byte] completed normally"); //Display in list
            }
        }

        //***************************************************************************
        //   Set parallel poll response
        //***************************************************************************
        private void cmdParallelpollSetting_Click(object sender, System.EventArgs e)
        {
            short Response = 0;
            short Ist = 0;
            int Ret;
            string strRet;

            if (optPPollResponseNot.Checked == true)
            {
                //No responsev
                Response = 0;
            }
            else if (optPPollResponseIst0.Checked == true)
            {
                //[Response of ISTbit(IST=0)]
                Response = 1;
                Ist = 0;
            }
            else if (optPPollResponseIst1.Checked == true)
            {
                //[Response of ISTbit(IST=1)]
                Response = 1;
                Ist = 1;
            }
            else if (optPPollResponseSrq.Checked == true)
            {
                //[Response with SRQ]
                Response = 2;
            }

            //Perform the function of setting parallel poll response
            Ret = gpib.SetPPollResponse(m_Id, Response);
            if (Ret != 0)
            {
                //error
                gpib.GetErrorString(Ret, out strRet);
                funcSetlstRet(strRet);  //Display in list
            }
            else
            {
                funcSetlstRet("[Set parallel poll response] completed normally");   //Display in list
            }

            if (Response == 1)
            {
                //Perform the function of settong ISTbit
                Ret = gpib.SetIst(m_Id, Ist);
                if (Ret != 0)
                {
                    //error
                    gpib.GetErrorString(Ret, out strRet);
                    funcSetlstRet(strRet);  //Display in list
                }
                else
                {
                    funcSetlstRet("[Set IST of parallel poll response] completed normally");    //Display in list
                }
            }
        }

        //***************************************************************************
        //   Clear send/receive text data
        //***************************************************************************
        private void cmdDataDel_Click(object sender, System.EventArgs e)
        {
            txtSend.Text = "";
            txtRec.Text = "";
        }

        //***************************************************************************
        //    Clear return code
        //***************************************************************************
        private void cmdRetDel_Click(object sender, System.EventArgs e)
        {
            lstRet.Items.Clear();
        }
    }
}