// private static MainWindow instance = void CameraControl() { //halcon支持多线程更新?所以这个地方将就不卡界面??? bool self_lock = false; while (true) { mEvt_CamCtrl.WaitOne(); while (self_lock) { Thread.Sleep(5); } self_lock = true; #if DEBUG global.GetIns().FromPLC = "T1,T2,T3,T4,\r\n"; #endif string[] sArray = global.GetIns().FromPLC.Split(',');// CamSel.Split(','); global.GetIns().FromPLC = ""; bool isFound = false; if (sArray.Contains("T1")) { isFound = true; int idx = 0; global.GetIns().GotImage[idx] = -1; mEvt_Cam[idx].Set(); } if (sArray.Contains("T2")) { isFound = true; int idx = 1; global.GetIns().GotImage[idx] = -1; mEvt_Cam[idx].Set(); } if (sArray.Contains("T3")) { isFound = true; int idx = 2; global.GetIns().GotImage[idx] = -1; mEvt_Cam[idx].Set(); } if (sArray.Contains("T4")) { isFound = true; int idx = 3; global.GetIns().GotImage[idx] = -1; mEvt_Cam[idx].Set(); } string toPLCdata = ""; if (sArray.Contains("T1")) { int idx = 0; int tick = Environment.TickCount; while (global.GetIns().GotImage[idx] == -1) { int CTick = Environment.TickCount; if ((CTick - tick) > 1000) { global.GetIns().GotImage[idx] = 0; } } if (0 == global.GetIns().GotImage[idx]) { history.HistoryNotify += DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss ") + "相机一抓图失败...\r\n"; toPLCdata += "T1," + "22,"; } else { try { global.GetIns().res[idx] = "T1," + ImageOperate.check_axis(Obj[idx], idx, ImageOperate.Track_Model, this.Cam1_Disp.HalconID) + ","; toPLCdata += global.GetIns().res[idx]; } catch (Exception ex) { history.HistoryNotify += DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss ") + "相机一参数错误...\r\n"; toPLCdata += "T1," + "22,"; } } } else { toPLCdata += "T1," + "99,"; } if (sArray.Contains("T2")) { int idx = 1; int tick = Environment.TickCount; while (global.GetIns().GotImage[idx] == -1) { int CTick = Environment.TickCount; if ((CTick - tick) > 1000) { global.GetIns().GotImage[idx] = 0; } Thread.Sleep(5); } if (0 == global.GetIns().GotImage[idx]) { history.HistoryNotify += DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss ") + "相机二抓图失败...\r\n"; toPLCdata += "T2," + "22,"; } else { try { global.GetIns().res[idx] = "T2," + ImageOperate.check_axis(Obj[idx], idx, ImageOperate.Track_Model, this.Cam2_Disp.HalconID) + ","; toPLCdata += global.GetIns().res[idx]; } catch (Exception ex) { history.HistoryNotify += DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss ") + "相机二参数错误...\r\n"; toPLCdata += "T2," + "22,"; } // toPLCdata += "T2," + CameraBDisp.check_axis(Obj[1], this.Cam2_Disp.HalconID) + ","; } } else { toPLCdata += "T2," + "99,"; } if (sArray.Contains("T3")) { int idx = 2; int tick = Environment.TickCount; while (global.GetIns().GotImage[idx] == -1) { int CTick = Environment.TickCount; if ((CTick - tick) > 1000) { global.GetIns().GotImage[idx] = 0; } Thread.Sleep(5); } if (0 == global.GetIns().GotImage[idx]) { history.HistoryNotify += DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss ") + "相机三抓图失败...\r\n"; toPLCdata += "T3," + "22,"; } else { try { global.GetIns().res[idx] = "T3," + ImageOperate.check_axis(Obj[idx], idx, ImageOperate.Track_Model, this.Cam3_Disp.HalconID) + ","; toPLCdata += global.GetIns().res[idx]; } catch (Exception ex) { history.HistoryNotify += DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss ") + "相机三抓数错误...\r\n"; toPLCdata += "T3," + "22,"; } } } else { //占位符号 toPLCdata += "T3," + "99,"; } if (sArray.Contains("T4")) { int idx = 3; int tick = Environment.TickCount; while (global.GetIns().GotImage[idx] == -1) { int CTick = Environment.TickCount; if ((CTick - tick) > 1000) { global.GetIns().GotImage[idx] = 0; } Thread.Sleep(5); } if (0 == global.GetIns().GotImage[idx]) { history.HistoryNotify += DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss ") + "相机四抓图失败或者参数错误...\r\n"; toPLCdata += "T4," + "22,"; } else { toPLCdata += global.GetIns().res[idx]; // history.HistoryNotify += DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss ") + "相机四参数错误...\r\n"; // toPLCdata += "T4," + "22,"; /* * try * { * * toPLCdata += global.GetIns().res[idx]; * * } * catch (Exception) * { * * } */ } } else { toPLCdata += "T4," + "99,"; } toPLCdata += "\r\n"; if (isFound) { Sender_data_to_plc(toPLCdata); } self_lock = false; } /* * * this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, * (ThreadStart)delegate () * { * ParserStringCheck("T4"); * } * ); */ //instance. // Dispatcher.Invoke(new HandleInterfaceUpdateDelagate(ParserStringCheck), text); // Dispatcher.Invoke(new HandleInterfaceUpdateDelagate(Dispatcher.Invoke(new HandleInterfaceUpdateDelagate(ParserStringCheck), text);), " "); }