using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Net;
using System.Net.Sockets;

namespace DUCP_Demo
{
    public partial class Form1 : Form
    {
        /*定义网络环境变量 默认IP为192.168.0.10，端口为7*/
        public static UdpClient DUCP_NetSocket;
        public static IPAddress DeviceIP = IPAddress.Parse("192.168.0.60");
        public static IPEndPoint RPoint = new IPEndPoint(DeviceIP, 8888);
        public static IPEndPoint TPoint = new IPEndPoint(DeviceIP, 8888);
        public static int volume = 10;
        /*
            #0.实现协议栈数据输出回调函数
        */
        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        unsafe public delegate byte cbDUCP_DataOutType(byte* Data, int Size);
        [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
        extern static void MB_STK_SetOutCallback(cbDUCP_DataOutType cb);
        [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
        unsafe extern static sbyte MB_STK_In(byte* Data, int Size);
        private static cbDUCP_DataOutType cbDuCP_dataout;
        unsafe static byte cbDUCP_DataOut(byte* Data, int Size)
        {
            sbyte Ret;
            byte[] buff = new byte[256];
            byte[] RxBuff = new byte[256];
            for (int i = 0; i < Size; i++)
            {
                buff[i] = Data[i];
            }

            /*发送数据到显示屏*/
            DUCP_NetSocket.Send(buff, Size, TPoint);

            /*接收显示屏回复的数据*/
            RxBuff = DUCP_NetSocket.Receive(ref RPoint);

            /*将显示屏的回复的数据传输给协议栈*/
            unsafe
            {
                fixed (byte* pArray = RxBuff)
                    Ret = MB_STK_In(pArray, RxBuff.Length);
            }
            return (byte)Ret;
        }

        public Form1()
        {
            InitializeComponent();
            /*#1.设置协议栈的回调函数*/
            unsafe
            {
                cbDuCP_dataout = new cbDUCP_DataOutType(cbDUCP_DataOut);
                MB_STK_SetOutCallback(cbDuCP_dataout);
            }
            //GC.KeepAlive(cbDuCP_dataout);
            /*#2.创建UDP网络套接字*/
            DUCP_NetSocket = new UdpClient();

            TextBox.Text = "请缴费100元";
            TextBox_WID.Text = "0";
            DUCP_HOST_SYS_SetVol(volume);

        }

        //调节音量
        [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
        unsafe extern static byte DUCP_HOST_SYS_SetVol(int vol);
        //播放语音
        [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
        unsafe extern static byte DUCP_HOST_TTS_Play(byte* pText, byte Opt);
        //停止播放语音
        [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
        unsafe extern static byte DUCP_HOST_TTS_Stop();
        //重启系统
        [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
        unsafe extern static byte DUCP_HOST_SYS_Reset();
        //显示文本
        [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
        unsafe extern static byte DUCP_HOST_TWIN_DisText(int WinID,
                                                            byte* pText,
                                                            int EnterMode,
                                                            int EnterSpeed,
                                                            int DelayMode,
                                                            int DelayTime,
                                                            int EixtMode,
                                                            int EixtSpeed,
                                                            int FontIndex,
                                                            int TextColor,
                                                            int BkColor,
                                                            int DisTimes);
        private void DISTEXT_Click(object sender, EventArgs e)
        {

            unsafe
            {
                byte[] Buff = System.Text.Encoding.Default.GetBytes(TextBox.Text);
                //byte[] Str = new byte[Buff.Length + 1];
                int WID;
                //int i;
                //for(i = 0;i < Buff.Length;i ++) {
                //    Str[i] = Buff[i];
                //}
                //Str[i] = 0;
                int.TryParse(TextBox_WID.Text, out WID);
                fixed (byte* pArray = Buff)
                    try
                    {
                        DUCP_HOST_TWIN_DisText(WID, pArray,
                            0X15, 1,
                            0X0B, 3,
                            0X15, 1,
                            3, 0Xff, 0X00, 0);
                    }
                    catch (Exception ex) { Console.WriteLine(ex.Message); }
            }
        }

        private void 播放语音_Click(object sender, EventArgs e)
        {
            unsafe
            {
                try
                {
                    byte[] Buff = System.Text.Encoding.Default.GetBytes(TextBox.Text);
                    fixed (byte* pArray = Buff)
                    {
                        DUCP_HOST_TTS_Stop();
                        DUCP_HOST_TTS_Play(pArray, 0x01);
                    }
                }
                catch (Exception ex)
                { Console.WriteLine(ex.Message); }
            }
        }

        [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
        extern static byte DUCP_HOST_TWIN_Create(int WinID, int Stile, int x0, int y0, int xSize, int ySize);
        [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
        extern static byte DUCP_HOST_TWIN_DelFile(int WinID, int FileID);
        [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
        extern static byte DUCP_HOST_TWIN_Del(int WinID);
        [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
        extern static byte DUCP_HOST_PIC_Del(int WinID);
        [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
        extern static byte DUCP_HOST_2D_SetColor(int Color);
        [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
        extern static byte DUCP_HOST_2D_FillRect(int x0, int y0, int x1, int y1);
        [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
        extern static byte DUCP_HOST_2D_DrawSysIcon(int x0, int y0, int Num, int Denom, int IconIndex);
        [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
        extern static byte DUCP_HOST_PIC_Create(int WinID, int x0, int y0, int xSize, int ySize);
        [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
        extern static byte DUCP_HOST_PIC_Add(int WinID, int FID, byte EnterMode, byte EnterSpeed,
                                            byte StopMode, byte StopTime, byte ExitMode, byte ExitSpeed);
        //更新IP
        [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
        unsafe extern static byte DUCP_HOST_SYS_SetIP(byte* ip);
        //更新端口
        [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
        unsafe extern static byte DUCP_HOST_SYS_SetPort(int port);
        //显示已存文件
        [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
        unsafe extern static byte DUCP_HOST_TWIN_DisFile(int WinID, int FileID);
        [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
        unsafe extern static byte DUCP_HOST_TWIN_DownTextFile(int WinID,
                                                                int FileID,
                                                                byte* pText,
                                                                int EnterMode,
                                                                int EnterSpeed,
                                                                int DelayMode,
                                                                int DelayTime,
                                                                int EixtMode,
                                                                int EixtSpeed,
                                                                int FontIndex,
                                                                int TextColor,
                                                                int BkColor);

        /// <summary>
        /// CRC16 
        /// </summary>
        /// <param name="data">要进行计算的数组</param>
        /// <returns>计算后的数组</returns>
        private static byte[] CRC16(byte[] crcbuf)
        {
            int crc = 0xffff;
            int len = crcbuf.Length;
            byte[] redata = new byte[2];
            for (int n = 0; n < len; n++)
            {
                byte i;
                crc = crc ^ crcbuf[n];
                for (i = 0; i < 8; i++)
                {
                    int TT;
                    TT = crc & 1;
                    crc = crc >> 1;
                    crc = crc & 0x7fff;
                    if (TT == 1)
                    {
                        crc = crc ^ 0xa001;
                    }
                    crc = crc & 0xffff;
                }
            }
            redata[1] = (byte)((crc >> 8) & 0xff);
            redata[0] = (byte)((crc & 0xff));
            return redata;
        }

        private void BUTTON_DEMO1_Click(object sender, EventArgs e)
        {
            DUCP_HOST_TWIN_Del(0);
            DUCP_HOST_TWIN_Del(1);
            DUCP_HOST_TWIN_Del(2);
            DUCP_HOST_TWIN_Del(3);
            DUCP_HOST_TWIN_Create(0, 0, 0, 0, 64, 16);
            DUCP_HOST_TWIN_Create(1, 0, 0, 16, 64, 16);
            DUCP_HOST_TWIN_Create(2, 0, 0, 32, 64, 16);
            DUCP_HOST_TWIN_Create(3, 0, 0, 48, 64, 16);
            //DUCP_HOST_TWIN_Create(4, 0, 0, 64, 64, 16);
            //DUCP_HOST_TWIN_Create(5, 0, 0, 80, 64, 16);
            //DUCP_HOST_TWIN_Create(6, 0, 0, 96, 64, 16);
            //DUCP_HOST_TWIN_Create(7, 0, 0, 112, 64, 16);

            unsafe
            {
                byte[] Buff = System.Text.Encoding.Default.GetBytes("现在时间`C`Y年`M月`D日 星期`V `H:`N:`S");
                fixed (byte* pArray = Buff)
                    DUCP_HOST_TWIN_DownTextFile(0,
                                                0,
                                                pArray,
                                                0X15,
                                                1,
                                                0X00,
                                                2,
                                                0X15,
                                                1,
                                                3,
                                                0XFF,
                                                0);
                Buff = System.Text.Encoding.Default.GetBytes("刚才星期`V `H:`N:`S");
                fixed (byte* pArray = Buff)
                    DUCP_HOST_TWIN_DownTextFile(0,
                                                1,
                                                pArray,
                                                0X15,
                                                1,
                                                0X00,
                                                2,
                                                0X15,
                                                1,
                                                3,
                                                0XFF,
                                                0);
                Buff = System.Text.Encoding.Default.GetBytes("这是第三个`C`Y年`M月`D日");
                fixed (byte* pArray = Buff)
                    DUCP_HOST_TWIN_DownTextFile(0,
                                                2,
                                                pArray,
                                                0X15,
                                                1,
                                                0X00,
                                                2,
                                                0X15,
                                                1,
                                                3,
                                                0XFF,
                                                0);
                Buff = System.Text.Encoding.Default.GetBytes("这是第四个`C`Y年`M月`D日");
                fixed (byte* pArray = Buff)
                    DUCP_HOST_TWIN_DownTextFile(0,
                                                3,
                                                pArray,
                                                0X15,
                                                1,
                                                0X00,
                                                2,
                                                0X15,
                                                1,
                                                3,
                                                0XFF,
                                                0);
                byte[] Buff2 = System.Text.Encoding.Default.GetBytes("欢迎光临");
                fixed (byte* pArray = Buff2)
                    for (int i = 1; i < 8; i++)
                    {
                        DUCP_HOST_TWIN_DownTextFile(i,
                                                    0,
                                                    pArray,
                                                    0X15,
                                                    1,
                                                    0X00,
                                                    2,
                                                    0X15,
                                                    1,
                                                    3,
                                                    0XFF,
                                                    0);
                    }
            }
        }

        private void BUTTON_DEMO2_Click(object sender, EventArgs e)
        {
            // TODO:  在此添加控件通知处理程序代码
            //删除文本控件
            DUCP_HOST_TWIN_Del(0);
            DUCP_HOST_TWIN_Del(1);
            //删除图片控件
            DUCP_HOST_PIC_Del(4);
            DUCP_HOST_PIC_Del(5);

            DUCP_HOST_2D_SetColor(0);
            //DUCP_HOST_2D_FillRect(0, 0, 63, 31);
            DUCP_HOST_2D_DrawSysIcon(-7, 0, 1, 3, 43);

            DUCP_HOST_PIC_Create(4, 24, 16, 48, 16);
            DUCP_HOST_PIC_Add(4, 45, 1, 2, 0, 0, 6, 6);

            DUCP_HOST_TWIN_Create(0, 0, 19, 0, 48, 8);
            DUCP_HOST_TWIN_Create(1, 0, 17, 8, 48, 8);

            unsafe
            {
                byte[] Buff = System.Text.Encoding.Default.GetBytes("`H:`N:`S");
                fixed (byte* pArray = Buff)
                    DUCP_HOST_TWIN_DisText(0,
                                            pArray,
                                            0X00,
                                            1,
                                            0X08,
                                            255,
                                            0x00,
                                            1,
                                            0,
                                            0XFF,
                                            0, 0);
                byte[] Buff2 = System.Text.Encoding.Default.GetBytes("`D-`M-`Y");
                fixed (byte* pArray = Buff2)
                    DUCP_HOST_TWIN_DisText(1,
                                            pArray,
                                            0X00,
                                            1,
                                            0X08,
                                            255,
                                            0x00,
                                            1,
                                            0,
                                            0XFF,
                                            0, 0);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DUCP_HOST_SYS_SetVol(++volume);
            label3.Text = "音量：" + volume;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DUCP_HOST_SYS_SetVol(--volume);
            label3.Text = "音量：" + volume;
        }

        private void slider1_ValueChanged(object sender, EventArgs e)
        {
            volume = slider1.Value / 2;
            try
            {
                DUCP_HOST_SYS_SetVol(volume);
            }
            catch (Exception ex) { Console.WriteLine(ex.Message); }
            label3.Text = "音量：" + volume;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DUCP_HOST_SYS_Reset();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DUCP_HOST_TWIN_Del(0);
            DUCP_HOST_TWIN_Del(1);
            DUCP_HOST_TWIN_Del(2);
            DUCP_HOST_TWIN_Del(3);
            DUCP_HOST_TWIN_Create(0, 0, 0, 0, 64, 16);
            DUCP_HOST_TWIN_Create(1, 0, 0, 16, 64, 16);
            DUCP_HOST_TWIN_Create(2, 0, 0, 32, 64, 16);
            DUCP_HOST_TWIN_Create(3, 0, 0, 48, 64, 16);
            byte[] Buff1 = System.Text.Encoding.Default.GetBytes("*请重新停车*");
            byte[] Buff2 = System.Text.Encoding.Default.GetBytes("X轴超限");
            byte[] Buff3 = System.Text.Encoding.Default.GetBytes("Y轴超限");
            byte[] Buff4 = System.Text.Encoding.Default.GetBytes("角度超限");
            byte[][] Buffs = new byte[4][];
            Buffs[0] = Buff1;
            Buffs[1] = Buff2;
            Buffs[2] = Buff3;
            Buffs[3] = Buff4;
            unsafe
            {
                for (int i = 0; i < 4; i++)
                {
                    fixed (byte* pArray = Buffs[i])

                        DUCP_HOST_TWIN_DownTextFile(i,
                                                    0,
                                                    pArray,
                                                    0X15,
                                                    1,
                                                    0X00,
                                                    2,
                                                    0X15,
                                                    1,
                                                    3,
                                                    0XFF,
                                                    0);
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            DUCP_HOST_TWIN_DisFile(0, 0);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            IPAddress ipa = null;
            int port = 0;
            try
            {
                ipa = IPAddress.Parse(textBox_ip.Text);
                port = Int32.Parse(textBox_port.Text);
            }
            catch (Exception ex) { Console.WriteLine(ex.Message); }
            if (ipa != null)
            {

                unsafe
                {
                    try
                    {
                        byte[] protocol = new byte[] {
                            0x00,0x64,0xff,0xff,0x08,0x04,
                            0x00,0x00,0x00,0x00,
                            0xE6,0x17
                        };
                        byte[] Buff = ipa.GetAddressBytes();
                        Buff.CopyTo(protocol, 6);

                        byte[] temp = new byte[10];
                        Array.Copy(protocol, temp, 10);
                        byte[] crc = CRC16(temp);
                        Array.Copy(crc,0,protocol,10,2);
                        
                        //byte[] Buff2 = new byte[8];
                        //Buff.CopyTo(Buff2, 0);
                        //Buff.CopyTo(Buff2, 4);
                        fixed (byte* pArray = protocol)
                        {
                            cbDUCP_DataOut(pArray, 12);
                            //DUCP_HOST_SYS_SetIP(pArray);
                        }
                    }
                    catch (Exception ex)
                    { Console.WriteLine(ex.Message); }
                }
                DUCP_HOST_SYS_SetPort(port);
            }
        }

        /// <summary>
        /// 刷新屏幕
        /// </summary>
        public void DispRefresh()
        {
            DUCP_HOST_TWIN_Del(0);
            DUCP_HOST_TWIN_Del(1);
            DUCP_HOST_TWIN_Del(2);
            DUCP_HOST_TWIN_Del(3);
            DUCP_HOST_TWIN_Create(0, 0, 0, 0, 64, 16);
            DUCP_HOST_TWIN_Create(1, 0, 0, 16, 64, 16);
            DUCP_HOST_TWIN_Create(2, 0, 0, 32, 64, 16);
            DUCP_HOST_TWIN_Create(3, 0, 0, 48, 64, 16);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            DispRefresh();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            byte[] protocol = new byte[] {
                            0x00,0x64,0xff,0xff,0x08,0x04,
                            0xc0,0xa8,0x00,0xbc,
                        };
            byte[] temp = CRC16(protocol);
            //Console.WriteLine(Convert.ToString(temp[0],2)+","+ Convert.ToString(temp[1], 2));
        }
    }
}
