using MvCamCtrl.NET;
using Newtonsoft.Json;
using ProductionControl.Device;
using ProductionControl.UI;
using ProductionControl.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProductionControl
{
    public partial class Form2 : Form
    {
        enum Motor { X = 'X', Y = 'Y' };
        [DllImport("PomeasDualDll-Z.dll", EntryPoint= "?open@CDComm@@QAEHH@Z")] 
        private static extern bool open(int port);
        [DllImport("PomeasDualDll-Z.dll")]
        private static extern void close();
        [DllImport("PomeasDualDll-Z.dll")]
        private static extern void MoveHome(Motor m);

        //运动到指定位置 *
        [DllImport("PomeasDualDll-Z.dll")]
        private static extern void MoveGoto(Motor m,long dest);
        //获取总长度
        [DllImport("PomeasDualDll-Z.dll")]
        private static extern long MoveMaxLength(Motor m);
        //获取当前位置
        [DllImport("PomeasDualDll-Z.dll")]
        private static extern long MovePos(Motor m);

        [DllImport("PMSOpticalDll.dll", CharSet = CharSet.Ansi, EntryPoint = "PMSOptical_OpenComm", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr PMSOptical_OpenComm(string comname, int nBaud);
        SSerialPort sSerialPort;// = new SSerialPort("COM4", 2400,1); 
        HeightDev dev = new HeightDev(); 
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        { 
            return;
            string Scanner_GENTL_CTI = "ScannerRuntime\\Win64_x64\\MvFGProducerCXP(MV-GX1004).cti";
            MyCamera.MV_GENTL_IF_INFO_LIST m_stIFInfoList = new MyCamera.MV_GENTL_IF_INFO_LIST();
            int nRet = MyCamera.MV_CC_EnumInterfacesByGenTL_NET(ref m_stIFInfoList, Scanner_GENTL_CTI);
            if (0 != nRet)
            {
                return;
            }

            //this.uiScannerDev1.init(Device.ScannerDev.ScannerType.GENTL);
            //uiScannerDev1.log += new Action<int, string>((level, info) =>
            //{

            //});

            //dev.start("192.168.3.35", 64000);
            //var cc = PMSOptical_OpenComm("COM2", 9600);
            return;
            //byte[] getMaxPos = new byte[] { 0x58, 0x4D, 0x0D };
            //sSerialPort.send(getMaxPos, 0, getMaxPos.Length);
            //Thread.Sleep(20);
            //byte[] recv = sSerialPort.getMessage(1000);//9 byte
            //if (recv == null) recv = new byte[0];
            //this.label1.Text = $"{recv.Length} | ";
            //if (recv.Length > 0)
            //{
            //    byte[] maxPos = subBuff(recv, 2, 5);//5 byte
            //    for(int i = 0;i<maxPos.Length;i++)
            //        this.label1.Text += maxPos[i].ToString("X")+" ";
            //}
        }

        private void button1_Click(object sender, EventArgs e)
        {
              
            return;
            //testArr.testArr1 += 1;
            //this.propertyGrid1.Refresh();
            //str = JsonConvert.SerializeObject(testArr);
            MessageBox.Show(string.Join("|", SSerialPort.getPortNames()));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Text = dev.getHeight().ToString();



            return;
            //int x = 100;
            //string hex = x.ToString("X").PadLeft(6, ' ');
            //byte[] gotoX = new byte[9];
            //gotoX[0] = 0x58;
            //gotoX[1] = 0x47;
            //for(int i = 0;i<hex.Length;i++)
            //    gotoX[i+2] = (byte)(hex[i]);
            //gotoX[8] = 0x0D;
            //sSerialPort.send(gotoX, 0, gotoX.Length);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //byte[] getState = new byte[] { 0x58, 0x5A, 0x0D };
            //sSerialPort.send(getState, 0, getState.Length);
            //Thread.Sleep(20);
            //byte[] recv=sSerialPort.getMessage(1000);
            //if (recv == null) recv = new byte[0];
            //this.Text = $"{recv.Length} | ";
            //if (recv.Length > 0)
            //    this.Text += recv[0].ToString("X");
        }
        private void button4_Click(object sender, EventArgs e)
        {
            //byte[] goHome = new byte[] { 0x58, 0x48, 0x0D };
            //sSerialPort.send(goHome, 0, goHome.Length);

            //var buff = sSerialPort.getMessage(-1);
            //if (buff == null)
            //    return;
            //if (buff.Length % 11 != 0)
            //    throw new Exception("数据错误！");
            //if (buff.Length > 11)
            //    buff = subBuff(buff, buff.Length - 11, 11);
            //if (buff[0] != 0x10)
            //    throw new Exception("数据0错误！");
            //int add = 0;
            //for (int i = 1; i < 10; i++)
            //    add += (int)buff[i];
            //if (add != buff[10])
            //    throw new Exception("数据ADD错误！");

            //string unit = buff[3] == 0x10 ? "N/cm" : "Kgf/cm";
            //string data = "";
            //int zf = 1;
            //for (int i = 4; i <= 8; i++)
            //{
            //    if (buff[i] == 0x0a) continue;
            //    if (buff[i] == 0x0b) zf = -1;
            //    if (buff[i] <0x0a)
            //        data += (int)buff[i];
            //}
            //double lfNum = Convert.ToSingle(data) * zf;
            //if (buff[9] > 0) lfNum /= Math.Pow(10, buff[9]);
            //this.Text = lfNum + " " + unit;
        }
        
        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            dev.stop();
            //sSerialPort.close();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            byte[] getPos = new byte[] { 0x58, 0x4E, 0x0D };
            sSerialPort.send(getPos, 0, getPos.Length);
            Thread.Sleep(20);
            byte[] recv = sSerialPort.getMessage(1000);//9 byte
            if (recv == null) recv = new byte[0];
            this.label2.Text = $"{recv.Length} | ";
            if (recv.Length > 0)
            {
                byte[] maxPos = subBuff(recv, 2, 5);//5 byte
                for (int i = 0; i < maxPos.Length; i++)
                    this.label2.Text += maxPos[i].ToString("X") + " ";
            }
        }

        private byte[] subBuff(byte[] buff,int start,int length)
        {
            byte[] res=new byte[length];
            for(int i=start;i<buff.Length && i<start+length;i++)
                res[i-start] = buff[i];
            return res;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //button4_Click(sender, e);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //label3.Text = dev.getDecimal().ToString();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            dev.reset();
        }
    }
}
