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.Windows.Forms.DataVisualization.Charting;
using System.Threading;
using System.IO;

using System.Diagnostics;
using System.Runtime.InteropServices;

using System.Management; //还需要在引用里面添加System.Management，不然还是提示找不到引用

namespace Kanban
{
    public partial class frmBlueLight : Form
    {
        Random random = new Random();
        

        int iLogSaveDays = 30;
        int iImageSaveNum = 1000;
        int iImageSaveDays = 30;
        bool isSaveSourceImage = false;
        bool isSaveProcessImage = false;
        


        public frmBlueLight()
        {
            InitializeComponent();


            StartPosition = FormStartPosition.CenterScreen;
            //WindowState = FormWindowState.Maximized;
            //this.FormBorderStyle = FormBorderStyle.None;
            this.Size = Screen.PrimaryScreen.WorkingArea.Size; //设置窗体大小为显示器工作区大小(不包括任务栏)。窗体设置成 无边框 最大化 会盖住任务栏
            
            

            /////方法1
            //bool bb= WhetherProcessRunning();
            //if (bb)
            //{
            //    MessageBox.Show("程序进程已经运行！");
            //    Process.GetCurrentProcess().Kill();
            //}

            /////方法2 将已经存在的实例显示出来到最前
            //1.这里判定是否已经有实例在运行
            //只运行一个实例
            System.Diagnostics.Process instanceProc = RunningInstance();
            if (instanceProc == null)
            {
                //1.1 没有实例在运行
                //Application.Run(new FormMain()); /////放在Program.cs里面
            }
            else
            {
                MessageBox.Show("程序进程已经运行！");
                //1.2 已经有一个实例在运行
                HandleRunningInstance(instanceProc);

                Process.GetCurrentProcess().Kill();
            }


        }

        private void lbTitle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmBlueLight_Load(object sender, EventArgs e)
        {
            //左上 饼状图
            chartPie.Series.Clear();
            ChartHelper.AddSeries(chartPie, "饼状图", SeriesChartType.Pie, Color.Lime, Color.Red, false);
            ChartHelper.SetStyle(chartPie, Color.Transparent, Color.White);
            ChartHelper.SetLegend(chartPie, Docking.Top, StringAlignment.Center, Color.Transparent, Color.White,false);

            //左中 柱状图
            chartBar.Series.Clear();
            ChartHelper.AddSeries(chartBar, "柱状图", SeriesChartType.Column, Color.Lime, Color.Transparent, true);
            //ChartHelper.AddSeries(chartBar, "曲线图", SeriesChartType.Spline, Color.Red, Color.Red);
            //ChartHelper.SetTitle(chartBar, "柱状图与曲线图", new Font("微软雅黑", 12), Docking.Bottom, Color.White);
            ChartHelper.SetStyle(chartBar, Color.Transparent, Color.White);
            ChartHelper.SetLegend(chartBar, Docking.Top, StringAlignment.Center, Color.Transparent, Color.White, false);
            //ChartHelper.SetXY(chartBar, "序号", "数值", StringAlignment.Center, Color.White, Color.White, AxisArrowStyle.SharpTriangle, 1, 2);
            ChartHelper.SetXY(chartBar, "", "车流量", StringAlignment.Center, Color.White, Color.White, AxisArrowStyle.None, 1, 2);
            ChartHelper.SetMajorGrid(chartBar, Color.Gray, 20, 2);
            chartBar.ChartAreas[0].AxisX.LabelStyle.Angle = -45;

            //左下 曲线图
            chartLine.Series.Clear();
            ChartHelper.AddSeries(chartLine, "曲线图", SeriesChartType.SplineRange, Color.FromArgb(100, 46, 199, 201), Color.Red, true);
            //ChartHelper.SetTitle(chartLine, "曲线图", new Font("微软雅黑", 12), Docking.Bottom, Color.FromArgb(46, 199, 201));
            ChartHelper.SetStyle(chartLine, Color.Transparent, Color.White);
            ChartHelper.SetLegend(chartLine, Docking.Top, StringAlignment.Center, Color.Transparent, Color.White, false);
            //ChartHelper.SetXY(chartLine, "序号", "数值", StringAlignment.Center, Color.White, Color.White, AxisArrowStyle.SharpTriangle, 1, 2);
            ChartHelper.SetXY(chartLine, "", "车流量", StringAlignment.Center, Color.White, Color.White, AxisArrowStyle.None, 1, 2);
            ChartHelper.SetMajorGrid(chartLine, Color.Gray, 20, 2);
            chartLine.ChartAreas[0].AxisX.LabelStyle.Angle = -45;

            //中上 电子地图

            //中下 叠加柱状图
            chartBig2.Series.Clear();
            ChartHelper.AddSeries(chartBig2, "LightBlue", SeriesChartType.StackedColumn, Color.FromArgb(250, 65, 140, 240), Color.Red, true);
            ChartHelper.AddSeries(chartBig2, "Gold", SeriesChartType.StackedColumn, Color.FromArgb(250, 252, 180, 65), Color.Red, true);
            ChartHelper.AddSeries(chartBig2, "Red", SeriesChartType.StackedColumn, Color.FromArgb(250, 224, 64, 10), Color.Red, true);
            ChartHelper.AddSeries(chartBig2, "DarkBlue", SeriesChartType.StackedColumn, Color.FromArgb(250, 5, 100, 146), Color.Red, true);
            //ChartHelper.SetTitle(chartBig2, "曲线图", new Font("微软雅黑", 12), Docking.Bottom, Color.FromArgb(46, 199, 201));
            ChartHelper.SetStyle(chartBig2, Color.Transparent, Color.White);
            ChartHelper.SetLegend(chartBig2, Docking.Top, StringAlignment.Center, Color.Transparent, Color.White, false);
            //ChartHelper.SetXY(chartBig2, "序号", "数值", StringAlignment.Center, Color.White, Color.White, AxisArrowStyle.SharpTriangle, 1, 2);
            ChartHelper.SetXY(chartBig2, "", "车流量", StringAlignment.Center, Color.White, Color.White, AxisArrowStyle.None, 1, 50);
            ChartHelper.SetMajorGrid(chartBig2, Color.Gray, 1, 50);
            chartBig2.ChartAreas[0].AxisX.LabelStyle.Angle = -45;
            chartBig2.ChartAreas[0].AxisX.IsMarginVisible = true;
            //chartBig2.ChartAreas[0].Area3DStyle.Enable3D = true;

            //右上 甜圈图
            chartRight1.Series.Clear();
            ChartHelper.AddSeries(chartRight1, "Default", SeriesChartType.Doughnut, Color.FromArgb(250, 65, 140, 240), Color.Red, true);
            //ChartHelper.SetTitle(chartRight1, "甜圈图", new Font("微软雅黑", 12), Docking.Bottom, Color.FromArgb(46, 199, 201));
            ChartHelper.SetStyle(chartRight1, Color.Transparent, Color.White);
            ChartHelper.SetLegend(chartRight1, Docking.Bottom, StringAlignment.Center, Color.Transparent, Color.White, true);
            //chartRight1.ChartAreas[0].Area3DStyle.Enable3D = true;
            chartRight1.Series[0]["PieLabelStyle"] = "Inside";
            chartRight1.Series[0]["DoughnutRadius"] = "60";
            chartRight1.Series[0]["PieDrawingStyle"] = "SoftEdge";

            //右中 甜圈图
            chartRight2.Series.Clear();
            ChartHelper.AddSeries(chartRight2, "Default", SeriesChartType.Doughnut, Color.FromArgb(250, 65, 140, 240), Color.Red, true);
            //ChartHelper.SetTitle(chartRight2, "甜圈图", new Font("微软雅黑", 12), Docking.Bottom, Color.FromArgb(46, 199, 201));
            ChartHelper.SetStyle(chartRight2, Color.Transparent, Color.White);
            ChartHelper.SetLegend(chartRight2, Docking.Bottom, StringAlignment.Center, Color.Transparent, Color.White, true);
            //chartRight2.ChartAreas[0].Area3DStyle.Enable3D = true;
            chartRight2.Series[0]["PieLabelStyle"] = "Inside";
            chartRight2.Series[0]["DoughnutRadius"] = "60";
            chartRight2.Series[0]["PieDrawingStyle"] = "SoftEdge";

            //右下 甜圈图
            chartRight3.Series.Clear();
            ChartHelper.AddSeries(chartRight3, "Default", SeriesChartType.Doughnut, Color.FromArgb(250, 65, 140, 240), Color.Red, true);
            //ChartHelper.SetTitle(chartRight3, "甜圈图", new Font("微软雅黑", 12), Docking.Bottom, Color.FromArgb(46, 199, 201));
            ChartHelper.SetStyle(chartRight3, Color.Transparent, Color.White);
            ChartHelper.SetLegend(chartRight3, Docking.Bottom, StringAlignment.Center, Color.Transparent, Color.White, true);
            //chartRight3.ChartAreas[0].Area3DStyle.Enable3D = true;
            chartRight3.Series[0]["PieLabelStyle"] = "Inside";
            chartRight3.Series[0]["DoughnutRadius"] = "60";
            chartRight3.Series[0]["PieDrawingStyle"] = "SoftEdge";

            RefreshData();


            Init();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            new Thread(new ThreadStart(RefreshData)).Start();
        }

        public void RefreshData()
        {
            Random ra = new Random();

            List<string> xPie = new List<string>() { "停车", "逆行", "抛洒物", "行人" };
            List<int> yPie = new List<int>() {
                ra.Next(1, 10),
                ra.Next(1, 10),
                ra.Next(1, 10),
                ra.Next(1, 10)};
            BeginInvoke(new Action(() =>
            {
                chartPie.Series[0].Points.DataBindXY(xPie, yPie);
                List<Color> colors = new List<Color>() {
                        Color.FromArgb(255,0,51),
                        Color.FromArgb(153,204,51),
                        Color.FromArgb(0,153,204),
                        Color.FromArgb(153,51,204),
                        Color.FromArgb(255,153,0)
                    };
                DataPointCollection points = chartPie.Series[0].Points;
                for (int i = 0; i < points.Count; i++) {
                    points[i].Color = colors[i];
                }
            }));

            List<string> x1 = new List<string>() { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
            List<int> y1 = new List<int>() {
                ra.Next(1, 10),
                ra.Next(1, 10),
                ra.Next(1, 10),
                ra.Next(1, 10),
                ra.Next(1, 10),
                ra.Next(1, 10),
                ra.Next(1, 10)
            };
            BeginInvoke(new Action(() =>
            {
                chartBar.Series[0].Points.DataBindXY(x1, y1);
                chartLine.Series[0].Points.DataBindXY(x1, y1);
            }));

            List<string> x2 = new List<string>() { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
            List<int> y21 =  new List<int>() {ra.Next(45, 95),ra.Next(45, 95),ra.Next(45, 95),ra.Next(45, 95),ra.Next(45, 95),ra.Next(45, 95),ra.Next(45, 95)};
            List<int> y22 = new List<int>() { ra.Next(45, 95), ra.Next(45, 95), ra.Next(45, 95), ra.Next(45, 95), ra.Next(45, 95), ra.Next(45, 95), ra.Next(45, 95) };
            List<int> y23 = new List<int>() { ra.Next(45, 95), ra.Next(45, 95), ra.Next(45, 95), ra.Next(45, 95), ra.Next(45, 95), ra.Next(45, 95), ra.Next(45, 95) };
            List<int> y24 = new List<int>() { ra.Next(45, 95), ra.Next(45, 95), ra.Next(45, 95), ra.Next(45, 95), ra.Next(45, 95), ra.Next(45, 95), ra.Next(45, 95) };
            BeginInvoke(new Action(() =>
            {
                chartBig2.Series[0].Points.DataBindXY(x2, y21);
                chartBig2.Series[1].Points.DataBindXY(x2, y22);
                chartBig2.Series[2].Points.DataBindXY(x2, y23);
                chartBig2.Series[3].Points.DataBindXY(x2, y24);
            }));

            List<string> xNut1 = new List<string>() { "停车", "逆行", "抛洒物", "行人" };
            List<int> yNut1 = new List<int>() {ra.Next(1, 100),ra.Next(1, 100),ra.Next(1, 100),ra.Next(1, 100)};
            BeginInvoke(new Action(() =>
            {
                chartRight1.Series[0].Points.DataBindXY(xNut1, yNut1);
                chartRight1.Series["Default"].Points[0]["Exploded"] = "true";
                List<Color> colors = new List<Color>() {
                        Color.FromArgb(255,0,51),
                        Color.FromArgb(153,204,51),
                        Color.FromArgb(0,153,204),
                        Color.FromArgb(153,51,204),
                        Color.FromArgb(255,153,0)
                    };
                DataPointCollection points = chartRight1.Series[0].Points;
                for (int i = 0; i < points.Count; i++) {
                    points[i].Color = colors[i];
                }
            }));

            List<string> xNut2= new List<string>() { "停车", "逆行", "抛洒物", "行人" };
            List<int> yNut2 = new List<int>() { ra.Next(1, 100), ra.Next(1, 100), ra.Next(1, 100), ra.Next(1, 100) };
            BeginInvoke(new Action(() =>
            {
                chartRight2.Series[0].Points.DataBindXY(xNut2, yNut2);
                chartRight2.Series["Default"].Points[0]["Exploded"] = "true";
                List<Color> colors = new List<Color>() {
                        Color.FromArgb(255,0,51),
                        Color.FromArgb(153,204,51),
                        Color.FromArgb(0,153,204),
                        Color.FromArgb(153,51,204),
                        Color.FromArgb(255,153,0)
                    };
                DataPointCollection points = chartRight2.Series[0].Points;
                for (int i = 0; i < points.Count; i++) {
                    points[i].Color = colors[i];
                }
            }));

            List<string> xNut3 = new List<string>() { "停车", "逆行", "抛洒物", "行人" };
            List<int> yNut3 = new List<int>() { ra.Next(1, 100), ra.Next(1, 100), ra.Next(1, 100), ra.Next(1, 100) };
            BeginInvoke(new Action(() =>
            {
                chartRight3.Series[0].Points.DataBindXY(xNut3, yNut3);
                chartRight3.Series["Default"].Points[0]["Exploded"] = "true";
                List<Color> colors = new List<Color>() {
                        Color.FromArgb(255,0,51),
                        Color.FromArgb(153,204,51),
                        Color.FromArgb(0,153,204),
                        Color.FromArgb(153,51,204),
                        Color.FromArgb(255,153,0)
                    };
                DataPointCollection points = chartRight3.Series[0].Points;
                for (int i = 0; i < points.Count; i++) {
                    points[i].Color = colors[i];
                }
            }));
        }

        private void frmBlueLight_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.timer1.Enabled = false;



            DialogResult dr = MessageBox.Show("确定退出程序吗？！", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {

                //if (threadAutoRun != null)
                //{
                //    mExitEvent.Set();

                //    if (threadAutoRun.IsAlive)
                //    { //*Tips:if not abort the thread,the application is still the debugging status even though you've closed it.
                //        threadAutoRun.Abort();
                //    }
                //}



                backgroundProgrsmTtop();



                //if (PlcStatus)
                //    tcpSocket.StopClient();
                //udpSock.Close();



                //ControllerClose(Com);
                tj_ipcsys_drv_exit();



                ////方法一：
                ////FormClosing事件执行的时候程序已经准备退出了就不必调用Application.Exit();如果再调用一次Application.Exit();的话就会再激活一次FormCloseing事件，所以就弹出两次对话框！
                ////采用另外的一种方法把Application.Exit();换成 Application.ExitThread();就OK了！
                ////Application.Exit();//退出程序  
                //Application.ExitThread();


                ////方法二：
                //this.FormClosing -= new FormClosingEventHandler(this.FormMain_FormClosing);   //为保证Application.Exit();时 不再弹出提示，所以将FormClosing事件取消
                //Application.Exit(); //退出整个应用程序
                //Environment.Exit(0);

                System.Diagnostics.Process.GetCurrentProcess().Kill();
            }
            else
            {
                e.Cancel = true; //取消关闭事件
            }


        }








        #region IO函数
        //驱动初始化接口，主要功能包括加载 IPCSYS 驱动和分配系统资源， 必须在所有功能函数之前调用该函数。
        [DllImport("tj_ipcsys64.dll", EntryPoint = "tj_ipcsys_drv_init", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        private static extern int tj_ipcsys_drv_init();

        //卸载 IPCSYS 驱动，释放系统资源，在程序结束之前调用。
        [DllImport("tj_ipcsys64.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern int tj_ipcsys_drv_exit();

        //获取 IPCSYS 驱动版本
        [DllImport("tj_ipcsys64.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern byte tj_ipcsys_get_drv_version();

        ////GPIO 初始化函数，初始化 GPIO 复用引脚功能，必须在调用 GPIO 接口之前 调 用 一 次 。 参 数 gpio_type
        [DllImport("tj_ipcsys64.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern byte tj_ipcsys_gpio_init(int gpio_type);

        //获取主板上 GPIO 的个数。
        [DllImport("tj_ipcsys64.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern int tj_ipcsys_gpio_get_num();

        //设置GPIO模式，0为输入，1为输出
        [DllImport("tj_ipcsys64.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern int tj_ipcsys_gpio_set_pin_mode(byte pin, byte pin_mode);

        //设置某个 GPIO 输出引脚的电平。有效值为 0：低电平，1：高电平
        [DllImport("tj_ipcsys64.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern int tj_ipcsys_gpio_set_pin_level(byte pin, byte pin_level);

        //读取某个 GPIO 输入引脚的电平,pin - GPIO 硬件接口引脚序号 pin_level - GPIO 引脚的输入电平，有效值为 0：低电平，1：高电平
        [DllImport("tj_ipcsys64.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern int tj_ipcsys_gpio_get_pin_level(byte pin, ref byte pin_level);


        //输入IO默认高电平1
        byte RecIO2 = 1;
        byte RecIO4 = 1;
        byte RecIO6 = 1;
        byte RecIO8 = 1;
        byte RecIO10 = 1;
        byte RecIO12 = 1;
        byte RecIO14 = 1;
        byte RecIO16 = 1;
        byte RecIO18 = 1;
        byte RecIO20 = 1;
        byte RecIO22 = 1;
        byte RecIO24 = 1;
        byte RecIO26 = 1;
        byte RecIO28 = 1;
        byte RecIO30 = 1;
        byte RecIO32 = 1;

        //TJ_IPCSYS Error Code
        //#define TJ_IPCSYS_SUCCESS                 0
        //#define TJ_IPCSYS_INVALID_PARAMETER       1
        //#define TJ_IPCSYS_TIMEOUT                 2
        //#define TJ_IPCSYS_UNSUPPORTED             3
        //#define TJ_IPCSYS_FAILED                  4


        ////GPIO Pin index macro define
        //#define TJ_IPCSYS_GPIO_PCH_NUM8       0x10
        //#define TJ_IPCSYS_GPIO_IT8786_NUM8    0x20
        //#define TJ_IPCSYS_GPIO_IT8786_NUM16   0x30 

        //#define TJ_IPCSYS_GPI         0
        //#define TJ_IPCSYS_GPO         1
        //#define TJ_IPCSYS_GPO_LOW     0
        //#define TJ_IPCSYS_GPO_HIGH    1


        //#define TJ_IPCSYS_GPIO_HW_PIN1   1
        //#define TJ_IPCSYS_GPIO_HW_PIN2   2
        //#define TJ_IPCSYS_GPIO_HW_PIN3   3
        //#define TJ_IPCSYS_GPIO_HW_PIN4   4
        //#define TJ_IPCSYS_GPIO_HW_PIN5   5
        //#define TJ_IPCSYS_GPIO_HW_PIN6   6
        //#define TJ_IPCSYS_GPIO_HW_PIN7   7
        //#define TJ_IPCSYS_GPIO_HW_PIN8   8
        //#define TJ_IPCSYS_GPIO_HW_PIN9   9
        //#define TJ_IPCSYS_GPIO_HW_PIN10  10
        //#define TJ_IPCSYS_GPIO_HW_PIN11  11
        //#define TJ_IPCSYS_GPIO_HW_PIN12  12
        //#define TJ_IPCSYS_GPIO_HW_PIN13  13
        //#define TJ_IPCSYS_GPIO_HW_PIN14  14
        //#define TJ_IPCSYS_GPIO_HW_PIN15  15
        //#define TJ_IPCSYS_GPIO_HW_PIN16  16
        //#define TJ_IPCSYS_GPIO_HW_PIN17  17
        //#define TJ_IPCSYS_GPIO_HW_PIN18  18
        //#define TJ_IPCSYS_GPIO_HW_PIN19  19
        //#define TJ_IPCSYS_GPIO_HW_PIN20  20
        //#define TJ_IPCSYS_GPIO_HW_PIN21  21
        //#define TJ_IPCSYS_GPIO_HW_PIN22  22
        //#define TJ_IPCSYS_GPIO_HW_PIN23  23
        //#define TJ_IPCSYS_GPIO_HW_PIN24  24
        #endregion

        #region 光源函数
       
        #endregion

        #region 新版光源函数 Modbus版
        //打开Modbus串口
        [DllImport("tj_ipcsys64.dll", EntryPoint = "tj_ipcsys_modbus_init", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        private static extern int tj_ipcsys_modbus_init(int ComNum,int BaudRate,int data,int stop,int parity);

        //打开Modbus串口
        [DllImport("tj_ipcsys64.dll", EntryPoint = "tj_ipcsys_modbus_init_ex", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        private static extern int tj_ipcsys_modbus_init_ex(IntPtr serial_handle);
        
        //获取软件版本
        [DllImport("tj_ipcsys64.dll", EntryPoint = "tj_ipcsys_modbus_get_version", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        private static extern byte tj_ipcsys_modbus_get_version();

        //读取通道亮度值
        [DllImport("tj_ipcsys64.dll", EntryPoint = "tj_ipcsys_modbus_get_backlight_value", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        private static extern ushort tj_ipcsys_modbus_get_backlight_value(int channel);

        //设置通道亮度并保持点亮
        [DllImport("tj_ipcsys64.dll", EntryPoint = "tj_ipcsys_modbus_set_backlight_value", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        private static extern int tj_ipcsys_modbus_set_backlight_value(int channel, ushort backlight_value);

        //设置通道亮度和触发点亮时间
        [DllImport("tj_ipcsys64.dll", EntryPoint = "tj_ipcsys_modbus_set_backlight_time_value", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        private static extern int tj_ipcsys_modbus_set_backlight_time_value(int channel, ushort backlight_value,ushort timeout);

        //打开通道
        [DllImport("tj_ipcsys64.dll", EntryPoint = "tj_ipcsys_modbus_enable_channel", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        private static extern int tj_ipcsys_modbus_enable_channel(int channel);

        //关闭通道
        [DllImport("tj_ipcsys64.dll", EntryPoint = "tj_ipcsys_modbus_disable_channel", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        private static extern int tj_ipcsys_modbus_disable_channel(int channel);
        #endregion


        //USB开
        [DllImport("tj_ipcsys64.dll", EntryPoint = "tj_ipcsys_usb_power_enable", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        private static extern int tj_ipcsys_usb_power_enable();
        //USB关
        [DllImport("tj_ipcsys64.dll", EntryPoint = "tj_ipcsys_usb_power_disable", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        private static extern int tj_ipcsys_usb_power_disable();






        int Com = 0; //光源控制器连接端口
        int Res = 0; //光源操作函数返回值
        bool bIoInit = false;
        private void Init()
        {

            /////光源控制初始化
            //System.IO.Ports.SerialPort serialPort = new System.IO.Ports.SerialPort();
            //string[] strPortArr = System.IO.Ports.SerialPort.GetPortNames();
            //cmbLightPort.Items.Clear();
            //for (int i = 0; i < strPortArr.Length; i++)
            //{
            //    cmbLightPort.Items.Add(strPortArr[i]);
            //}

            cmbLightPort.SelectedIndex = 0;
                                     
            /////老版光源库
            //if (cmbLightPort.Items.Count > 0)
            //{
            //    cmbLightPort.SelectedIndex = 0;
            //    for (int i = 0; i < cmbLightPort.Items.Count; i++)
            //    {
            //        try
            //        {
            //            //Com = cmbLightPort.SelectedIndex;
            //            Com = i;
            //            Res = 0;
            //            Res = ControllerInitialize(Com);

            //            if (Res == 1)
            //            {
            //                //MessageBox.Show("初始化成功");
            //                showRunLog(e_showMethod.正常, "初始化成功", this.richTextBox1);
            //                cmbLightPort.SelectedIndex = Com;
            //                break;
            //            }
            //            else
            //            {
            //                //MessageBox.Show("初始化异常，请选择其他端口");
            //            }
            //        }
            //        catch (Exception ex)
            //        {
            //            //MessageBox.Show("光源控制器初始化出错:"+ex.Message);
            //            showRunLog(e_showMethod.错误, "光源控制器初始化出错:" + ex.Message, this.richTextBox1);
            //            break;
            //        }            
            //    }

            //    if (Res != 1)
            //        //MessageBox.Show("光源控制器自动初始化失败");
            //        showRunLog(e_showMethod.错误, "光源控制器自动初始化失败", this.richTextBox1);

            //}

            ///新版光源库 Modbus
            ////循环打开
            //if (cmbLightPort.Items.Count > 0)
            //{
            //    cmbLightPort.SelectedIndex = 0;
            //    for (int i = 0; i < cmbLightPort.Items.Count; i++)
            //    {
            //        try
            //        {
            //            //Com = cmbLightPort.SelectedIndex;
            //            Com = i;
            //            Res = -1;
            //            Res = tj_ipcsys_modbus_init(Com, 9600, 8, 1, 0);

            //            //if (Res == 0)
            //            if (Res == 1)
            //            {
            //                //MessageBox.Show("初始化成功");
            //                showRunLog(e_showMethod.正常, "初始化成功", this.richTextBox1);
            //                cmbLightPort.SelectedIndex = Com;
            //                break;
            //            }
            //            else
            //            {
            //                //MessageBox.Show("初始化异常，请选择其他端口");
            //            }
            //        }
            //        catch (Exception ex)
            //        {
            //            //MessageBox.Show("光源控制器初始化出错:"+ex.Message);
            //            showRunLog(e_showMethod.错误, "光源控制器初始化出错:" + ex.Message, this.richTextBox1);
            //            break;
            //        }
            //    }

            //    //if (Res != 0)
            //    if (Res != 1)
            //        //MessageBox.Show("光源控制器自动初始化失败");
            //        showRunLog(e_showMethod.错误, "光源控制器自动初始化失败", this.richTextBox1);
            //    else
            //    {
            //        byte bt = tj_ipcsys_modbus_get_version();
            //        showRunLog(e_showMethod.正常, bt.ToString(), this.richTextBox1);
            //        ushort sd = tj_ipcsys_modbus_get_backlight_value(0);
            //        showRunLog(e_showMethod.正常, sd.ToString(), this.richTextBox1);

            //    }

            //}

            ////自动获取USB转串口的串口号
            //Com = GetUsbComNum();
            //if (Com > -1)
            //{
            //    try
            //    {
            //        Res = -1;
            //        Res = tj_ipcsys_modbus_init(Com, 9600, 8, 1, 0);

            //        //if (Res == 0)
            //        if (Res == 1)
            //        {
            //            //MessageBox.Show("初始化成功");
            //            showRunLog(e_showMethod.正常, "初始化成功", this.richTextBox1);
            //            cmbLightPort.SelectedIndex = Com;
            //        }
            //        else
            //        {
            //            //MessageBox.Show("初始化异常，请选择其他端口");
            //        }
            //    }
            //    catch (Exception ex)
            //    {
            //        //MessageBox.Show("光源控制器初始化出错:"+ex.Message);
            //        showRunLog(e_showMethod.错误, "光源控制器初始化出错:" + ex.Message, this.richTextBox1);
            //    }

            //    //if (Res != 0)
            //    if (Res != 1)
            //        //MessageBox.Show("光源控制器自动初始化失败");
            //        showRunLog(e_showMethod.错误, "光源控制器自动初始化失败", this.richTextBox1);
            //    else
            //    {
            //        byte bt = tj_ipcsys_modbus_get_version();
            //        showRunLog(e_showMethod.正常, "版本号：" + bt.ToString("X2"), this.richTextBox1);
            //        ushort sd = tj_ipcsys_modbus_get_backlight_value(0);
            //        showRunLog(e_showMethod.正常, sd.ToString(), this.richTextBox1);
            //    }
            //}


            cmbChannelTrigger.SelectedIndex = 0;



            /////IO控制初始化

            //驱动初始化接口
            int a = tj_ipcsys_drv_init();
            if (a == 0)
            {
                bIoInit = true;
                //MessageBox.Show("io模块初始化成功");
                showRunLog(e_showMethod.正常, "io模块初始化成功", this.richTextBox2);
            }
            else
            {
                //MessageBox.Show("io模块初始化异常");
                showRunLog(e_showMethod.错误, "io模块初始化异常", this.richTextBox2);
            }

            if (bIoInit)
            {
                //GPIO 初始化函数，初始化 GPIO 复用引脚功能PCH_NUM8，
                //if (radioButton1.Checked == true)
                {
                    tj_ipcsys_gpio_init(0x10);

                    //设置某个GPIO 引脚的模式,0是输入，1是输出
                    tj_ipcsys_gpio_set_pin_mode(1, 1);
                    tj_ipcsys_gpio_set_pin_mode(3, 1);
                    tj_ipcsys_gpio_set_pin_mode(5, 1);
                    tj_ipcsys_gpio_set_pin_mode(7, 1);
                    tj_ipcsys_gpio_set_pin_mode(9, 1);
                    tj_ipcsys_gpio_set_pin_mode(11, 1);
                    tj_ipcsys_gpio_set_pin_mode(13, 1);
                    tj_ipcsys_gpio_set_pin_mode(15, 1);
                    tj_ipcsys_gpio_set_pin_mode(17, 1);
                    tj_ipcsys_gpio_set_pin_mode(19, 1);
                    tj_ipcsys_gpio_set_pin_mode(21, 1);
                    tj_ipcsys_gpio_set_pin_mode(23, 1);
                    tj_ipcsys_gpio_set_pin_mode(25, 1);
                    tj_ipcsys_gpio_set_pin_mode(27, 1);
                    tj_ipcsys_gpio_set_pin_mode(29, 1);
                    tj_ipcsys_gpio_set_pin_mode(31, 1);

                    ////设置某个 GPIO 输出引脚的电平。有效值为 0：低电平，1：高电平
                    //tj_ipcsys_gpio_set_pin_level(1, 0);
                    //tj_ipcsys_gpio_set_pin_level(1, 1);

                    //tj_ipcsys_gpio_set_pin_level(3, 0);
                    //tj_ipcsys_gpio_set_pin_level(3, 1);

                    //tj_ipcsys_gpio_set_pin_level(5, 0);
                    //tj_ipcsys_gpio_set_pin_level(5, 1);

                    //tj_ipcsys_gpio_set_pin_level(7, 0);
                    //tj_ipcsys_gpio_set_pin_level(7, 1);

                    tj_ipcsys_gpio_set_pin_mode(2, 0);
                    tj_ipcsys_gpio_set_pin_mode(4, 0);
                    tj_ipcsys_gpio_set_pin_mode(6, 0);
                    tj_ipcsys_gpio_set_pin_mode(8, 0);
                    tj_ipcsys_gpio_set_pin_mode(10, 0);
                    tj_ipcsys_gpio_set_pin_mode(12, 0);
                    tj_ipcsys_gpio_set_pin_mode(14, 0);
                    tj_ipcsys_gpio_set_pin_mode(16, 0);
                    tj_ipcsys_gpio_set_pin_mode(18, 0);
                    tj_ipcsys_gpio_set_pin_mode(20, 0);
                    tj_ipcsys_gpio_set_pin_mode(22, 0);
                    tj_ipcsys_gpio_set_pin_mode(24, 0);
                    tj_ipcsys_gpio_set_pin_mode(26, 0);
                    tj_ipcsys_gpio_set_pin_mode(28, 0);
                    tj_ipcsys_gpio_set_pin_mode(30, 0);
                    tj_ipcsys_gpio_set_pin_mode(32, 0);

                    tj_ipcsys_gpio_set_pin_level(2, 1);
                    tj_ipcsys_gpio_set_pin_level(4, 1);
                    tj_ipcsys_gpio_set_pin_level(6, 1);
                    tj_ipcsys_gpio_set_pin_level(8, 1);
                    tj_ipcsys_gpio_set_pin_level(10, 1);
                    tj_ipcsys_gpio_set_pin_level(12, 1);
                    tj_ipcsys_gpio_set_pin_level(14, 1);
                    tj_ipcsys_gpio_set_pin_level(16, 1);
                    tj_ipcsys_gpio_set_pin_level(18, 1);
                    tj_ipcsys_gpio_set_pin_level(20, 1);
                    tj_ipcsys_gpio_set_pin_level(22, 1);
                    tj_ipcsys_gpio_set_pin_level(24, 1);
                    tj_ipcsys_gpio_set_pin_level(26, 1);
                    tj_ipcsys_gpio_set_pin_level(28, 1);
                    tj_ipcsys_gpio_set_pin_level(30, 1);
                    tj_ipcsys_gpio_set_pin_level(32, 1);



                    ////输入 有效值为0：低电平，1：高电平
                    //tj_ipcsys_gpio_get_pin_level(2, ref RecIO2);
                    //tj_ipcsys_gpio_get_pin_level(4, ref RecIO4);
                    //tj_ipcsys_gpio_get_pin_level(6, ref RecIO6);
                    //tj_ipcsys_gpio_get_pin_level(8, ref RecIO8);

                    // System.Drawing.Color.Red;
                    //labDI0.BackColor = System.Drawing.Color.LawnGreen;
                    //labDI1.BackColor = System.Drawing.Color.LawnGreen;
                    //labDI2.BackColor = System.Drawing.Color.LawnGreen;
                    //labDI3.BackColor = System.Drawing.Color.LawnGreen;
                    //labDI0.Enabled = false;
                    //labDI1.Enabled = false;
                    //labDI2.Enabled = false;
                    //labDI3.Enabled = false;

                    //btnDO0.BackColor = System.Drawing.Color.LawnGreen;
                    //btnDO1.BackColor = System.Drawing.Color.LawnGreen;
                    //btnDO2.BackColor = System.Drawing.Color.LawnGreen;
                    //btnDO3.BackColor = System.Drawing.Color.LawnGreen;
                    //btnDO0.Enabled = false;
                    //btnDO1.Enabled = false;
                    //btnDO2.Enabled = false;
                    //btnDO3.Enabled = false;

                }

                ////IT8786_NUM8
                //if (radioButton2.Checked == true)
                //{
                //    tj_ipcsys_gpio_init(0x20);

                //    //设置某个GPIO 引脚的模式,0是输入，1是输出
                //    tj_ipcsys_gpio_set_pin_mode(1, 1);
                //    tj_ipcsys_gpio_set_pin_mode(3, 1);
                //    tj_ipcsys_gpio_set_pin_mode(5, 1);
                //    tj_ipcsys_gpio_set_pin_mode(7, 1);

                //    tj_ipcsys_gpio_set_pin_mode(2, 0);
                //    tj_ipcsys_gpio_set_pin_mode(4, 0);
                //    tj_ipcsys_gpio_set_pin_mode(6, 0);
                //    tj_ipcsys_gpio_set_pin_mode(8, 0);

                //    DI1.BackColor = System.Drawing.Color.LawnGreen;
                //    DI2.BackColor = System.Drawing.Color.LawnGreen;
                //    DI3.BackColor = System.Drawing.Color.LawnGreen;
                //    DI4.BackColor = System.Drawing.Color.LawnGreen;
                //    DI5.Enabled = false;
                //    DI6.Enabled = false;
                //    DI7.Enabled = false;
                //    DI8.Enabled = false;

                //    DO1.BackColor = System.Drawing.Color.LawnGreen;
                //    DO2.BackColor = System.Drawing.Color.LawnGreen;
                //    DO3.BackColor = System.Drawing.Color.LawnGreen;
                //    DO4.BackColor = System.Drawing.Color.LawnGreen;
                //    DO5.Enabled = false;
                //    DO6.Enabled = false;
                //    DO7.Enabled = false;
                //    DO8.Enabled = false;
                //}
                ////IT8786_NUM16
                //if (radioButton3.Checked == true)
                //{
                //    tj_ipcsys_gpio_init(0x30);

                //    //设置某个GPIO 引脚的模式,0是输入，1是输出
                //    tj_ipcsys_gpio_set_pin_mode(1, 1);
                //    tj_ipcsys_gpio_set_pin_mode(3, 1);
                //    tj_ipcsys_gpio_set_pin_mode(5, 1);
                //    tj_ipcsys_gpio_set_pin_mode(7, 1);
                //    tj_ipcsys_gpio_set_pin_mode(9, 1);
                //    tj_ipcsys_gpio_set_pin_mode(11, 1);
                //    tj_ipcsys_gpio_set_pin_mode(13, 1);
                //    tj_ipcsys_gpio_set_pin_mode(15, 1);

                //    tj_ipcsys_gpio_set_pin_mode(2, 0);
                //    tj_ipcsys_gpio_set_pin_mode(4, 0);
                //    tj_ipcsys_gpio_set_pin_mode(6, 0);
                //    tj_ipcsys_gpio_set_pin_mode(8, 0);
                //    tj_ipcsys_gpio_set_pin_mode(10, 0);
                //    tj_ipcsys_gpio_set_pin_mode(12, 0);
                //    tj_ipcsys_gpio_set_pin_mode(14, 0);
                //    tj_ipcsys_gpio_set_pin_mode(16, 0);

                //    DI1.BackColor = System.Drawing.Color.LawnGreen;
                //    DI2.BackColor = System.Drawing.Color.LawnGreen;
                //    DI3.BackColor = System.Drawing.Color.LawnGreen;
                //    DI4.BackColor = System.Drawing.Color.LawnGreen;
                //    DI5.BackColor = System.Drawing.Color.LawnGreen;
                //    DI6.BackColor = System.Drawing.Color.LawnGreen;
                //    DI7.BackColor = System.Drawing.Color.LawnGreen;
                //    DI8.BackColor = System.Drawing.Color.LawnGreen;

                //    DO1.BackColor = System.Drawing.Color.LawnGreen;
                //    DO2.BackColor = System.Drawing.Color.LawnGreen;
                //    DO3.BackColor = System.Drawing.Color.LawnGreen;
                //    DO4.BackColor = System.Drawing.Color.LawnGreen;
                //    DO5.BackColor = System.Drawing.Color.LawnGreen;
                //    DO6.BackColor = System.Drawing.Color.LawnGreen;
                //    DO7.BackColor = System.Drawing.Color.LawnGreen;
                //    DO8.BackColor = System.Drawing.Color.LawnGreen;
                //}

            }

            thReceiveSignalTime = new Thread(ReceiveSignalTime);
            thReceiveSignalTime.IsBackground = true;
            thReceiveSignalTime.Start();

            thIoTest = new Thread(IoTest);
            thIoTest.IsBackground = true;
            thIoTest.Start();




            ///////串口测试初始化
            ////通过WMI获取COM端口
            //string[] ss = MulGetHardwareInfo(HardwareEnum.Win32_PnPEntity, "Name");
            //for (int i = 0; i < ss.Length; i++)
            //{
            //    lstBoxComFound.Items.Add(ss[i]);
            //}
            
            string[] strPortArr = System.IO.Ports.SerialPort.GetPortNames();
            lstBoxComFound.Items.Clear();
            for (int i = 0; i < strPortArr.Length; i++)
            {
                lstBoxComFound.Items.Add(strPortArr[i]);
            }



            /////网口测试初始化

            


            //backgroundProgramStart();



            try
            {
                btnUsb.BackColor = Color.LimeGreen;
                tj_ipcsys_usb_power_enable();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }



        }





        #region ////////////////////////////////光源控制////////////////////////////////   
        byte Channel;
        ushort Data;
        int channelModbus;
        
        private void btnLightConnect_Click(object sender, EventArgs e)
        {
            try
            {
                Com = cmbLightPort.SelectedIndex;
                Res = -1;
                //Res = ControllerInitialize(Com);
                Res = tj_ipcsys_modbus_init(Com, 9600, 8, 1, 0);

                //if (Res == 1)
                if (Res == 0)
                {
                    //MessageBox.Show("初始化成功");
                    showRunLog(e_showMethod.正常, "初始化成功", this.richTextBox1);

                    byte bt = tj_ipcsys_modbus_get_version();
                    showRunLog(e_showMethod.正常, "版本号："+bt.ToString("X2"), this.richTextBox1);
                    ushort sd = tj_ipcsys_modbus_get_backlight_value(0); //读取第一个通道，通道编号从0开始
                    showRunLog(e_showMethod.正常, sd.ToString(), this.richTextBox1);
                    bLightInit = true;
                    numLight1.Value = Convert.ToDecimal(sd);
                    trackBarCH1.Value = sd;

                    sd = tj_ipcsys_modbus_get_backlight_value(1); //读取第2个通道，通道编号从0开始
                    numLight2.Value = Convert.ToDecimal(sd);
                    trackBarCH2.Value = sd;

                    sd = tj_ipcsys_modbus_get_backlight_value(2);
                    numLight3.Value = Convert.ToDecimal(sd);
                    trackBarCH3.Value = sd;

                    sd = tj_ipcsys_modbus_get_backlight_value(3);
                    numLight4.Value = Convert.ToDecimal(sd);
                    trackBarCH4.Value = sd;

                    sd = tj_ipcsys_modbus_get_backlight_value(4);
                    numLight5.Value = Convert.ToDecimal(sd);
                    trackBarCH5.Value = sd;

                    sd = tj_ipcsys_modbus_get_backlight_value(5);
                    numLight6.Value = Convert.ToDecimal(sd);
                    trackBarCH6.Value = sd;

                    sd = tj_ipcsys_modbus_get_backlight_value(6);
                    numLight7.Value = Convert.ToDecimal(sd);
                    trackBarCH7.Value = sd;

                    sd = tj_ipcsys_modbus_get_backlight_value(7);
                    numLight8.Value = Convert.ToDecimal(sd);
                    trackBarCH8.Value = sd;

                    bLightInit = false;
                }
                else
                {
                    //MessageBox.Show("初始化异常，请选择其他端口");
                    showRunLog(e_showMethod.错误, "初始化异常，请选择其他端口", this.richTextBox1);
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show("光源控制器初始化出错:" + ex.Message);
                showRunLog(e_showMethod.错误, "光源控制器初始化出错:" + ex.Message, this.richTextBox1);
            }
            
        }

        bool bLightInit = false;
        private void trackBarCH1_Scroll(object sender, EventArgs e)
        {
            numLight1.Value = trackBarCH1.Value; //会触发numLight1_ValueChanged事件，只需在numLight1_ValueChanged里面设置亮度就可以了

            /////只需在numLight1_ValueChanged里面设置亮度就可以了
            //try
            //{
            //    //Channel = byte.Parse(textBoxCH.Text);
            //    Channel = 1;
            //    //Data = ushort.Parse(textBoxIntensity.Text);
            //    Data = ushort.Parse(trackBarCH1.Value.ToString());
            //    //int re=SetControllerIntensity(Com, Channel, Data);
            //    channelModbus = 0; //读取第一个通道，通道编号从0开始
            //    tj_ipcsys_modbus_set_backlight_value(channelModbus, Data);
            //}
            //catch (Exception)
            //{

            //}
        }

        private void trackBarCH2_Scroll(object sender, EventArgs e)
        {
            numLight2.Value = trackBarCH2.Value;

            //只需在numLight2_ValueChanged里面设置亮度就可以了
            //try
            //{
            //    //Channel = byte.Parse(textBoxCH.Text);
            //    Channel = 2;
            //    //Data = ushort.Parse(textBoxIntensity.Text);
            //    Data = ushort.Parse(trackBarCH2.Value.ToString());
            //    //int re=SetControllerIntensity(Com, Channel, Data);
            //    channelModbus = 1;
            //    tj_ipcsys_modbus_set_backlight_value(channelModbus, Data);
            //}
            //catch (Exception)
            //{

            //}
        }

        private void trackBarCH3_Scroll(object sender, EventArgs e)
        {
            numLight3.Value = trackBarCH3.Value;

            //只需在numLight3_ValueChanged里面设置亮度就可以了
            //try
            //{
            //    //Channel = byte.Parse(textBoxCH.Text);
            //    Channel = 3;
            //    //Data = ushort.Parse(textBoxIntensity.Text);
            //    Data = ushort.Parse(trackBarCH3.Value.ToString());
            //    //int re=SetControllerIntensity(Com, Channel, Data);
            //    channelModbus = 2;
            //    tj_ipcsys_modbus_set_backlight_value(channelModbus, Data);
            //}
            //catch (Exception)
            //{

            //}
        }

        private void trackBarCH4_Scroll(object sender, EventArgs e)
        {
            numLight4.Value = trackBarCH4.Value;

            //只需在numLight4_ValueChanged里面设置亮度就可以了
            //try
            //{
            //    //Channel = byte.Parse(textBoxCH.Text);
            //    Channel = 4;
            //    //Data = ushort.Parse(textBoxIntensity.Text);
            //    Data = ushort.Parse(trackBarCH4.Value.ToString());
            //    //int re=SetControllerIntensity(Com, Channel, Data);
            //    channelModbus = 3;
            //    tj_ipcsys_modbus_set_backlight_value(channelModbus, Data);
            //}
            //catch (Exception)
            //{

            //}
        }
        
        private void trackBarCH5_Scroll(object sender, EventArgs e)
        {
            numLight5.Value = trackBarCH5.Value;
        }

        private void trackBarCH6_Scroll(object sender, EventArgs e)
        {
            numLight6.Value = trackBarCH6.Value;
        }

        private void trackBarCH7_Scroll(object sender, EventArgs e)
        {
            numLight7.Value = trackBarCH7.Value;
        }

        private void trackBarCH8_Scroll(object sender, EventArgs e)
        {
            numLight8.Value = trackBarCH8.Value;
        }


        private void numLight1_ValueChanged(object sender, EventArgs e)
        {
            if (bLightInit)
                return;

            trackBarCH1.Value = Convert.ToInt32(numLight1.Value); //不会触发trackBarCH1_Scroll事件

            try
            {
                //Channel = byte.Parse(textBoxCH.Text);
                Channel = 1;
                //Data = ushort.Parse(textBoxIntensity.Text);
                Data = ushort.Parse(trackBarCH1.Value.ToString());
                //int re = SetControllerIntensity(Com, Channel, Data);    
                channelModbus = 0; //设置第一个通道，通道编号从0开始
                tj_ipcsys_modbus_set_backlight_value(channelModbus, Data);
            }
            catch (Exception)
            {

            }
        }

        private void numLight2_ValueChanged(object sender, EventArgs e)
        {
            if (bLightInit)
                return;

            trackBarCH2.Value = Convert.ToInt32(numLight2.Value);

            try
            {
                //Channel = byte.Parse(textBoxCH.Text);
                Channel = 2;
                //Data = ushort.Parse(textBoxIntensity.Text);
                Data = ushort.Parse(trackBarCH2.Value.ToString());
                //int re = SetControllerIntensity(Com, Channel, Data);    
                channelModbus = 1;
                tj_ipcsys_modbus_set_backlight_value(channelModbus, Data);
            }
            catch (Exception)
            {

            }
        }

        private void numLight3_ValueChanged(object sender, EventArgs e)
        {
            if (bLightInit)
                return;

            trackBarCH3.Value = Convert.ToInt32(numLight3.Value);

            try
            {
                //Channel = byte.Parse(textBoxCH.Text);
                Channel = 3;
                //Data = ushort.Parse(textBoxIntensity.Text);
                Data = ushort.Parse(trackBarCH3.Value.ToString());
                //int re = SetControllerIntensity(Com, Channel, Data);    
                channelModbus = 2;
                tj_ipcsys_modbus_set_backlight_value(channelModbus, Data);
            }
            catch (Exception)
            {

            }
        }

        private void numLight4_ValueChanged(object sender, EventArgs e)
        {
            if (bLightInit)
                return;

            trackBarCH4.Value = Convert.ToInt32(numLight4.Value);

            try
            {
                //Channel = byte.Parse(textBoxCH.Text);
                Channel = 4;
                //Data = ushort.Parse(textBoxIntensity.Text);
                Data = ushort.Parse(trackBarCH4.Value.ToString());
                //int re = SetControllerIntensity(Com, Channel, Data);    
                channelModbus = 3;
                tj_ipcsys_modbus_set_backlight_value(channelModbus, Data);
            }
            catch (Exception)
            {

            }
        }

        private void numLight5_ValueChanged(object sender, EventArgs e)
        {
            if (bLightInit)
                return;

            trackBarCH5.Value = Convert.ToInt32(numLight5.Value);

            try
            {
                //Channel = byte.Parse(textBoxCH.Text);
                Channel = 5;
                //Data = ushort.Parse(textBoxIntensity.Text);
                Data = ushort.Parse(trackBarCH5.Value.ToString());
                //int re = SetControllerIntensity(Com, Channel, Data);    
                channelModbus = 4;
                tj_ipcsys_modbus_set_backlight_value(channelModbus, Data);
            }
            catch (Exception)
            {

            }
        }

        private void numLight6_ValueChanged(object sender, EventArgs e)
        {
            if (bLightInit)
                return;

            trackBarCH6.Value = Convert.ToInt32(numLight6.Value);

            try
            {
                //Channel = byte.Parse(textBoxCH.Text);
                Channel = 6;
                //Data = ushort.Parse(textBoxIntensity.Text);
                Data = ushort.Parse(trackBarCH6.Value.ToString());
                //int re = SetControllerIntensity(Com, Channel, Data);    
                channelModbus = 5;
                tj_ipcsys_modbus_set_backlight_value(channelModbus, Data);
            }
            catch (Exception)
            {

            }
        }

        private void numLight7_ValueChanged(object sender, EventArgs e)
        {
            if (bLightInit)
                return;

            trackBarCH7.Value = Convert.ToInt32(numLight7.Value);

            try
            {
                //Channel = byte.Parse(textBoxCH.Text);
                Channel = 7;
                //Data = ushort.Parse(textBoxIntensity.Text);
                Data = ushort.Parse(trackBarCH7.Value.ToString());
                //int re = SetControllerIntensity(Com, Channel, Data);    
                channelModbus = 6;
                tj_ipcsys_modbus_set_backlight_value(channelModbus, Data);
            }
            catch (Exception)
            {

            }
        }

        private void numLight8_ValueChanged(object sender, EventArgs e)
        {
            if (bLightInit)
                return;

            trackBarCH8.Value = Convert.ToInt32(numLight8.Value);

            try
            {
                //Channel = byte.Parse(textBoxCH.Text);
                Channel = 8;
                //Data = ushort.Parse(textBoxIntensity.Text);
                Data = ushort.Parse(trackBarCH8.Value.ToString());
                //int re = SetControllerIntensity(Com, Channel, Data);    
                channelModbus = 7;
                tj_ipcsys_modbus_set_backlight_value(channelModbus, Data);
            }
            catch (Exception)
            {

            }
        }



        private void btnSetTrigger_Click(object sender, EventArgs e)
        {
            switch(cmbChannelTrigger.SelectedIndex)
            {
                case 0:
                    Data = ushort.Parse(trackBarCH1.Value.ToString());break;
                case 1:
                    Data = ushort.Parse(trackBarCH2.Value.ToString()); break;
                case 2:
                    Data = ushort.Parse(trackBarCH3.Value.ToString()); break;
                case 3:
                    Data = ushort.Parse(trackBarCH4.Value.ToString()); break;
                case 4:
                    Data = ushort.Parse(trackBarCH5.Value.ToString()); break;
                case 5:
                    Data = ushort.Parse(trackBarCH6.Value.ToString()); break;
                case 6:
                    Data = ushort.Parse(trackBarCH7.Value.ToString()); break;
                case 7:
                    Data = ushort.Parse(trackBarCH8.Value.ToString()); break;
                default:
                    Data = ushort.Parse(trackBarCH1.Value.ToString()); break;
            }
            ushort timeOut=ushort.Parse(numTriggerTime.Value.ToString());
            tj_ipcsys_modbus_set_backlight_time_value(cmbChannelTrigger.SelectedIndex, Data, timeOut);
        }

        private void btnSetTriggerAll_Click(object sender, EventArgs e)
        {
            ushort da1 = ushort.Parse(trackBarCH1.Value.ToString());
            ushort da2 = ushort.Parse(trackBarCH2.Value.ToString());
            ushort da3 = ushort.Parse(trackBarCH3.Value.ToString());
            ushort da4 = ushort.Parse(trackBarCH4.Value.ToString());
            ushort da5 = ushort.Parse(trackBarCH5.Value.ToString());
            ushort da6 = ushort.Parse(trackBarCH6.Value.ToString());
            ushort da7 = ushort.Parse(trackBarCH7.Value.ToString());
            ushort da8 = ushort.Parse(trackBarCH8.Value.ToString());

            ushort timeOut=ushort.Parse(numTriggerTime.Value.ToString());
            tj_ipcsys_modbus_set_backlight_time_value(0, da1, timeOut);
            tj_ipcsys_modbus_set_backlight_time_value(1, da2, timeOut);
            tj_ipcsys_modbus_set_backlight_time_value(2, da3, timeOut);
            tj_ipcsys_modbus_set_backlight_time_value(3, da4, timeOut);
            tj_ipcsys_modbus_set_backlight_time_value(4, da5, timeOut);
            tj_ipcsys_modbus_set_backlight_time_value(5, da6, timeOut);
            tj_ipcsys_modbus_set_backlight_time_value(6, da7, timeOut);
            tj_ipcsys_modbus_set_backlight_time_value(7, da8, timeOut);
        }



        private void btnOpenChannel_Click(object sender, EventArgs e)
        {
            tj_ipcsys_modbus_enable_channel(cmbChannelTrigger.SelectedIndex + 1); //通道参数编号从1开始（设置亮度的编号是从0开始的）
        }

        private void btnCloseChannel_Click(object sender, EventArgs e)
        {
            tj_ipcsys_modbus_disable_channel(cmbChannelTrigger.SelectedIndex + 1);
        }
        

        private void btnOpen500_Click(object sender, EventArgs e)
        {
            numLight1.Value = Convert.ToDecimal(500);
            numLight2.Value = Convert.ToDecimal(500);
            numLight3.Value = Convert.ToDecimal(500);
            numLight4.Value = Convert.ToDecimal(500);
            numLight5.Value = Convert.ToDecimal(500);
            numLight6.Value = Convert.ToDecimal(500);
            numLight7.Value = Convert.ToDecimal(500);
            numLight8.Value = Convert.ToDecimal(500);
        }

        private void btnClose500_Click(object sender, EventArgs e)
        {
            numLight1.Value = Convert.ToDecimal(0);
            numLight2.Value = Convert.ToDecimal(0);
            numLight3.Value = Convert.ToDecimal(0);
            numLight4.Value = Convert.ToDecimal(0);
            numLight5.Value = Convert.ToDecimal(0);
            numLight6.Value = Convert.ToDecimal(0);
            numLight7.Value = Convert.ToDecimal(0);
            numLight8.Value = Convert.ToDecimal(0);
        }




        ////////////////////////////////光源控制////////////////////////////////
        #endregion




        #region ////////////////////////////////IO控制////////////////////////////////
        private void btnDO0_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnDO0.BackColor == System.Drawing.Color.DarkGray)
                {
                    //DO1.BackColor = System.Drawing.Color.Red;
                    tj_ipcsys_gpio_set_pin_level(1, 0);
                    btnDO0.BackColor = System.Drawing.Color.Lime;
                    //ListBoxLogMsg("DO1设置低电平");
                }
                else
                {
                    tj_ipcsys_gpio_set_pin_level(1, 1);
                    btnDO0.BackColor = System.Drawing.Color.DarkGray;
                    //ListBoxLogMsg("DO1设置高电平");
                }
            }
            catch (Exception)
            {

            }
        }

        private void btnDO1_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnDO1.BackColor == System.Drawing.Color.DarkGray)
                {
                    //DO1.BackColor = System.Drawing.Color.Red;
                    tj_ipcsys_gpio_set_pin_level(3, 0);
                    btnDO1.BackColor = System.Drawing.Color.Lime;
                    //ListBoxLogMsg("DO1设置低电平");
                }
                else
                {
                    tj_ipcsys_gpio_set_pin_level(3, 1);
                    btnDO1.BackColor = System.Drawing.Color.DarkGray;
                    //ListBoxLogMsg("DO1设置高电平");
                }
            }
            catch (Exception)
            {

            }
        }

        private void btnDO2_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnDO2.BackColor == System.Drawing.Color.DarkGray)
                {
                    //DO1.BackColor = System.Drawing.Color.Red;
                    tj_ipcsys_gpio_set_pin_level(5, 0);
                    btnDO2.BackColor = System.Drawing.Color.Lime;
                    //ListBoxLogMsg("DO1设置低电平");
                }
                else
                {
                    tj_ipcsys_gpio_set_pin_level(5, 1);
                    btnDO2.BackColor = System.Drawing.Color.DarkGray;
                    //ListBoxLogMsg("DO1设置高电平");
                }
            }
            catch (Exception)
            {

            }
        }

        private void btnDO3_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnDO3.BackColor == System.Drawing.Color.DarkGray)
                {
                    //DO1.BackColor = System.Drawing.Color.Red;
                    tj_ipcsys_gpio_set_pin_level(7, 0);
                    btnDO3.BackColor = System.Drawing.Color.Lime;
                    //ListBoxLogMsg("DO1设置低电平");
                }
                else
                {
                    tj_ipcsys_gpio_set_pin_level(7, 1);
                    btnDO3.BackColor = System.Drawing.Color.DarkGray;
                    //ListBoxLogMsg("DO1设置高电平");
                }
            }
            catch (Exception)
            {

            }
        }

        private void btnDO4_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnDO4.BackColor == System.Drawing.Color.DarkGray)
                {
                    //DO1.BackColor = System.Drawing.Color.Red;
                    tj_ipcsys_gpio_set_pin_level(9, 0);
                    btnDO4.BackColor = System.Drawing.Color.Lime;
                    //ListBoxLogMsg("DO1设置低电平");
                }
                else
                {
                    tj_ipcsys_gpio_set_pin_level(9, 1);
                    btnDO4.BackColor = System.Drawing.Color.DarkGray;
                    //ListBoxLogMsg("DO1设置高电平");
                }
            }
            catch (Exception)
            {

            }
        }

        private void btnDO5_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnDO5.BackColor == System.Drawing.Color.DarkGray)
                {
                    //DO1.BackColor = System.Drawing.Color.Red;
                    tj_ipcsys_gpio_set_pin_level(11, 0);
                    btnDO5.BackColor = System.Drawing.Color.Lime;
                    //ListBoxLogMsg("DO1设置低电平");
                }
                else
                {
                    tj_ipcsys_gpio_set_pin_level(11, 1);
                    btnDO5.BackColor = System.Drawing.Color.DarkGray;
                    //ListBoxLogMsg("DO1设置高电平");
                }
            }
            catch (Exception)
            {

            }
        }

        private void btnDO6_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnDO6.BackColor == System.Drawing.Color.DarkGray)
                {
                    //DO1.BackColor = System.Drawing.Color.Red;
                    tj_ipcsys_gpio_set_pin_level(13, 0);
                    btnDO6.BackColor = System.Drawing.Color.Lime;
                    //ListBoxLogMsg("DO1设置低电平");
                }
                else
                {
                    tj_ipcsys_gpio_set_pin_level(13, 1);
                    btnDO6.BackColor = System.Drawing.Color.DarkGray;
                    //ListBoxLogMsg("DO1设置高电平");
                }
            }
            catch (Exception)
            {

            }
        }

        private void btnDO7_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnDO7.BackColor == System.Drawing.Color.DarkGray)
                {
                    //DO1.BackColor = System.Drawing.Color.Red;
                    tj_ipcsys_gpio_set_pin_level(15, 0);
                    btnDO7.BackColor = System.Drawing.Color.Lime;
                    //ListBoxLogMsg("DO1设置低电平");
                }
                else
                {
                    tj_ipcsys_gpio_set_pin_level(15, 1);
                    btnDO7.BackColor = System.Drawing.Color.DarkGray;
                    //ListBoxLogMsg("DO1设置高电平");
                }
            }
            catch (Exception)
            {

            }
        }

        private void btnDO8_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnDO8.BackColor == System.Drawing.Color.DarkGray)
                {
                    //DO1.BackColor = System.Drawing.Color.Red;
                    tj_ipcsys_gpio_set_pin_level(17, 0);
                    btnDO8.BackColor = System.Drawing.Color.Lime;
                    //ListBoxLogMsg("DO1设置低电平");
                }
                else
                {
                    tj_ipcsys_gpio_set_pin_level(17, 1);
                    btnDO8.BackColor = System.Drawing.Color.DarkGray;
                    //ListBoxLogMsg("DO1设置高电平");
                }
            }
            catch (Exception)
            {

            }
        }

        private void btnDO9_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnDO9.BackColor == System.Drawing.Color.DarkGray)
                {
                    //DO1.BackColor = System.Drawing.Color.Red;
                    tj_ipcsys_gpio_set_pin_level(19, 0);
                    btnDO9.BackColor = System.Drawing.Color.Lime;
                    //ListBoxLogMsg("DO1设置低电平");
                }
                else
                {
                    tj_ipcsys_gpio_set_pin_level(19, 1);
                    btnDO9.BackColor = System.Drawing.Color.DarkGray;
                    //ListBoxLogMsg("DO1设置高电平");
                }
            }
            catch (Exception)
            {

            }
        }

        private void btnDO10_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnDO10.BackColor == System.Drawing.Color.DarkGray)
                {
                    //DO1.BackColor = System.Drawing.Color.Red;
                    tj_ipcsys_gpio_set_pin_level(21, 0);
                    btnDO10.BackColor = System.Drawing.Color.Lime;
                    //ListBoxLogMsg("DO1设置低电平");
                }
                else
                {
                    tj_ipcsys_gpio_set_pin_level(21, 1);
                    btnDO10.BackColor = System.Drawing.Color.DarkGray;
                    //ListBoxLogMsg("DO1设置高电平");
                }
            }
            catch (Exception)
            {

            }
        }

        private void btnDO11_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnDO11.BackColor == System.Drawing.Color.DarkGray)
                {
                    //DO1.BackColor = System.Drawing.Color.Red;
                    tj_ipcsys_gpio_set_pin_level(23, 0);
                    btnDO11.BackColor = System.Drawing.Color.Lime;
                    //ListBoxLogMsg("DO1设置低电平");
                }
                else
                {
                    tj_ipcsys_gpio_set_pin_level(23, 1);
                    btnDO11.BackColor = System.Drawing.Color.DarkGray;
                    //ListBoxLogMsg("DO1设置高电平");
                }
            }
            catch (Exception)
            {

            }
        }

        private void btnDO12_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnDO12.BackColor == System.Drawing.Color.DarkGray)
                {
                    //DO1.BackColor = System.Drawing.Color.Red;
                    tj_ipcsys_gpio_set_pin_level(25, 0);
                    btnDO12.BackColor = System.Drawing.Color.Lime;
                    //ListBoxLogMsg("DO1设置低电平");
                }
                else
                {
                    tj_ipcsys_gpio_set_pin_level(25, 1);
                    btnDO12.BackColor = System.Drawing.Color.DarkGray;
                    //ListBoxLogMsg("DO1设置高电平");
                }
            }
            catch (Exception)
            {

            }
        }

        private void btnDO13_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnDO13.BackColor == System.Drawing.Color.DarkGray)
                {
                    //DO1.BackColor = System.Drawing.Color.Red;
                    tj_ipcsys_gpio_set_pin_level(27, 0);
                    btnDO13.BackColor = System.Drawing.Color.Lime;
                    //ListBoxLogMsg("DO1设置低电平");
                }
                else
                {
                    tj_ipcsys_gpio_set_pin_level(27, 1);
                    btnDO13.BackColor = System.Drawing.Color.DarkGray;
                    //ListBoxLogMsg("DO1设置高电平");
                }
            }
            catch (Exception)
            {

            }
        }

        private void btnDO14_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnDO14.BackColor == System.Drawing.Color.DarkGray)
                {
                    //DO1.BackColor = System.Drawing.Color.Red;
                    tj_ipcsys_gpio_set_pin_level(29, 0);
                    btnDO14.BackColor = System.Drawing.Color.Lime;
                    //ListBoxLogMsg("DO1设置低电平");
                }
                else
                {
                    tj_ipcsys_gpio_set_pin_level(29, 1);
                    btnDO14.BackColor = System.Drawing.Color.DarkGray;
                    //ListBoxLogMsg("DO1设置高电平");
                }
            }
            catch (Exception)
            {

            }
        }

        private void btnDO15_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnDO15.BackColor == System.Drawing.Color.DarkGray)
                {
                    //DO1.BackColor = System.Drawing.Color.Red;
                    tj_ipcsys_gpio_set_pin_level(31, 0);
                    btnDO15.BackColor = System.Drawing.Color.Lime;
                    //ListBoxLogMsg("DO1设置低电平");
                }
                else
                {
                    tj_ipcsys_gpio_set_pin_level(31, 1);
                    btnDO15.BackColor = System.Drawing.Color.DarkGray;
                    //ListBoxLogMsg("DO1设置高电平");
                }
            }
            catch (Exception)
            {

            }
        }




        Thread thReceiveSignalTime;
        bool RecIO2Flag = true;
        int RecIO2Time = 0;
        bool RecIO4Flag = true;
        int RecIO4Time = 0;
        bool RecIO6Flag = true;
        int RecIO6Time = 0;
        bool RecIO8Flag = true;
        int RecIO8Time = 0;
        bool RecIO10Flag = true;
        bool RecIO12Flag = true;
        bool RecIO14Flag = true;
        bool RecIO16Flag = true;
        bool RecIO18Flag = true;
        bool RecIO20Flag = true;
        bool RecIO22Flag = true;
        bool RecIO24Flag = true;
        bool RecIO26Flag = true;
        bool RecIO28Flag = true;
        bool RecIO30Flag = true;
        bool RecIO32Flag = true;

        bool RecIO1Flag = true;
        bool RecIO3Flag = true;
        bool RecIO5Flag = true;
        bool RecIO7Flag = true;
        bool RecIO9Flag = true;
        bool RecIO11Flag = true;
        bool RecIO13Flag = true;
        bool RecIO15Flag = true;
        bool RecIO17Flag = true;
        bool RecIO19Flag = true;
        bool RecIO21Flag = true;
        bool RecIO23Flag = true;
        bool RecIO25Flag = true;
        bool RecIO27Flag = true;
        bool RecIO29Flag = true;
        bool RecIO31Flag = true;
        public void ReceiveSignalTime()
        {
            try
            {
                if (bIoInit)
                {
                    for (; ; )
                    {
                        if (rdbIoNormal.Checked)
                        {
                            Thread.Sleep(1);
                            tj_ipcsys_gpio_get_pin_level(2, ref RecIO2);
                            tj_ipcsys_gpio_get_pin_level(4, ref RecIO4);
                            tj_ipcsys_gpio_get_pin_level(6, ref RecIO6);
                            tj_ipcsys_gpio_get_pin_level(8, ref RecIO8);
                            tj_ipcsys_gpio_get_pin_level(10, ref RecIO10);
                            tj_ipcsys_gpio_get_pin_level(12, ref RecIO12);
                            tj_ipcsys_gpio_get_pin_level(14, ref RecIO14);
                            tj_ipcsys_gpio_get_pin_level(16, ref RecIO16);
                            tj_ipcsys_gpio_get_pin_level(18, ref RecIO18);
                            tj_ipcsys_gpio_get_pin_level(20, ref RecIO20);
                            tj_ipcsys_gpio_get_pin_level(22, ref RecIO22);
                            tj_ipcsys_gpio_get_pin_level(24, ref RecIO24);
                            tj_ipcsys_gpio_get_pin_level(26, ref RecIO26);
                            tj_ipcsys_gpio_get_pin_level(28, ref RecIO28);
                            tj_ipcsys_gpio_get_pin_level(30, ref RecIO30);
                            tj_ipcsys_gpio_get_pin_level(32, ref RecIO32);

                            if (RecIO2 == 0)
                            {
                                if (RecIO2Flag == true)
                                {
                                    RecIO2Flag = false;
                                    RecIO2Time++;
                                    //ListBoxLogMsg("DI1读取低电平" + RecIO2Time.ToString() + "次");

                                    labDI0.Invoke(new Action(() =>
                                    {
                                        labDI0.BackColor = Color.Lime;
                                    }));
                                }

                            }
                            else
                            {
                                RecIO2Flag = true;

                                labDI0.Invoke(new Action(() =>
                                {
                                    labDI0.BackColor = Color.DarkGray;
                                }));
                            }

                            if (RecIO4 == 0)
                            {
                                if (RecIO4Flag == true)
                                {
                                    RecIO4Flag = false;
                                    RecIO4Time++;
                                    //ListBoxLogMsg("DI2读取低电平" + RecIO4Time.ToString() + "次");

                                    BeginInvoke(new Action(() =>
                                    {
                                        labDI1.BackColor = Color.Lime;
                                    }));
                                }

                            }
                            else
                            {
                                RecIO4Flag = true;

                                BeginInvoke(new Action(() =>
                                {
                                    labDI1.BackColor = Color.DarkGray;
                                }));
                            }

                            if (RecIO6 == 0)
                            {
                                if (RecIO6Flag == true)
                                {
                                    RecIO6Flag = false;
                                    RecIO6Time++;
                                    //ListBoxLogMsg("DI3读取低电平" + RecIO6Time.ToString() + "次");

                                    BeginInvoke(new Action(() =>
                                    {
                                        labDI2.BackColor = Color.Lime;
                                    }));
                                }

                            }
                            else
                            {
                                RecIO6Flag = true;

                                BeginInvoke(new Action(() =>
                                {
                                    labDI2.BackColor = Color.DarkGray;
                                }));
                            }

                            if (RecIO8 == 0)
                            {
                                if (RecIO8Flag == true)
                                {
                                    RecIO8Flag = false;
                                    RecIO8Time++;
                                    //ListBoxLogMsg("DI4读取低电平" + RecIO8Time.ToString() + "次");

                                    BeginInvoke(new Action(() =>
                                    {
                                        labDI3.BackColor = Color.Lime;
                                    }));
                                }

                            }
                            else
                            {
                                RecIO8Flag = true;

                                BeginInvoke(new Action(() =>
                                {
                                    labDI3.BackColor = Color.DarkGray;
                                }));
                            }

                            if (RecIO10 == 0)
                            {
                                if (RecIO10Flag == true)
                                {
                                    RecIO10Flag = false;
                                    //RecIO8Time++;
                                    //ListBoxLogMsg("DI4读取低电平" + RecIO8Time.ToString() + "次");

                                    BeginInvoke(new Action(() =>
                                    {
                                        labDI4.BackColor = Color.Lime;
                                    }));
                                }

                            }
                            else
                            {
                                RecIO10Flag = true;

                                BeginInvoke(new Action(() =>
                                {
                                    labDI4.BackColor = Color.DarkGray;
                                }));
                            }

                            if (RecIO12 == 0)
                            {
                                if (RecIO12Flag == true)
                                {
                                    RecIO12Flag = false;
                                    //RecIO8Time++;
                                    //ListBoxLogMsg("DI4读取低电平" + RecIO8Time.ToString() + "次");

                                    BeginInvoke(new Action(() =>
                                    {
                                        labDI5.BackColor = Color.Lime;
                                    }));
                                }

                            }
                            else
                            {
                                RecIO12Flag = true;

                                BeginInvoke(new Action(() =>
                                {
                                    labDI5.BackColor = Color.DarkGray;
                                }));
                            }

                            if (RecIO14 == 0)
                            {
                                if (RecIO14Flag == true)
                                {
                                    RecIO14Flag = false;
                                    //RecIO8Time++;
                                    //ListBoxLogMsg("DI4读取低电平" + RecIO8Time.ToString() + "次");

                                    BeginInvoke(new Action(() =>
                                    {
                                        labDI6.BackColor = Color.Lime;
                                    }));
                                }

                            }
                            else
                            {
                                RecIO14Flag = true;

                                BeginInvoke(new Action(() =>
                                {
                                    labDI6.BackColor = Color.DarkGray;
                                }));
                            }

                            if (RecIO16 == 0)
                            {
                                if (RecIO16Flag == true)
                                {
                                    RecIO16Flag = false;
                                    //RecIO8Time++;
                                    //ListBoxLogMsg("DI4读取低电平" + RecIO8Time.ToString() + "次");

                                    BeginInvoke(new Action(() =>
                                    {
                                        labDI7.BackColor = Color.Lime;
                                    }));
                                }

                            }
                            else
                            {
                                RecIO16Flag = true;

                                BeginInvoke(new Action(() =>
                                {
                                    labDI7.BackColor = Color.DarkGray;
                                }));
                            }

                            if (RecIO18 == 0)
                            {
                                if (RecIO18Flag == true)
                                {
                                    RecIO18Flag = false;
                                    //RecIO8Time++;
                                    //ListBoxLogMsg("DI4读取低电平" + RecIO8Time.ToString() + "次");

                                    BeginInvoke(new Action(() =>
                                    {
                                        labDI8.BackColor = Color.Lime;
                                    }));
                                }

                            }
                            else
                            {
                                RecIO18Flag = true;

                                BeginInvoke(new Action(() =>
                                {
                                    labDI8.BackColor = Color.DarkGray;
                                }));
                            }

                            if (RecIO20 == 0)
                            {
                                if (RecIO20Flag == true)
                                {
                                    RecIO20Flag = false;
                                    //RecIO8Time++;
                                    //ListBoxLogMsg("DI4读取低电平" + RecIO8Time.ToString() + "次");

                                    BeginInvoke(new Action(() =>
                                    {
                                        labDI9.BackColor = Color.Lime;
                                    }));
                                }

                            }
                            else
                            {
                                RecIO20Flag = true;

                                BeginInvoke(new Action(() =>
                                {
                                    labDI9.BackColor = Color.DarkGray;
                                }));
                            }

                            if (RecIO22 == 0)
                            {
                                if (RecIO22Flag == true)
                                {
                                    RecIO22Flag = false;
                                    //RecIO8Time++;
                                    //ListBoxLogMsg("DI4读取低电平" + RecIO8Time.ToString() + "次");

                                    BeginInvoke(new Action(() =>
                                    {
                                        labDI10.BackColor = Color.Lime;
                                    }));
                                }

                            }
                            else
                            {
                                RecIO22Flag = true;

                                BeginInvoke(new Action(() =>
                                {
                                    labDI10.BackColor = Color.DarkGray;
                                }));
                            }

                            if (RecIO24 == 0)
                            {
                                if (RecIO24Flag == true)
                                {
                                    RecIO24Flag = false;
                                    //RecIO8Time++;
                                    //ListBoxLogMsg("DI4读取低电平" + RecIO8Time.ToString() + "次");

                                    BeginInvoke(new Action(() =>
                                    {
                                        labDI11.BackColor = Color.Lime;
                                    }));
                                }

                            }
                            else
                            {
                                RecIO24Flag = true;

                                BeginInvoke(new Action(() =>
                                {
                                    labDI11.BackColor = Color.DarkGray;
                                }));
                            }

                            if (RecIO26 == 0)
                            {
                                if (RecIO26Flag == true)
                                {
                                    RecIO26Flag = false;
                                    //RecIO8Time++;
                                    //ListBoxLogMsg("DI4读取低电平" + RecIO8Time.ToString() + "次");

                                    BeginInvoke(new Action(() =>
                                    {
                                        labDI12.BackColor = Color.Lime;
                                    }));
                                }

                            }
                            else
                            {
                                RecIO26Flag = true;

                                BeginInvoke(new Action(() =>
                                {
                                    labDI12.BackColor = Color.DarkGray;
                                }));
                            }

                            if (RecIO28 == 0)
                            {
                                if (RecIO28Flag == true)
                                {
                                    RecIO28Flag = false;
                                    //RecIO8Time++;
                                    //ListBoxLogMsg("DI4读取低电平" + RecIO8Time.ToString() + "次");

                                    BeginInvoke(new Action(() =>
                                    {
                                        labDI13.BackColor = Color.Lime;
                                    }));
                                }

                            }
                            else
                            {
                                RecIO28Flag = true;

                                BeginInvoke(new Action(() =>
                                {
                                    labDI13.BackColor = Color.DarkGray;
                                }));
                            }

                            if (RecIO30 == 0)
                            {
                                if (RecIO30Flag == true)
                                {
                                    RecIO30Flag = false;
                                    //RecIO8Time++;
                                    //ListBoxLogMsg("DI4读取低电平" + RecIO8Time.ToString() + "次");

                                    BeginInvoke(new Action(() =>
                                    {
                                        labDI14.BackColor = Color.Lime;
                                    }));
                                }

                            }
                            else
                            {
                                RecIO30Flag = true;

                                BeginInvoke(new Action(() =>
                                {
                                    labDI14.BackColor = Color.DarkGray;
                                }));
                            }

                            if (RecIO32 == 0)
                            {
                                if (RecIO32Flag == true)
                                {
                                    RecIO32Flag = false;
                                    //RecIO8Time++;
                                    //ListBoxLogMsg("DI4读取低电平" + RecIO8Time.ToString() + "次");

                                    BeginInvoke(new Action(() =>
                                    {
                                        labDI15.BackColor = Color.Lime;
                                    }));
                                }

                            }
                            else
                            {
                                RecIO32Flag = true;

                                BeginInvoke(new Action(() =>
                                {
                                    labDI15.BackColor = Color.DarkGray;
                                }));
                            }
                        }
                        else
                        {
                            //输入IO默认高电平1
                            byte RecIO = 1;

                            if(cmbIO1.SelectedIndex==0)
                            {                                
                                tj_ipcsys_gpio_get_pin_level(1, ref RecIO);
                                if (RecIO == 0)
                                {
                                    if (RecIO1Flag == true)
                                    {
                                        RecIO1Flag = false;                                        
                                        BeginInvoke(new Action(() =>
                                        {
                                            btnIO1.BackColor = Color.Lime;
                                        }));
                                    }
                                }
                                else
                                {
                                    RecIO1Flag = true;
                                    BeginInvoke(new Action(() =>
                                    {
                                        btnIO1.BackColor = Color.DarkGray;
                                    }));
                                }
                            }
                            
                            if (cmbIO2.SelectedIndex == 0)
                            {
                                tj_ipcsys_gpio_get_pin_level(2, ref RecIO);
                                if (RecIO == 0)
                                {
                                    if (RecIO2Flag == true)
                                    {
                                        RecIO2Flag = false;
                                        BeginInvoke(new Action(() =>
                                        {
                                            btnIO2.BackColor = Color.Lime;
                                        }));
                                    }
                                }
                                else
                                {
                                    RecIO2Flag = true;
                                    BeginInvoke(new Action(() =>
                                    {
                                        btnIO2.BackColor = Color.DarkGray;
                                    }));
                                }
                            }

                            if (cmbIO3.SelectedIndex == 0)
                            {
                                tj_ipcsys_gpio_get_pin_level(3, ref RecIO);
                                if (RecIO == 0)
                                {
                                    if (RecIO3Flag == true)
                                    {
                                        RecIO3Flag = false;
                                        BeginInvoke(new Action(() =>
                                        {
                                            btnIO3.BackColor = Color.Lime;
                                        }));
                                    }
                                }
                                else
                                {
                                    RecIO3Flag = true;
                                    BeginInvoke(new Action(() =>
                                    {
                                        btnIO3.BackColor = Color.DarkGray;
                                    }));
                                }
                            }

                            if (cmbIO4.SelectedIndex == 0)
                            {
                                tj_ipcsys_gpio_get_pin_level(4, ref RecIO);
                                if (RecIO == 0)
                                {
                                    if (RecIO4Flag == true)
                                    {
                                        RecIO4Flag = false;
                                        BeginInvoke(new Action(() =>
                                        {
                                            btnIO4.BackColor = Color.Lime;
                                        }));
                                    }
                                }
                                else
                                {
                                    RecIO4Flag = true;
                                    BeginInvoke(new Action(() =>
                                    {
                                        btnIO4.BackColor = Color.DarkGray;
                                    }));
                                }
                            }

                            if (cmbIO5.SelectedIndex == 0)
                            {
                                tj_ipcsys_gpio_get_pin_level(5, ref RecIO);
                                if (RecIO == 0)
                                {
                                    if (RecIO5Flag == true)
                                    {
                                        RecIO5Flag = false;
                                        BeginInvoke(new Action(() =>
                                        {
                                            btnIO5.BackColor = Color.Lime;
                                        }));
                                    }
                                }
                                else
                                {
                                    RecIO5Flag = true;
                                    BeginInvoke(new Action(() =>
                                    {
                                        btnIO5.BackColor = Color.DarkGray;
                                    }));
                                }
                            }

                            if (cmbIO6.SelectedIndex == 0)
                            {
                                tj_ipcsys_gpio_get_pin_level(6, ref RecIO);
                                if (RecIO == 0)
                                {
                                    if (RecIO6Flag == true)
                                    {
                                        RecIO6Flag = false;
                                        BeginInvoke(new Action(() =>
                                        {
                                            btnIO6.BackColor = Color.Lime;
                                        }));
                                    }
                                }
                                else
                                {
                                    RecIO6Flag = true;
                                    BeginInvoke(new Action(() =>
                                    {
                                        btnIO6.BackColor = Color.DarkGray;
                                    }));
                                }
                            }

                            if (cmbIO7.SelectedIndex == 0)
                            {
                                tj_ipcsys_gpio_get_pin_level(7, ref RecIO);
                                if (RecIO == 0)
                                {
                                    if (RecIO7Flag == true)
                                    {
                                        RecIO7Flag = false;
                                        BeginInvoke(new Action(() =>
                                        {
                                            btnIO7.BackColor = Color.Lime;
                                        }));
                                    }
                                }
                                else
                                {
                                    RecIO7Flag = true;
                                    BeginInvoke(new Action(() =>
                                    {
                                        btnIO7.BackColor = Color.DarkGray;
                                    }));
                                }
                            }

                            if (cmbIO8.SelectedIndex == 0)
                            {
                                tj_ipcsys_gpio_get_pin_level(8, ref RecIO);
                                if (RecIO == 0)
                                {
                                    if (RecIO8Flag == true)
                                    {
                                        RecIO8Flag = false;
                                        BeginInvoke(new Action(() =>
                                        {
                                            btnIO8.BackColor = Color.Lime;
                                        }));
                                    }
                                }
                                else
                                {
                                    RecIO8Flag = true;
                                    BeginInvoke(new Action(() =>
                                    {
                                        btnIO8.BackColor = Color.DarkGray;
                                    }));
                                }
                            }

                            if (cmbIO9.SelectedIndex == 0)
                            {
                                tj_ipcsys_gpio_get_pin_level(9, ref RecIO);
                                if (RecIO == 0)
                                {
                                    if (RecIO9Flag == true)
                                    {
                                        RecIO9Flag = false;
                                        BeginInvoke(new Action(() =>
                                        {
                                            btnIO9.BackColor = Color.Lime;
                                        }));
                                    }
                                }
                                else
                                {
                                    RecIO9Flag = true;
                                    BeginInvoke(new Action(() =>
                                    {
                                        btnIO9.BackColor = Color.DarkGray;
                                    }));
                                }
                            }

                            if (cmbIO10.SelectedIndex == 0)
                            {
                                tj_ipcsys_gpio_get_pin_level(10, ref RecIO);
                                if (RecIO == 0)
                                {
                                    if (RecIO10Flag == true)
                                    {
                                        RecIO10Flag = false;
                                        BeginInvoke(new Action(() =>
                                        {
                                            btnIO10.BackColor = Color.Lime;
                                        }));
                                    }
                                }
                                else
                                {
                                    RecIO10Flag = true;
                                    BeginInvoke(new Action(() =>
                                    {
                                        btnIO10.BackColor = Color.DarkGray;
                                    }));
                                }
                            }

                            if (cmbIO11.SelectedIndex == 0)
                            {
                                tj_ipcsys_gpio_get_pin_level(11, ref RecIO);
                                if (RecIO == 0)
                                {
                                    if (RecIO11Flag == true)
                                    {
                                        RecIO11Flag = false;
                                        BeginInvoke(new Action(() =>
                                        {
                                            btnIO11.BackColor = Color.Lime;
                                        }));
                                    }
                                }
                                else
                                {
                                    RecIO11Flag = true;
                                    BeginInvoke(new Action(() =>
                                    {
                                        btnIO11.BackColor = Color.DarkGray;
                                    }));
                                }
                            }

                            if (cmbIO12.SelectedIndex == 0)
                            {
                                tj_ipcsys_gpio_get_pin_level(12, ref RecIO);
                                if (RecIO == 0)
                                {
                                    if (RecIO12Flag == true)
                                    {
                                        RecIO12Flag = false;
                                        BeginInvoke(new Action(() =>
                                        {
                                            btnIO12.BackColor = Color.Lime;
                                        }));
                                    }
                                }
                                else
                                {
                                    RecIO12Flag = true;
                                    BeginInvoke(new Action(() =>
                                    {
                                        btnIO12.BackColor = Color.DarkGray;
                                    }));
                                }
                            }

                            if (cmbIO13.SelectedIndex == 0)
                            {
                                tj_ipcsys_gpio_get_pin_level(13, ref RecIO);
                                if (RecIO == 0)
                                {
                                    if (RecIO13Flag == true)
                                    {
                                        RecIO13Flag = false;
                                        BeginInvoke(new Action(() =>
                                        {
                                            btnIO13.BackColor = Color.Lime;
                                        }));
                                    }
                                }
                                else
                                {
                                    RecIO13Flag = true;
                                    BeginInvoke(new Action(() =>
                                    {
                                        btnIO13.BackColor = Color.DarkGray;
                                    }));
                                }
                            }

                            if (cmbIO14.SelectedIndex == 0)
                            {
                                tj_ipcsys_gpio_get_pin_level(14, ref RecIO);
                                if (RecIO == 0)
                                {
                                    if (RecIO14Flag == true)
                                    {
                                        RecIO14Flag = false;
                                        BeginInvoke(new Action(() =>
                                        {
                                            btnIO14.BackColor = Color.Lime;
                                        }));
                                    }
                                }
                                else
                                {
                                    RecIO14Flag = true;
                                    BeginInvoke(new Action(() =>
                                    {
                                        btnIO14.BackColor = Color.DarkGray;
                                    }));
                                }
                            }

                            if (cmbIO15.SelectedIndex == 0)
                            {
                                tj_ipcsys_gpio_get_pin_level(15, ref RecIO);
                                if (RecIO == 0)
                                {
                                    if (RecIO15Flag == true)
                                    {
                                        RecIO15Flag = false;
                                        BeginInvoke(new Action(() =>
                                        {
                                            btnIO15.BackColor = Color.Lime;
                                        }));
                                    }
                                }
                                else
                                {
                                    RecIO15Flag = true;
                                    BeginInvoke(new Action(() =>
                                    {
                                        btnIO15.BackColor = Color.DarkGray;
                                    }));
                                }
                            }

                            if (cmbIO16.SelectedIndex == 0)
                            {
                                tj_ipcsys_gpio_get_pin_level(16, ref RecIO);
                                if (RecIO == 0)
                                {
                                    if (RecIO16Flag == true)
                                    {
                                        RecIO16Flag = false;
                                        BeginInvoke(new Action(() =>
                                        {
                                            btnIO16.BackColor = Color.Lime;
                                        }));
                                    }
                                }
                                else
                                {
                                    RecIO16Flag = true;
                                    BeginInvoke(new Action(() =>
                                    {
                                        btnIO16.BackColor = Color.DarkGray;
                                    }));
                                }
                            }



                        }


                        Thread.Sleep(1);
                        Application.DoEvents();
                        Thread.Sleep(1);
                    }
                }
            }
            catch (Exception)
            {

            }
        }


        Thread thIoTest;
        private void IoTest()
        {
            if (chkIoLaoHua.Checked && bIoInit)
            {
                int iIndex = Convert.ToInt32( numIoOut.Value) * 2 + 1;
                byte bPin = Convert.ToByte(iIndex);
                try
                {
                    if (btnDO0.BackColor == System.Drawing.Color.DarkGray)
                    {
                        //DO1.BackColor = System.Drawing.Color.Red;
                        tj_ipcsys_gpio_set_pin_level(bPin, 0);
                        BeginInvoke(new Action(() =>
                        {
                            btnDO0.BackColor = System.Drawing.Color.Lime;
                        }));
                        //ListBoxLogMsg("DO1设置低电平");
                    }
                    else
                    {
                        tj_ipcsys_gpio_set_pin_level(bPin, 1);
                        BeginInvoke(new Action(() =>
                        {
                            btnDO0.BackColor = System.Drawing.Color.DarkGray;
                        }));
                        //ListBoxLogMsg("DO1设置高电平");
                    }
                }
                catch (Exception)
                {

                }
            }

            Thread.Sleep(Convert.ToInt32(numIoTime.Value));
            Application.DoEvents();
            Thread.Sleep(1);
        }

        ////////////////////////////////IO控制////////////////////////////////
        #endregion







        //////////C#获取windows串口号对应的串口(设备)名称
        /// <summary>
        /// 枚举win32 api
        /// </summary>
        public enum HardwareEnum
        {
            // 硬件
            Win32_Processor, // CPU 处理器
            Win32_PhysicalMemory, // 物理内存条
            Win32_Keyboard, // 键盘
            Win32_PointingDevice, // 点输入设备，包括鼠标。
            Win32_FloppyDrive, // 软盘驱动器
            Win32_DiskDrive, // 硬盘驱动器
            Win32_CDROMDrive, // 光盘驱动器
            Win32_BaseBoard, // 主板
            Win32_BIOS, // BIOS 芯片
            Win32_ParallelPort, // 并口
            Win32_SerialPort, // 串口
            Win32_SerialPortConfiguration, // 串口配置
            Win32_SoundDevice, // 多媒体设置，一般指声卡。
            Win32_SystemSlot, // 主板插槽 (ISA & PCI & AGP)
            Win32_USBController, // USB 控制器
            Win32_NetworkAdapter, // 网络适配器
            Win32_NetworkAdapterConfiguration, // 网络适配器设置
            Win32_Printer, // 打印机
            Win32_PrinterConfiguration, // 打印机设置
            Win32_PrintJob, // 打印机任务
            Win32_TCPIPPrinterPort, // 打印机端口
            Win32_POTSModem, // MODEM
            Win32_POTSModemToSerialPort, // MODEM 端口
            Win32_DesktopMonitor, // 显示器
            Win32_DisplayConfiguration, // 显卡
            Win32_DisplayControllerConfiguration, // 显卡设置
            Win32_VideoController, // 显卡细节。
            Win32_VideoSettings, // 显卡支持的显示模式。

            // 操作系统
            Win32_TimeZone, // 时区
            Win32_SystemDriver, // 驱动程序
            Win32_DiskPartition, // 磁盘分区
            Win32_LogicalDisk, // 逻辑磁盘
            Win32_LogicalDiskToPartition, // 逻辑磁盘所在分区及始末位置。
            Win32_LogicalMemoryConfiguration, // 逻辑内存配置
            Win32_PageFile, // 系统页文件信息
            Win32_PageFileSetting, // 页文件设置
            Win32_BootConfiguration, // 系统启动配置
            Win32_ComputerSystem, // 计算机信息简要
            Win32_OperatingSystem, // 操作系统信息
            Win32_StartupCommand, // 系统自动启动程序
            Win32_Service, // 系统安装的服务
            Win32_Group, // 系统管理组
            Win32_GroupUser, // 系统组帐号
            Win32_UserAccount, // 用户帐号
            Win32_Process, // 系统进程
            Win32_Thread, // 系统线程
            Win32_Share, // 共享
            Win32_NetworkClient, // 已安装的网络客户端
            Win32_NetworkProtocol, // 已安装的网络协议
            Win32_PnPEntity,//all device
        }
        /// <summary>
        /// WMI取硬件信息
        /// </summary>
        /// <param name="hardType"></param>
        /// <param name="propKey"></param>
        /// <returns></returns>
        public static string[] MulGetHardwareInfo(HardwareEnum hardType, string propKey)
        {

            List<string> strs = new List<string>();
            try
            {
                using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + hardType))
                {
                    var hardInfos = searcher.Get();
                    foreach (var hardInfo in hardInfos)
                    {
                        if (hardInfo.Properties[propKey].Value.ToString().Contains("COM"))
                        {
                            strs.Add(hardInfo.Properties[propKey].Value.ToString());
                        }

                    }
                    searcher.Dispose();
                }
                return strs.ToArray();
            }
            catch
            {
                return null;
            }
            finally
            { strs = null; }
        }

        ////通过WMI获取COM端口
        //string[] ss = MulGetHardwareInfo(HardwareEnum.Win32_PnPEntity, "Name");


        //////获取USB转串口的串口号
        /////1、情境：
        //做项目的时候要打开串口然后进行一些库函数的调用来操作目标板。串口使用的是usb转串口，板子插进拔出的，每次都使用不一样的usb口，debug的时候懒得每次改com口，又不想在UI上加上一个选择
        //com口的combox，于是就使用了下面这个方法。
        /// <summary>
    /// Get the target com num.
    /// </summary>
    /// <returns></returns>
        public static int GetUsbComNum()
        {
            int comNum = -1;
            string[] strArr = MulGetHardwareInfo(HardwareEnum.Win32_PnPEntity, "Name");
            foreach (string s in strArr)
            {
                //Debug.WriteLine(s);

                if (s.Length >= 23 && s.Contains("CH340"))
                {
                    int start = s.IndexOf("(") + 3;
                    int end = s.IndexOf(")");
                    comNum = Convert.ToInt32(s.Substring(start + 1, end - start - 1));
                }
            }

            return comNum;
        }





        public enum e_showMethod
        {
            正常,
            警告,
            错误
        }
        delegate void MyDelegate(e_showMethod showMethod, string str, RichTextBox richText);
        //public static void showRunLog(e_showMethod showMethod, string str)
        public void showRunLog(e_showMethod showMethod, string str,RichTextBox richText)
        {
            ////RichTextBox obj = frmMain.form1.rtxRunLog;
            //RichTextBox obj = this.richTextBox1;
            RichTextBox obj = richText;
            if (obj.InvokeRequired)
            {
                MyDelegate showLog = showRunLog;
                obj.Invoke(showLog, showMethod, str);
            }
            else
            {
                string showStr;
                //showStr = "[" + DateTime.Now.ToString() + "]:" + str;
                showStr = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "--> " + str;
                //DataProcessMethod.WriteLogText(showStr);
                if (showMethod == e_showMethod.正常)
                {
                    obj.SelectionColor = Color.White;
                }
                else if (showMethod == e_showMethod.警告)
                {
                    obj.SelectionColor = Color.Yellow;
                }
                else
                {
                    obj.SelectionColor = Color.Red;
                }
                if (obj.Lines.Length > 200) /////如果不清空，程序会越跑越慢
                    obj.Clear();
                //obj.Rtf = "";
                obj.AppendText(showStr + "\r\n");
                obj.ScrollToCaret();


                //int N = 3;
                //if (obj.Lines.Length > N) /////保留最后N条记录
                //{
                //    //obj.Clear();
                //    //obj.Rtf = "";

                //    string[] sLines = obj.Lines;
                //    string[] sNewLines = new string[sLines.Length - N];

                //    Array.Copy(sLines, N, sNewLines, 0, sNewLines.Length);
                //    obj.Lines = sNewLines;
                //}

            }
        }

        //public System.Diagnostics.PerformanceCounter CpuOccupied = new System.Diagnostics.PerformanceCounter("Memory","Available MBytes"); //内存可用
        public System.Diagnostics.PerformanceCounter CpuOccupied = new System.Diagnostics.PerformanceCounter("Processor", "% Processor Time","_Total");

        private void tmrSysTime_Tick(object sender, EventArgs e)
        {
            //toolStripStatusLabel3.Text = "                SystemTime：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss    dddd"); //中文的星期
            labSysTime.Text =DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss  ") + DateTime.Now.DayOfWeek; //英文的星期

            //获取CPU使用率
            float cpuUsage= CpuOccupied.NextValue();
            labCPU.Text = "Cpu:"+cpuUsage.ToString()+"%";

            var ramCounter = new System.Diagnostics.PerformanceCounter("Memory", "% Committed Bytes In Use");
            labRam.Text = "Ram:" + ramCounter.NextValue().ToString() + "%";

        }

        int i = 0;
        private void tmrLightLaoHua_Tick(object sender, EventArgs e)
        {            

            if (chkLightLaoHua.Checked)
            {
                int iTime = Convert.ToInt32(numLightLaoHuaTime.Value);
                if (tmrLightLaoHua.Interval != iTime)
                    tmrLightLaoHua.Interval = iTime;

                if (i >= 0)
                {
                    numLight1.Value = i * 100;
                    numLight2.Value = i * 100;
                    numLight3.Value = i * 100;
                    numLight4.Value = i * 100;
                    numLight5.Value = i * 100;
                    numLight6.Value = i * 100;
                    numLight7.Value = i * 100;
                    numLight8.Value = i * 100;
                }
                i++;
                if (i > 10)
                    i = -1;
            }

            
        }












        private int UploadData(string path, string file, string strData, ref string strErrMes)
        {
            strErrMes = "";

            //if (!System.IO.Directory.Exists("D:/data"))
            //    System.IO.Directory.CreateDirectory("D:/data");

            //if (System.IO.Directory.Exists("D:/data")) /////如果没有D盘，无法创建文件夹
            if (System.IO.Directory.Exists(path)) /////如果没有D盘，无法创建文件夹
            {
                //WriteTXT writeTxt = new WriteTXT("D:/data/data.txt");
                WriteTXT writeTxt = new WriteTXT(file);

                for (int i = 0; i < 1; i++)
                {
                    try
                    {
                        writeTxt.TextWrite(strData);
                        break;
                    }
                    catch (Exception ee)
                    {
                        string str = ee.Message;

                        strErrMes = str;

                        return 1;
                    }

                    System.Threading.Thread.Sleep(10);
                }

                //System.IO.File.Copy("D:/data/dataTemp.txt", "D:/data/data.txt", true);
            }
            else
                return 2;


            return 0;
        }



        /// <summary>
        /// 运行日志文件路径
        /// </summary>
        private string logFilePath = @"D:\ImageAndData\运行日志\";
        /// <summary>
        /// 源图文件路径
        /// </summary>
        private string sourceImageFilePath = @"D:\ImageAndData\图片\源图\";
        private string sourceImageFilePath1 = "";
        /// <summary>
        /// 处理图文件路径
        /// </summary>
        private string handleImageFilePath = @"D:\ImageAndData\图片\处理图\";
        private string handleImageFilePath1 = "";
        /// <summary>
        /// 是否按天为文件夹来保存图片
        /// </summary>
        bool bSaveImageAsDay = true;

        #region 后台程序

        Thread backgroundThread;
        /// <summary>
        /// 软件是否打开
        /// </summary>
        bool programIsOpen = true;
        /// <summary>
        /// 启动后台程序
        /// </summary>
        private void backgroundProgramStart()
        {
            backgroundThread = new Thread(backgroundProgram);
            backgroundThread.Start();
        }
        /// <summary>
        /// 停止后台程序
        /// </summary>
        private void backgroundProgrsmTtop()
        {
            programIsOpen = false;
        }
        /// <summary>
        /// 后台程序
        /// </summary>
        private void backgroundProgram()
        {
            while (programIsOpen)
            {
                if (bSaveImageAsDay)
                {
                    sourceImageFilePath1 = sourceImageFilePath + DateTime.Now.ToString("yyyy-MM-dd") + @"\";
                    handleImageFilePath1 = handleImageFilePath + DateTime.Now.ToString("yyyy-MM-dd") + @"\";
                    iImageSaveNum = iImageSaveDays;
                }
                else
                {
                    sourceImageFilePath1 = sourceImageFilePath;
                    handleImageFilePath1 = handleImageFilePath;
                }

                if (!Directory.Exists(logFilePath))
                    Directory.CreateDirectory(logFilePath);
                if (!Directory.Exists(sourceImageFilePath1))
                    Directory.CreateDirectory(sourceImageFilePath1);
                if (!Directory.Exists(handleImageFilePath1))
                    Directory.CreateDirectory(handleImageFilePath1);
                if (!Directory.Exists(@"D:\ImageAndData\Data\"))
                    Directory.CreateDirectory(@"D:\ImageAndData\Data\");

                Delay(5000);
                string Path = logFilePath + DateTime.Now.ToString("yyyyMMdd") + ".log";
                if (Directory.Exists(logFilePath)) //如果没有D盘,就创建不了文件夹
                    DataProcessMethod.WriteFile(Path);
                //DataProcessMethod.RunningLog(logFilePath, StoreData.runLogSaveNumber);
                //DataProcessMethod.SaveAndDeleteSourceImage(StoreData.isSaveSourceImage, StoreData.saveSourceImageAmount, sourceImageFilePath);
                //DataProcessMethod.SaveAndDeleteProcessImage(StoreData.isSaveProcessingImage, StoreData.saveProcessingImageAmount, handleImageFilePath);
                if (Directory.Exists(logFilePath))
                    DataProcessMethod.RunningLog(logFilePath, iLogSaveDays);
                if (Directory.Exists(sourceImageFilePath1))
                {
                    string strEr = "";
                    //DataProcessMethod.SaveAndDeleteSourceImage(isSaveSourceImage, iImageSaveNum, sourceImageFilePath1, bSaveImageAsDay, out strEr);
                    //if (strEr != "")
                        //showRunLog(e_showMethod.错误, "保存图片出错，请检查磁盘容量！ " + strEr);
                }
                if (Directory.Exists(handleImageFilePath1))
                {
                    string strEr = "";
                    //DataProcessMethod.SaveAndDeleteProcessImage(isSaveProcessImage, iImageSaveNum, handleImageFilePath1, bSaveImageAsDay, out strEr);
                    //if (strEr != "")
                        //showRunLog(e_showMethod.错误, "保存图片出错，请检查磁盘容量！ " + strEr);
                }

                if (Directory.Exists(@"D:\ImageAndData\Data\")) //如果没有D盘,就创建不了文件夹
                    RunningData(@"D:\ImageAndData\Data\", iLogSaveDays);
            }
        }



        #endregion

        public static void RunningData(string logFilePath, int number)
        {
            DirectoryInfo runLog = new DirectoryInfo(logFilePath);
            FileInfo[] runLogList = runLog.GetFiles("*.log", SearchOption.AllDirectories);
            while (runLogList.Count() > number)
            {
                string deleteLog = logFilePath + runLogList[0].ToString();
                File.Delete(deleteLog);
                runLogList = runLog.GetFiles("*.log", SearchOption.AllDirectories);
            }
        }


        //延时
        public static void Delay(int milliSecond)
        {
            int start = Environment.TickCount;
            while (Math.Abs(Environment.TickCount - start) < milliSecond)//毫秒
            {
                Application.DoEvents();
            }
        }



        private bool WhetherProcessRunning()
        {
            /////判断不了
            //bool flag = false;
            //System.Threading.Mutex hMutex = new System.Threading.Mutex(true, Application.ProductName, out flag);
            //bool bb = hMutex.WaitOne(0, false);
            //if(!flag)
            //{
            //    hMutex.ReleaseMutex();
            //    MessageBox.Show("程序进程已经运行！");
            //    Environment.Exit(1);
            //    return true;
            //}



            //string strProcessName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
            //string strApplicationName = Application.ProductName;

            ////string sss = Process.GetProcessesByName(strProcessName)

            //string sss = Process.GetProcesses().ToList().ToString();

            //foreach (Process p in Process.GetProcesses())
            //{
            //    if (p.ProcessName == strApplicationName)
            //        return true;
            //}



            Process current = default(Process);
            current = System.Diagnostics.Process.GetCurrentProcess();
            Process[] processes = null;
            processes = System.Diagnostics.Process.GetProcessesByName(current.ProcessName);

            Process process = default(Process);

            foreach (Process tempLoopVar_process in processes)
            {
                process = tempLoopVar_process;

                if (process.Id != current.Id)
                {
                    if (System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
                    {
                        return true;
                    }
                }
            }



            return false;
        }


        //2.在进程中查找是否已经有实例在运行
        #region 确保程序只运行一个实例
        private static System.Diagnostics.Process RunningInstance()
        {
            System.Diagnostics.Process current = System.Diagnostics.Process.GetCurrentProcess();
            System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName(current.ProcessName);
            //遍历与当前进程名称相同的进程列表 
            foreach (System.Diagnostics.Process process in processes)
            {
                //如果实例已经存在则忽略当前进程 
                if (process.Id != current.Id)
                {
                    //保证要打开的进程同已经存在的进程来自同一文件路径
                    if (System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
                    {
                        //返回已经存在的进程
                        return process;
                    }
                }
            }
            return null;
        }
        //3.已经有了就把它激活，并将其窗口放置最前端
        private static void HandleRunningInstance(System.Diagnostics.Process instance)
        {
            ShowWindowAsync(instance.MainWindowHandle, 1); //调用api函数，正常显示窗口
            SetForegroundWindow(instance.MainWindowHandle); //将窗口放置最前端
        }
        [System.Runtime.InteropServices.DllImport("User32.dll")]
        private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
        [System.Runtime.InteropServices.DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(System.IntPtr hWnd);
        #endregion



        public static bool bDiskFull = false;
        private void CheckDiskFreeSpace(string strDisk, int GB)
        {
            long availableFreeSpace = 0;
            long totalSize = 0;

            DriveInfo[] driveInfos = DriveInfo.GetDrives();
            driveInfos.ToList().ForEach((item) =>
            {
                if (item.IsReady)
                {
                    //if (item.Name == @"D:\")
                    if (item.Name == strDisk)
                    {
                        //Console.WriteLine(item.AvailableFreeSpace / 1024 / 1024 / 1024);//指示驱动器上的可用空闲空间总量（以GB为单位）。
                        //Console.WriteLine(item.TotalSize / 1024 / 1024 / 1024);//获取驱动器上存储空间的总大小（以字节为单位）。
                        availableFreeSpace = item.AvailableFreeSpace / 1024 / 1024 / 1024;//指示驱动器上的可用空闲空间总量（以GB为单位）。
                        totalSize = item.TotalSize / 1024 / 1024 / 1024;//获取驱动器上存储空间的总大小（以字节为单位）。
                    }
                }
            });

            if (Directory.Exists(strDisk))
            {
                if (availableFreeSpace < GB)
                {
                    bDiskFull = true;
                    //showRunLog(e_showMethod.错误, strDisk.Substring(0, 1) + "盘磁盘已满");
                }
                else
                    bDiskFull = false;
            }

            return;
        }








        [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
        private static extern int SetProcessWorkingSetSize(IntPtr process, int minimumWorkingSetSize, int maximumWorkingSetSize);

        private void ClearMemory()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
        }



        /////获取鼠标键盘无动作的时间
        [StructLayout(LayoutKind.Sequential)]
        struct LASTINPUTINFO
        {
            [MarshalAs(UnmanagedType.U4)]
            public int cbSize;
            [MarshalAs(UnmanagedType.U4)]
            public uint dwTime;
        }
        [DllImport("user32.dll")]
        static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
        /// <summary>
        /// GetLastInputTime()（毫秒） 所得到的时间即为当前用户无操作(鼠标键盘无动作)的时间
        /// </summary>
        /// <returns></returns>
        static long GetLastInputTime()
        {
            LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
            vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
            if (!GetLastInputInfo(ref vLastInputInfo)) return 0;
            return Environment.TickCount - (long)vLastInputInfo.dwTime;
        }






        // Paint事件 修改Group Box边框颜色
        void groupBox_Paint(object sender, PaintEventArgs e)
        {
            GroupBox gBox = (GroupBox)sender;

            e.Graphics.Clear(gBox.BackColor);  //清除掉原有的。（groupbox要设置背景色，不能是Transparent，不然清除之后背景色是一片黑色）
            e.Graphics.DrawString(gBox.Text, gBox.Font, Brushes.Yellow, 10, 1); //重绘文字
            var vSize = e.Graphics.MeasureString(gBox.Text, gBox.Font);
            Pen pn = new Pen(Color.Yellow, 2);
            //e.Graphics.DrawLine(Pens.Gold, 1, vSize.Height / 2, 8, vSize.Height / 2);
            e.Graphics.DrawLine(pn, 1, vSize.Height / 2, 8, vSize.Height / 2);
            e.Graphics.DrawLine(new Pen(Color.Yellow, 2), vSize.Width + 8, vSize.Height / 2, gBox.Width - 2, vSize.Height / 2);
            e.Graphics.DrawLine(new Pen(Color.Yellow, 2), 1, vSize.Height / 2, 1, gBox.Height - 2);
            e.Graphics.DrawLine(new Pen(Color.Yellow, 2), 1, gBox.Height - 2, gBox.Width - 2, gBox.Height - 2);
            e.Graphics.DrawLine(new Pen(Color.Yellow, 2), gBox.Width - 2, vSize.Height / 2, gBox.Width - 2, gBox.Height - 2);
        }





        private void btnMinimize_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }





        private void rdbIoNormal_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbIoNormal.Checked)
            {
                if (bIoInit)
                {
                    //GPIO 初始化函数，初始化 GPIO 复用引脚功能PCH_NUM8，
                    //if (radioButton1.Checked == true)
                    //{
                    tj_ipcsys_gpio_init(0x10);

                    //设置某个GPIO 引脚的模式,0是输入，1是输出
                    tj_ipcsys_gpio_set_pin_mode(1, 1);
                    tj_ipcsys_gpio_set_pin_mode(3, 1);
                    tj_ipcsys_gpio_set_pin_mode(5, 1);
                    tj_ipcsys_gpio_set_pin_mode(7, 1);
                    tj_ipcsys_gpio_set_pin_mode(9, 1);
                    tj_ipcsys_gpio_set_pin_mode(11, 1);
                    tj_ipcsys_gpio_set_pin_mode(13, 1);
                    tj_ipcsys_gpio_set_pin_mode(15, 1);
                    tj_ipcsys_gpio_set_pin_mode(17, 1);
                    tj_ipcsys_gpio_set_pin_mode(19, 1);
                    tj_ipcsys_gpio_set_pin_mode(21, 1);
                    tj_ipcsys_gpio_set_pin_mode(23, 1);
                    tj_ipcsys_gpio_set_pin_mode(25, 1);
                    tj_ipcsys_gpio_set_pin_mode(27, 1);
                    tj_ipcsys_gpio_set_pin_mode(29, 1);
                    tj_ipcsys_gpio_set_pin_mode(31, 1);

                    tj_ipcsys_gpio_set_pin_mode(2, 0);
                    tj_ipcsys_gpio_set_pin_mode(4, 0);
                    tj_ipcsys_gpio_set_pin_mode(6, 0);
                    tj_ipcsys_gpio_set_pin_mode(8, 0);
                    tj_ipcsys_gpio_set_pin_mode(10, 0);
                    tj_ipcsys_gpio_set_pin_mode(12, 0);
                    tj_ipcsys_gpio_set_pin_mode(14, 0);
                    tj_ipcsys_gpio_set_pin_mode(16, 0);
                    tj_ipcsys_gpio_set_pin_mode(18, 0);
                    tj_ipcsys_gpio_set_pin_mode(20, 0);
                    tj_ipcsys_gpio_set_pin_mode(22, 0);
                    tj_ipcsys_gpio_set_pin_mode(24, 0);
                    tj_ipcsys_gpio_set_pin_mode(26, 0);
                    tj_ipcsys_gpio_set_pin_mode(28, 0);
                    tj_ipcsys_gpio_set_pin_mode(30, 0);
                    tj_ipcsys_gpio_set_pin_mode(32, 0);
                }

                tabControl1.SelectedTab = tabControl1.TabPages[0];
            }
            else
            {
                cmbIO1.SelectedIndex = 0;
                cmbIO2.SelectedIndex = 1;
                cmbIO3.SelectedIndex = 0;
                cmbIO4.SelectedIndex = 1;
                cmbIO5.SelectedIndex = 0;
                cmbIO6.SelectedIndex = 1;
                cmbIO7.SelectedIndex = 0;
                cmbIO8.SelectedIndex = 1;
                cmbIO9.SelectedIndex = 0;
                cmbIO10.SelectedIndex = 1;
                cmbIO11.SelectedIndex = 0;
                cmbIO12.SelectedIndex = 1;
                cmbIO13.SelectedIndex = 0;
                cmbIO14.SelectedIndex = 1;
                cmbIO15.SelectedIndex = 0;
                cmbIO16.SelectedIndex = 1;
                cmbIO17.SelectedIndex = 0;
                cmbIO18.SelectedIndex = 1;
                cmbIO19.SelectedIndex = 0;
                cmbIO20.SelectedIndex = 1;
                cmbIO21.SelectedIndex = 0;
                cmbIO22.SelectedIndex = 1;
                cmbIO23.SelectedIndex = 0;
                cmbIO24.SelectedIndex = 1;
                cmbIO25.SelectedIndex = 0;
                cmbIO26.SelectedIndex = 1;
                cmbIO27.SelectedIndex = 0;
                cmbIO28.SelectedIndex = 1;
                cmbIO29.SelectedIndex = 0;
                cmbIO30.SelectedIndex = 1;
                cmbIO31.SelectedIndex = 0;
                cmbIO32.SelectedIndex = 1;

                if (cmbIO1.SelectedIndex == 0)
                {
                    btnIO1.Enabled = false;
                    btnIO1.BackColor = Color.DarkGray;
                }
                else
                {
                    btnIO1.Enabled = true;
                    btnIO1.BackColor = Color.Teal;
                }

                if (cmbIO2.SelectedIndex == 0)
                {
                    btnIO2.Enabled = false;
                    btnIO2.BackColor = Color.DarkGray;
                }
                else
                {
                    btnIO2.Enabled = true;
                    btnIO2.BackColor = Color.Teal;
                }

                if (cmbIO3.SelectedIndex == 0)
                {
                    btnIO3.Enabled = false;
                    btnIO3.BackColor = Color.DarkGray;
                }
                else
                {
                    btnIO3.Enabled = true;
                    btnIO3.BackColor = Color.Teal;
                }

                if (cmbIO4.SelectedIndex == 0)
                {
                    btnIO4.Enabled = false;
                    btnIO4.BackColor = Color.DarkGray;
                }
                else
                {
                    btnIO4.Enabled = true;
                    btnIO4.BackColor = Color.Teal;
                }

                if (cmbIO5.SelectedIndex == 0)
                {
                    btnIO5.Enabled = false;
                    btnIO5.BackColor = Color.DarkGray;
                }
                else
                {
                    btnIO5.Enabled = true;
                    btnIO5.BackColor = Color.Teal;
                }

                if (cmbIO6.SelectedIndex == 0)
                {
                    btnIO6.Enabled = false;
                    btnIO6.BackColor = Color.DarkGray;
                }
                else
                {
                    btnIO6.Enabled = true;
                    btnIO6.BackColor = Color.Teal;
                }

                if (cmbIO7.SelectedIndex == 0)
                {
                    btnIO7.Enabled = false;
                    btnIO7.BackColor = Color.DarkGray;
                }
                else
                {
                    btnIO7.Enabled = true;
                    btnIO7.BackColor = Color.Teal;
                }

                if (cmbIO8.SelectedIndex == 0) { btnIO8.Enabled = false; btnIO8.BackColor = Color.DarkGray; }
                else { btnIO8.Enabled = true; btnIO8.BackColor = Color.Teal; }

                if (cmbIO9.SelectedIndex == 0) { btnIO9.Enabled = false; btnIO9.BackColor = Color.DarkGray; }
                else { btnIO9.Enabled = true; btnIO9.BackColor = Color.Teal; }

                if (cmbIO10.SelectedIndex == 0) { btnIO10.Enabled = false; btnIO10.BackColor = Color.DarkGray; }
                else { btnIO10.Enabled = true; btnIO10.BackColor = Color.Teal; }

                if (cmbIO11.SelectedIndex == 0) { btnIO11.Enabled = false; btnIO11.BackColor = Color.DarkGray; }
                else { btnIO11.Enabled = true; btnIO11.BackColor = Color.Teal; }

                if (cmbIO12.SelectedIndex == 0) { btnIO12.Enabled = false; btnIO12.BackColor = Color.DarkGray; }
                else { btnIO12.Enabled = true; btnIO12.BackColor = Color.Teal; }

                if (cmbIO13.SelectedIndex == 0) { btnIO13.Enabled = false; btnIO13.BackColor = Color.DarkGray; }
                else { btnIO13.Enabled = true; btnIO13.BackColor = Color.Teal; }

                if (cmbIO14.SelectedIndex == 0) { btnIO14.Enabled = false; btnIO14.BackColor = Color.DarkGray; }
                else { btnIO14.Enabled = true; btnIO14.BackColor = Color.Teal; }

                if (cmbIO15.SelectedIndex == 0) { btnIO15.Enabled = false; btnIO15.BackColor = Color.DarkGray; }
                else { btnIO15.Enabled = true; btnIO15.BackColor = Color.Teal; }

                if (cmbIO16.SelectedIndex == 0) { btnIO16.Enabled = false; btnIO16.BackColor = Color.DarkGray; }
                else { btnIO16.Enabled = true; btnIO16.BackColor = Color.Teal; }

                if (cmbIO17.SelectedIndex == 0) { btnIO17.Enabled = false; btnIO17.BackColor = Color.DarkGray; }
                else { btnIO17.Enabled = true; btnIO17.BackColor = Color.Teal; }

                if (cmbIO18.SelectedIndex == 0) { btnIO18.Enabled = false; btnIO18.BackColor = Color.DarkGray; }
                else { btnIO18.Enabled = true; btnIO18.BackColor = Color.Teal; }

                if (cmbIO19.SelectedIndex == 0) { btnIO19.Enabled = false; btnIO19.BackColor = Color.DarkGray; }
                else { btnIO19.Enabled = true; btnIO19.BackColor = Color.Teal; }

                if (cmbIO20.SelectedIndex == 0) { btnIO20.Enabled = false; btnIO20.BackColor = Color.DarkGray; }
                else { btnIO20.Enabled = true; btnIO20.BackColor = Color.Teal; }

                if (cmbIO21.SelectedIndex == 0) { btnIO21.Enabled = false; btnIO21.BackColor = Color.DarkGray; }
                else { btnIO21.Enabled = true; btnIO21.BackColor = Color.Teal; }

                if (cmbIO22.SelectedIndex == 0) { btnIO22.Enabled = false; btnIO22.BackColor = Color.DarkGray; }
                else { btnIO22.Enabled = true; btnIO22.BackColor = Color.Teal; }

                if (cmbIO23.SelectedIndex == 0) { btnIO23.Enabled = false; btnIO23.BackColor = Color.DarkGray; }
                else { btnIO23.Enabled = true; btnIO23.BackColor = Color.Teal; }

                if (cmbIO24.SelectedIndex == 0) { btnIO24.Enabled = false; btnIO24.BackColor = Color.DarkGray; }
                else { btnIO24.Enabled = true; btnIO24.BackColor = Color.Teal; }

                if (cmbIO25.SelectedIndex == 0) { btnIO25.Enabled = false; btnIO25.BackColor = Color.DarkGray; }
                else { btnIO25.Enabled = true; btnIO25.BackColor = Color.Teal; }

                if (cmbIO26.SelectedIndex == 0) { btnIO26.Enabled = false; btnIO26.BackColor = Color.DarkGray; }
                else { btnIO26.Enabled = true; btnIO26.BackColor = Color.Teal; }

                if (cmbIO27.SelectedIndex == 0) { btnIO27.Enabled = false; btnIO27.BackColor = Color.DarkGray; }
                else { btnIO27.Enabled = true; btnIO27.BackColor = Color.Teal; }

                if (cmbIO28.SelectedIndex == 0) { btnIO28.Enabled = false; btnIO28.BackColor = Color.DarkGray; }
                else { btnIO28.Enabled = true; btnIO28.BackColor = Color.Teal; }

                if (cmbIO29.SelectedIndex == 0) { btnIO29.Enabled = false; btnIO29.BackColor = Color.DarkGray; }
                else { btnIO29.Enabled = true; btnIO29.BackColor = Color.Teal; }

                if (cmbIO30.SelectedIndex == 0) { btnIO30.Enabled = false; btnIO30.BackColor = Color.DarkGray; }
                else { btnIO30.Enabled = true; btnIO30.BackColor = Color.Teal; }

                if (cmbIO31.SelectedIndex == 0) { btnIO31.Enabled = false; btnIO31.BackColor = Color.DarkGray; }
                else { btnIO31.Enabled = true; btnIO31.BackColor = Color.Teal; }

                if (cmbIO32.SelectedIndex == 0) { btnIO32.Enabled = false; btnIO32.BackColor = Color.DarkGray; }
                else { btnIO32.Enabled = true; btnIO32.BackColor = Color.Teal; }
                

                if (bIoInit)
                {
                    //GPIO 初始化函数，初始化 GPIO 复用引脚功能PCH_NUM8，
                    //if (radioButton1.Checked == true)
                    //{
                    //tj_ipcsys_gpio_init(0x10);
                    
                    //设置某个GPIO 引脚的模式,0是输入，1是输出
                    tj_ipcsys_gpio_set_pin_mode(1, Convert.ToByte(cmbIO1.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(3, Convert.ToByte(cmbIO3.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(5, Convert.ToByte(cmbIO5.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(7, Convert.ToByte(cmbIO7.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(9, Convert.ToByte(cmbIO9.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(11, Convert.ToByte(cmbIO11.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(13, Convert.ToByte(cmbIO13.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(15, Convert.ToByte(cmbIO15.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(17, Convert.ToByte(cmbIO17.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(19, Convert.ToByte(cmbIO19.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(21, Convert.ToByte(cmbIO21.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(23, Convert.ToByte(cmbIO23.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(25, Convert.ToByte(cmbIO25.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(27, Convert.ToByte(cmbIO27.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(29, Convert.ToByte(cmbIO29.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(31, Convert.ToByte(cmbIO31.SelectedIndex));

                    tj_ipcsys_gpio_set_pin_mode(2, Convert.ToByte(cmbIO2.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(4, Convert.ToByte(cmbIO4.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(6, Convert.ToByte(cmbIO6.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(8, Convert.ToByte(cmbIO8.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(10, Convert.ToByte(cmbIO10.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(12, Convert.ToByte(cmbIO12.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(14, Convert.ToByte(cmbIO14.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(16, Convert.ToByte(cmbIO16.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(18, Convert.ToByte(cmbIO18.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(20, Convert.ToByte(cmbIO20.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(22, Convert.ToByte(cmbIO22.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(24, Convert.ToByte(cmbIO24.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(26, Convert.ToByte(cmbIO26.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(28, Convert.ToByte(cmbIO28.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(30, Convert.ToByte(cmbIO30.SelectedIndex));
                    tj_ipcsys_gpio_set_pin_mode(32, Convert.ToByte(cmbIO32.SelectedIndex));
                }

                tabControl1.SelectedTab = tabControl1.TabPages[1];
            }
        }

        private void cmbIO1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO1.SelectedIndex == 0) { btnIO1.Enabled = false; btnIO1.BackColor = Color.DarkGray; }
            else { btnIO1.Enabled = true; btnIO1.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(1, Convert.ToByte(cmbIO1.SelectedIndex));
        }

        private void cmbIO2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO2.SelectedIndex == 0) { btnIO2.Enabled = false; btnIO2.BackColor = Color.DarkGray; }
            else { btnIO2.Enabled = true; btnIO2.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(2, Convert.ToByte(cmbIO2.SelectedIndex));
        }

        private void cmbIO3_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO3.SelectedIndex == 0) { btnIO3.Enabled = false; btnIO3.BackColor = Color.DarkGray; }
            else { btnIO3.Enabled = true; btnIO3.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(3, Convert.ToByte(cmbIO3.SelectedIndex));
        }

        private void cmbIO4_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO4.SelectedIndex == 0) { btnIO4.Enabled = false; btnIO4.BackColor = Color.DarkGray; }
            else { btnIO4.Enabled = true; btnIO4.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(4, Convert.ToByte(cmbIO4.SelectedIndex));
        }

        private void cmbIO5_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO5.SelectedIndex == 0) { btnIO5.Enabled = false; btnIO5.BackColor = Color.DarkGray; }
            else { btnIO5.Enabled = true; btnIO5.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(5, Convert.ToByte(cmbIO5.SelectedIndex));
        }

        private void cmbIO6_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO6.SelectedIndex == 0) { btnIO6.Enabled = false; btnIO6.BackColor = Color.DarkGray; }
            else { btnIO6.Enabled = true; btnIO6.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(6, Convert.ToByte(cmbIO6.SelectedIndex));
        }

        private void cmbIO7_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO7.SelectedIndex == 0) { btnIO7.Enabled = false; btnIO7.BackColor = Color.DarkGray; }
            else { btnIO7.Enabled = true; btnIO7.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(7, Convert.ToByte(cmbIO7.SelectedIndex));
        }

        private void cmbIO8_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO8.SelectedIndex == 0) { btnIO8.Enabled = false; btnIO8.BackColor = Color.DarkGray; }
            else { btnIO8.Enabled = true; btnIO8.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(8, Convert.ToByte(cmbIO8.SelectedIndex));
        }

        private void cmbIO9_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO9.SelectedIndex == 0) { btnIO9.Enabled = false; btnIO9.BackColor = Color.DarkGray; }
            else { btnIO9.Enabled = true; btnIO9.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(9, Convert.ToByte(cmbIO9.SelectedIndex));
        }

        private void cmbIO10_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO10.SelectedIndex == 0) { btnIO10.Enabled = false; btnIO10.BackColor = Color.DarkGray; }
            else { btnIO10.Enabled = true; btnIO10.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(10, Convert.ToByte(cmbIO10.SelectedIndex));
        }

        private void cmbIO11_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO11.SelectedIndex == 0) { btnIO11.Enabled = false; btnIO11.BackColor = Color.DarkGray; }
            else { btnIO11.Enabled = true; btnIO11.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(11, Convert.ToByte(cmbIO11.SelectedIndex));
        }

        private void cmbIO12_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO12.SelectedIndex == 0) { btnIO12.Enabled = false; btnIO12.BackColor = Color.DarkGray; }
            else { btnIO12.Enabled = true; btnIO12.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(12, Convert.ToByte(cmbIO12.SelectedIndex));
        }

        private void cmbIO13_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO13.SelectedIndex == 0) { btnIO13.Enabled = false; btnIO13.BackColor = Color.DarkGray; }
            else { btnIO13.Enabled = true; btnIO13.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(13, Convert.ToByte(cmbIO13.SelectedIndex));
        }

        private void cmbIO14_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO14.SelectedIndex == 0) { btnIO14.Enabled = false; btnIO14.BackColor = Color.DarkGray; }
            else { btnIO14.Enabled = true; btnIO14.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(14, Convert.ToByte(cmbIO14.SelectedIndex));
        }

        private void cmbIO15_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO15.SelectedIndex == 0) { btnIO15.Enabled = false; btnIO15.BackColor = Color.DarkGray; }
            else { btnIO15.Enabled = true; btnIO15.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(15, Convert.ToByte(cmbIO15.SelectedIndex));
        }

        private void cmbIO16_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO16.SelectedIndex == 0) { btnIO16.Enabled = false; btnIO16.BackColor = Color.DarkGray; }
            else { btnIO16.Enabled = true; btnIO16.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(16, Convert.ToByte(cmbIO16.SelectedIndex));
        }

        private void cmbIO17_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO17.SelectedIndex == 0) { btnIO17.Enabled = false; btnIO17.BackColor = Color.DarkGray; }
            else { btnIO17.Enabled = true; btnIO17.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(17, Convert.ToByte(cmbIO17.SelectedIndex));
        }

        private void cmbIO18_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO18.SelectedIndex == 0) { btnIO18.Enabled = false; btnIO18.BackColor = Color.DarkGray; }
            else { btnIO18.Enabled = true; btnIO18.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(18, Convert.ToByte(cmbIO18.SelectedIndex));
        }

        private void cmbIO19_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO19.SelectedIndex == 0) { btnIO19.Enabled = false; btnIO19.BackColor = Color.DarkGray; }
            else { btnIO19.Enabled = true; btnIO19.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(19, Convert.ToByte(cmbIO19.SelectedIndex));
        }

        private void cmbIO20_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO20.SelectedIndex == 0) { btnIO20.Enabled = false; btnIO20.BackColor = Color.DarkGray; }
            else { btnIO20.Enabled = true; btnIO20.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(20, Convert.ToByte(cmbIO20.SelectedIndex));
        }

        private void cmbIO21_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO21.SelectedIndex == 0) { btnIO21.Enabled = false; btnIO21.BackColor = Color.DarkGray; }
            else { btnIO21.Enabled = true; btnIO21.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(21, Convert.ToByte(cmbIO21.SelectedIndex));
        }

        private void cmbIO22_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO22.SelectedIndex == 0) { btnIO22.Enabled = false; btnIO22.BackColor = Color.DarkGray; }
            else { btnIO22.Enabled = true; btnIO22.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(22, Convert.ToByte(cmbIO22.SelectedIndex));
        }

        private void cmbIO23_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO23.SelectedIndex == 0) { btnIO23.Enabled = false; btnIO23.BackColor = Color.DarkGray; }
            else { btnIO23.Enabled = true; btnIO23.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(23, Convert.ToByte(cmbIO23.SelectedIndex));
        }

        private void cmbIO24_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO24.SelectedIndex == 0) { btnIO24.Enabled = false; btnIO24.BackColor = Color.DarkGray; }
            else { btnIO24.Enabled = true; btnIO24.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(24, Convert.ToByte(cmbIO24.SelectedIndex));
        }

        private void cmbIO25_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO25.SelectedIndex == 0) { btnIO25.Enabled = false; btnIO25.BackColor = Color.DarkGray; }
            else { btnIO25.Enabled = true; btnIO25.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(25, Convert.ToByte(cmbIO25.SelectedIndex));
        }

        private void cmbIO26_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO26.SelectedIndex == 0) { btnIO26.Enabled = false; btnIO26.BackColor = Color.DarkGray; }
            else { btnIO26.Enabled = true; btnIO26.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(26, Convert.ToByte(cmbIO26.SelectedIndex));
        }

        private void cmbIO27_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO27.SelectedIndex == 0) { btnIO27.Enabled = false; btnIO27.BackColor = Color.DarkGray; }
            else { btnIO27.Enabled = true; btnIO27.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(27, Convert.ToByte(cmbIO27.SelectedIndex));
        }

        private void cmbIO28_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO28.SelectedIndex == 0) { btnIO28.Enabled = false; btnIO28.BackColor = Color.DarkGray; }
            else { btnIO28.Enabled = true; btnIO28.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(28, Convert.ToByte(cmbIO28.SelectedIndex));
        }

        private void cmbIO29_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO29.SelectedIndex == 0) { btnIO29.Enabled = false; btnIO29.BackColor = Color.DarkGray; }
            else { btnIO29.Enabled = true; btnIO29.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(29, Convert.ToByte(cmbIO29.SelectedIndex));
        }

        private void cmbIO30_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO30.SelectedIndex == 0) { btnIO30.Enabled = false; btnIO30.BackColor = Color.DarkGray; }
            else { btnIO30.Enabled = true; btnIO30.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(30, Convert.ToByte(cmbIO30.SelectedIndex));
        }

        private void cmbIO31_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO31.SelectedIndex == 0) { btnIO31.Enabled = false; btnIO31.BackColor = Color.DarkGray; }
            else { btnIO31.Enabled = true; btnIO31.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(31, Convert.ToByte(cmbIO31.SelectedIndex));
        }

        private void cmbIO32_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIO32.SelectedIndex == 0) { btnIO32.Enabled = false; btnIO32.BackColor = Color.DarkGray; }
            else { btnIO32.Enabled = true; btnIO32.BackColor = Color.Teal; }

            if (bIoInit)
                //设置某个GPIO 引脚的模式,0是输入，1是输出
                tj_ipcsys_gpio_set_pin_mode(32, Convert.ToByte(cmbIO32.SelectedIndex));
        }

        private void btnIO1_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO1.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(1, 0); btnIO1.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(1, 1); btnIO1.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO2_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO2.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(2, 0); btnIO2.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(2, 1); btnIO2.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO3_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO3.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(3, 0); btnIO3.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(3, 1); btnIO3.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO4_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO4.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(4, 0); btnIO4.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(4, 1); btnIO4.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO5_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO5.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(5, 0); btnIO5.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(5, 1); btnIO5.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO6_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO6.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(6, 0); btnIO6.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(6, 1); btnIO6.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO7_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO7.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(7, 0); btnIO7.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(7, 1); btnIO7.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO8_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO8.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(8, 0); btnIO8.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(8, 1); btnIO8.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO9_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO9.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(9, 0); btnIO9.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(9, 1); btnIO9.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO10_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO10.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(10, 0); btnIO10.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(10, 1); btnIO10.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO11_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO11.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(11, 0); btnIO11.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(11, 1); btnIO11.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO12_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO12.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(12, 0); btnIO12.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(12, 1); btnIO12.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO13_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO13.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(13, 0); btnIO13.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(13, 1); btnIO13.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO14_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO14.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(14, 0); btnIO14.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(14, 1); btnIO14.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO15_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO15.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(15, 0); btnIO15.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(15, 1); btnIO15.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO16_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO16.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(16, 0); btnIO16.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(16, 1); btnIO16.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO17_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO17.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(17, 0); btnIO17.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(17, 1); btnIO17.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO18_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO18.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(18, 0); btnIO18.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(18, 1); btnIO18.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO19_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO19.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(19, 0); btnIO19.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(19, 1); btnIO19.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO20_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO20.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(20, 0); btnIO20.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(20, 1); btnIO20.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO21_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO21.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(21, 0); btnIO21.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(21, 1); btnIO21.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO22_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO22.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(22, 0); btnIO22.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(22, 1); btnIO22.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO23_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO23.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(23, 0); btnIO23.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(23, 1); btnIO23.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO24_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO24.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(24, 0); btnIO24.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(24, 1); btnIO24.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO25_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO25.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(25, 0); btnIO25.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(25, 1); btnIO25.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO26_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO26.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(26, 0); btnIO26.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(26, 1); btnIO26.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO27_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO27.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(27, 0); btnIO27.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(27, 1); btnIO27.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO28_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO28.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(28, 0); btnIO28.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(28, 1); btnIO28.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO29_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO29.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(29, 0); btnIO29.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(29, 1); btnIO29.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO30_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO30.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(30, 0); btnIO30.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(30, 1); btnIO30.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO31_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO31.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(31, 0); btnIO31.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(31, 1); btnIO31.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }

        private void btnIO32_Click(object sender, EventArgs e)
        {
            if (!bIoInit)
                return;

            try
            {
                if (btnIO32.BackColor == System.Drawing.Color.DarkGray)
                { tj_ipcsys_gpio_set_pin_level(32, 0); btnIO32.BackColor = System.Drawing.Color.Lime; }
                else
                { tj_ipcsys_gpio_set_pin_level(32, 1); btnIO32.BackColor = System.Drawing.Color.DarkGray; }
            }
            catch (Exception)
            { }
        }





        private void btnUsb_Click(object sender, EventArgs e)
        {
            try
            {
                if (btnUsb.Text == "USB开")
                {
                    tj_ipcsys_usb_power_disable();
                    btnUsb.Text = "USB关";
                    btnUsb.BackColor = Color.Transparent;
                }
                else
                {
                    tj_ipcsys_usb_power_enable();
                    btnUsb.Text = "USB开";
                    btnUsb.BackColor = Color.LimeGreen;
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }










        
    }
}
