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

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

        ///////////////////////////////////////////////////////// Setting ///
        string szBuffer = new string(' ', 256);
        uint Ret;
        int Count, Stppu;
        uint Madr;
        uint Yradr1, Yradr2;
        uint PDataLine;
        uint[] Cmd2 = new uint[32];
        uint[] Pstb = new uint[32];
        uint[] Pstb2 = new uint[32];
        string csBuf = "";
        GCHandle gChCmd;                                // GCHandle for cmd array
        GCHandle gChPstb;                               // GCHandle for Pstb array
        IntPtr pPstb;                                   // IntPtr for Pstb array
        GCHandle gChCmd2;                               // GCHandle for cmd2 array
        GCHandle gChPstb2;                              // GCHandle for Pstb2 array
        IntPtr pPstb2;                                  // IntPtr for Pstb2 array
        IntPtr pCmd2;                                   // IntPtr for cmd2 array
        /////////////////////////////////////////////////////////////////////

        public Form1()
        {
            InitializeComponent();
        }

        private void Parallel_Load(object sender, System.EventArgs e)
        {
            //******************************************************* Setting ***
            cboYrAdrBox1.Text = "1";
            cboYrAdrBox2.Text = "2";
            //*******************************************************************

            // Specify Pinned handle to Cmd
            if (gChCmd.IsAllocated == false)
            {
                gChCmd = GCHandle.Alloc(SubFunc.Cmd, GCHandleType.Pinned);
            }
            if (gChCmd2.IsAllocated == false)
            {
                gChCmd2 = GCHandle.Alloc(Cmd2, GCHandleType.Pinned);
            }
            // Specify Pinned handle to Pstb
            if (gChPstb.IsAllocated == false)
            {
                gChPstb = GCHandle.Alloc(Pstb, GCHandleType.Pinned);
            }
            if (gChPstb2.IsAllocated == false)
            {
                gChPstb2 = GCHandle.Alloc(Pstb2, GCHandleType.Pinned);
            }
            // Get address object by Pinned handle
            SubFunc.pCmd = gChCmd.AddrOfPinnedObject();
            pCmd2 = gChCmd2.AddrOfPinnedObject();
            pPstb = gChPstb.AddrOfPinnedObject();
            pPstb2 = gChPstb2.AddrOfPinnedObject();
        }

        private void Parallel_FormClosed(object sender, FormClosedEventArgs e)
        {
            SubFunc.GpibExit();

            // GCHandle free
            if (gChCmd.IsAllocated == true)
            {
                gChCmd.Free();
            }
            if (gChPstb.IsAllocated == true)
            {
                gChPstb.Free();
            }
            if (gChCmd2.IsAllocated == true)
            {
                gChCmd2.Free();
            }
            if (gChPstb2.IsAllocated == true)
            {
                gChPstb2.Free();
            }
        }

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

            SubFunc.GpibInit(out csBuf);
            gpib.Boardsts(0x08, out Madr);
            DelMadr = string.Format("{0:d}", Madr);
            Ret = (uint)cboYrAdrBox1.FindStringExact(DelMadr, -1);
            if (Ret >= 0 && Ret <= 30)
            {
                cboYrAdrBox1.Items.Remove(Ret.ToString());
                cboYrAdrBox1.SelectedIndex = 0;
                Yradr1 = Convert.ToUInt32(cboYrAdrBox1.Text);
                cboYrAdrBox2.Items.Remove(Ret.ToString());
                cboYrAdrBox2.SelectedIndex = 1;
            }
            txtRet.Text = csBuf;

            cboYrAdrBox1.Enabled = true;
            cboYrAdrBox2.Enabled = true;
            btnPolling.Enabled = true;

            Yradr1 = Convert.ToUInt32(cboYrAdrBox1.Text);
            Yradr2 = Convert.ToUInt32(cboYrAdrBox2.Text);
        }

        /////////////////////////////////////////////// Polling ///
        private void btnPolling_Click(object sender, System.EventArgs e)
        {
            string csBuf1, csBuf2;

            for (Count = 0; Count < 16; Count++)
            {   // Pstb[],Pstb2[]Initialize
                Pstb[Count] = 0x00;
                Pstb2[Count] = 0x00;
            }
            txtPollSet.Text = "";
            txtPollStart.Text = "";
            txtPollSts.Text = "";

            SubFunc.Cmd[0] = 2;                         // Number of Address of device 
            SubFunc.Cmd[1] = Yradr1;                    // 1st
            SubFunc.Cmd[2] = 0x69;                      // Line 69H = DIO2
            SubFunc.Cmd[3] = Yradr2;                    // 2nd
            SubFunc.Cmd[4] = 0x6a;                      // Line 6aH = DIO3
            Stppu = 1;                                  // PPU Setting

            Ret = gpib.Stppoll(SubFunc.pCmd, (uint)Stppu);      // Parallel Poll
            SubFunc.CheckRet("GpStppoll", Ret, out csBuf);
            txtPollSet.Text = csBuf;
            if ((Ret & 0xFF) != 0)
            {
                return;
            }
            Ret = gpib.Exppoll(out PDataLine);          // Serial Poll
            SubFunc.CheckRet("GpExppoll", Ret, out csBuf);
            txtPollStart.Text = csBuf;
            if ((Ret & 0xFF) != 0)
            {
                return;
            }

            switch (PDataLine)
            {                                           // Select value of PDataLine
                case 2:                                 // [ 00000010(DIO2) ]
                    SubFunc.Cmd[0] = 1;
                    SubFunc.Cmd[1] = Yradr1;
                    Ret = gpib.Poll(SubFunc.pCmd, pPstb);
                    SubFunc.CheckRet("GpPoll", Ret, out csBuf);
                    if (((Ret & 0xFF) != 0) && ((Ret & 0xFF) != 1))
                    {
                        txtPollSts.Text = csBuf;
                        return;
                    }
                    szBuffer = string.Format("Talker = [ {0:d} ] StatusByte =[ {1:x} ]H", SubFunc.Cmd[Pstb[0]], Pstb[Pstb[0]]);
                    txtPollSts.Text = szBuffer;
                    break;
                case 4:                                 // [ 00000100(DIO3) ]
                    SubFunc.Cmd[0] = 1;
                    SubFunc.Cmd[1] = Yradr2;
                    Ret = gpib.Poll(SubFunc.pCmd, pPstb);
                    SubFunc.CheckRet("GpPoll", Ret, out csBuf);
                    if (((Ret & 0xFF) != 0) && ((Ret & 0xFF) != 1))
                    {
                        txtPollSts.Text = csBuf;
                        return;
                    }
                    szBuffer = string.Format("Talker = [ {0:d} ] StatusByte = [ {1:x} ]H", SubFunc.Cmd[Pstb[0]], Pstb[Pstb[0]]);
                    txtPollSts.Text = szBuffer;
                    break;
                case 6:                                 // [ 00000110(DIO2 | DIO3) ]
                    SubFunc.Cmd[0] = 1;
                    SubFunc.Cmd[1] = Yradr1;
                    Ret = gpib.Poll(SubFunc.pCmd, pPstb);
                    SubFunc.CheckRet("Target1", Ret, out csBuf1);
                    if (((Ret & 0xFF) != 0) && ((Ret & 0xFF) != 1))
                    {
                        txtPollSts.Text = csBuf1;
                        return;
                    }

                    Cmd2[0] = 1;
                    Cmd2[1] = Yradr2;
                    Ret = gpib.Poll(pCmd2, pPstb2);
                    SubFunc.CheckRet("Target2", Ret, out csBuf2);
                    if (((Ret & 0xFF) != 0) && ((Ret & 0xFF) != 1))
                    {
                        txtPollSts.Text = csBuf2;
                        return;
                    }

                    szBuffer = string.Format("Talker = [ {0:d} ] StatusByte = [ {1:x} ]H / Talker = [ {2:d} ] StatusByte =[ {3:x} ]H",
                        SubFunc.Cmd[Pstb[0]], Pstb[Pstb[0]], Cmd2[Pstb2[0]], Pstb2[Pstb2[0]]);
                    txtPollSts.Text = szBuffer;
                    break;
                default:
                    csBuf = "[SRQ] is not found out.";
                    txtPollSts.Text = csBuf;
                    break;
            }
        }

        /////////////////////////////////////// Address of device 1 Change ///
        private void cboYrAdrBox1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            Yradr1 = Convert.ToUInt32(cboYrAdrBox1.Text);
        }

        /////////////////////////////////////// Address of device 2 Change ///
        private void cboYrAdrBox2_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            Yradr2 = Convert.ToUInt32(cboYrAdrBox2.Text);
        }

        /////////////////////////////////////////// Delete Poll ///
        private void btnPollDel_Click(object sender, System.EventArgs e)
        {
            txtPollSet.Text = "";
            txtPollStart.Text = "";
            txtPollSts.Text = "";
        }

        ///////////////////////////////////// ReturnCode Delete ///
        private void btnRetDel_Click(object sender, System.EventArgs e)
        {
            txtRet.Text = "";
        }

        ////////////////////////////////////////////////// Exit ///
        private void btnExt_Click(object sender, System.EventArgs e)
        {
            SubFunc.GpibExit();
            this.Close();
        }
    }
}