using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

//Source: http://www.caliper2pc.de

namespace CSharp_API_Demo
{
    public partial class CSharp_API_Demo : Form
    {
        static IntPtr p1;
        static IntPtr p2;
        static IntPtr p3;
        static IntPtr p4;
        static IntPtr p5;
        static IntPtr p6;
        static IntPtr p7;
        static IntPtr p8;
        static IntPtr p9;
        static IntPtr p10;
        static IntPtr p11;
        static IntPtr p12;
        static IntPtr p13;
        static IntPtr p14;

        //******************** 32 BIT ********************
        //"Exchange.dll" für 32 bit Systeme benutzen. 
        //Use "Exchange.dll" on 32 bit Systems. 
        const string MyDLL = "Exchange.dll";

        //******************** 64 BIT ********************
        //"Exchange64.dll" für 64 bit Systeme benutzen. 
        //Use "Exchange64.dll" on 64 bit Systems. 
        //const string MyDLL = "Exchange64.dll";

        //Funktionsdeklarationen
        //Die "Exchange.dll" (bzw. "Exchange64.dll") muss sich im gleichen Verzeinis befinden wie die *.exe Datei!!!

        //Function declaration
        //The "Exchange.dll" (or "Exchange64.dll") and the *.exe file must be in the same directory!!!

        public CSharp_API_Demo()
        {
            InitializeComponent();
        }

        [DllImport(MyDLL)]
        static extern IntPtr GetChannel1();
        [DllImport(MyDLL)]
        static extern IntPtr GetChannel2();
        [DllImport(MyDLL)]
        static extern IntPtr GetChannel3();
        [DllImport(MyDLL)]
        static extern IntPtr GetChannel4();
        [DllImport(MyDLL)]
        static extern IntPtr GetChannel5();
        [DllImport(MyDLL)]
        static extern IntPtr GetChannel6();

        [DllImport(MyDLL)]
        static extern IntPtr GetPort1();
        [DllImport(MyDLL)]
        static extern IntPtr GetPort2();
        [DllImport(MyDLL)]
        static extern IntPtr GetPort3();
        [DllImport(MyDLL)]
        static extern IntPtr GetPort4();
        [DllImport(MyDLL)]
        static extern IntPtr GetPort5();
        [DllImport(MyDLL)]
        static extern IntPtr GetPort6();

        [DllImport(MyDLL)]
        static extern IntPtr GetSensor1();
        [DllImport(MyDLL)]
        static extern IntPtr GetSensor2();

        [DllImport(MyDLL)]
        static extern IntPtr SetPort1(byte status);
        [DllImport(MyDLL)]
        static extern IntPtr SetPort2(byte status);
        [DllImport(MyDLL)]
        static extern IntPtr SetPort3(byte status);
        [DllImport(MyDLL)]
        static extern IntPtr SetPort4(byte status);
        [DllImport(MyDLL)]
        static extern IntPtr SetPort5(byte status);
        [DllImport(MyDLL)]
        static extern IntPtr SetPort6(byte status);

        static void Tuwas()
        {
            p1 = GetChannel1();
            p2 = GetChannel2();
            p3 = GetChannel3();
            p4 = GetChannel4();
            p5 = GetChannel5();
            p6 = GetChannel6();

            p7 = GetPort1(); //Switch status (1=ON 0=OFF)
            p8 = GetPort2(); //Switch status (1=ON 0=OFF)
            p9 = GetPort3(); //Switch status (1=ON 0=OFF)
            p10 = GetPort4(); //Switch status (1=ON 0=OFF)
            p11 = GetPort5(); //Switch status (1=ON 0=OFF)
            p12 = GetPort6(); //Switch status (1=ON 0=OFF)

            p13 = GetSensor1();
            p14 = GetSensor2();
        }

        private void tmrRefresh_Tick(object sender, EventArgs e)
        {
            Tuwas();

            tbxP1.Text = Marshal.PtrToStringAnsi(p1);
            if (Marshal.PtrToStringAnsi(p1) != "No Server!")
            {
                Marshal.FreeHGlobal(p1);
            }

            tbxP2.Text = Marshal.PtrToStringAnsi(p2);
            if (Marshal.PtrToStringAnsi(p2) != "No Server!")
            {
                Marshal.FreeHGlobal(p2);
            }

            tbxP3.Text = Marshal.PtrToStringAnsi(p3);
            if (Marshal.PtrToStringAnsi(p3) != "No Server!")
            {
                Marshal.FreeHGlobal(p3);
            }

            tbxP4.Text = Marshal.PtrToStringAnsi(p4);
            if (Marshal.PtrToStringAnsi(p4) != "No Server!")
            {
                Marshal.FreeHGlobal(p4);
            }

            tbxP5.Text = Marshal.PtrToStringAnsi(p5);
            if (Marshal.PtrToStringAnsi(p5) != "No Server!")
            {
                Marshal.FreeHGlobal(p5);
            }

            tbxP6.Text = Marshal.PtrToStringAnsi(p6);
            if (Marshal.PtrToStringAnsi(p6) != "No Server!")
            {
                Marshal.FreeHGlobal(p6);
            }

            tbxTasterP1.Text = GetPort1().ToString(); //Switch status (1=ON 0=OFF)
            tbxTasterP2.Text = GetPort2().ToString(); //Switch status (1=ON 0=OFF)
            tbxTasterP3.Text = GetPort3().ToString(); //Switch status (1=ON 0=OFF)
            tbxTasterP4.Text = GetPort4().ToString(); //Switch status (1=ON 0=OFF)
            tbxTasterP5.Text = GetPort5().ToString(); //Switch status (1=ON 0=OFF)
            tbxTasterP6.Text = GetPort6().ToString(); //Switch status (1=ON 0=OFF)

            tbxSensor1.Text = Marshal.PtrToStringAnsi(p13);
            if (Marshal.PtrToStringAnsi(p13) != "No Server!")
            {
                Marshal.FreeHGlobal(p13);
            }

            tbxSensor2.Text = Marshal.PtrToStringAnsi(p14);
            if (Marshal.PtrToStringAnsi(p14) != "No Server!")
            {
                Marshal.FreeHGlobal(p14);
            }
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void cmdOn1_Click(object sender, EventArgs e)
        {
            SetPort1(1); //ON
        }
        private void cmdOff1_Click(object sender, EventArgs e)
        {
            SetPort1(0); //OFF
        }
        private void cmdOn2_Click(object sender, EventArgs e)
        {
            SetPort2(1); //ON
        }
        private void cmdOff2_Click(object sender, EventArgs e)
        {
            SetPort2(0); //OFF
        }
        private void cmdOn3_Click(object sender, EventArgs e)
        {
            SetPort3(1); //ON
        }
        private void cmdOff3_Click(object sender, EventArgs e)
        {
            SetPort3(0); //OFF
        }
        private void cmdOn4_Click(object sender, EventArgs e)
        {
            SetPort4(1); //ON
        }
        private void cmdOff4_Click(object sender, EventArgs e)
        {
            SetPort4(0); //OFF
        }
        private void cmdOn5_Click(object sender, EventArgs e)
        {
            SetPort5(1); //ON
        }
        private void cmdOff5_Click(object sender, EventArgs e)
        {
            SetPort5(0); //OFF
        }
        private void cmdOn6_Click(object sender, EventArgs e)
        {
            SetPort6(1); //ON
        }
        private void cmdOff6_Click(object sender, EventArgs e)
        {
            SetPort6(0); //OFF
        }

        private void CSharp_API_Demo_Load(object sender, EventArgs e)
        {

        }
    }
}