/// <summary> /// 相机取像回调函数. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnImageGrabbed(Object sender, ImageGrabbedEventArgs e) { try { // Acquire the image from the camera. Only show the latest image. The camera may acquire images faster than the images can be displayed. // Get the grab result. if ( getcpu ) { if (sortnum == 0) { GetCpu.SetThreadAffinityMask(GetCpu.GetCurrentThread(), new UIntPtr(0x1)); } else { GetCpu.SetThreadAffinityMask(GetCpu.GetCurrentThread(), new UIntPtr(0x2)); } Thread.CurrentThread.Priority = ThreadPriority.Highest; getcpu = false; } IGrabResult grabResult =e.GrabResult; // Check if the image can be displayed. if (grabResult.GrabSucceeded) { //stopWatch.Restart(); // 判断是否是黑白图片格式 if (grabResult.PixelTypeValue == PixelType.Mono8) { //allocate the m_stream_size amount of bytes in non-managed environment if (latestFrameAddress == IntPtr.Zero) { latestFrameAddress = Marshal.AllocHGlobal((Int32)grabResult.PayloadSize); } converter.OutputPixelFormat = PixelType.Mono8; converter.Convert(latestFrameAddress, grabResult.PayloadSize, grabResult); // 转换为Halcon图像显示 HOperatorSet.GenImage1(out hPylonImage, "byte", (HTuple)grabResult.Width, (HTuple)grabResult.Height, (HTuple)latestFrameAddress); } else if (grabResult.PixelTypeValue == PixelType.BayerBG8 || grabResult.PixelTypeValue == PixelType.BayerGB8 || grabResult.PixelTypeValue == PixelType.BayerRG8 || grabResult.PixelTypeValue == PixelType.BayerGR8) { int imageWidth = grabResult.Width - 1; int imageHeight = grabResult.Height - 1; int payloadSize = imageWidth * imageHeight; //allocate the m_stream_size amount of bytes in non-managed environment if (latestFrameAddress == IntPtr.Zero) { latestFrameAddress = Marshal.AllocHGlobal((Int32)(3 * payloadSize)); } converter.OutputPixelFormat = PixelType.BGR8packed; // 根据bayer格式不同切换以下代码 converter.Parameters[PLPixelDataConverter.InconvertibleEdgeHandling].SetValue("Clip"); converter.Convert(latestFrameAddress, 3 * payloadSize, grabResult); HOperatorSet.GenImageInterleaved(out hPylonImage, latestFrameAddress, "bgr", (HTuple)imageWidth, (HTuple)imageHeight, -1, "byte", (HTuple)imageWidth, (HTuple)imageHeight, 0, 0, -1, 0); } // 抛出图像处理事件 if (hPylonImage == null) { MessageBox.Show("hPylonImage null"); } //Console.WriteLine("CAMmm" + sortnum + "when--1--" + DateTime.Now.ToString("mm-ss-fff")); poscmin = Turntable.Instance.copos[sortnum].pos.FirstOrDefault(); // poscmin = Turntable.Instance.copos[sortnum].pos.First(); //Console.WriteLine("w" + sortnum + "ss" + poscmin); Turntable.Instance.copos[sortnum].pos.Clear(); imagenum++; //if (kkk > 1 && sortnum == 0) //{ // HOperatorSet.WriteImage(hPylonImage, "bmp", 0, @"C:\Users\mxw\Desktop\a2.bmp"); // Console.WriteLine(sortnum+1); ////} HObject mimage = new HObject(); HOperatorSet.GenEmptyObj(out mimage); mimage.Dispose(); // TImage.Dispose(); HOperatorSet.CopyImage(hPylonImage, out mimage); // listimage.Add(mimage); listimage1.Enqueue(mimage); hPylonImage.Dispose(); } else { MessageBox.Show("Grab faild!\n" + grabResult.ErrorDescription, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception exception) { Console.WriteLine(" ShowException(exception);"); // ShowException(exception); // Turntable.Instance.copos[sortnum + 1].onwrite = false; poscmin = Turntable.Instance.copos[sortnum].pos.FirstOrDefault(); Turntable.Instance.copos[sortnum].pos.Clear(); } finally { // Dispose the grab result if needed for returning it to the grab loop. e.DisposeGrabResultIfClone(); // Console.WriteLine("CAM" + sortnum + "when--2--" + DateTime.Now.ToString("mm-ss-fff")); } }
public MainForm() { //将当前线程绑定到指定的cpu核心上 GetCpu.SetThreadAffinityMask(GetCpu.GetCurrentThread(), new UIntPtr(0x0008)); InitializeComponent(); //Control.CheckForIllegalCrossThreadCalls = true; pn = new ProductNum(); isfirstrun = true; //HOperatorSet.GenEmptyObj(out Image1); //HOperatorSet.GenEmptyObj(out Image2); //HOperatorSet.GenEmptyObj(out Image3); //HOperatorSet.GenEmptyObj(out Image4); //Image1.Dispose(); //Image2.Dispose(); //Image3.Dispose(); //Image4.Dispose(); this.gridView1.RowCellStyle += new DevExpress.XtraGrid.Views.Grid.RowCellStyleEventHandler(gridView1_RowCellStyle); HOperatorSet.GenEmptyObj(out ImageTemp); ImageTemp.Dispose(); ccameras = new List <CCamera>(); CCD = new Dictionary <string, CCamera>(); ds = new DataSet(); dt = new DataTable("datacopy"); CurrentCCD = "CCD1"; MSStartTimer.Tick += new EventHandler(MSStartTimer_OnTime); MSStoptTimer.Tick += new EventHandler(MSStoptTimer_OnTime); InitialDataGrid(gridControlResult, ds); ReadCameraSetting(); FormLoadProduct f = new FormLoadProduct(); //Thread th = new Thread(new ThreadStart(DoSplash)); //th.Start(); //Thread.Sleep(3000); //th.Abort(); //Thread.Sleep(1000); //if (splashResult == DialogResult.Cancel) //{ // this.Close(); //} DialogResult dialogr = f.ShowDialog(); if (dialogr == DialogResult.OK) { try { FileHelper.LoadCameraResultToShowData(ccameras, PathHelper.currentProductPath); FileHelper.LoadProductData(gridView1, gridControlResult, ds, PathHelper.currentProductPath); FileHelper.LoadCameraToolsData(ccameras, PathHelper.currentProductPath); labelpname.Text = PathHelper.currentProduct; } catch { MessageBox.Show("相机或产品信息加载错误"); } } else if (dialogr == DialogResult.Cancel) { this.Close(); } string path = @"c:\zyw.txt"; if (!File.Exists(path)) { File.AppendAllText(path, "zhongwen", Encoding.Default); } zyw = File.ReadAllText(path, Encoding.Default); if (zyw == "zhongwen") { label1.Text = "产品名称:"; simpleButton1.Text = "生成报表"; BtnSaveParam.Text = "保存参数"; BtnSaveNewProduct.Text = "保存新产品"; BtnControlSet.Text = "控制设定"; BtnLogin.Text = "登录"; BtnLoadProduct.Text = "计数清零"; BtnSetting.Text = "设定"; BtnStop.Text = "停止"; BtnStart.Text = "启动"; } if (zyw == "yinwen") { label1.Text = "productname:"; simpleButton1.Text = "GenerateReport"; BtnSaveParam.Text = "SaveParameter"; BtnSaveNewProduct.Text = "SaveNewProduct"; BtnControlSet.Text = "ControlSet"; BtnLogin.Text = "Login"; BtnLoadProduct.Text = "ResetAllCounters"; BtnSetting.Text = "Setting"; BtnStop.Text = "Stop"; BtnStart.Text = "Start"; } Turntable.Instance.ReadPos(PathHelper.dataPath + @"\Position.xml"); // Turntable.Instance.OnUpdateProductInfoEvent += new Turntable.UpdateProductInfo(UpdateProInfo); Turntable.Instance.StrongPressEventHandler += new Turntable.StrongPress(Instance_StrongPressEventHandler); Turntable.Instance.wuliao += new Turntable.wuliaohandler(wuliaotingzhiEventHandler); }
void Run() { //将当前线程绑定到指定的cpu核心上 GetCpu.SetThreadAffinityMask(GetCpu.GetCurrentThread(), new UIntPtr(0x4)); kzwbmlt12 = DateTime.Now; string path = PathHelper.currentProductPath + @"\" + "spd" + ".txt"; DateTime t1; int xjfwi; spd = Card.maxspeed; int preSignal = 0, signal = 0; PCI408.PCI408_counter_config(0, 3); PCI408.PCI408_set_encoder(0, 0); int fwjs1 = 0, fwjs2 = 0; sdkz3 = 100000; long cuowu = PCI408.PCI408_compare_set_filter_Extern(Card.cardNo, 100000); PCI408.PCI408_config_latch_mode(Card.cardNo, Card.zhouhao); sdkzt1 = DateTime.Now; sdkzt2 = DateTime.Now; sdkzt4 = DateTime.Now; sdkzt3 = DateTime.Now; long jcqbz; for (int xjh = 0; xjh < PosArray.Count(); xjh++) { PCI408.PCI408_compare_clear_points_Extern(0, (ushort)(xjh + 1)); PCI408.PCI408_compare_config_Extern(Card.cardNo, (ushort)(xjh + 1), 1, Card.zhouhao, IOs[xjh]); PCI408.PCI408_compare_set_pulsetimes_Extern(0, (ushort)(xjh + 1), 20); } PCI408.PCI408_reset_latch_flag(Card.cardNo); long s5; currentPos = (int)PCI408.PCI408_get_encoder(Card.cardNo); int[] sigci = new int[5], psigci = new int[5]; for (int sigfi = 0; sigfi < 5; sigfi++) { sigci[sigfi] = 0; psigci[sigfi] = 0; } //? int zdwy = 40000; while (isStart) { try { // for (xjfwi = 1; xjfwi < cameraCount + 1; xjfwi++) //{ // xjfws[xjfwi].t2 = DateTime.Now; // //if (xjfws[xjfwi].tsc() > 200000 && copos[xjfwi].pos.Count() > 0) // //if (xjfws[xjfwi].tsc() > 200000) // // { // // //copos[0].pos.Clear(); // // //copos[1].pos.Clear(); // // //copos[2].pos.Clear(); // // //copos[3].pos.Clear(); // // //copos[4].pos.Clear(); // // //copos[5].pos.Clear(); // // //copos[6].pos.Clear(); // // nutqueue.Clear(); // // xjfws.Initialize(); // // copos.Initialize(); // // timers.Initialize(); // // xjfws = new xjfw[7]; // // //for (xjfwi = 0; xjfwi < 5; xjfwi++) // // //{ // // // xjfws[xjfwi] = new xjfw(); // // // xjfws[xjfwi].ID = xjfwi; // // // xjfws[xjfwi].t1 = DateTime.Now; // // // xjfws[xjfwi].t2 = DateTime.Now; // // //} // // copos = new coppos[7]; // // //for (int k = 0; k < 5; k++) // // //{ // // // copos[k] = new coppos(); // // // copos[k].ID = k; // // // //copos[k].onwrite = false; // // //} // // timers = new MyTimer[7]; // // //for (int i = 0; i < timers.Length; i++) // // //{ // // // timers[i] = new MyTimer(); // // // timers[i].ID = i; // // // //timers[i].OnTimerEvent += new MyTimer.OnTimerEventHandler(OnTimer); // // //} // // //Thread.Sleep(1000); // // } //} // t1 = DateTime.Now; currentPos = (int)PCI408.PCI408_get_encoder(Card.zhouhao); for (int sr = 0; sr < cameraCount; sr++) { sigci[sr] = PCI408.PCI408_read_inbit(Card.cardNo, (ushort)xjzsr[sr]); if (sigci[sr] == 0 && psigci[sr] == 1) { int length1 = dpdl[sr].Count(), jr = 200, jr1 = 5000; for (int listi = 0; listi < length1; listi++) { int ssg = Math.Abs(dpdl[sr][listi] + PosArray[sr] - currentPos); if (ssg < jr1) { jr1 = ssg; jr = listi; } } if (jr != 200 && jr1 < 5000) { copos[sr].pos.Add(dpdl[sr][jr]); // Console.WriteLine("trun " + DateTime.Now.ToString("mm-ss-fff")); dpdl[sr].RemoveAt(jr); } } psigci[sr] = sigci[sr]; } for (int sr1 = 0; sr1 < cameraCount; sr1++) { int length2 = dpdl[sr1].Count(); for (int listi1 = 0; listi1 < length2; listi1++) { if (Math.Abs(currentPos - dpdl[sr1][listi1]) > zdwy) { dpdl[sr1].RemoveAt(listi1); } } } jcqbz = PCI408.PCI408_get_latch_flag(Card.cardNo); //读取锁存器状态 if ((jcqbz & 0xf00) > 0) { PCI408.PCI408_reset_latch_flag(Card.cardNo); signal = PCI408.PCI408_get_latch_value(Card.zhouhao); } if (fwjs1 - fwjs2 > 4000) { fwjs1 = fwjs2; for (int sr = 0; sr < cameraCount; sr++) { sigci[sr] = 0; psigci[sr] = 0; dpdl[sr].Clear(); copos[sr].pos.Clear(); } for (int xjh = 0; xjh < PosArray.Count(); xjh++) { PCI408.PCI408_set_encoder(0, 0); PCI408.PCI408_compare_clear_points_Extern(0, (ushort)(xjh + 1)); PCI408.PCI408_compare_config_Extern(Card.cardNo, (ushort)(xjh + 1), 1, Card.zhouhao, IOs[xjh]); PCI408.PCI408_compare_set_pulsetimes_Extern(0, (ushort)(xjh + 1), 5); } for (int xjs = 0; xjs < cameraCount; xjs++) { jcqdls[xjs].clear(); //File.AppendAllText(path,signal.ToString()+" ", Encoding.Default); } PCI408.PCI408_reset_latch_flag(Card.cardNo); preSignal = signal; if (isStart) { nutqueue.Clear(); } } if (signal != preSignal) { kzwbmlt12 = DateTime.Now; s5 = PCI408.PCI408_compare_get_points_remained_Extern(0, 1); if (s5 > cameraCount + 3) { Nut nut = new Nut(); fwjs1 += 1; nut.initialPos = signal; nutqueue.Add(nut); nut.cas = cameraCount; pn.totalCount++; speedTemp++; ss = pn.totalCount; for (int xjs = 0; xjs < cameraCount; xjs++) { PCI408.PCI408_compare_config_Extern(Card.cardNo, (ushort)(xjs + 1), 1, Card.zhouhao, 1); PCI408.PCI408_compare_add_point_Extern(Card.cardNo, (ushort)(xjs + 1), PosArray[xjs] + (int)signal, 1, 9, IOs[xjs]); // copos[nut.posNo + 1].pos.Add (nut.initialPos); // copos[nut.posNo + 1].onwrite = true; dpdl[xjs].Add(signal); // Console.WriteLine("ss" + signal); //File.AppendAllText(path,signal.ToString()+" ", Encoding.Default); } } } niuli(); for (int i = nutqueue.Count - 1; i > -1; i--) { Nut nutc = new Nut(); nutc = nutqueue[i]; //if (currentPos < nutc.initialPos) //{ // nutqueue.Remove(nutc); // break; //} if (currentPos - nutc.initialPos > PosArray[cameraCount] - 1000) { int a = nutc.getTheHole(); switch (a.ToString()) { case "1": //pn.mnum++; //Console.WriteLine("cipin" + pn.mnum); PCI408.PCI408_compare_config_Extern(Card.cardNo, (ushort)(cameraCount + 1), 1, Card.zhouhao, 1); PCI408.PCI408_compare_set_pulsetimes_Extern(0, (ushort)(cameraCount + 1), 10); PCI408.PCI408_compare_add_point_Extern(Card.cardNo, (ushort)(cameraCount + 1), PosArray[cameraCount] + (int)nutc.initialPos, 1, 9, IOs[cameraCount]); break; //case "3": //pn.nnum++; //Console.WriteLine("未识别" + pn.nnum); //PCI408.PCI408_compare_config_Extern(Card.cardNo, (ushort)(cameraCount + 1), 1, Card.zhouhao, 1); //PCI408.PCI408_compare_set_pulsetimes_Extern(0, (ushort)(cameraCount + 1), 20); //PCI408.PCI408_compare_add_point_Extern(Card.cardNo, (ushort)(cameraCount + 1), PosArray[cameraCount] + (int)nutc.initialPos, 1, 9, IOs[cameraCount]); //break; case "2": pn.goodNum++; fwjs2 = fwjs1; PCI408.PCI408_compare_config_Extern(Card.cardNo, (ushort)(cameraCount + 2), 1, Card.zhouhao, 1); PCI408.PCI408_compare_set_pulsetimes_Extern(0, (ushort)(cameraCount + 2), 10); PCI408.PCI408_compare_add_point_Extern(Card.cardNo, (ushort)(cameraCount + 2), PosArray[cameraCount + 1] + (int)nutc.initialPos, 1, 9, IOs[cameraCount + 1]); break; } jcjg2 = nutc.jiance; Thread ccjcjg = new Thread(cunchujiancejieguo); ccjcjg.Start(); nutqueue.Remove(nutc); pn.badNum = pn.totalCount - pn.goodNum; } } preSignal = signal; kongzhiwaibu(); sdkzt1 = DateTime.Now; sdkzt4 = DateTime.Now; if (Card.sjc(sdkzt3, sdkzt4) < sdkz3) { sdkz3 = Card.sjc(sdkzt3, sdkzt4); } sdkzt3 = DateTime.Now; //if (Card.sjc(sdkzt2, sdkzt1) > 60000) //{ // sdkzt2 = DateTime.Now; // if (sdkz3 < 10) // { // spd = spd + 100; // PCI408.PCI408_change_speed(Card.cardNo, spd); // File.AppendAllText(path, spd.ToString() + " ", Encoding.Default); // } //} } catch (Exception e) { MyDebug.ShowMessage(e, "运动控制出错"); } finally { //MyDebug.ShowMessage(nutqueue.Count.ToString()); } } }