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

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

        ///////////////////////////////////////////////////////// Setting ///
        string szBuffer = new string(' ', 1024);
        uint Ret;
        int Count;
        uint Myadr, Yradr;
        //uint Srlen;
        uint[] Pstb = new uint[32];
        string srBuffer = new string(' ', 10000);
        string csBuf = "";
        string PollBuf = "";
        GCHandle gChCmd;                                // GCHandle for cmd array
        GCHandle gChPstb;                               // GCHandle for Pstb array
        IntPtr pPstb;                                   // IntPtr for Pstb array
        /////////////////////////////////////////////////////////////////////

        public Form1()
        {
            InitializeComponent();
        }

        private void Polling_Load(object sender, System.EventArgs e)
        {
            // ****************************************************** Setting ***
            cboYrAdrBox.SelectedIndex = 1;
            Yradr = Convert.ToUInt32(cboYrAdrBox.Text);
            // ******************************************************************

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

        private void Polling_FormClosed(object sender, FormClosedEventArgs e)
        {
            SubFunc.GpibExit();
            // GCHandle free
            if (gChCmd.IsAllocated == true)
            {
                gChCmd.Free();
            }
            if (gChPstb.IsAllocated == true)
            {
                gChPstb.Free();
            }
        }

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

            btnPolling.Enabled = true;
            SubFunc.GpibInit(out csBuf);
            gpib.Boardsts(0x08, out Myadr);
            DelMyadr = string.Format("{0:d}", Myadr);
            Ret = (uint)cboYrAdrBox.FindStringExact(DelMyadr, -1);
            if (Ret >= 0)
            {
                cboYrAdrBox.Items.Remove(Ret.ToString());
                if (Myadr == Yradr)
                {
                    cboYrAdrBox.SelectedIndex = 0;
                }
            }
            txtRet.Text = csBuf;
            txtMyAdr.Text = Myadr.ToString();

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

        /////////////////////////////////////////////// Polling ///
        private void btnPolling_Click(object sender, System.EventArgs e)
        {
            txtRet.Text = "Polling...";
            txtRet.Refresh();
            csBuf = "GpPoll";
            for (Count = 0; Count < 16; Count++)
            {
                Pstb[Count] = 0x00;
            }
            SubFunc.Cmd[0] = 1;
            SubFunc.Cmd[1] = Yradr;

            Ret = gpib.Poll(SubFunc.pCmd, pPstb);
            if ((Ret & 0xFF) == 0)
            {
                PollBuf = string.Format("[SRQ] is found. Talker = [ {0:x} ] / StatusByte = [ {1:x} ]H", SubFunc.Cmd[Pstb[0]], Pstb[Pstb[0]]);
            }
            else if ((Ret & 0xFF) == 1)
            {
                PollBuf = string.Format("[SRQ]&[EOI] is found. Talker = [{0:x}] / StatusByte = [ {1:x} ]H", SubFunc.Cmd[Pstb[0]], Pstb[Pstb[0]]);
            }
            else if ((Ret & 0xFF) == 128)
            {
                PollBuf = string.Format("[SRQ] was not found out.");
            }
            SubFunc.CheckRet("GpPoll", Ret, out csBuf);         // Not 0,1,128

            txtPollingSts.Text = PollBuf;
            txtRet.Text = csBuf;
        }

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

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

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

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