//异步抓取图片 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 }
public void UpdateUI(int sel = 0) { if (sel > 2) { this.Bt_Adjust.Visibility = Visibility.Hidden; this.Bt_Height.Visibility = Visibility.Hidden; this.Bt_Slot.Visibility = Visibility.Hidden; this.Bt_D.Visibility = Visibility.Hidden; this.D1_Min.Visibility = Visibility.Hidden; this.D1_Max.Visibility = Visibility.Hidden; this.D2_Min.Visibility = Visibility.Hidden; this.D2_Max.Visibility = Visibility.Hidden; this.D3_Min.Visibility = Visibility.Hidden; this.D3_Max.Visibility = Visibility.Hidden; this.L_D1_Min.Visibility = Visibility.Hidden; this.L_D1_Max.Visibility = Visibility.Hidden; this.L_D2_Min.Visibility = Visibility.Hidden; this.L_D2_Max.Visibility = Visibility.Hidden; this.L_D3_Min.Visibility = Visibility.Hidden; this.L_D3_Max.Visibility = Visibility.Hidden; this.L_D3_BaseH_.Visibility = Visibility.Hidden; this.D3_BaseH_.Visibility = Visibility.Hidden; this.Bias.Visibility = Visibility.Hidden; this.L_Bias.Visibility = Visibility.Hidden; this.L_Bias_.Visibility = Visibility.Hidden; this.L_Bias__Copy1.Visibility = Visibility.Hidden; this.L_Bias__Copy2.Visibility = Visibility.Hidden; this.L_Bias__Copy3.Visibility = Visibility.Hidden; this.L_Bias__Copy4.Visibility = Visibility.Hidden; this.L_Bias__Copy5.Visibility = Visibility.Hidden; this.L_Bias__Copy6.Visibility = Visibility.Hidden; this.Bt_Track.Visibility = Visibility.Hidden; this.Ratio.Visibility = Visibility.Hidden; this.L_Ratio.Visibility = Visibility.Hidden; this.L_Ratio_.Visibility = Visibility.Hidden; this.Bt_Gear.Visibility = Visibility.Hidden; this.Gear_Threshold.Visibility = Visibility.Visible; this.L_Gear_Threshold.Visibility = Visibility.Visible; this.Bt_ImgThreshold.Visibility = Visibility.Visible; this.Image_Threshold.Visibility = Visibility.Visible; this.L_Image_Threshold.Visibility = Visibility.Visible; this.Image_Threshold.Text = Convert.ToInt32(INI.gear_roi.imgthreshold.ToString()).ToString(); this.Gear_Threshold.Text = INI.gear_roi.threshold.ToString(); } else { this.Bt_Adjust.Visibility = Visibility.Visible; this.Bt_Height.Visibility = Visibility.Visible; this.Bt_Slot.Visibility = Visibility.Visible; this.Bt_D.Visibility = Visibility.Visible; this.D1_Min.Visibility = Visibility.Visible; this.D1_Max.Visibility = Visibility.Visible; this.D2_Min.Visibility = Visibility.Visible; this.D2_Max.Visibility = Visibility.Visible; this.D3_Min.Visibility = Visibility.Visible; this.D3_Max.Visibility = Visibility.Visible; this.L_D1_Min.Visibility = Visibility.Visible; this.L_D1_Max.Visibility = Visibility.Visible; this.L_D2_Min.Visibility = Visibility.Visible; this.L_D2_Max.Visibility = Visibility.Visible; this.L_D3_Min.Visibility = Visibility.Visible; this.L_D3_Max.Visibility = Visibility.Visible; this.Bias.Visibility = Visibility.Visible; this.L_Bias.Visibility = Visibility.Visible; this.L_Bias_.Visibility = Visibility.Visible; this.L_Bias__Copy1.Visibility = Visibility.Visible; this.L_Bias__Copy2.Visibility = Visibility.Visible; this.L_Bias__Copy3.Visibility = Visibility.Visible; this.L_Bias__Copy4.Visibility = Visibility.Visible; this.L_Bias__Copy5.Visibility = Visibility.Visible; this.L_Bias__Copy6.Visibility = Visibility.Visible; this.L_D3_BaseH_.Visibility = Visibility.Visible; this.D3_BaseH_.Visibility = Visibility.Visible; this.Bt_Track.Visibility = Visibility.Visible; this.Ratio.Visibility = Visibility.Visible; this.L_Ratio.Visibility = Visibility.Visible; this.L_Ratio_.Visibility = Visibility.Visible; this.Bt_Gear.Visibility = Visibility.Hidden; this.Gear_Threshold.Visibility = Visibility.Hidden; this.L_Gear_Threshold.Visibility = Visibility.Hidden; this.Bt_ImgThreshold.Visibility = Visibility.Hidden; this.Image_Threshold.Visibility = Visibility.Hidden; this.L_Image_Threshold.Visibility = Visibility.Hidden; this.D1_Min.Text = INI.axis_roi[sel].d1_min.ToString(); this.D1_Max.Text = INI.axis_roi[sel].d1_max.ToString(); this.D2_Min.Text = INI.axis_roi[sel].d2_min.ToString(); this.D2_Max.Text = INI.axis_roi[sel].d2_max.ToString(); this.D3_Min.Text = INI.axis_roi[sel].d3_min.ToString(); this.D3_Max.Text = INI.axis_roi[sel].d3_max.ToString(); this.D3_BaseH_.Text = INI.axis_roi[sel].d3_base_h.ToString(); this.Ratio.Text = INI.axis_roi[sel].d1_mmppix.ToString(); this.Bias.Text = INI.axis_roi[sel].d1_bias.ToString(); } INI.writting(); }
private void Button_Click_Gear_Get(object sender, RoutedEventArgs e) { #if DEBUG Console.WriteLine("drawing circle"); #endif int idx = 3; if (idx != global.GetIns().CamSel) { return; } if (false == Obj[idx].IsInitialized()) { return; } HObject Ho_Circle = null; HTuple hv_y = 0, hv_x = 0, hv_r = 0, threshold_var = INI.gear_roi.imgthreshold;; HOperatorSet.GenEmptyObj(out Ho_Circle); /* * try * { * HOperatorSet.SetDraw(this.CamSetting.HalconID, "margin"); * HOperatorSet.SetColor(this.CamSetting.HalconID, "yellow"); * HOperatorSet.DispObj(Obj[idx], this.CamSetting.HalconID); * HOperatorSet.DrawCircle(this.CamSetting.HalconID, out hv_y, out hv_x, out hv_r); * HOperatorSet.GenCircle(out Ho_Circle, hv_y, hv_x, hv_r); * } * catch (HalconException ex) * { * Ho_Circle.Dispose(); * MessageBox.Show("画搜索框异常"); * return; * } */ #if DEBUG Console.WriteLine("drawing end"); #endif try { ImageOperate.create_gear_model(Obj[idx], threshold_var, this.CamSetting.HalconID); INI.gear_roi.center_x = hv_x; INI.gear_roi.center_x = hv_y; INI.gear_roi.center_x = hv_r; INI.writting(); } catch (HalconException ex) { Console.WriteLine(ex.ToString()); MessageBox.Show("设置失败,可能图像效果不好"); } Ho_Circle.Dispose(); INI.gear_roi.center_x = hv_x; INI.gear_roi.center_y = hv_y; INI.gear_roi.radius = hv_r; INI.writting(); }
private void Button_Click_Height_Measure(object sender, RoutedEventArgs e) { int Cam_idx = global.GetIns().CamSel; Info_Ctrl Infc = new Info_Ctrl(); if (false == ImageOperate.FindTrackPos(Obj[Cam_idx], this.CamSetting.HalconID, out Infc, ImageOperate.Track_Model, false)) { return; } HObject Ho_Image; HTuple hv_c = null, hv_r = null; HOperatorSet.GenEmptyObj(out Ho_Image); try { double Angle = CameraADisp.Disp_Adjust_Line(Obj[Cam_idx], INI.axis_roi[Cam_idx].adjust_r1, INI.axis_roi[Cam_idx].adjust_c1, INI.axis_roi[Cam_idx].adjust_phi, INI.axis_roi[Cam_idx].adjust_r2, INI.axis_roi[Cam_idx].adjust_c2, this.CamSetting.HalconID, false); //HOperatorSet.RotateImage(Obj[Cam_idx], out Ho_Image, Angle, "constant"); HOperatorSet.DispObj(Obj[Cam_idx], this.CamSetting.HalconID); //恢复信息 double r1 = INI.axis_roi[Cam_idx].axis_d1_r1 + Infc.pos_y; double c1 = INI.axis_roi[Cam_idx].axis_d1_c1 + Infc.pos_x; CameraADisp.Measure_Diameter(Obj[Cam_idx], r1, c1, new HTuple(-Angle).TupleRad(), INI.axis_roi[Cam_idx].axis_d1_r2, INI.axis_roi[Cam_idx].axis_d1_c2, out hv_c, out hv_r, this.CamSetting.HalconID, false); //找到垂直线和找到图像的高度和宽度,来找到在哪儿画横线和纵线 HTuple hv_Width, hv_Height, hv_HalfHeight, hv_HalfWidth; HOperatorSet.GetImageSize(Obj[Cam_idx], out hv_Width, out hv_Height); hv_HalfHeight = hv_Height / 2; hv_HalfWidth = hv_c; CameraADisp.draw_line(hv_HalfHeight, 0, hv_HalfHeight, hv_Width, this.CamSetting.HalconID, "blue"); //操作系统,操作系统本身自带选择功能,qt做映射 //win api double x = 0; double y = 0; CameraADisp.GetPoint(hv_c, hv_r, Angle, out x, out y); Console.WriteLine("hv_c:" + hv_c.ToString() + " hv_r:" + hv_r.ToString() + " end x:" + x.ToString() + " end y;" + y.ToString()); CameraADisp.draw_line(hv_r, hv_c, y, x, this.CamSetting.HalconID, "green"); CameraADisp.GetPoint(hv_c, hv_r, Angle + 180, out x, out y); CameraADisp.draw_line(hv_r, hv_c, y, x, this.CamSetting.HalconID, "green"); //INI.axis_roi[Cam_idx].d3_min = hv_c; INI.axis_roi[Cam_idx].axis_d3_r1 = hv_r; INI.axis_roi[Cam_idx].axis_d3_c1 = hv_c; INI.axis_roi[Cam_idx].axis_d3_relative_phi = Angle; //this.D3_Min.Text = INI.axis_roi[Cam_idx].d3_min.ToString(); INI.writting(); } catch (HalconException ex) { Console.WriteLine(ex.ToString()); } Ho_Image.Dispose(); }
//打开串口 void OpenSerialPort() { Thread.Sleep(500); try { serial_port.Close(); } catch (Exception e) { Console.WriteLine("failed to close serial port " + e.ToString()); } try { serial_port.PortName = INI.com_sel; serial_port.BaudRate = Convert.ToInt32(INI.BaudRate); serial_port.DataBits = Convert.ToInt32(INI.DataBits); serial_port.NewLine = "\r\n"; Console.Write("COM " + INI.com_sel + " BD " + INI.BaudRate + " DataBIt " + INI.DataBits + " Pa " + INI.Parity + " Stopbit " + INI.StopBits); if (INI.Parity == "Odd") { serial_port.Parity = Parity.Odd; } else if (INI.Parity == "Even") { serial_port.Parity = Parity.Even; } else { // INI.Parity = "None"; serial_port.Parity = Parity.None; } if ("2" == INI.StopBits) { serial_port.StopBits = StopBits.One; } else if ("1.5" == INI.StopBits) { serial_port.StopBits = StopBits.OnePointFive; } else { // INI.StopBits = "One"; serial_port.StopBits = StopBits.One; } serial_port.Open(); INI.writting(); // Port_Data.port = serial_port; //Port_Data.str = ""; serial_port.DataReceived += new SerialDataReceivedEventHandler(serialport_DataReceived); } catch (Exception e) { Console.WriteLine("串口打开失败"); throw e; } }