using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Excel = Microsoft.Office.Interop.Excel;

namespace CAN_2084_demo
{
    public partial class Form1 : Form
    {
        //Global Variable for time value, last voltage value and timer

        System.Windows.Forms.Timer Timer1 = new System.Windows.Forms.Timer();

        Int64[] value = new Int64[8];
        Int64[] overflow = new Int64[8];
        int[] mode = new int[8];
        int[] signal = new int[8];
        bool[] channel = new bool[8];
        Byte VxCAN = 0x0;


        public Form1()
        {
            InitializeComponent();
        }

        private void Btn_GetDLLVersion_Click(object sender, EventArgs e)
        {

        }

        private void Btn_SearchModule_Click(object sender, EventArgs e)
        {
            UInt32 ret;
            byte Percentage = 0;
            pBar_SearchModule.Value = 0;

            ret = VxCAN_DotNET.VxCAN_DotNET.VxCAN_SearchCANModule();
            if (ret == 0)
            {
                while (pBar_SearchModule.Value < pBar_SearchModule.Maximum)
                {

                    ret = VxCAN_DotNET.VxCAN_DotNET.VxCAN_IsSearchCANModuleOK(ref Percentage);
                    pBar_SearchModule.Value = Convert.ToInt32(Percentage);
                }

                MessageBox.Show("Search Done!");
            }
        }

        private void Btn_GetModuleList_Click(object sender, EventArgs e)
        {
            Byte _TotalVxCANPort = 0;
            Byte[] _VxCAN_LocalPortIDList = new Byte[50];
            UInt32 ret = 0;
            UInt32[] _VxCAN_NameIDList = new UInt32[50];
            UInt32[] _VxCAN_IDList = new UInt32[50];
            UInt32[] _VxCAN_COMPortBaudRate = new UInt32[50];
            String[] _VxCAN_NameStr = new String[50];
            String[] _VxCAN_VerStr = new String[50];

            dataGridView1.Columns.Clear();
            dataGridView1.Rows.Clear();
            dataGridView1.Columns.Add("VxCANPort", "VxCANPort");
            dataGridView1.Columns.Add("VxCAN_NameIDList", "VxCAN_NameIDList");
            dataGridView1.Columns.Add("VxCAN_IDList", "VxCAN_IDList");
            dataGridView1.Columns.Add("VxCAN_COMPortBaudRate", "VxCAN_COMPortBaudRate");
            dataGridView1.Columns.Add("VxCAN_NameStr", "VxCAN_NameStr");
            dataGridView1.Columns.Add("VxCAN_VerStr", "VxCAN_VerStr");
            dataGridView1.Columns.Add("VxCAN_LocalPortIDList", "VxCAN_LocalPortIDList");



            for (int i = 0; i < 50; i++)
            {
                //Set Default Value
                _VxCAN_NameStr[i] = new String('\0', 50);
                _VxCAN_VerStr[i] = new String('\0', 50);
            }



            ret = VxCAN_DotNET.VxCAN_DotNET.VxCAN_TotalCANPort(ref _TotalVxCANPort, _VxCAN_NameIDList, _VxCAN_IDList, _VxCAN_COMPortBaudRate, _VxCAN_NameStr, _VxCAN_VerStr, _VxCAN_LocalPortIDList);
            if (ret == 0)
            {
                for (int i = 0; i < _TotalVxCANPort; i++)
                {
                    dataGridView1.Rows.Add();
                    dataGridView1["VxCANPort", dataGridView1.Rows.Count - 1].Value = i;
                    dataGridView1["VxCAN_NameIDList", dataGridView1.Rows.Count - 1].Value = _VxCAN_NameIDList[i];
                    dataGridView1["VxCAN_NameIDList", dataGridView1.Rows.Count - 1].Value = _VxCAN_NameIDList[i];
                    dataGridView1["VxCAN_IDList", dataGridView1.Rows.Count - 1].Value = _VxCAN_IDList[i];

                    //2084C is not a UART module so its VxCAN_COMPortBaudRate = 0xFFFFFFFF
                    //we use -1 to represent that
                    if (_VxCAN_COMPortBaudRate[i] == 0xFFFFFFFF)
                        dataGridView1["VxCAN_COMPortBaudRate", dataGridView1.Rows.Count - 1].Value = -1;
                    else
                        dataGridView1["VxCAN_COMPortBaudRate", dataGridView1.Rows.Count - 1].Value = _VxCAN_COMPortBaudRate[i];
                    dataGridView1["VxCAN_NameStr", dataGridView1.Rows.Count - 1].Value = _VxCAN_NameStr[i];
                    dataGridView1["VxCAN_VerStr", dataGridView1.Rows.Count - 1].Value = _VxCAN_VerStr[i];
                    dataGridView1["VxCAN_LocalPortIDList", dataGridView1.Rows.Count - 1].Value = _VxCAN_LocalPortIDList[i];
                }
            }
        }


        private void Btn_OpenCAN_Click(object sender, EventArgs e)
        {
            UInt32 ret = 0;

            ret = VxCAN_DotNET.VxCAN_DotNET.VxCAN_OpenCAN((byte)dataGridView1.CurrentRow.Index, (UInt32)VxCAN_DotNET.VxCAN_DotNET.CAN_BaudRate.BR_1000K);
            if (ret == 0)
            {
                MessageBox.Show("Open CAN Port done!!");
            }
            VxCAN = (byte)dataGridView1.CurrentRow.Index;
            for (int i = 0; i < 8; i++)
            {
                value[i] = 0;
                overflow[i] = 0;
                mode[i] = 3;
                signal[i] = 1;
                channel[i] = false;
            }
            input_signal.SelectedIndex = 1;
            counter_mode.SelectedIndex = 3;

            dataGridView2.Columns.Clear();
            dataGridView2.Rows.Clear();
            dataGridView2.Columns.Add("Channel", "Channel");
            dataGridView2.Columns.Add("Mode", "Mode");
            dataGridView2.Columns.Add("Value", "Value");


            dataGridView2.Columns["Channel"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            dataGridView2.Columns["Mode"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            dataGridView2.Columns["Value"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

            for (int i = 0; i < 8; i++)
            {
                dataGridView2.Rows.Add();
                dataGridView2["Channel", dataGridView2.Rows.Count - 1].Value = "Channel " + i.ToString();
                dataGridView2["Mode", dataGridView2.Rows.Count - 1].Value = mode[i].ToString();
                dataGridView2["Value", dataGridView2.Rows.Count - 1].Value = value[i].ToString();
            }

        }

        private void Btn_CloseCAN_Click(object sender, EventArgs e)
        {
            UInt32 ret = 0;

            ret = VxCAN_DotNET.VxCAN_DotNET.VxCAN_ClearRxBuffer(0);
            ret = VxCAN_DotNET.VxCAN_DotNET.VxCAN_ClearTxBuffer(0);
            ret = VxCAN_DotNET.VxCAN_DotNET.VxCAN_CloseCAN(0);

        }


        private void Btn_SendCANMsg_Click(object sender, EventArgs e)
        {
            UInt32 NumID = 0x000;
            NumID = (UInt32)(comboBox1.SelectedIndex + 1);
            Byte DataID = 0x0;
            DataID = (Byte)(comboBox1.SelectedIndex + 1);
            Byte VxCANPort = VxCAN, Mode = 0x0, RTR = 0x0, Len = 0x0;
            UInt32 ID = 0x000;
            Byte[] Data = new Byte[8];
            
                ID = 0x600 + NumID;
                Len = 0x8;
                Data[0] = 0x2B;
                Data[1] = 0x01;
                Data[2] = 0x18;
                Data[3] = 0x05;
                Data[4] = 0xE8;
                Data[5] = 0x03;
                Data[6] = 0x00;
                Data[7] = 0x00;
                VxCAN_DotNET.VxCAN_DotNET.VxCAN_Send(VxCANPort, ID, Mode, RTR, Len, Data);

                Thread.Sleep(50);

                ID = 0x600 + NumID;
                Len = 0x8;
                Data[0] = 0x2B;
                Data[1] = 0x02;
                Data[2] = 0x18;
                Data[3] = 0x05;
                Data[4] = 0xE8;
                Data[5] = 0x03;
                Data[6] = 0x00;
                Data[7] = 0x00;
                VxCAN_DotNET.VxCAN_DotNET.VxCAN_Send(VxCANPort, ID, Mode, RTR, Len, Data);

                Thread.Sleep(50);

                ID = 0x600 + NumID;
                Len = 0x8;
                Data[0] = 0x2B;
                Data[1] = 0x03;
                Data[2] = 0x18;
                Data[3] = 0x05;
                Data[4] = 0xE8;
                Data[5] = 0x03;
                Data[6] = 0x00;
                Data[7] = 0x00;
                VxCAN_DotNET.VxCAN_DotNET.VxCAN_Send(VxCANPort, ID, Mode, RTR, Len, Data);

                Thread.Sleep(50);

                ID = 0x600 + NumID;
                Len = 0x8;
                Data[0] = 0x23;
                Data[1] = 0x04;
                Data[2] = 0x18;
                Data[3] = 0x01;
                Data[4] = 0x99;
                Data[5] = 0x04;
                Data[6] = 0x00;
                Data[7] = 0x00;
                VxCAN_DotNET.VxCAN_DotNET.VxCAN_Send(VxCANPort, ID, Mode, RTR, Len, Data);

                Thread.Sleep(50);

                ID = 0x600 + NumID;
                Len = 0x8;
                Data[0] = 0x2B;
                Data[1] = 0x04;
                Data[2] = 0x18;
                Data[3] = 0x05;
                Data[4] = 0xE8;
                Data[5] = 0x03;
                Data[6] = 0x00;
                Data[7] = 0x00;
                VxCAN_DotNET.VxCAN_DotNET.VxCAN_Send(VxCANPort, ID, Mode, RTR, Len, Data);

                timer2.Enabled = true;

        }


        private void Btn_ResetCAN_Click(object sender, EventArgs e)
        {
            UInt32 ret = 0;
            ret = VxCAN_DotNET.VxCAN_DotNET.VxCAN_ResetCAN(0);
            if (ret == 0)
                MessageBox.Show("Reset CAN Port 0 done!");
        }

        private void Btn_CANStatus_Click(object sender, EventArgs e)
        {
            UInt32 ret = 0;
            UInt32 ModuleStatus = 0;
            UInt32 CANChipStatus = 0;

            ret = VxCAN_DotNET.VxCAN_DotNET.VxCAN_CANStatus(0, ref ModuleStatus, ref CANChipStatus);
            if (ret == 0)
            {
                MessageBox.Show("Module Status = " + ModuleStatus.ToString());
                MessageBox.Show("CAN Chip Status = " + CANChipStatus.ToString());
            }
        }
        private void ask_overflow()
        {
            UInt32 NumID = 0x000;
            NumID = (UInt32)(comboBox1.SelectedIndex + 1);
            Byte DataID = 0x0;
            DataID = (Byte)(comboBox1.SelectedIndex + 1);
            Byte VxCANPort = VxCAN, Mode = 0x0, RTR = 0x0, Len = 0x0;
            UInt32 ID = 0x000;
            Byte[] Data = new Byte[8];


            for (int i = 0; i < 8; i++)
            {
                ID = 0x600 + NumID;
                Len = 0x8;
                Data[0] = 0x40;
                Data[1] = 0x01;
                Data[2] = 0x30;
                Data[3] = (byte)(i + 1);
                Data[4] = 0x00;
                Data[5] = 0x00;
                Data[6] = 0x00;
                Data[7] = 0x00;
                VxCAN_DotNET.VxCAN_DotNET.VxCAN_Send(VxCANPort, ID, Mode, RTR, Len, Data);

                Thread.Sleep(20);
            }


        }

        //
        private void timer2_Tick(object sender, EventArgs e)
        {
            //Initialize parameters
            UInt32 NumID = 0x000;
            NumID = (UInt32)(comboBox1.SelectedIndex + 1);
            byte Mode = 0, RTR = 0, Len = 0;
            byte[] Data = new Byte[8];
            ushort RxCnt = 0;
            UInt32 ret = 0, ID = 0;
            Double MsgTimeStamps = 0;
            UInt32 TimeOut = 0;

            ask_overflow();

            //get the amount of CAN message in Virtual CAN buffer


            while (true && TimeOut++ < 20000)
            {
                //get the amount of CAN message in Virtual CAN buffer
                ret = VxCAN_DotNET.VxCAN_DotNET.VxCAN_RxMsgCount(VxCAN, ref RxCnt);
                
                if (ret == 0)
                {
                    if (RxCnt > 0)
                    {
                        //output all CAN message in Virtual CAN buffer
                        for (int i = 0; i < RxCnt; i++)
                        {
                            ret = VxCAN_DotNET.VxCAN_DotNET.VxCAN_Receive(VxCAN, ref ID, ref Mode, ref RTR, ref Len, Data, ref MsgTimeStamps);
                            if (ret == 0)
                            {
                                if (ID == (0x280 + NumID))
                                {
                                    value[0] = (Int64)Data[3] * 256 * 256 * 256 + (Int64)Data[2] * 256 * 256 + (Int64)Data[1] * 256 + Data[0];
                                    dataGridView2["Mode", 0].Value = mode[0].ToString();
                                    dataGridView2["Value", 0].Value = value[0].ToString();

                                    value[1] = (Int64)Data[7] * 256 * 256 * 256 + (Int64)Data[6] * 256 * 256 + (Int64)Data[5] * 256 + Data[4];
                                    dataGridView2["Mode", 1].Value = mode[1].ToString();
                                    dataGridView2["Value", 1].Value = value[1].ToString();
                                }
                                else if (ID == (0x380 + NumID))
                                {
                                    value[2] = (Int64)Data[3] * 256 * 256 * 256 + (Int64)Data[2] * 256 * 256 + (Int64)Data[1] * 256 + Data[0];
                                    dataGridView2["Mode", 2].Value = mode[2].ToString();
                                    dataGridView2["Value", 2].Value = value[2].ToString();

                                    value[3] = (Int64)Data[7] * 256 * 256 * 256 + (Int64)Data[6] * 256 * 256 + (Int64)Data[5] * 256 + Data[4];
                                    dataGridView2["Mode", 3].Value = mode[3].ToString();
                                    dataGridView2["Value", 3].Value = value[3].ToString();
                                }
                                else if (ID == (0x480+NumID))
                                {
                                    value[4] = (Int64)Data[3] * 256 * 256 * 256 + (Int64)Data[2] * 256 * 256 + (Int64)Data[1] * 256 + Data[0];
                                    dataGridView2["Mode", 4].Value = mode[4].ToString();
                                    dataGridView2["Value", 4].Value = value[4].ToString();

                                    value[5] = (Int64)Data[7] * 256 * 256 * 256 + (Int64)Data[6] * 256 * 256 + (Int64)Data[5] * 256 + Data[4];
                                    dataGridView2["Mode", 5].Value = mode[5].ToString();
                                    dataGridView2["Value", 5].Value = value[5].ToString();
                                }
                                else if (ID == 0x499)
                                {
                                    value[6] = (Int64)Data[3] * 256 * 256 * 256 + (Int64)Data[2] * 256 * 256 + (Int64)Data[1] * 256 + Data[0];
                                    dataGridView2["Mode", 6].Value = mode[6].ToString();
                                    dataGridView2["Value", 6].Value = value[6].ToString();

                                    value[7] = (Int64)Data[7] * 256 * 256 * 256 + (Int64)Data[6] * 256 * 256 + (Int64)Data[5] * 256 + Data[4];
                                    dataGridView2["Mode", 7].Value = mode[7].ToString();
                                    dataGridView2["Value", 7].Value = value[7].ToString();
                                }
                                else if ((ID == (0x580+NumID))&&Data[2]==0x30&&Data[1]==0x01)
                                {
                                    overflow[Data[3]-1] = (Int64)Data[7] * 256 * 256 * 256 + (Int64)Data[6] * 256 * 256 + (Int64)Data[5] * 256 + Data[4];
                                    if ((overflow[Data[3] - 1] & (Int64)0x80000000) != 0)
                                    {
                                        value[Data[3] - 1] -= (0xFFFFFFFF);
                                        value[Data[3] - 1] -= 1;
                                        dataGridView2["Value", Data[3] - 1].Value = value[Data[3] - 1].ToString();
                                    }
                                }
                            }
                        }


                    }
                }

            }
        }


        private void Btn_SendStopMsg_Click(object sender, EventArgs e)
        {
            UInt32 NumID = 0x000;
            NumID = (UInt32)(comboBox1.SelectedIndex + 1);
            Byte DataID = 0x0;
            DataID = (Byte)(comboBox1.SelectedIndex + 1);
            Byte VxCANPort = VxCAN, Mode = 0x0, RTR = 0x0, Len = 0x0;
            UInt32 ret = 0, ID = 0x000;
            Byte[] Data = new Byte[8];

            //Set 0 second interval of event timer to disable event timer
            ID = 0x600 + NumID;
            Len = 0x8;
            Data[0] = 0x2B;
            Data[1] = 0x01;
            Data[2] = 0x18;
            Data[3] = 0x05;
            Data[4] = 0x00;
            Data[5] = 0x00;
            Data[6] = 0x00;
            Data[7] = 0x00;
            ret = VxCAN_DotNET.VxCAN_DotNET.VxCAN_Send(VxCANPort, ID, Mode, RTR, Len, Data);
            if (ret != 0)
            {
                MessageBox.Show("Close Event Timer Fail!!");
            }


            ID = 0x600 + NumID;
            Len = 0x8;
            Data[0] = 0x2F;
            Data[1] = 0x02;
            Data[2] = 0x30;
            Data[3] = 0x01;
            Data[4] = 0x00;
            Data[5] = 0x00;
            Data[6] = 0x00;
            Data[7] = 0x00;
            ret = VxCAN_DotNET.VxCAN_DotNET.VxCAN_Send(VxCANPort, ID, Mode, RTR, Len, Data);

            ID = 0x600 + NumID;
            Len = 0x8;
            Data[0] = 0x2B;
            Data[1] = 0x01;
            Data[2] = 0x18;
            Data[3] = 0x05;
            Data[4] = 0x00;
            Data[5] = 0x00;
            Data[6] = 0x00;
            Data[7] = 0x00;
            VxCAN_DotNET.VxCAN_DotNET.VxCAN_Send(VxCANPort, ID, Mode, RTR, Len, Data);

            Thread.Sleep(50);

            ID = 0x600 + NumID;
            Len = 0x8;
            Data[0] = 0x2B;
            Data[1] = 0x02;
            Data[2] = 0x18;
            Data[3] = 0x05;
            Data[4] = 0x00;
            Data[5] = 0x00;
            Data[6] = 0x00;
            Data[7] = 0x00;
            VxCAN_DotNET.VxCAN_DotNET.VxCAN_Send(VxCANPort, ID, Mode, RTR, Len, Data);

            Thread.Sleep(50);

            ID = 0x600 + NumID;
            Len = 0x8;
            Data[0] = 0x2B;
            Data[1] = 0x03;
            Data[2] = 0x18;
            Data[3] = 0x05;
            Data[4] = 0x00;
            Data[5] = 0x00;
            Data[6] = 0x00;
            Data[7] = 0x00;
            VxCAN_DotNET.VxCAN_DotNET.VxCAN_Send(VxCANPort, ID, Mode, RTR, Len, Data);

            Thread.Sleep(50);

            ID = 0x600 + NumID;
            Len = 0x8;
            Data[0] = 0x2B;
            Data[1] = 0x04;
            Data[2] = 0x18;
            Data[3] = 0x05;
            Data[4] = 0x00;
            Data[5] = 0x00;
            Data[6] = 0x00;
            Data[7] = 0x00;
            VxCAN_DotNET.VxCAN_DotNET.VxCAN_Send(VxCANPort, ID, Mode, RTR, Len, Data);

            //Stop timer
            timer2.Enabled = false;
;
            GC.Collect();
        }
        //-------------------- make 2084C into operation mode -------------------------------------------------
        private void operation_Click(object sender, EventArgs e)
        {
            UInt32 NumID = 0x000;
            NumID = (UInt32)(comboBox1.SelectedIndex + 1);
            Byte DataID = 0x0;
            DataID = (Byte)(comboBox1.SelectedIndex + 1);
            Byte VxCANPort = VxCAN, Mode = 0x0, RTR = 0x0, Len = 0x0;
            UInt32 ID = 0x000;
            Byte[] Data = new Byte[8];

            ID = 0x000;
            Len = 0x2;
            Data[0] = 0x01;
            Data[1] = DataID;
            VxCAN_DotNET.VxCAN_DotNET.VxCAN_Send(VxCANPort, ID, Mode, RTR, Len, Data);
            MessageBox.Show("CAN2084C is in operation mode!");
        }

        //---------------------- set Counter mode and input signal type ---------------------------------------
        private void set_Click(object sender, EventArgs e)
        {
            UInt32 NumID = 0x000;
            NumID = (UInt32)(comboBox1.SelectedIndex + 1);
            Byte DataID = 0x0;
            DataID = (Byte)(comboBox1.SelectedIndex + 1);
            Byte VxCANPort = VxCAN, Mode = 0x0, RTR = 0x0, Len = 0x0;
            UInt32 ID = 0x000;
            Byte[] Data = new Byte[8];

            if (ch0.Checked)
                channel[0] = true;
            else
                channel[0] = false;
            if (ch1.Checked)
                channel[1] = true;
            else
                channel[1] = false;
            if (ch2.Checked)
                channel[2] = true;
            else
                channel[2] = false;
            if (ch3.Checked)
                channel[3] = true;
            else
                channel[3] = false;
            if (ch4.Checked)
                channel[4] = true;
            else
                channel[4] = false;
            if (ch5.Checked)
                channel[5] = true;
            else
                channel[5] = false;
            if (ch6.Checked)
                channel[6] = true;
            else
                channel[6] = false;
            if (ch7.Checked)
                channel[7] = true;
            else
                channel[7] = false;

            for (int i = 0; i < 8; i++)
            {
                if (channel[i])
                {
                    
                    mode[i] = counter_mode.SelectedIndex;
                    if ((i % 2) != 0)
                    {
                        if (mode[i - 1] == 0 || mode[i - 1] == 1 || mode[i - 1] == 4)
                            mode[i - 1] = mode[i];
                        else if(mode[i] == 0 || mode[i] == 1 || mode[i] == 4)
                            mode[i - 1] = mode[i];
                    }
                    else
                    {
                        if (mode[i + 1] == 0 || mode[i + 1] == 1 || mode[i + 1] == 4)
                            mode[i + 1] = mode[i];
                        else if (mode[i] == 0 || mode[i] == 1 || mode[i] == 4)
                            mode[i + 1] = mode[i];
                    }
                    ID = 0x600 + NumID;
                    Len = 0x8;
                    Data[0] = 0x2F;
                    Data[1] = 0x04;
                    Data[2] = 0x20;
                    Data[3] = (byte)(i + 1);
                    Data[4] = (byte)counter_mode.SelectedIndex;
                    Data[5] = 0x00;
                    Data[6] = 0x00;
                    Data[7] = 0x00;
                    VxCAN_DotNET.VxCAN_DotNET.VxCAN_Send(VxCANPort, ID, Mode, RTR, Len, Data);

                    Thread.Sleep(20);
                    signal[i] = input_signal.SelectedIndex;
                    Data[0] = 0x2F;
                    Data[1] = 0x08;
                    Data[2] = 0x30;
                    Data[3] = (byte)(i + 1);
                    Data[4] = (byte)input_signal.SelectedIndex;
                    Data[5] = 0x00;
                    Data[6] = 0x00;
                    Data[7] = 0x00;
                    VxCAN_DotNET.VxCAN_DotNET.VxCAN_Send(VxCANPort, ID, Mode, RTR, Len, Data);

                    Thread.Sleep(20);

                }

            }
           

        }
      

        //---------------------- set all channel Counter mode and input signal type ---------------------------
        private void set_all_Click(object sender, EventArgs e)
        {
            UInt32 NumID = 0x000;
            NumID = (UInt32)(comboBox1.SelectedIndex + 1);
            Byte DataID = 0x0;
            DataID = (Byte)(comboBox1.SelectedIndex + 1);
            Byte VxCANPort = VxCAN, Mode = 0x0, RTR = 0x0, Len = 0x0;
            UInt32 ID = 0x000;
            Byte[] Data = new Byte[8];


            for (int i = 0; i < 8; i++)
            {

                    mode[i] = counter_mode.SelectedIndex;
                    
                    ID = 0x600 + NumID;
                    Len = 0x8;
                    Data[0] = 0x2F;
                    Data[1] = 0x04;
                    Data[2] = 0x20;
                    Data[3] = (byte)(i + 1);
                    Data[4] = (byte)counter_mode.SelectedIndex;
                    Data[5] = 0x00;
                    Data[6] = 0x00;
                    Data[7] = 0x00;
                    VxCAN_DotNET.VxCAN_DotNET.VxCAN_Send(VxCANPort, ID, Mode, RTR, Len, Data);

                    Thread.Sleep(20);
                    signal[i] = input_signal.SelectedIndex;
                    Data[0] = 0x2F;
                    Data[1] = 0x08;
                    Data[2] = 0x30;
                    Data[3] = (byte)(i + 1);
                    Data[4] = (byte)input_signal.SelectedIndex;
                    Data[5] = 0x00;
                    Data[6] = 0x00;
                    Data[7] = 0x00;
                    VxCAN_DotNET.VxCAN_DotNET.VxCAN_Send(VxCANPort, ID, Mode, RTR, Len, Data);

                    Thread.Sleep(20);

            }
        }

        //---------------------- clear Counter value ----------------------------------------------------------
        private void clear_Click(object sender, EventArgs e)
        {
            UInt32 NumID = 0x000;
            NumID = (UInt32)(comboBox1.SelectedIndex + 1);
            Byte DataID = 0x0;
            DataID = (Byte)(comboBox1.SelectedIndex + 1);
            Byte VxCANPort = VxCAN, Mode = 0x0, RTR = 0x0, Len = 0x0;
            UInt32 ID = 0x000;
            Byte[] Data = new Byte[8];

            if (ch0.Checked)
                channel[0] = true;
            else
                channel[0] = false;
            if (ch1.Checked)
                channel[1] = true;
            else
                channel[1] = false;
            if (ch2.Checked)
                channel[2] = true;
            else
                channel[2] = false;
            if (ch3.Checked)
                channel[3] = true;
            else
                channel[3] = false;
            if (ch4.Checked)
                channel[4] = true;
            else
                channel[4] = false;
            if (ch5.Checked)
                channel[5] = true;
            else
                channel[5] = false;
            if (ch6.Checked)
                channel[6] = true;
            else
                channel[6] = false;
            if (ch7.Checked)
                channel[7] = true;
            else
                channel[7] = false;

            for (int i = 0; i < 8; i++)
            {
                if (channel[i])
                {

                    ID = 0x600 + NumID;
                    Len = 0x8;
                    Data[0] = 0x2F;
                    Data[1] = 0x02;
                    Data[2] = 0x30;
                    Data[3] = (byte)(i + 1);
                    Data[4] = 0x00;
                    Data[5] = 0x00;
                    Data[6] = 0x00;
                    Data[7] = 0x00;
                    VxCAN_DotNET.VxCAN_DotNET.VxCAN_Send(VxCANPort, ID, Mode, RTR, Len, Data);

                    Thread.Sleep(20);

                }

            }
        }

        //---------------------- clear all Counter value ------------------------------------------------------
        private void clear_all_Click(object sender, EventArgs e)
        {
            UInt32 NumID = 0x000;
            NumID = (UInt32)(comboBox1.SelectedIndex + 1);
            Byte DataID = 0x0;
            DataID = (Byte)(comboBox1.SelectedIndex + 1);
            Byte VxCANPort = VxCAN, Mode = 0x0, RTR = 0x0, Len = 0x0;
            UInt32 ID = 0x000;
            Byte[] Data = new Byte[8];

            for (int i = 0; i < 8; i++)
            {

                ID = 0x600 + NumID;
                Len = 0x8;
                Data[0] = 0x2F;
                Data[1] = 0x02;
                Data[2] = 0x30;
                Data[3] = (byte)(i + 1);
                Data[4] = 0x00;
                Data[5] = 0x00;
                Data[6] = 0x00;
                Data[7] = 0x00;
                VxCAN_DotNET.VxCAN_DotNET.VxCAN_Send(VxCANPort, ID, Mode, RTR, Len, Data);

                Thread.Sleep(20);


            }
        }

        //---------------------- Counter value refresh time ---------------------------------------------------
        private void refresh_time_SelectedIndexChanged(object sender, EventArgs e)
        {
            UInt32 NumID = 0x000;
            NumID = (UInt32)(comboBox1.SelectedIndex + 1);
            Byte DataID = 0x0;
            DataID = (Byte)(comboBox1.SelectedIndex + 1);
            Byte VxCANPort = VxCAN, Mode = 0x0, RTR = 0x0, Len = 0x0;
            UInt32 ID = 0x000;
            Byte[] Data = new Byte[8];

            for (int i = 0; i < 4; i++)
            {
                switch (refresh_time.SelectedIndex)
                {
                    case 0:
                        ID = 0x600 + NumID;
                        Len = 0x8;
                        Data[0] = 0x2B;
                        Data[1] = (byte)(i+1);
                        Data[2] = 0x18;
                        Data[3] = 0x05;
                        Data[4] = 0xE8;
                        Data[5] = 0x03;
                        Data[6] = 0x00;
                        Data[7] = 0x00;
                        VxCAN_DotNET.VxCAN_DotNET.VxCAN_Send(VxCANPort, ID, Mode, RTR, Len, Data);

                        Thread.Sleep(20);
                        break;
                    case 1:
                        ID = 0x600 + NumID;
                        Len = 0x8;
                        Data[0] = 0x2B;
                        Data[1] = (byte)(i + 1);
                        Data[2] = 0x18;
                        Data[3] = 0x05;
                        Data[4] = 0xF4;
                        Data[5] = 0x01;
                        Data[6] = 0x00;
                        Data[7] = 0x00;
                        VxCAN_DotNET.VxCAN_DotNET.VxCAN_Send(VxCANPort, ID, Mode, RTR, Len, Data);

                        Thread.Sleep(20);
                        break;
                    case 2:
                        ID = 0x600 + NumID;
                        Len = 0x8;
                        Data[0] = 0x2B;
                        Data[1] = (byte)(i + 1);
                        Data[2] = 0x18;
                        Data[3] = 0x05;
                        Data[4] = 0x2C;
                        Data[5] = 0x01;
                        Data[6] = 0x00;
                        Data[7] = 0x00;
                        VxCAN_DotNET.VxCAN_DotNET.VxCAN_Send(VxCANPort, ID, Mode, RTR, Len, Data);

                        Thread.Sleep(20);
                        break;
                }
                switch (refresh_time.SelectedIndex)
                {
                    case 0:
                        timer2.Interval = 500;
                        break;
                    case 1:
                        timer2.Interval = 500;
                        break;
                    case 2:
                        timer2.Interval = 300;
                        break;
                }

            }
        }

       






    }
}
