using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using iTekCAN;
using System.Runtime.InteropServices;
using System.Threading;
using SM4;
using iTekCANFDDemon;
namespace iTek_simple
{

    public partial class Form1 : Form
    {
        private delegate void DelegateSetListBox(string direction, string id, string time, string cantype, string frametype, string frameformat, string len, string data);
        IntPtr device_handle;
        IntPtr channel_handle;
        string[] m_ABaudRate =
        {
           " 01F31302",
            "00F91302",
            "007C1302",
            "00630a02",
            "00311302",
            "001D1204",
            "00181302",
            "00131302",
            "00130A02",
            "00091302",
            "00090A02",
            "00070A02",
            "00021006",
            "00040702"
        };
        string[] m_DBaudRate = {
            "001D0E30",
            "001F0A20",
            "00130A20",
            "000F0A20",
            "00090A20",
            "00070A20",
            "00040A20",
            "00040720",
            "00010A20",
            "00000D40",
            "00000A20",
            "00000720"
        };
        uint[][] m_DevType = {
            new uint[] { 1, 2, 11, 12 },
            new uint[] { 3, 4, 13, 14 },
            new uint[] { 7, 8 }
        };
        int m_channel = 0;
        bool m_openstop;
        recvdatathread recv_data_thread_;
        bool m_bStart = false;
        uint deindex;
        string[][] deviceTypes = {
            new string[] { "CANFD-X100", "CANFD-X200", "CANFD-X400","CANFD-X800" },
            new string[] { "CANFD-L100", "CANFD-L200", "CANFD-L400","CANFD-L800" },
            new string[] { "CANFD-W100", "CANFD-W200" },
        };


        // string m_direction = null;
        // string m_time = null;
        // string m_ID = null;
        //  string m_cantype = null;
        //  string m_frametype;
        //  string m_frameformat = null;
        //  string m_len = null;
        //  string m_data = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void CANfrom_laod(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0; //接口选择
            m_pComboxDeviceType.SelectedIndex = 0;
            m_pComboxnIndex.SelectedIndex = 0;
            m_pComboxProtrol.SelectedIndex = 2;
            m_PComboxChannel.SelectedIndex = 0;
            m_pComboxFDStandard.SelectedIndex = 0;
            m_pComboxABit.SelectedIndex = 11;
            m_pComboxDBit.SelectedIndex = 8;
            m_pComboxWorkModel.SelectedIndex = 0;
            comboBox11.SelectedIndex = 0;
            comboBox12.SelectedIndex = 0;
            comboBox13.SelectedIndex = 1;
            comboBox14.SelectedIndex = 0;
            comboBox15.SelectedIndex = 0;
            comboBox16.SelectedIndex = 0;
            m_pComboxNetWorkModel.SelectedIndex = 0;
            textBox1.Text = "1";
            textBox2.Text = "00 01 02 03 04 05 06 07 08 09 0A 0B 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00";
            textBox5.Text = "1fffffff";
            textBox6.Text = "0";
            textBox3.Text = "0";
            textBox4.Text = "7FF";
            button3.Enabled = false;
            button2.Enabled = false;
            m_pComboxProtrol.Enabled = false;
            string ip = GetLocalIP();
            string[] ipArray = ip.Split(',');
            for (int i = 0; i < ipArray.Length; i++)
            {
                m_pComBoxLocalip.Items.Insert(i, ipArray[i]);
            }
            m_pComBoxLocalip.SelectedIndex = 0;

        }

        private int SplitData(string data, ref byte[] transData, int maxLen)
        {
            string[] dataArray = data.Split(' ');
            for (int i = 0; (i < maxLen) && (i < dataArray.Length); i++)
            {
                transData[i] = Convert.ToByte(dataArray[i].Substring(0, 2), 16);
            }

            return dataArray.Length;
        }

        private int SplitData_(string data, ref byte[] transData, int maxLen)
        {
            string[] dataArray = data.Split(' ');
            for (int i = 0; (i < maxLen) && (i < dataArray.Length); i++)
            {
                transData[i] = Convert.ToByte(dataArray[i].Substring(0, 2), 16);
            }
           
           
            return data.Length;
        }

        private void AddData(iTek_CANFD_Receive_DATA[] data, uint len)
        {
            for (int j = 0; j < len; j++)
            {
                string m_direction = "";
                string m_time = "";
                string m_ID = "";
                string m_cantype = "";
                string m_frametype = "";
                string m_frameformat = "";
                string m_len = "";
                string m_data = "";
                iTek_CANFD_Receive_DATA RecvData = data[j];

                int nDirect = RecvData.frame.can_type << 1;
                nDirect >>= 7;
                if (nDirect == 1)
                {
                    m_direction = "发送";
                }
                else
                {
                    m_direction = "接收";
                }
                m_time = DateTime.Now.TimeOfDay.ToString();

                m_time = m_time.Substring(0, 12);

                uint nID = RecvData.frame.canid & 0x1fffffff;//ID
                uint frametype = (RecvData.frame.canid >> 30);
                frametype &= 0x1;

                uint frameFormat = (RecvData.frame.canid >> 29);
                frameFormat &= 0x1;

                m_ID = nID.ToString("X8");//String.Format("{0:X8}", nID);
                int can_type = RecvData.frame.can_type;
                can_type = can_type & 0x03;
                if (can_type == 0)
                {
                    m_cantype = "CAN";

                }
                else if (can_type == 2)
                {
                    m_cantype = "CANFD";
                }
                else if (can_type == 3)
                {
                    m_cantype = "CANFD加速";
                }

                if (frametype == 0)
                {
                    m_frametype = "标准帧";
                }
                else if (frametype == 1)
                {
                    m_frametype = "扩展帧";
                }

                if (frameFormat == 0)
                {
                    m_frameformat = "数据帧";
                }
                else if (frameFormat == 1)
                {
                    m_frameformat = "远程帧";
                }

                m_len = RecvData.frame.len.ToString();

                for (int k = 0; k < RecvData.frame.len; k++)
                {
                    m_data = String.Format("{0:G}{1:X2}", m_data, RecvData.frame.data[k]);
                }
                //Object[] list = { this, System.EventArgs.Empty };
                this.listBox1.BeginInvoke(new DelegateSetListBox(SetListBox),new object[] {m_direction,m_ID,m_time,m_cantype,m_frametype,m_frameformat,m_len,m_data } );

            }

        }
        private void SetListBox(string direction,string id,string time,string cantype,string frametype,string frameformat,string len,string data)
        {
            string list_box = direction+" " + "ID:" + id + " " + cantype + " " + frametype + " " + frameformat + " " + "数据长度:" + len + "数据：" + data;
            int index = listBox1.Items.Add(list_box);
            listBox1.SelectedIndex = index;

        }
        private void button1_Click(object sender, EventArgs e)
        {
            

            m_channel = m_PComboxChannel.SelectedIndex;
            uint device_index = (uint)this.m_pComboxnIndex.SelectedIndex;
            device_handle = iTekCanDLL.iTek_OpenDevice(deindex, device_index, 0);
            if ((int)device_handle == -1)
            {
                MessageBox.Show("打开设备失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            iTek_CANFD_CHANNEL_INIT_CONFIG config = new iTek_CANFD_CHANNEL_INIT_CONFIG();
            if (deindex == 3 || deindex == 4 || deindex == 7 || deindex == 8|| deindex == 13 || deindex == 14)
            {
                char[] value = new char[2];
                value[0] = (char)0x04;
                tag_IP_Info IPData = new tag_IP_Info();
                IPData.local_ip = new byte[25];
                IPData.dest_ip = new byte[25];
                
                int nSize = Marshal.SizeOf(IPData);
                IPData.model = (byte)m_pComboxNetWorkModel.SelectedIndex;
                if (IPData.model == 0)
                {
                    
                    byte[] dataArray = System.Text.Encoding.Default.GetBytes(m_pComBoxLocalip.Text);
                    Buffer.BlockCopy(dataArray, 0, IPData.local_ip, 0, dataArray.Length);
                    IPData.local_port = Convert.ToUInt16(m_pLineLocalPort.Text, 10);
                    byte[] dataArraydest = System.Text.Encoding.Default.GetBytes(m_pLineDestIP.Text);
                    Buffer.BlockCopy(dataArraydest, 0, IPData.dest_ip, 0, dataArraydest.Length);
                    IPData.dest_port = Convert.ToUInt16(m_pLineDestPort.Text, 10);
                    
                }
                else if (IPData.model == 1)
                {
                    
                    byte[] dataArray = System.Text.Encoding.Default.GetBytes(m_pComBoxLocalip.Text);
                    Buffer.BlockCopy(dataArray, 0, IPData.local_ip, 0, dataArray.Length);
                    IPData.local_port = Convert.ToUInt16(m_pLineLocalPort.Text, 10);
                    

                }
                else if (IPData.model == 2)
                {
                    
                    byte[] dataArray = System.Text.Encoding.Default.GetBytes(m_pLineDestIP.Text);
                    Buffer.BlockCopy(dataArray, 0, IPData.dest_ip, 0, dataArray.Length);
                    IPData.dest_port = Convert.ToUInt16(m_pLineDestPort.Text, 10);
                }
                IntPtr pIPData = Marshal.AllocHGlobal(Marshal.SizeOf(IPData));
                Marshal.StructureToPtr(IPData, pIPData, true);

                iTekCanDLL.iTek_Set_Value(device_handle, value, pIPData);

            }
            else
            {

                string char_buf = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
                Random rd = new Random();
                string buf = null;
                for (int i = 0; i < 44; i++)
                {
                    int ni = (rd.Next() % 36);
                    buf += char_buf.Substring(ni, 1);
                }
                byte[] buf1 = new byte[16];
                iTekCanDLL.iTek_Authenticate(device_handle, 0, buf, 16, buf1);
                string buffer = Convert.ToBase64String(buf1);
                //int a = buffer.Length;

                MainSm4 sm4 = new MainSm4();
                string cleartext = "8888888888888888";

                string buffer1 = System.Text.Encoding.UTF8.GetString(buf1);


                string strdata = sm4.Decrypt_ECB(cleartext, false, buf1);
                buf = buf.Substring(0, 16);
                if (strdata != buf)
                {
                    MessageBox.Show("验证失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                // IntPtr iTek_CANFD_CHANNEL_INIT_CONFIG config;

                
                config.can_type = (byte)m_pComboxProtrol.SelectedIndex;
                config.workMode = (byte)m_pComboxWorkModel.SelectedIndex;
                config.CANFDStandad = (byte)m_pComboxFDStandard.SelectedIndex;
                string abit = m_pLineABitV.Text;
                config.abit_timing = UInt32.Parse(abit, System.Globalization.NumberStyles.HexNumber);
                string dbit = m_pLineDBitV.Text;
                config.dbit_timing = UInt32.Parse(dbit, System.Globalization.NumberStyles.HexNumber);

                config.standard.num = 1;
                config.standard.fitlterdatastandard = new FilterData[128];
                FilterData st_data = new FilterData();
                st_data.frametype = 0;
                st_data.filtertype = (byte)comboBox15.SelectedIndex;
                string st_ID1 = textBox3.Text;
                st_data.ID1 = Convert.ToUInt32(st_ID1, 16);
                string st_ID2 = textBox4.Text;
                // st_data.ID2 = UInt32.Parse(st_ID2);
                st_data.ID2 = Convert.ToUInt32(st_ID2, 16);
                config.standard.fitlterdatastandard[0] = st_data;
                config.extend.num = 1;
                config.extend.filterdataextend = new FilterData[64];
                FilterData ex_data = new FilterData();
                ex_data.frametype = 1;
                ex_data.filtertype = (byte)comboBox16.SelectedIndex;
                string ex_ID1 = textBox6.Text;
                ex_data.ID1 = Convert.ToUInt32(ex_ID1, 16);
                string ex_ID2 = textBox5.Text;
                ex_data.ID2 = Convert.ToUInt32(ex_ID2, 16);
                config.extend.filterdataextend[0] = ex_data;
                

            }
            IntPtr pConfig = Marshal.AllocHGlobal(Marshal.SizeOf(config));
            Marshal.StructureToPtr(config, pConfig, true);


            channel_handle = iTekCanDLL.iTek_InitCAN(device_handle, (byte)m_channel, pConfig);


            if (0 == (int)channel_handle)
            {
                MessageBox.Show("初始化CAN失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            bool ret = iTekCanDLL.iTek_StartCAN(channel_handle);
            if (!ret)
            {
                MessageBox.Show("启动CAN失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);;
                iTekCanDLL.iTek_ResetCAN(channel_handle);
                iTekCanDLL.iTek_CloseDevice(device_handle);
                return;
            }
            m_bStart = true;
            if (null == recv_data_thread_)
            {
                recv_data_thread_ = new recvdatathread();
                recv_data_thread_.setChannelHandle(channel_handle);
                recv_data_thread_.setStart(m_bStart);
                recv_data_thread_.RecvCANData += this.AddData;
            }
            else
            {
                recv_data_thread_.setChannelHandle(channel_handle);
            }
            m_pButtonDevice.Enabled = false;
            button2.Enabled = true;
            button3.Enabled = true;
            m_openstop = true;
        }

        private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
        {
           
            {
                m_pLineABitV.Text = m_ABaudRate[m_pComboxABit.SelectedIndex];
            }
        }

        private void comboBox7_SelectedIndexChanged(object sender, EventArgs e)
        {
           
            {
                m_pLineDBitV.Text = m_DBaudRate[m_pComboxDBit.SelectedIndex];
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            if (m_openstop == true)
            {
                recv_data_thread_.setStart(false);
                recv_data_thread_.RecvCANData -= this.AddData;
                recv_data_thread_ = null;

                iTekCanDLL.iTek_ResetCAN(channel_handle);
                iTekCanDLL.iTek_CloseDevice(device_handle);
                m_pButtonDevice.Enabled = true;
                button2.Enabled = false;
                button3.Enabled = false;
            }
            m_openstop = false;
        }
        private void send_data(iTek_CANAFD_Transmit_Data[] data,int len)
        {
         for (int j = 0; j<len; j++)
        {
            string m_direction = "";
            string m_time = "";
            string m_ID = "";
            string m_cantype = "";
            string m_frametype = "";
            string m_frameformat = "";
            string m_len = "";
            string m_data = "";
                iTek_CANAFD_Transmit_Data RecvData = data[j];

            int nDirect = RecvData.frame.can_type << 1;
            nDirect >>= 7;
            if (nDirect == 1)
            {
                m_direction = "发送";
            }
            else
            {
                m_direction = "接收";
            }
            m_time = DateTime.Now.TimeOfDay.ToString();

            m_time = m_time.Substring(0, 12);

            uint nID = RecvData.frame.canid & 0x1fffffff;//ID
            uint frametype = (RecvData.frame.canid >> 30);
            frametype &= 0x1;

            uint frameFormat = (RecvData.frame.canid >> 29);
            frameFormat &= 0x1;

            m_ID = nID.ToString("X8");//String.Format("{0:X8}", nID);
            int can_type = RecvData.frame.can_type;
            can_type = can_type & 0x03;
            if (can_type == 0)
            {
                m_cantype = "CAN";

            }
            else if (can_type == 2)
            {
                m_cantype = "CANFD";
            }
            else if (can_type == 3)
            {
                m_cantype = "CANFD加速";
            }

            if (frametype == 0)
            {
                m_frametype = "标准帧";
            }
            else if (frametype == 1)
            {
                m_frametype = "扩展帧";
            }

            if (frameFormat == 0)
            {
                m_frameformat = "数据帧";
            }
            else if (frameFormat == 1)
            {
                m_frameformat = "远程帧";
            }

            m_len = RecvData.frame.len.ToString();

            for (int k = 0; k < RecvData.frame.len; k++)
            {
                m_data = String.Format("{0:G}{1:X2}", m_data, RecvData.frame.data[k]);
            }
            //Object[] list = { this, System.EventArgs.Empty };
            this.listBox1.BeginInvoke(new DelegateSetListBox(SetListBox), new object[] { m_direction, m_ID, m_time, m_cantype, m_frametype, m_frameformat, m_len, m_data });
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            iTek_CANAFD_Transmit_Data[] Send_data = new iTek_CANAFD_Transmit_Data[1];
            int farmeType = comboBox12.SelectedIndex;
            int frameformat = comboBox14.SelectedIndex;
            Send_data[0].send_type = (ushort)comboBox11.SelectedIndex;
            string send_id = textBox1.Text;
            Send_data[0].frame.canid = Convert.ToUInt32(send_id,16);
            Send_data[0].frame.canid |= (uint)(farmeType << 30);
            Send_data[0].frame.canid |= (uint)(frameformat << 29);
            Send_data[0].frame.can_type = (byte)comboBox13.SelectedIndex;
            if (Send_data[0].frame.can_type > 0)
            {
                Send_data[0].frame.can_type++;
            }
            Send_data[0].frame.can_type |= 0x40;
            Send_data[0].frame.flage = 0;
            Send_data[0].frame.res = 0;
            string send_msg = textBox2.Text;
            Send_data[0].frame.data = new byte[64];
            Send_data[0].frame.len = (byte)SplitData(send_msg, ref Send_data[0].frame.data, 64);
            
            IntPtr PSend_data = Marshal.AllocHGlobal(Marshal.SizeOf(Send_data[0]));
            Marshal.StructureToPtr(Send_data[0], PSend_data, true);

            int send_len = iTekCanDLL.iTek_Transmit(channel_handle, PSend_data, 1);
            if (send_len == 0)
            {
                MessageBox.Show("发送数据失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            send_data(Send_data,1);
           
            
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int INDEXTYPE = comboBox1.SelectedIndex;//012345
            int device_type = m_pComboxDeviceType.SelectedIndex;
            switch (INDEXTYPE){
                case 0:
                case 3:
                case 4:
                case 5:
                    deindex = m_DevType[0][device_type];
                    break;
                case 1:
                    deindex = m_DevType[1][device_type];
                    break;
                case 2:
                    deindex = m_DevType[2][device_type];
                    break;
                default:
                    break;
            }
            
            /*   int INDEXTYPE = comboBox1.SelectedIndex;//012345
               if(INDEXTYPE!=1|| INDEXTYPE != 2)
               {
                   groupBox5.Enabled = false;
                   m_pLabelProtrol.Enabled = true;
                   m_pComboxProtrol.Enabled = true;
                   m_pLabelChannel.Enabled = true;
                   m_PComboxChannel.Enabled = true;
                   m_pLabelFDStandard.Enabled = true;
                   m_pComboxFDStandard.Enabled = true;
                   m_pLabelWorkmodel.Enabled = true;
                   m_pComboxWorkModel.Enabled = true;
                   m_pLabelABit.Enabled = true;
                   m_pComboxABit.Enabled = true;
                   m_pLabelABitV.Enabled = true;
                   m_pLineABitV.Enabled = true;
                   m_pLabelDBit.Enabled = true;
                   m_pComboxDBit.Enabled = true;
                   m_pLabelDBitV.Enabled = true;
                   m_pLineDBitV.Enabled = true;
                   groupBox4.Enabled = true;
               }
               else{
                   m_pLabelProtrol.Enabled = false;
                   m_pComboxProtrol.Enabled = false;
                   m_pLabelChannel.Enabled = false;
                   m_PComboxChannel.Enabled = false;
                   m_pLabelFDStandard.Enabled = false;
                   m_pComboxFDStandard.Enabled = false;
                   m_pLabelWorkmodel.Enabled = false;
                   m_pComboxWorkModel.Enabled = false;
                   m_pLabelABit.Enabled = false;
                   m_pComboxABit.Enabled = false;
                   m_pLabelABitV.Enabled = false;
                   m_pLineABitV.Enabled = false;
                   m_pLabelDBit.Enabled = false;
                   m_pComboxDBit.Enabled = false;
                   m_pLabelDBitV.Enabled = false;
                   m_pLineDBitV.Enabled = false;
                   groupBox4.Enabled = false;
                   groupBox5.Enabled = true;
               }*/
            /*    int nindex = m_pComboxDeviceType.SelectedIndex;
                if (nindex == 4|| nindex == 5 ||nindex == 6 ||nindex == 7)
                {
                    m_pLabelProtrol.Enabled = false;
                    m_pComboxProtrol.Enabled = false;
                    m_pLabelChannel.Enabled = false;
                    m_PComboxChannel.Enabled = false;
                    m_pLabelFDStandard.Enabled = false;
                    m_pComboxFDStandard.Enabled = false;
                    m_pLabelWorkmodel.Enabled = false;
                    m_pComboxWorkModel.Enabled = false;
                    m_pLabelABit.Enabled = false;
                    m_pComboxABit.Enabled = false;
                    m_pLabelABitV.Enabled = false;
                    m_pLineABitV.Enabled = false;
                    m_pLabelDBit.Enabled = false;
                    m_pComboxDBit.Enabled = false;
                    m_pLabelDBitV.Enabled = false;
                    m_pLineDBitV.Enabled = false;
                    groupBox4.Enabled = false;
                    groupBox5.Enabled = true;

                }
                else
                {
                    groupBox5.Enabled = false;
                    m_pLabelProtrol.Enabled = true;
                    m_pComboxProtrol.Enabled = true;
                    m_pLabelChannel.Enabled = true;
                    m_PComboxChannel.Enabled = true;
                    m_pLabelFDStandard.Enabled = true;
                    m_pComboxFDStandard.Enabled = true;
                    m_pLabelWorkmodel.Enabled = true;
                    m_pComboxWorkModel.Enabled = true;
                    m_pLabelABit.Enabled = true;
                    m_pComboxABit.Enabled = true;
                    m_pLabelABitV.Enabled = true;
                    m_pLineABitV.Enabled = true;
                    m_pLabelDBit.Enabled = true;
                    m_pComboxDBit.Enabled = true;
                    m_pLabelDBitV.Enabled = true;
                    m_pLineDBitV.Enabled = true;
                    groupBox4.Enabled = true;
                }*/
        }

        private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {

        }
        private string GetLocalIP()
        {
            StringBuilder Info = new StringBuilder("");
            //本机IP地址 
            string strLocalIP = "";
            //得到计算机名 
            string strPcName = Dns.GetHostName();
            //得到本机IP地址数组 
            IPHostEntry ipEntry = Dns.GetHostEntry(strPcName);
            //遍历数组 
            foreach (var IPadd in ipEntry.AddressList)
            {
                //判断当前字符串是否为正确IP地址 
                if (IsRightIP(IPadd.ToString()))
                {
                    //得到本地IP地址 
                    strLocalIP = IPadd.ToString();
                    Info.Append(strLocalIP);
                    Info.Append(",");
                    //结束循环 
                    //break;
                }
            }

            //返回本地IP地址 
            return Info.ToString(); ;
        }
        public static bool IsRightIP(string strIPadd)
          {
              //利用正则表达式判断字符串是否符合IPv4格式 
              if (Regex.IsMatch(strIPadd, @"^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$"))
              {
                  //根据小数点分拆字符串 
                  string[] ips = strIPadd.Split('.');
                  if (ips.Length == 4 || ips.Length == 6)
                  {
                      //如果符合IPv4规则 
                      if (System.Int32.Parse(ips[0]) < 256 && System.Int32.Parse(ips[1]) < 256 & System.Int32.Parse(ips[2]) < 256 & System.Int32.Parse(ips[3]) < 256)
                      {
                          if(IsPingIP(strIPadd))
                              //正确 
                              return true;
                          else
                              //错误 
                              return false;
                      }
                          
                      //如果不符合 
                      else
                          //错误 
                          return false;
                  }
                  else
                      //错误 
                      return false;
              }
              else
                  //错误 
                  return false;
          }
        public static bool IsPingIP(string strIP)
         {
             try
             {
                 //创建Ping对象 
                 Ping ping = new Ping();
                 //接受Ping返回值 
                 PingReply reply = ping.Send(strIP, 1000);
                 //Ping通 
                 return true;
             }
             catch
             {
                 //Ping失败 
                 return false;
             }
         }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)

        {

            if (m_openstop == true)
            {
                recv_data_thread_.setStart(false);
                recv_data_thread_ = null;
                iTekCanDLL.iTek_ResetCAN(channel_handle);
                iTekCanDLL.iTek_CloseDevice(device_handle);
                m_pButtonDevice.Enabled = true;
                button2.Enabled = false;
                button3.Enabled = false;
            }

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void m_pLabelFDStandard_Click(object sender, EventArgs e)
        {

        }

        private void m_pLabelABit_Click(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void m_pLabelDBit_Click(object sender, EventArgs e)
        {

        }

        private void m_pLabelProtrol_Click(object sender, EventArgs e)
        {

        }

        private void m_pLabelDeviceType_Click(object sender, EventArgs e)
        {

        }

        private void m_pLabelWorkmodel_Click(object sender, EventArgs e)
        {

        }

        private void m_pLabelABitV_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged_2(object sender, EventArgs e)
        {
            uint index = (uint)comboBox1.SelectedIndex;
            m_pComboxDeviceType.Items.Clear();    //清空
            switch(index)
            {       
                case 0:
                case 3:
                case 4:
                case 5:
                    m_pComboxDeviceType.Items.AddRange(deviceTypes[0]);
                    break;
                case 1:
                    m_pComboxDeviceType.Items.AddRange(deviceTypes[1]);
                    break;
                case 2:
                    m_pComboxDeviceType.Items.AddRange(deviceTypes[2]);
                    break;
                default:
                    break;
            }
            m_pComboxDeviceType.SelectedIndex = 0;


            if (index != 1 && index != 2)
            {
                groupBox5.Enabled = false;
                m_pLabelProtrol.Enabled = true;
                m_pComboxProtrol.Enabled = true;
                m_pLabelChannel.Enabled = true;
                m_PComboxChannel.Enabled = true;
                m_pLabelFDStandard.Enabled = true;
                m_pComboxFDStandard.Enabled = true;
                m_pLabelWorkmodel.Enabled = true;
                m_pComboxWorkModel.Enabled = true;
                m_pLabelABit.Enabled = true;
                m_pComboxABit.Enabled = true;
                m_pLabelABitV.Enabled = true;
                m_pLineABitV.Enabled = true;
                m_pLabelDBit.Enabled = true;
                m_pComboxDBit.Enabled = true;
                m_pLabelDBitV.Enabled = true;
                m_pLineDBitV.Enabled = true;
                groupBox4.Enabled = true;
            }
            else
            {
                m_pLabelProtrol.Enabled = false;
                m_pComboxProtrol.Enabled = false;
                m_pLabelChannel.Enabled = false;
                m_PComboxChannel.Enabled = false;
                m_pLabelFDStandard.Enabled = false;
                m_pComboxFDStandard.Enabled = false;
                m_pLabelWorkmodel.Enabled = false;
                m_pComboxWorkModel.Enabled = false;
                m_pLabelABit.Enabled = false;
                m_pComboxABit.Enabled = false;
                m_pLabelABitV.Enabled = false;
                m_pLineABitV.Enabled = false;
                m_pLabelDBit.Enabled = false;
                m_pComboxDBit.Enabled = false;
                m_pLabelDBitV.Enabled = false;
                m_pLineDBitV.Enabled = false;
                groupBox4.Enabled = false;
                groupBox5.Enabled = true;
            }

        }
    }
}
