using System.Runtime.InteropServices;
using CgpibCs;
using CSubFuncCs;

namespace MltLine
{
    public partial class Form1 : Form
    {
        Cgpib gpib = new Cgpib();
        CSubFunc SubFunc = new CSubFunc();

        // Static //////////////////////////////////////////////////////////
        const int GP_GTL = 0x01;
        const int GP_SDC = 0x04;
        const int GP_PPC = 0x05;
        const int GP_GET = 0x08;
        const int GP_TCT = 0x09;
        const int GP_LLO = 0x11;
        const int GP_DCL = 0x14;
        const int GP_PPU = 0x15;
        const int GP_SPE = 0x18;
        const int GP_SPD = 0x19;
        const int GP_MLA = 0x20;
        const int GP_UNL = 0x3F;
        const int GP_MTA = 0x40;
        const int GP_UNT = 0x5F;
        // Setting /////////////////////////////////////////////////////////////
        uint Myadr, Yradr;                      // MyAddress,Address of device
        string csBuf = "";                      // Character string
        bool bRet;                              // BOOL value in AddComand function 
        int CmdNum, CmdNumPos;                  // Number of command,Position of command 
        uint Ret;                               // Return code
        int[] CmdNumArray = new int[50];        // Command Array(2)
        string DelMyadr = new string(' ', 8);   // For delete MyAddress in listbox
        GCHandle gChCmd;                        // GCHandle for cmd array
        /////////////////////////////////////////////////////////////////////////////

        public Form1()
        {
            InitializeComponent();
        }

        private void MltLine_Load(object sender, System.EventArgs e)
        {
            // ****************************************************** Setting *** 
            CmdNum = 1;
            CmdNumPos = 0;
            cboYrAdrBox.Text = "1";
            // ******************************************************************

            // Specify Pinned handle to Cmd
            if (gChCmd.IsAllocated == false)
            {
                gChCmd = GCHandle.Alloc(SubFunc.Cmd, GCHandleType.Pinned);
            }
            // Get address object by Pinned handle
            SubFunc.pCmd = gChCmd.AddrOfPinnedObject();
        }

        private void MltLine_FormClosed(object sender, FormClosedEventArgs e)
        {
            SubFunc.GpibExit();                                 // SubClass.h is called
                                                                // GCHandle free
            if (gChCmd.IsAllocated == true)
            {
                gChCmd.Free();
            }
        }

        //////////////////////////// [DispCommandList] function ///
        private string DispCommandList()
        {
            uint TxtRet;
            int Count;                                          // For Text of Return code
            string Text = "", TmpString;                        // Making message,All message

            for (Count = 1; Count < CmdNum; Count++)
            {
                TxtRet = SubFunc.Cmd[Count];
                TmpString = string.Format(" [{0:x}H]", TxtRet);
                Text = Text + TmpString;
            }
            return Text;
        }

        ///////////////////////////////// [AddCommand] function ///
        private bool AddCommand(int CommandType, int CommandNum)
        {
            string ErrMsg;                                      // For Error Message

            if (CmdNum > 30)
            {                                                   // In terms of a message in the case of 30 or more
                ErrMsg = "Message is over!!";
                MessageBox.Show(ErrMsg);
                return false;
            }
            if (CommandNum == 2)
            {                                                   // In case of necessary two message
                SubFunc.Cmd[CmdNum] = (Yradr | 0x20);           // Add Address of device to 0x20
                CmdNum = CmdNum + 1;
            }
            SubFunc.Cmd[CmdNum] = (uint)CommandType;
            CmdNum = CmdNum + 1;

            return true;                                        // A normal end
        }

        //////////////////////////////////////////// Initialize ///
        private void btnInit_Click(object sender, System.EventArgs e)
        {
            string DelMyadr = new string(' ', 8);

            SubFunc.GpibInit(out csBuf);                        // SubFunc.h is called
            gpib.Boardsts(0x08, out Myadr);                     // My Address is acquired
            txtRet.Text = csBuf;
            btnSend.Enabled = true;

            DelMyadr = string.Format("{0:d}", Myadr);           // Show MyAddress
            Ret = (uint)cboYrAdrBox.FindStringExact(DelMyadr, -1);
            if (Ret >= 0)
            {                                                   // Seaching MyAddress
                cboYrAdrBox.Items.Remove(Ret.ToString());       // Delete MyAddress 
                if (Myadr == Yradr)
                {
                    cboYrAdrBox.SelectedIndex = 0;
                }
            }

            Yradr = Convert.ToUInt32(cboYrAdrBox.Text);
        }

        ////////////////////////////////////////////////// Send ///
        private void btnSend_Click(object sender, System.EventArgs e)
        {
            SubFunc.Cmd[0] = (uint)CmdNum - 1;

            if (CmdNum <= 1)
            {
                csBuf = "There are not transmission data.";
            }
            else
            {
                Ret = gpib.Comand(SubFunc.pCmd);                // Send command
                SubFunc.CheckRet("GpComand", Ret, out csBuf);   // Error check
            }
            txtRet.Text = csBuf;
        }

        ////////////////////////////////////////////////// DCL ////
        private void btnDCL_Click(object sender, System.EventArgs e)
        {
            bRet = AddCommand(GP_DCL, 1);
            if (bRet == true)
            {
                CmdNumArray[CmdNumPos] = 1;
                CmdNumPos = CmdNumPos + 1;
                txtRet.Text = "";
            }
            txtMsg.Text = DispCommandList();
        }

        /////////////////////////////////////////////////// GET ///
        private void btnGET_Click(object sender, System.EventArgs e)
        {
            bRet = AddCommand(GP_GET, 2);
            if (bRet == true)
            {
                CmdNumArray[CmdNumPos] = 2;
                CmdNumPos = CmdNumPos + 1;
                txtRet.Text = "";
            }
            txtMsg.Text = DispCommandList();
        }

        /////////////////////////////////////////////////// GTL ///
        private void btnGTL_Click(object sender, System.EventArgs e)
        {
            bRet = AddCommand(GP_GTL, 2);                       // AddCommand(Static, Command number)
            if (bRet == true)
            {
                CmdNumArray[CmdNumPos] = 2;
                CmdNumPos = CmdNumPos + 1;
                txtRet.Text = "";
            }
            txtMsg.Text = DispCommandList();
        }

        /////////////////////////////////////////////////// LLO ///
        private void btnLLO_Click(object sender, System.EventArgs e)
        {
            bRet = AddCommand(GP_LLO, 1);
            if (bRet == true)
            {
                CmdNumArray[CmdNumPos] = 1;
                CmdNumPos = CmdNumPos + 1;
                txtRet.Text = "";
            }
            txtMsg.Text = DispCommandList();
        }

        /////////////////////////////////////////////////// SDC ///
        private void btnSDC_Click(object sender, System.EventArgs e)
        {
            bRet = AddCommand(GP_SDC, 2);
            if (bRet == true)
            {
                CmdNumArray[CmdNumPos] = 2;
                CmdNumPos = CmdNumPos + 1;
                txtRet.Text = "";
            }
            txtMsg.Text = DispCommandList();
        }

        /////////////////////////////////////////////////// UNL ///
        private void btnUNL_Click(object sender, System.EventArgs e)
        {
            bRet = AddCommand(GP_UNL, 1);
            if (bRet == true)
            {
                CmdNumArray[CmdNumPos] = 1;
                CmdNumPos = CmdNumPos + 1;
                txtRet.Text = "";
            }
            txtMsg.Text = DispCommandList();
        }

        /////////////////////////////////////////////////// UNT ///
        private void btnUNT_Click(object sender, System.EventArgs e)
        {
            bRet = AddCommand(GP_UNT, 1);
            if (bRet == true)
            {
                CmdNumArray[CmdNumPos] = 1;
                CmdNumPos = CmdNumPos + 1;
                txtRet.Text = "";
            }
            txtMsg.Text = DispCommandList();
        }

        //////////////////////////////////////// Address of device Change ///
        private void cboYrAdrBox_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            Yradr = Convert.ToUInt32(cboYrAdrBox.Text);
        }

        //////////////////////////////////////////// ALL DELETE ///
        private void btnAllDel_Click(object sender, System.EventArgs e)
        {
            CmdNum = 1;
            CmdNumPos = 0;

            txtMsg.Text = DispCommandList();
        }

        //////////////////////////////////////////// ONE DELETE ///
        private void btnDel_Click(object sender, System.EventArgs e)
        {
            if (CmdNumPos == 0)
            {                           // In case of Command is no
                txtRet.Text = "There is not a message.";
            }
            else
            {
                if (CmdNumArray[CmdNumPos - 1] == 1)
                {
                    SubFunc.Cmd[CmdNum] = 0;
                    CmdNum = CmdNum - 1;
                }
                else
                {
                    SubFunc.Cmd[CmdNum] = 0;
                    SubFunc.Cmd[CmdNum - 1] = 0;
                    CmdNum = CmdNum - 2;
                }
                CmdNumArray[CmdNumPos - 1] = 0;
                CmdNumPos = CmdNumPos - 1;
                txtRet.Text = "";
            }
            txtMsg.Text = DispCommandList();
        }

        ////////////////////////////////////////////////// Exit ///
        private void btnExt_Click(object sender, System.EventArgs e)
        {
            SubFunc.GpibExit();                                 // SubClass.h is called
            this.Close();                                       // End of program
        }
    }
}