Beispiel #1
0
        private void PLC_Update_100_ms(PLCInterface sender, PLCInterfaceEventArgs e)
        {
            String msg = "";

            // Start analize slike PRVOG RUBA S1
            if (((bool)e.StatusData.Kamere.CAM4ZahtjevZaAnalizomS1.Value) && (!edgeDetection)) //Edge detection
            {
                HDevExp.InitHalcon();
                Thread exportThread = new Thread(new ThreadStart(this.RunDia1));
                exportThread.Start();
            }
            edgeDetection = (bool)e.StatusData.Kamere.CAM4ZahtjevZaAnalizomS1.Value == true; //Edge detection help marker

            // Start analize slike PRVOG RUBA S2
            if (((bool)e.StatusData.Kamere.CAM4ZahtjevZaAnalizomS2.Value) && (!edgeDetection))
            {
                HDevExp.InitHalcon();
                Thread exportThread = new Thread(new ThreadStart(this.RunDia2));
                exportThread.Start();
            }
            edgeDetection = (bool)e.StatusData.Kamere.CAM4ZahtjevZaAnalizomS2.Value == true;

            // Prioritet poruka (najviši prioritet je na dnu)
            //if ((bool)e.StatusData.RotacijskaOs.AutomaticActive.Value) msg = "MJERENJE SRHA U TIJEKU";
            //if ((bool)e.StatusData.VertikalnaOs.AutomaticActive.Value) msg = "MJERENJE VALOVITOSTI U TIJEKU";
            //if ((bool)e.StatusData.Ticalo.AutomaticActive.Value) msg = "MJERENJE DIMENZIJA U TIJEKU";
            //if ((bool)e.StatusData.HorizontalnaOs.ReferencedX.Value == false) msg = "OS X NIJE REFERENCIRANA";
            //if ((bool)e.StatusData.HorizontalnaOs.ReferencedY.Value == false) msg = "OS Y NIJE REFERENCIRANA";
            //if ((bool)e.StatusData.HorizontalnaOs.FaultX.Value == true) msg = "GREŠKA OSI X";
            //if ((bool)e.StatusData.HorizontalnaOs.FaultY.Value == true) msg = "GREŠKA OSI Y";
            if (mwHandle != null)
            {
                mwHandle.tb_statusMessage.Dispatcher.BeginInvoke((Action)(() => { mwHandle.tb_statusMessage.Text = msg; }));
            }
        }
Beispiel #2
0
 public Form1()
 {
     InitializeComponent();
     getAddress();
     hwin = hSmartWindowControl1.HalconWindow;
     hd.InitHalcon(hwin);
     // 图片缩放订阅监听
     this.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.my_MouseWheel);
 }
Beispiel #3
0
        /// <summary>
        /// 检测函数
        /// </summary>
        private void Detect()
        {
            pbImage.Visible     = false;
            hWindowCtrl.Visible = true;
            if (imgOri != null)
            {
                Int32  nDefects = 0;
                Int32  nMode    = 0; // detection method selection
                String strFullFileName;

                if (string.IsNullOrEmpty(ParamSetting.ImageSavePath))
                {
                    ParamSetting.ImageSavePath = Application.StartupPath;
                }

                strFullFileName = ParamSetting.ImageSavePath + "\\" + strFileName;

                // save temp image
                imgOri.Save(strFullFileName);

                switch (nMode)
                {
                case 0:
                    // method 0: dynamic thresholding
                    detectInstanceDyn = new HDevelopExport();
                    detectInstanceDyn.InitHalcon();
                    if (File.Exists(strFullFileName))
                    {
                        detectInstanceDyn.RunHalcon(hWindowCtrl.HalconWindow, strFullFileName, ParamSetting.MeasureParam.MinArea, ParamSetting.MeasureParam.FilterSize, ParamSetting.MeasureParam.DynamicRange, out nDefects);
                    }
                    break;

                case 1:
                    // method 1: normal thresholding
                    detectInstanceNorm = new HDevelopExport1();
                    detectInstanceNorm.InitHalcon();
                    if (File.Exists(strFullFileName))
                    {
                        detectInstanceNorm.RunHalcon(hWindowCtrl.HalconWindow, strFullFileName, ParamSetting.MeasureParam.MinArea, ParamSetting.MeasureParam.GrayThreshold, out nDefects);
                    }
                    break;
                }

                if (nDefects > 0)
                {
                    lblResult.Text      = "NG";
                    lblResult.BackColor = Color.Red;
                    if (!string.IsNullOrEmpty(ParamSetting.ImageSavePath))
                    {
                        strSavePathNG = ParamSetting.ImageSavePath + "\\" + DateTime.Now.ToShortDateString().Replace("/", "-") + "\\NG";
                        CheckAndCreatePath(strSavePathNG);
                        if (pbImage.Image != null)
                        {
                            pbImage.Image.Save(strSavePathNG + "\\" + strFileName);
                        }
                    }
                }
                else
                {
                    lblResult.Text      = "OK";
                    lblResult.BackColor = Color.Green;
                    if (!string.IsNullOrEmpty(ParamSetting.ImageSavePath))
                    {
                        strSavePathOK = ParamSetting.ImageSavePath + "\\" + DateTime.Now.ToShortDateString().Replace("/", "-") + "\\OK";
                        CheckAndCreatePath(strSavePathOK);
                        if (pbImage.Image != null)
                        {
                            pbImage.Image.Save(strSavePathOK + "\\" + strFileName);
                        }
                    }
                }
            }

            pbImage.Image = imgOri;
        }
        //异步抓取图片

        public MainWindow()
        {
            InitializeComponent();
            App_Startup();
            HOperatorSet.GenEmptyObj(out Obj[0]);
            HOperatorSet.GenEmptyObj(out Obj[1]);
            HOperatorSet.GenEmptyObj(out Obj[2]);
            HOperatorSet.GenEmptyObj(out Obj[3]);
            INI.reading();

#if DEBUG
            ConsoleManager.Show();
            System.Console.WriteLine("程序已经启动...");
#endif
            int width  = (int)this.Cam1_Disp.Width;
            int height = (int)this.Cam1_Disp.Height;
            HOperatorSet.GenEmptyObj(out Obj[0]);
            CameraADisp.InitHalcon(width, height);

            width  = (int)this.Cam2_Disp.Width;
            height = (int)this.Cam2_Disp.Height;
            HOperatorSet.GenEmptyObj(out Obj[1]);
            CameraBDisp.InitHalcon(width, height);

            width  = (int)this.Cam3_Disp.Width;
            height = (int)this.Cam3_Disp.Height;
            HOperatorSet.GenEmptyObj(out Obj[2]);
            CameraCDisp.InitHalcon(width, height);

            width  = (int)this.Cam4_Disp.Width;
            height = (int)this.Cam4_Disp.Height;
            HOperatorSet.GenEmptyObj(out Obj[3]);
            CameraDDisp.InitHalcon(width, height);

            width  = (int)this.CamSetting.Width;
            height = (int)this.CamSetting.Height;
            CameraSettingDisp.InitHalcon(width, height);



            //halcon 参数载入初始化
            try
            {
                HOperatorSet.ReadShapeModel(AppDomain.CurrentDomain.BaseDirectory + "/" + ImageOperate.Model_File_Name, out ImageOperate.Gear_Model);
            }
            catch (HalconException ex)
            {
                Console.WriteLine(ex.ToString());
                MessageBox.Show("模板文件载入失败,无法检测内齿");
            }

            try
            {
                HOperatorSet.ReadShapeModel(AppDomain.CurrentDomain.BaseDirectory + "/" + ImageOperate.Track_Model_Name, out ImageOperate.Track_Model);
            }
            catch (HalconException ex)
            {
                Console.WriteLine(ex.ToString());
                MessageBox.Show("模板文件载入失败,无法检追踪图像");
            }



            hd.InitHalcon(width, height);
            //相机的combox选择
            List <ComboxBind> lstCmbBind = new List <ComboxBind>();
            lstCmbBind.Add(new ComboxBind("三销轴检测相机1", 0));
            lstCmbBind.Add(new ComboxBind("三销轴检测相机2", 1));
            lstCmbBind.Add(new ComboxBind("三销轴检测相机3", 2));
            lstCmbBind.Add(new ComboxBind("漏拉复拉检测相机", 3));

            this.pCamSel.ItemsSource  = lstCmbBind;
            pCamSel.DisplayMemberPath = "CmbText"; //类ComboxBind中的属性
            pCamSel.SelectedValuePath = "CmbSel";  //类ComboxBind中的属性
            pCamSel.SelectedIndex     = 0;



            bd.Source = history;
            bd.Path   = new PropertyPath("HistoryNotify");
            BindingOperations.SetBinding(this.pTextBoxHistory, TextBox.TextProperty, bd);
            string c_ = DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss ") + "启动程序...\r\n";
            history.HistoryNotify += c_;
//            HardwareInit();

            //new Thread(new ThreadStart(HardwareInit)).Start();
            HardwareInit();
            try
            {
                bool          ToWrite = false;
                List <string> Baund   = new List <string>();
                Baund.Add("9600");
                pBaundSel.ItemsSource = Baund;
                if (!Baund.Contains(INI.BaudRate))
                {
                    pBaundSel.SelectedItem = "9600";
                    ToWrite = true;
                }
                else
                {
                    pBaundSel.SelectedItem = INI.BaudRate;
                }

                List <string> DataBits = new List <string>();
                DataBits.Add("8");
                pDataBitsSel.ItemsSource = DataBits;
                if (!DataBits.Contains(INI.DataBits))
                {
                    pDataBitsSel.SelectedItem = "8";
                    ToWrite = true;
                }
                else
                {
                    pDataBitsSel.SelectedItem = INI.DataBits;
                }

                List <string> StopBits = new List <string>();
                StopBits.Add("1");
                StopBits.Add("1.5");
                StopBits.Add("2");
                pStopBitsSel.ItemsSource = StopBits;
                if (!StopBits.Contains(INI.StopBits))
                {
                    pStopBitsSel.SelectedItem = "1";
                    ToWrite = true;
                }
                else
                {
                    pStopBitsSel.SelectedItem = INI.StopBits;
                }

                List <string> _Parity = new List <string>();
                _Parity.Add("None");
                _Parity.Add("Odd");
                _Parity.Add("Even");
                pParitySel.ItemsSource = _Parity;
                if (!_Parity.Contains(INI.Parity))
                {
                    pParitySel.SelectedItem = "None";
                    ToWrite = true;
                }
                else
                {
                    pParitySel.SelectedItem = INI.Parity;
                }

                if (ToWrite)
                {
                    throw new Exception("未知的串口参数");
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("串口参数选择错误,请在串口设置里重新选择!" + e.ToString());
            }

            //串口的combox选择
            string[] sAllPort = null;
            try
            {
                sAllPort = SerialPort.GetPortNames();
            }
            catch (Exception ex)
            {
                throw new Exception("获取计算机COM口列表失败!\r\n错误信息:" + ex.Message);
            }

            foreach (var name in sAllPort)
            {
                lstCOM.Add(name);
            }
            this.pComSel.ItemsSource = lstCOM;
            if (lstCOM.Contains(INI.com_sel))
            {
                pComSel.SelectedItem = INI.com_sel;
            }


            try
            {
                OpenSerialPort();
            }
            catch (Exception e)
            {
                try
                {
                    pComSel.SelectedItem = sAllPort.ElementAt(0);
                    INI.com_sel          = sAllPort.ElementAt(0);
                    INI.BaudRate         = "9600";
                    INI.DataBits         = "8";
                    INI.Parity           = "None";
                    INI.StopBits         = "1";
                    OpenSerialPort();
                    INI.writting();
                }
                catch (Exception exception_info)
                {
                    history.HistoryNotify += DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss ") + "没有找到任何串口,会导致无法通信PLC\r\n";
                }
            }



            global.GetIns().Window[0] = this.Cam1_Disp.HalconID;
            global.GetIns().Window[1] = this.Cam2_Disp.HalconID;
            global.GetIns().Window[2] = this.Cam3_Disp.HalconID;
            global.GetIns().Window[3] = this.Cam4_Disp.HalconID;

            new Thread(new ThreadStart(CameraADeal)).Start();
            new Thread(new ThreadStart(CameraBDeal)).Start();
            new Thread(new ThreadStart(CameraCDeal)).Start();
            new Thread(new ThreadStart(CameraDDeal)).Start();
            new Thread(new ThreadStart(CameraControl)).Start();

            UpdateUI();
#if DEBUG
            this.TextBt.Visibility = Visibility.Visible;
#else
            this.TextBt.Visibility = Visibility.Hidden;
#endif
        }
Beispiel #5
0
 private void Window_Loaded(object sender, RoutedEventArgs e)
 {
     HDevExp.InitHalcon();
 }
Beispiel #6
0
 private void Form1_Load(object sender, System.EventArgs e)
 {
     HDevExp.InitHalcon();
 }
Beispiel #7
0
        /* 检测按钮事件 */
        private void btnDetect_Click(object sender, EventArgs e)
        {
            pbImage.Visible     = false;
            hWindowCtrl.Visible = true;
            if (imgOri != null)
            {
                Int32  nDefects = 0;
                Int32  nMode    = 1;
                String strFullFileName;

                if (strSavePath == "")
                {
                    strSavePath = Application.StartupPath;
                }

                strFullFileName = strSavePath + strFileName;

                // save temp image

                imgOri.Save(strFullFileName);

                switch (nMode)
                {
                case 0:
                    // method 0: dynamic thresholding
                    detectInstanceDyn = new HDevelopExport();
                    detectInstanceDyn.InitHalcon();
                    if (File.Exists(strFullFileName))
                    {
                        detectInstanceDyn.RunHalcon(hWindowCtrl.HalconWindow, strFullFileName, myConfig.面积, myConfig.滤波尺寸, myConfig.动态二值化阈值, out nDefects);
                    }
                    break;

                case 1:
                    // method 1: normal thresholding
                    detectInstanceNorm = new HDevelopExport1();
                    detectInstanceNorm.InitHalcon();
                    if (File.Exists(strFullFileName))
                    {
                        detectInstanceNorm.RunHalcon(hWindowCtrl.HalconWindow, strFullFileName, myConfig.面积, myConfig.灰度值, out nDefects);
                    }
                    break;
                }

                if (nDefects > 0)
                {
                    lblResult.Text      = "NG";
                    lblResult.BackColor = Color.Red;
                    if (strSavePath != null && strSavePath.Length != 0)
                    {
                        strSavePathNG = strSavePath + "\\NG";
                        CheckAndCreatePath(strSavePathNG);
                        if (pbImage.Image != null)
                        {
                            pbImage.Image.Save(strSavePathNG + "\\" + strFileName);
                        }
                    }
                }
                else
                {
                    lblResult.Text      = "OK";
                    lblResult.BackColor = Color.Green;
                    if (strSavePath != null && strSavePath.Length != 0)
                    {
                        strSavePathOK = strSavePath + "\\OK";
                        CheckAndCreatePath(strSavePathOK);
                        if (pbImage.Image != null)
                        {
                            pbImage.Image.Save(strSavePathOK + "\\" + strFileName);
                        }
                    }
                }
            }

            pbImage.Image = imgOri;
        }