private void SaveImageButtonClick(object sender, RoutedEventArgs e)
        {
            SaveFileDialog saveDlg = new SaveFileDialog();//打开保存文件对话框

            saveDlg.Filter = "图片(*.bmp)|*.bmp";
            if (saveDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Camera.SaveImage("bmp", saveDlg.FileName);//保存图片
            }
        }
        private async void Run()
        {
            Mysql mysql; int result;
            await Task.Delay(1000);

            while (true)
            {
                try
                {
                    switch (StationNo)
                    {
                    case 1:
                        #region 1号机内容


                        if (H3u.ReadM("M2000"))
                        {
                            AddMessage("进料位-等待进板");
                            H3u.SetM("M2000", false);
                            short[] newBoard = new short[70];
                            for (int i = 0; i < 70; i++)
                            {
                                Random rd = new Random();
                                if (rd.Next(100) < 80)
                                {
                                    newBoard[i] = 1;
                                }
                                else
                                {
                                    newBoard[i] = 0;
                                }
                            }
                            H3u.WriteMultD("D4000", newBoard);
                            await Task.Delay(500);

                            H3u.SetM("M2100", true);
                            AddMessage("进料位-进板完成");
                        }

                        if (H3u.ReadM("M2011"))
                        {
                            AddMessage("工位1-进板完成");
                            H3u.SetM("M2011", false);
                            mysql = new Mysql();
                            if (await Task.Run <bool>(() => { return(mysql.Connect()); }))
                            {
                                var guid = Guid.NewGuid();
                                CurBoardID = guid.ToString();
                                string stm = $"UPDATE avilinestate SET M1BoardID = '{guid}'";
                                result = mysql.executeQuery(stm);
                                if (result > 0)
                                {
                                    AddMessage($"板编号 {guid}_{MachineID} 更新成功");
                                }
                            }
                            else
                            {
                                AddMessage("数据库未连接");
                                StatusDataBase = false;
                            }
                            mysql.DisConnect();
                        }



                        if (H3u.ReadM("M2010"))
                        {
                            int d2010 = H3u.ReadW("D2010");
                            AddMessage($"工位1-{d2010}-开始拍照");
                            H3u.SetM("M2010", false);
                            if (mycam.GrabImage(0, false))
                            {
                                AddMessage("拍照成功");
                                CameraIamge = mycam.CurrentImage;
                                if (await Task.Run <bool>(() => { return(mycam.SaveImage("bmp", Path.Combine(RemotePath, $"{CurBoardID}_{MachineID}_{d2010}.bmp"))); }))
                                {
                                    AddMessage("图片保存成功");
                                    tcpNet.TCPSend($"{CurBoardID}_{MachineID}_{d2010}.bmp");
                                }
                                else
                                {
                                    AddMessage("图片保存失败");
                                }
                                H3u.SetM("M2110", true);
                            }
                        }

                        #endregion
                        break;

                    case 2:
                        #region 2号机内容
                        if (H3u.ReadM("M2021"))
                        {
                            AddMessage("工位2-进板完成");
                            H3u.SetM("M2021", false);
                            mysql = new Mysql();
                            if (await Task.Run <bool>(() => { return(mysql.Connect()); }))
                            {
                                string    stm = $"SELECT * FROM avilinestate LIMIT 1";
                                DataSet   ds  = mysql.Select(stm);
                                DataTable dt  = ds.Tables["table0"];

                                CurBoardID = (string)dt.Rows[0]["M1BoardID"];

                                stm    = $"UPDATE avilinestate SET M2BoardID = '{CurBoardID}'";
                                result = mysql.executeQuery(stm);
                                if (result > 0)
                                {
                                    AddMessage($"板编号 {CurBoardID}_{MachineID} 更新成功");
                                }
                            }
                            else
                            {
                                AddMessage("数据库未连接");
                                StatusDataBase = false;
                            }
                            mysql.DisConnect();
                        }



                        if (H3u.ReadM("M2020"))
                        {
                            int d2020 = H3u.ReadW("D2020");
                            AddMessage($"工位2-{d2020}-开始拍照");
                            H3u.SetM("M2020", false);
                            if (mycam.GrabImage(0, false))
                            {
                                AddMessage("拍照成功");
                                CameraIamge = mycam.CurrentImage;
                                if (await Task.Run <bool>(() => { return(mycam.SaveImage("bmp", Path.Combine(RemotePath, $"{CurBoardID}_{MachineID}_{d2020}.bmp"))); }))
                                {
                                    AddMessage("图片保存成功");
                                    tcpNet.TCPSend($"{CurBoardID}_{MachineID}_{d2020}.bmp");
                                }
                                else
                                {
                                    AddMessage("图片保存失败");
                                }
                                H3u.SetM("M2120", true);
                            }
                        }
                        #endregion
                        break;

                    case 3:
                        #region 3号机内容
                        if (H3u.ReadM("M2031"))
                        {
                            AddMessage("工位3-进板完成");
                            H3u.SetM("M2031", false);
                            mysql = new Mysql();
                            if (await Task.Run <bool>(() => { return(mysql.Connect()); }))
                            {
                                string    stm = $"SELECT * FROM avilinestate LIMIT 1";
                                DataSet   ds  = mysql.Select(stm);
                                DataTable dt  = ds.Tables["table0"];

                                CurBoardID = (string)dt.Rows[0]["M2BoardID"];

                                stm    = $"UPDATE avilinestate SET M3BoardID = '{CurBoardID}'";
                                result = mysql.executeQuery(stm);
                                if (result > 0)
                                {
                                    AddMessage($"板编号 {CurBoardID}_{MachineID} 更新成功");
                                }
                            }
                            else
                            {
                                AddMessage("数据库未连接");
                                StatusDataBase = false;
                            }
                            mysql.DisConnect();
                        }



                        if (H3u.ReadM("M2030"))
                        {
                            int d2030 = H3u.ReadW("D2030");
                            AddMessage($"工位3-{d2030}-开始拍照");
                            H3u.SetM("M2030", false);
                            if (mycam.GrabImage(0, false))
                            {
                                AddMessage("拍照成功");
                                CameraIamge = mycam.CurrentImage;
                                if (await Task.Run <bool>(() => { return(mycam.SaveImage("bmp", Path.Combine(RemotePath, $"{CurBoardID}_{MachineID}_{d2030}.bmp"))); }))
                                {
                                    AddMessage("图片保存成功");
                                    tcpNet.TCPSend($"{CurBoardID}_{MachineID}_{d2030}.bmp");
                                }
                                else
                                {
                                    AddMessage("图片保存失败");
                                }
                                H3u.SetM("M2130", true);
                            }
                        }
                        #endregion
                        break;

                    case 4:
                        #region 4号机内容
                        if (H3u.ReadM("M2041"))
                        {
                            AddMessage("工位4-进板完成");
                            H3u.SetM("M2041", false);
                            mysql = new Mysql();
                            if (await Task.Run <bool>(() => { return(mysql.Connect()); }))
                            {
                                string    stm = $"SELECT * FROM avilinestate LIMIT 1";
                                DataSet   ds  = mysql.Select(stm);
                                DataTable dt  = ds.Tables["table0"];

                                CurBoardID = (string)dt.Rows[0]["M3BoardID"];

                                stm    = $"UPDATE avilinestate SET M4BoardID = '{CurBoardID}'";
                                result = mysql.executeQuery(stm);
                                if (result > 0)
                                {
                                    AddMessage($"板编号 {CurBoardID}_{MachineID} 更新成功");
                                }
                            }
                            else
                            {
                                AddMessage("数据库未连接");
                                StatusDataBase = false;
                            }
                            mysql.DisConnect();
                        }

                        if (H3u.ReadM("M2040"))
                        {
                            int d2040 = H3u.ReadW("D2040");
                            AddMessage($"工位4-{d2040}-开始拍照");
                            H3u.SetM("M2040", false);
                            if (mycam.GrabImage(0, false))
                            {
                                AddMessage("拍照成功");
                                CameraIamge = mycam.CurrentImage;
                                if (await Task.Run <bool>(() => { return(mycam.SaveImage("bmp", Path.Combine(RemotePath, $"{CurBoardID}_{MachineID}_{d2040}.bmp"))); }))
                                {
                                    AddMessage("图片保存成功");
                                    tcpNet.TCPSend($"{CurBoardID}_{MachineID}_{d2040}.bmp");
                                }
                                else
                                {
                                    AddMessage("图片保存失败");
                                }
                                H3u.SetM("M2140", true);
                            }
                        }
                        #endregion
                        break;

                    default:
                        break;
                    }


                    StatusPLC = H3u.ConnectState;
                }
                catch (Exception ex)
                {
                    AddMessage(ex.Message);
                }

                await Task.Delay(100);
            }
        }