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

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

        // ReLXg //////////////////////////////////////////////////////////
        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;

        // ݒ /////////////////////////////////////////////////////////////
        uint     Myadr, Yradr;                     // }CAhXA@AhX
        //uint   Delim,Eoi;                        // f~^
        bool     bRet;                             // AddComand֐BOOLl
        string   csBuf = "";                       // ̃obt@
        int      CmdNum, CmdNumPos;                // R}h,R}ḧʒu
        //int    NumPos;                           // 
        uint     Ret;                              // ߂l
        int[]    CmdNumArray = new int[50];        //
        string   DelMyadr = new string(' ', 8);    // }CAhX(폜p)
        GCHandle gChCmd;                           // CmdzpGCHandle
        /////////////////////////////////////////////////////////////////////////////

        public Form1()
        {
            InitializeComponent();
        }

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

            // PinnednhCmdɕz
            if (gChCmd.IsAllocated == false)
            {
                gChCmd = GCHandle.Alloc(SubFunc.Cmd, GCHandleType.Pinned);
            }
            // PinnednhŃIuWFNg̃AhX擾
            SubFunc.pCmd = gChCmd.AddrOfPinnedObject();
        }

        private void MltLine_FormClosed(object sender, FormClosedEventArgs e)
        {
            SubFunc.GpibExit();                                     // [SubClass.h] ̒̊֐R[܂B

            // GCHandlẻ
            if (gChCmd.IsAllocated == true)
            {
                gChCmd.Free();
            }
        }

        // bZ[WXg̍쐬 /////////////////// [DispCommandList] ֐ //////////
        private string DispCommandList()
        {
            int    TxtRet;                                          // 16ibZ[Wp
            int    Count;                                           // JE^
            string Text = "", TmpString;                            // TmpString = 쐬MSGText = SĂMSG

            for (Count = 1; Count < CmdNum; Count++)
            {                                                       // SĂMSG̍Ōɍ쐬MSGǉ
                TxtRet = (int)SubFunc.Cmd[Count];                   // ԖڂCmdz
                TmpString = string.Format(" [{0:x}H]", TxtRet);     // \p
                Text = Text + TmpString;                            // VKMSGǉ
            }
            return Text;
        }

        // bZ[W̔z쐬 //////////////////////// [AddCommand] ֐ //////////
        private bool AddCommand(uint CommandType, int CommandNum)
        {
            string ErrMsg;                                          // G[bZ[Wp

            if (CmdNum > 30)
            {                                                       // bZ[W30ȏ̎
                ErrMsg = "ȏݒł܂B";
                MessageBox.Show(ErrMsg);
                return false;
            }
            if (CommandNum == 2)
            {                                                       // bZ[Wɑ@AhX
                SubFunc.Cmd[CmdNum] = (Yradr | 0x20);               // ̗p
                CmdNum = CmdNum + 1;
            }
            SubFunc.Cmd[CmdNum] = CommandType;                      // ǂ̃bZ[WlœnB
            CmdNum = CmdNum + 1;

            return true;                                            // ÎƂ
        }

        // [[`] ///////////////////////////////////////////////////////////
        private void btnInit_Click(object sender, System.EventArgs e)
        {
            SubFunc.GpibInit(out csBuf);                            // Init֐̌ĂяoĂ܂B
            gpib.Boardsts(0x08, out Myadr);                         // }CAhX擾
            DelMyadr = string.Format("{0:d}", Myadr);               // 𕶎ɕϊ
            Ret = (uint)cboYrAdrBox.FindStringExact(DelMyadr, -1);  // }CAhX0-30̒ɂ邩
            if (Ret >= 0)
            {                                                       // vCfbNXΎs
                cboYrAdrBox.Items.Remove(Ret.ToString());           // }CAhXI{bNX폜B
                if (Myadr == Yradr)
                {
                    cboYrAdrBox.SelectedIndex = 0;
                }
            }
            txtRet.Text = csBuf;

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

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

            if (CmdNum <= 1)
            {                                                       // R}h [0] ̏ꍇ
                csBuf = "R}h͂ĂB";
            }
            else
            {
                Ret = gpib.Comand(SubFunc.pCmd);                    // R}h()̑M
                SubFunc.CheckRet("GpComand", Ret, out csBuf);       // GpComand̖߂l`FbN
            }
            txtRet.Text = csBuf;                                    // ߂l\
        }

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

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

        // [ 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();
        }

        // [ 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();
        }

        // [I[`] /////////////////////////////////////////////////////////
        private void btnExt_Click(object sender, System.EventArgs e)
        {
            SubFunc.GpibExit();                                     // [SubClass.h] ̒̊֐R[܂B
            this.Close();                                           // _CAO܂B(_CAȌI)
        }

        // [] /////////////////////////////////////////////////////////////////
        private void btnDel_Click(object sender, System.EventArgs e)
        {
            if (CmdNumPos == 0)
            {                                                       // bZ[WȂꍇ
                txtRet.Text = "}`CbZ[W܂B";
            }
            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();
        }

        // [SĎ] /////////////////////////////////////////////////////////////
        private void btnAllDel_Click(object sender, System.EventArgs e)
        {
            CmdNum    = 1;
            CmdNumPos = 0;

            txtMsg.Text = DispCommandList();
        }

        // [@AhXύX] /////////////////////////////////////////////////////
        private void cboYrAdrBox_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            Yradr = Convert.ToUInt32(cboYrAdrBox.Text);
        }
    }
}