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

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

        // ϐ萔Ő錾Ă܂B////////////////////////////////
        string        szBuffer = new string(' ', 1024);       // eLXg̃obt@
        uint          Ret;                                    // ߂l
        uint          Myadr, /*Yradr,*/ Mode;                 // }CAhXA@AhXA}X^
        uint          Delim, Eoi;                             // f~^
        uint          /*Timeout, Ifctime,Dmamode, */Srlen;
        uint          P_stb, P_eoi;                           // Xe[^XoCgAEOĨ|C^
        string        srBuffer = new string(' ', 10000);      // The buffer of send work
        //uint        uTimerTrm;                              // ^C}̓`FbNp
        //uint        uTimerEvent;                            // ^C}Cxgp
        string        csBuf = "";                             // eLXg\p
        string        pStb = "";
        StringBuilder rvBuffer = new StringBuilder(10000);    // The buffer of read work
        GCHandle      gChCmd;                                 // CmdzpGCHandle
        /////////////////////////////////////////////////////////////////////

        public Form1()
        {
            InitializeComponent();
        }

        private void Slave_Load(object sender, System.EventArgs e)
        {
            // ݒ
            cboStsByte.Text = "40";                                 // XĝOԖڂ\܂B
            txtSendData.Text = "_0123456789_ Test Data / 33 Bytes";
            cboDelimBox.SelectedIndex = 1;

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

        private void Slave_FormClosed(object sender, FormClosedEventArgs e)
        {
            SubFunc.GpibExit();                                     // I֐ĂяoĂ܂B

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

        // [[`] ///////////////////////////////////////////////////////////
        private void btnInit_Click(object sender, System.EventArgs e)
        {
            SubFunc.GpibInit(out csBuf);                            // ֐ĂяoĂ܂B

            gpib.Boardsts(0x0a, out Mode);
            gpib.Boardsts(0x08, out Myadr);
            txtMyAddress.Text = Myadr.ToString();

            if (Mode == 0)
            {                                                       // }X^̏ꍇ
                txtRet.Text = "̋@̓X[uł͂܂B";
                return;
            }
            else
            {
                txtRet.Text = csBuf;
            }

            cboDelimBox_SelectedIndexChanged(cboDelimBox, new System.EventArgs());
        }

        // [M[`] /////////////////////////////////////////////////////////////
        private void btnSend_Click(object sender, System.EventArgs e)
        {
            txtRet.Text = "f[^𑗐MĂ܂DDD";
            txtRet.Refresh();                                       // ʂĕ`悵Ă܂B

            srBuffer = txtSendData.Text;                            // Mf[^srBufferɃRs[܂B
            Srlen = (uint)srBuffer.Length;                          // MŁ[̒𑪂܂B

            if (Srlen != 0x0)
            {                                                       // Mf[^OȊO͎̎s܂B
                SubFunc.Cmd[0] = 0;                                 // R}hłB
                Ret = gpib.Talk(SubFunc.pCmd, Srlen, srBuffer);     // ̕ۂ̃f[^𑗐M镔łB
                SubFunc.CheckRet("GpTalk", Ret, out csBuf);         // GpTalk̖߂l`FbN
            }
            else
                csBuf = "Mf[^܂B";

            txtRet.Text = csBuf;                                    // ʂ\
        }

        // [M[`] /////////////////////////////////////////////////////////////
        private void btnReceive_Click(object sender, System.EventArgs e)
        {
            int StartIndex = 0;
            int Strlen = 0;
            txtRet.Text = "f[^MĂ܂DDD";
            txtRet.Refresh();                                       // ʂĕ`悵Ă܂B

            SubFunc.Cmd[0] = 0x00;                                  // R}h(X[uł0Œ)
            rvBuffer.Remove(0, rvBuffer.Length);                    // Mobt@NA܂B
            Srlen = (uint)rvBuffer.Capacity;                        // Mf[^̒𑪂܂B
            Ret = gpib.Listen(SubFunc.pCmd, ref Srlen, rvBuffer);   // ۂɎMsƂłB
            if (SubFunc.CheckRet("GpListen", Ret, out csBuf) == 0)
            {                                                       // GpListen̖߂l`FbN
                StartIndex = Convert.ToInt32(Srlen);
                Strlen = rvBuffer.Length - StartIndex;
                txtReceiveData.Text = rvBuffer.Remove(StartIndex, Strlen).ToString();    // srBuffer(Mf[^)\܂B
                szBuffer = string.Format("f[^M܂B [ {0:d} ] oCg擾܂B", Srlen);
                csBuf = szBuffer;                                   // szBuffer(MoCg)\܂B
            }
            txtRet.Text = csBuf;                                    // G[̕\܂B
        }

        // [SRQvON] ////////////////////////////////////////////////////////////////
        private void btnSrqOn_Click(object sender, System.EventArgs e)
        {
            pStb = cboStsByte.Text;                                 // oϐ̕擾܂B
            P_stb = SubFunc.chr2hex(pStb);                          // 𐔓IȒlɕϊ܂B
            Ret = gpib.Ppollmode(2);                                // p|[ɔݒłB
            SubFunc.CheckRet("GpPollmode", Ret, out csBuf);         // GpPpollmode̒l`FbN
            Ret = gpib.Stb(P_stb);                                  // Xe[^XoCgݒ肵܂B
            SubFunc.CheckRet("GpStb", Ret, out csBuf);              // GpStssĂۂɂ̓WX^ɏނłđo͍sĂ܂B
            Ret = gpib.Srq(1);                                      // SRQ(EOI ON)StsByteSRQ𑗏o܂B
            SubFunc.CheckRet("GpSrq", Ret, out csBuf);              // GpSrq̖߂l`FbN

            timer.Enabled = true;                                   // ^C}[`Ăяo܂B

            txtRet.Text = "}X^̃|[O҂Ă܂...";
            txtRet.Refresh();
        }

        // [SRQ~] //////////////////////////////////////////////////////////////////
        private void btnSrqOff_Click(object sender, System.EventArgs e)
        {
            timer.Enabled = false;
            txtRet.Text = "uSRQv̑o~܂B";
        }

        // [I[`] /////////////////////////////////////////////////////////
        private void btnExt_Click(object sender, System.EventArgs e)
        {
            SubFunc.GpibExit();                                     // I֐ĂяoĂ܂B
            this.Close();                                           // _CAO܂B(_CAO̔j)
        }

        // [OnTimer֐] //////////////////////////////////////////////////////////////
        private void timer_Tick(object sender, System.EventArgs e)
        {
            Ret = gpib.Chkstb(out P_stb, out P_eoi);                // Xe[^XoCgEOImF܂B
            SubFunc.CheckRet("StatusByte", Ret, out csBuf);         // Xe[^XoCg̃`FbN

            if ((P_stb & 0x40) != 0x40)
            {                                                       // 0x40(6rbg)`FbN
                txtRet.Text = "[SRQ] F܂B";
                timer.Enabled = false;                              // ^C}~܂B
            }
        }

        // [f[^] ///////////////////////////////////////////////////////////////
        private void btnDelData_Click(object sender, System.EventArgs e)
        {
            txtSendData.Text    = "";
            txtReceiveData.Text = "";
        }

        // [߂l] ///////////////////////////////////////////////////////////////
        private void btnDelRet_Click(object sender, System.EventArgs e)
        {
            txtRet.Text = "";
        }

        private void cboDelimBox_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            Eoi = 1;                                                // EOI Fgp
            Delim = (uint)cboDelimBox.SelectedIndex;
            Ret = gpib.Delim(Delim, Eoi);
        }
    }
}