| | |
| | | using LB_SmartVision.ProcessRun; |
| | | using LB_SmartVision.SQL; |
| | | using LB_SmartVision.Tool; |
| | | using LB_SmartVisionCameraDevice.PHM6000; |
| | | using LB_SmartVisionCommon; |
| | | using LB_SmartVisionLoginUI; |
| | | using LB_VisionProcesses; |
| | |
| | | using log4net.Config; |
| | | using Newtonsoft.Json; |
| | | using Newtonsoft.Json.Serialization; |
| | | using OpenCvSharp; |
| | | using Serilog; |
| | | using Serilog.Events; |
| | | using Sunny.UI; |
| | |
| | | using System.Text.RegularExpressions; |
| | | using System.Threading.Tasks; |
| | | using System.Windows.Forms; |
| | | using System.Windows.Media.Media3D; |
| | | |
| | | namespace LB_SmartVision |
| | | { |
| | |
| | | |
| | | // 窗体类的全局变量:标记是否允许切换Tab(默认允许) |
| | | private bool _isTabSwitchAllowed = true; |
| | | |
| | | PausableHeartbeatAsync heartbeat = new PausableHeartbeatAsync( |
| | | sendHeartbeatAsync: async () => |
| | | { |
| | | // 模拟异步心跳发送(如 HttpClient 请求) |
| | | await Task.Delay(100); // 模拟 I/O 延迟 |
| | | Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] 心跳已发送"); |
| | | }, |
| | | interval: TimeSpan.FromSeconds(0.5) |
| | | ); |
| | | |
| | | |
| | | #endregion |
| | | |
| | |
| | | materialTabControl.ControlRemoved += (s, e) => UpdateOverflowComboBox(); |
| | | |
| | | //HOperatorSet.SetSystem("max_mem_cache", 2048); |
| | | startPLCThread(); |
| | | } |
| | | |
| | | string removeName = string.Empty; |
| | |
| | | catch { return false; } |
| | | } |
| | | |
| | | public bool LoadAllPLCSettings(string allPLCSettingStringPath) |
| | | { |
| | | try |
| | | { |
| | | GlobalVar.allPlcSettings = ConfigManager<Dictionary<string, PlcConfig>>.LoadConfig<Dictionary<string, PlcConfig>>(allPLCSettingStringPath); |
| | | } |
| | | catch |
| | | { |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | public bool SaveAllPLCSettings() |
| | | { |
| | | try |
| | | { |
| | | ConfigManager<Dictionary<string, PlcConfig>>.SaveConfig<Dictionary<string, PlcConfig>>(GlobalVar.allPlcSettings, GlobalVar.allPlcSettingsPath); |
| | | } |
| | | catch |
| | | { |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | public static bool SaveAllProcessSetting() |
| | | { |
| | | try |
| | |
| | | LB_SmartVision.Tool.Tool.ReadStringConfig("User ID", out string User_ID); |
| | | LB_SmartVision.Tool.Tool.ReadStringConfig("Password", out string Password); |
| | | GlobalVar.strProductName = DateBaseName; |
| | | //加载LB相机参数 |
| | | if (LoadAllLBCameras(GlobalVar.allCamerasLBPath)) |
| | | { |
| | | LogInfo("LB3D相机配置文件加载成功!", LogInfoType.PASS); |
| | | } |
| | | else |
| | | { |
| | | LogInfo("LB3D相机配置文件加载失败", LogInfoType.ERROR); |
| | | } |
| | | //加载通讯 |
| | | foreach (BaseCommunicator com in GlobalVar.dicCommunicators.Values) |
| | | { |
| | |
| | | else |
| | | { |
| | | LogInfo("通讯加载失败", LogInfoType.ERROR); |
| | | } |
| | | if (LoadAllPLCSettings(GlobalVar.allPlcSettingsPath)) |
| | | { |
| | | LogInfo("PLC通讯加载成功", LogInfoType.PASS); |
| | | } |
| | | else |
| | | { |
| | | LogInfo("PLC通讯加载失败", LogInfoType.ERROR); |
| | | } |
| | | //加载相机 |
| | | foreach (BaseCamera camera in GlobalVar.dicCameras.Values) |
| | |
| | | if (GlobalVar.dicCommunicators.Keys.Contains("通讯0")) |
| | | { |
| | | SiemensLBS7 siemensLBS7 = GlobalVar.dicCommunicators["通讯0"] as SiemensLBS7; |
| | | siemensLBS7.Write(GlobalVar.dicMotionControlData[GlobalVar.strProductName][item].XAxisAddress, GlobalVar.dicMotionControlData[GlobalVar.strProductName][item].XAxisAddress); |
| | | //siemensLBS7.Write(GlobalVar.dicMotionControlData[GlobalVar.strProductName][item].XAxisAddress, GlobalVar.dicMotionControlData[GlobalVar.strProductName][item].XAxisAddress); |
| | | } |
| | | } |
| | | } |
| | |
| | | SaveMotionControlDatas(); |
| | | SaveSerialPorts(); |
| | | SaveAllBarcodeReaders(); |
| | | SaveAllPLCSettings(); |
| | | } |
| | | |
| | | |
| | |
| | | { |
| | | try |
| | | { |
| | | GlobalVar.dicMotionControlData = ConfigManager<Dictionary<string, Dictionary<string, RecordMotionControlData>>>.LoadConfig<Dictionary<string, Dictionary<string, RecordMotionControlData>>>(alMotionControlDataPath); |
| | | GlobalVar.dicMotionControlData = ConfigManager<Dictionary<string, Dictionary<string, PlcConfig>>>.LoadConfig<Dictionary<string, Dictionary<string, PlcConfig>>>(alMotionControlDataPath); |
| | | } |
| | | catch |
| | | { |
| | |
| | | GlobalVar.dicMotionControlData[item].Remove(itemSN); |
| | | } |
| | | } |
| | | ConfigManager<Dictionary<string, Dictionary<string, RecordMotionControlData>>>.SaveConfig<Dictionary<string, Dictionary<string, RecordMotionControlData>>>(GlobalVar.dicMotionControlData, GlobalVar.allMotionControlDataPath); |
| | | ConfigManager<Dictionary<string, Dictionary<string, PlcConfig>>>.SaveConfig<Dictionary<string, Dictionary<string, PlcConfig>>>(GlobalVar.dicMotionControlData, GlobalVar.allMotionControlDataPath); |
| | | } |
| | | catch |
| | | { |
| | |
| | | camera.isGrabbing = false; |
| | | } |
| | | } |
| | | |
| | | if (camera is LBCamera phmCamera) |
| | | { |
| | | if (GlobalVar.allLBCameras.ContainsKey(camera.SN) && phmCamera._isConnected) |
| | | { |
| | | phmCamera.UpdateSensorConfig(GlobalVar.allLBCameras[camera.SN]); |
| | | } |
| | | } |
| | | GlobalVar.dicCameras.TryAdd(CameraConnectionString.Key, camera); |
| | | } |
| | | |
| | |
| | | Debug.WriteLine("No match found."); |
| | | } |
| | | } |
| | | |
| | | if (GlobalVar.dicCommunicators != null && GlobalVar.dicCommunicators.Count > 0) |
| | | { |
| | | GlobalVar.currentCommunicators = GlobalVar.dicCommunicators.Keys.ToList()[0]; |
| | | } |
| | | return true; |
| | | } |
| | | catch { return false; } |
| | |
| | | return true; |
| | | } |
| | | catch { return false; } |
| | | } |
| | | |
| | | public bool LoadAllLBCameras(string allLBCamerasPath) |
| | | { |
| | | try |
| | | { |
| | | if (!File.Exists(allLBCamerasPath)) |
| | | { |
| | | Debug.WriteLine("文件不存在创建空文件"); |
| | | // 获取不带文件名的目录路径 |
| | | string directoryPath = Path.GetDirectoryName(allLBCamerasPath); |
| | | SaveAllLBCameras(); |
| | | return true; |
| | | } |
| | | GlobalVar.allLBCameras = ConfigManager<ConcurrentDictionary<string, PHM6000SensorConfig>>.LoadConfig<ConcurrentDictionary<string, PHM6000SensorConfig>>(GlobalVar.allCamerasLBPath); |
| | | } |
| | | catch |
| | | { |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | public bool SaveAllLBCameras() |
| | | { |
| | | try |
| | | { |
| | | if (GlobalVar.allLBCameras.Count >= 0) |
| | | { |
| | | ConfigManager<ConcurrentDictionary<string, PHM6000SensorConfig>>.SaveConfig<ConcurrentDictionary<string, PHM6000SensorConfig>>(GlobalVar.allLBCameras, GlobalVar.allCamerasLBPath); |
| | | } |
| | | else |
| | | { |
| | | return false; |
| | | } |
| | | } |
| | | catch |
| | | { |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | public bool LoadAllProcess(string allProcessConnectionStringPath) |
| | |
| | | } |
| | | |
| | | } |
| | | bool is3D = false; |
| | | bool is2D = false; |
| | | |
| | | private void TriggerRunMessageReceived(string name, string msg) |
| | | { |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | var matchedItems = GlobalVar.dicProcessSetting |
| | | .Where(item => |
| | | { |
| | |
| | | |
| | | if (matchedItems.Count <= 0) |
| | | { |
| | | matchedItems = GlobalVar.dicProcessSetting |
| | | .Where(item => |
| | | { |
| | | var value = item.Value; |
| | | var triggerComm = value["触发通讯"]; |
| | | var triggerChar = value["触发字符"]; |
| | | return triggerComm != null && !triggerComm.Equals("无"); |
| | | //return triggerComm != null && triggerComm.Equals(name) && |
| | | // (string.IsNullOrEmpty(triggerChar?.ToString()) || |
| | | // msg.StartsWith(triggerChar.ToString())); |
| | | }) |
| | | .ToList(); // 避免重复字典访问和装箱操作 |
| | | } |
| | | |
| | | |
| | | if (matchedItems.Count <= 0) |
| | | { |
| | | return; |
| | | } |
| | | |
| | |
| | | |
| | | GlobalVar.dicProcesses.Values.AsParallel().ForAll(v => v.bCompleted = false); |
| | | LogInfo(string.Format($"检查到可被触发的流程,清空所有流程运行完成标记位!"), LogInfoType.INFO); |
| | | Task.Factory.StartNew(() => |
| | | |
| | | if (!is3D && name.Contains("L")) |
| | | { |
| | | Parallel.ForEach(matchedItems, item => |
| | | Task.Factory.StartNew(() => |
| | | { |
| | | string ProcessName = item.Value["流程名"]; |
| | | LogInfo($"流程[{ProcessName}]开始运行", LogInfoType.INFO); |
| | | |
| | | if (!GlobalVar.dicProcesses.ContainsKey(ProcessName)) |
| | | if (name.Contains("L")) |
| | | { |
| | | LogInfo(string.Format("流程[{0}]不存在,请检查流程设置", ProcessName), LogInfoType.ERROR); |
| | | return; |
| | | is3D = true; |
| | | } |
| | | |
| | | ProcessRunBll RunBll = GlobalVar.dicProcesses[ProcessName]; |
| | | if (RunBll == null || RunBll.bRuning) |
| | | Parallel.ForEach(matchedItems, item => |
| | | { |
| | | LogInfo(string.Format("流程[{0}]上次未运行完成,触发失败", ProcessName) |
| | | , LogInfoType.ERROR); |
| | | return; |
| | | } |
| | | string ProcessName = item.Value["流程名"]; |
| | | LogInfo($"流程[{ProcessName}]开始运行", LogInfoType.INFO); |
| | | |
| | | try |
| | | { |
| | | bool result = false; |
| | | string msg = string.Empty; |
| | | int times = Convert.ToInt32(item.Value["重测次数"].ToString()); |
| | | string ConnecResult = item.Value["关联结果"]; |
| | | |
| | | if (times < 0) |
| | | if (!GlobalVar.dicProcesses.ContainsKey(ProcessName)) |
| | | { |
| | | result = RunBll.Run(); |
| | | msg = RunBll.Msg; |
| | | |
| | | if (!(string.IsNullOrEmpty(ConnecResult) || ConnecResult.Trim() == "未关联")) |
| | | { |
| | | if (!result) |
| | | { |
| | | LogInfo(string.Format("流程[{0}]运行结果失败,原因是[{1}]", ProcessName, msg), LogInfoType.NOSHOW); |
| | | } |
| | | RunBll.GetBooleanOutput(ConnecResult, out result); |
| | | RunBll.Result = result; |
| | | } |
| | | |
| | | if (!result) |
| | | { |
| | | LogInfo($"流程[{ProcessName}]被强制运行成功", LogInfoType.WARN); |
| | | RunBll.Result = true; |
| | | result = true; |
| | | } |
| | | LogInfo(string.Format("流程[{0}]不存在,请检查流程设置", ProcessName), LogInfoType.ERROR); |
| | | return; |
| | | } |
| | | else |
| | | |
| | | ProcessRunBll RunBll = GlobalVar.dicProcesses[ProcessName]; |
| | | if (RunBll == null || RunBll.bRuning) |
| | | { |
| | | while (times >= 0) |
| | | LogInfo(string.Format("流程[{0}]上次未运行完成,触发失败", ProcessName) |
| | | , LogInfoType.ERROR); |
| | | return; |
| | | } |
| | | |
| | | try |
| | | { |
| | | bool result = false; |
| | | string msg = string.Empty; |
| | | int times = Convert.ToInt32(item.Value["重测次数"].ToString()); |
| | | string ConnecResult = item.Value["关联结果"]; |
| | | |
| | | if (times < 0) |
| | | { |
| | | result = RunBll.Run(); |
| | | msg = RunBll.Msg; |
| | | |
| | | if (!(string.IsNullOrEmpty(ConnecResult) || ConnecResult.Trim() == "未关联")) |
| | | { |
| | | if (!result) |
| | | { |
| | | LogInfo(string.Format("流程[{0}]运行结果失败,原因是[{1}]", ProcessName, msg), LogInfoType.NOSHOW); |
| | | } |
| | | |
| | | RunBll.GetBooleanOutput(ConnecResult, out result); |
| | | RunBll.Result = result; |
| | | } |
| | | |
| | | if (result) |
| | | if (!result) |
| | | { |
| | | break; |
| | | LogInfo($"流程[{ProcessName}]被强制运行成功", LogInfoType.WARN); |
| | | RunBll.Result = true; |
| | | result = true; |
| | | } |
| | | else if (!result && times > 0) |
| | | { |
| | | LogInfo(string.Format("流程[{0}]运行失败重新测试,剩余次数[{1}]", ProcessName, times), LogInfoType.WARN); |
| | | } |
| | | |
| | | times--; |
| | | } |
| | | } |
| | | |
| | | string ConnectProcess = item.Value["关联流程"]; |
| | | if (!(ConnectProcess == null || string.IsNullOrEmpty(ConnectProcess) || ConnectProcess.Trim() == "")) |
| | | { |
| | | //用逗号或者分号去间隔关联流程 |
| | | string[] arrConnectProcess; |
| | | if (ConnectProcess.Split(';').Length >= ConnectProcess.Split(',').Length) |
| | | { |
| | | arrConnectProcess = ConnectProcess.Split(';'); |
| | | } |
| | | else |
| | | { |
| | | arrConnectProcess = ConnectProcess.Split(','); |
| | | } |
| | | |
| | | foreach (string strConnectProcess in arrConnectProcess) |
| | | { |
| | | if (GlobalVar.dicProcesses.ContainsKey(strConnectProcess)) |
| | | while (times >= 0) |
| | | { |
| | | ProcessRunBll ConnectRunBll = GlobalVar.dicProcesses[strConnectProcess]; |
| | | |
| | | int waitTime = 10; |
| | | DateTime startTime = DateTime.Now; |
| | | while ((DateTime.Now - startTime).TotalSeconds < waitTime |
| | | && (ConnectRunBll.bRuning || !ConnectRunBll.bCompleted)) |
| | | result = RunBll.Run(); |
| | | msg = RunBll.Msg; |
| | | if (!(string.IsNullOrEmpty(ConnecResult) || ConnecResult.Trim() == "未关联")) |
| | | { |
| | | LogInfo(string.Format("关联流程[{0}]未运行完成,剩余等待[{1}]s", strConnectProcess, (waitTime - ((DateTime.Now - startTime).TotalSeconds))) |
| | | , LogInfoType.NOSHOW); |
| | | Thread.Sleep(1000); |
| | | continue; |
| | | if (!result) |
| | | { |
| | | LogInfo(string.Format("流程[{0}]运行结果失败,原因是[{1}]", ProcessName, msg), LogInfoType.NOSHOW); |
| | | } |
| | | |
| | | RunBll.GetBooleanOutput(ConnecResult, out result); |
| | | RunBll.Result = result; |
| | | } |
| | | |
| | | if (ConnectRunBll.bRuning || !ConnectRunBll.bCompleted) |
| | | if (result) |
| | | { |
| | | GlobalVar.dicProcesses[ProcessName].Msg = string.Format("流程[{0}]未运行完成", ProcessName); |
| | | LogInfo(string.Format("关联流程[{0}]未运行完成", strConnectProcess), LogInfoType.ERROR); |
| | | result = false; |
| | | break; |
| | | } |
| | | else if (!ConnectRunBll.bRuning && ConnectRunBll.bCompleted) |
| | | else if (!result && times > 0) |
| | | { |
| | | LogInfo(string.Format("关联流程[{0}]运行完成", strConnectProcess), LogInfoType.INFO); |
| | | LogInfo(string.Format("流程[{0}]运行失败重新测试,剩余次数[{1}]", ProcessName, times), LogInfoType.WARN); |
| | | } |
| | | |
| | | result &= ConnectRunBll.Result; |
| | | if (!ConnectRunBll.Result) |
| | | times--; |
| | | } |
| | | } |
| | | |
| | | string ConnectProcess = item.Value["关联流程"]; |
| | | if (!(ConnectProcess == null || string.IsNullOrEmpty(ConnectProcess) || ConnectProcess.Trim() == "")) |
| | | { |
| | | //用逗号或者分号去间隔关联流程 |
| | | string[] arrConnectProcess; |
| | | if (ConnectProcess.Split(';').Length >= ConnectProcess.Split(',').Length) |
| | | { |
| | | arrConnectProcess = ConnectProcess.Split(';'); |
| | | } |
| | | else |
| | | { |
| | | arrConnectProcess = ConnectProcess.Split(','); |
| | | } |
| | | |
| | | foreach (string strConnectProcess in arrConnectProcess) |
| | | { |
| | | if (GlobalVar.dicProcesses.ContainsKey(strConnectProcess)) |
| | | { |
| | | LogInfo($"流程[{ProcessName}]的关联流程[{strConnectProcess}]运行失败", LogInfoType.ERROR); |
| | | msg = $"关联流程[{strConnectProcess}]运行失败"; |
| | | ProcessRunBll ConnectRunBll = GlobalVar.dicProcesses[strConnectProcess]; |
| | | |
| | | int waitTime = 10; |
| | | DateTime startTime = DateTime.Now; |
| | | while ((DateTime.Now - startTime).TotalSeconds < waitTime |
| | | && (ConnectRunBll.bRuning || !ConnectRunBll.bCompleted)) |
| | | { |
| | | LogInfo(string.Format("关联流程[{0}]未运行完成,剩余等待[{1}]s", strConnectProcess, (waitTime - ((DateTime.Now - startTime).TotalSeconds))) |
| | | , LogInfoType.NOSHOW); |
| | | Thread.Sleep(1000); |
| | | continue; |
| | | } |
| | | |
| | | if (ConnectRunBll.bRuning || !ConnectRunBll.bCompleted) |
| | | { |
| | | GlobalVar.dicProcesses[ProcessName].Msg = string.Format("流程[{0}]未运行完成", ProcessName); |
| | | LogInfo(string.Format("关联流程[{0}]未运行完成", strConnectProcess), LogInfoType.ERROR); |
| | | result = false; |
| | | break; |
| | | } |
| | | else if (!ConnectRunBll.bRuning && ConnectRunBll.bCompleted) |
| | | { |
| | | LogInfo(string.Format("关联流程[{0}]运行完成", strConnectProcess), LogInfoType.INFO); |
| | | } |
| | | |
| | | result &= ConnectRunBll.Result; |
| | | if (!ConnectRunBll.Result) |
| | | { |
| | | LogInfo($"流程[{ProcessName}]的关联流程[{strConnectProcess}]运行失败", LogInfoType.ERROR); |
| | | msg = $"关联流程[{strConnectProcess}]运行失败"; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | LogInfo(result ? $"流程[{ProcessName}]运行成功" : $"流程[{ProcessName}]运行失败,原因是{msg}", result ? LogInfoType.PASS : LogInfoType.ERROR); |
| | | LogInfo(result ? $"流程[{ProcessName}]运行成功" : $"流程[{ProcessName}]运行失败,原因是{msg}", result ? LogInfoType.PASS : LogInfoType.ERROR); |
| | | |
| | | string SendComName = result ? item.Value["成功通讯"] : item.Value["失败通讯"]; |
| | | string SendMsg = result ? item.Value["成功字符"] : item.Value["失败字符"]; |
| | | if (GlobalVar.dicCommunicators.ContainsKey(SendComName) && (!string.IsNullOrEmpty(SendMsg) || SendMsg.Trim() != "")) |
| | | { |
| | | GlobalVar.dicCommunicators[SendComName].SendMessage(SendMsg); |
| | | LogInfo(string.Format("发送给[{0}]了消息\"{1}\"", SendComName, SendMsg), LogInfoType.INFO); |
| | | } |
| | | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogInfo(string.Format("流程[{0}]运行发生了意外,原因是:{1}", ProcessName, ex.Message + $"【{ex.StackTrace}】"), LogInfoType.ERROR); |
| | | |
| | | RunBll.Result = false; |
| | | RunBll.Msg = $"[意外]{ex.Message}"; |
| | | } |
| | | finally |
| | | { |
| | | #region 从RunSettingPage和Layout中获取是否保存图片 |
| | | |
| | | string strImageType = "jpeg"; |
| | | bool bSaveRunImage = false; |
| | | bool bSaveResultImage = false; |
| | | long lImageQuality = 100L; |
| | | |
| | | //ckbSaveRunImage |
| | | if (GlobalVar.ControlStates.TryGetValue("ckbSaveRunImage_CheckBox", out object oSaveRunImage)) |
| | | { |
| | | if (oSaveRunImage != null && oSaveRunImage is bool) |
| | | string SendComName = result ? item.Value["成功通讯"] : item.Value["失败通讯"]; |
| | | string SendMsg = result ? item.Value["成功字符"] : item.Value["失败字符"]; |
| | | if (GlobalVar.dicCommunicators.ContainsKey(SendComName) && (!string.IsNullOrEmpty(SendMsg) || SendMsg.Trim() != "")) |
| | | { |
| | | bSaveRunImage = (bool)oSaveRunImage; |
| | | GlobalVar.dicCommunicators[SendComName].SendMessage(SendMsg); |
| | | LogInfo(string.Format("发送给[{0}]了消息\"{1}\"", SendComName, SendMsg), LogInfoType.INFO); |
| | | } |
| | | } |
| | | |
| | | //ckbSaveResultImage |
| | | if (GlobalVar.ControlStates.TryGetValue("ckbSaveResultImage_CheckBox", out object oSaveResultImage)) |
| | | { |
| | | if (oSaveResultImage != null && oSaveResultImage is bool) |
| | | { |
| | | bSaveResultImage = (bool)oSaveResultImage; |
| | | } |
| | | } |
| | | |
| | | //txtImageQuality |
| | | if (GlobalVar.ControlStates.TryGetValue("txtImageQuality_TextBox", out object oImageQuality)) |
| | | catch (Exception ex) |
| | | { |
| | | if (oImageQuality != null && oImageQuality is string) |
| | | { |
| | | lImageQuality = Convert.ToInt64((string)oImageQuality); |
| | | } |
| | | LogInfo(string.Format("流程[{0}]运行发生了意外,原因是:{1}", ProcessName, ex.Message + $"【{ex.StackTrace}】"), LogInfoType.ERROR); |
| | | |
| | | RunBll.Result = false; |
| | | RunBll.Msg = $"[意外]{ex.Message}"; |
| | | } |
| | | |
| | | //cmbImageType |
| | | if (GlobalVar.ControlStates.TryGetValue("cmbImageType_ComboBox", out object oImageType)) |
| | | finally |
| | | { |
| | | try |
| | | #region 从RunSettingPage和Layout中获取是否保存图片 |
| | | |
| | | string strImageType = "jpeg"; |
| | | bool bSaveRunImage = false; |
| | | bool bSaveResultImage = false; |
| | | long lImageQuality = 100L; |
| | | |
| | | //ckbSaveRunImage |
| | | if (GlobalVar.ControlStates.TryGetValue("ckbSaveRunImage_CheckBox", out object oSaveRunImage)) |
| | | { |
| | | // 动态解析ComboBox数据 |
| | | var json = JsonConvert.SerializeObject(oImageType); |
| | | var comboData = JsonConvert.DeserializeAnonymousType(json, new |
| | | if (oSaveRunImage != null && oSaveRunImage is bool) |
| | | { |
| | | Items = new List<object>(), |
| | | SelectedIndex = 0 |
| | | }); |
| | | |
| | | if (comboData != null && comboData.Items.Count > 0) |
| | | { |
| | | strImageType = comboData.Items[comboData.SelectedIndex].ToString(); |
| | | bSaveRunImage = (bool)oSaveRunImage; |
| | | } |
| | | } |
| | | catch { } |
| | | } |
| | | |
| | | // 生成图片并显示到控件中 |
| | | HObject InputImage = null; |
| | | HObject RecordImage = null; |
| | | |
| | | foreach (var layout in GlobalVar.dicLayout.Values |
| | | .Where(layout => layout.ProcessName == ProcessName) |
| | | .ToList()) |
| | | { |
| | | string title = layout.Title; |
| | | string strImagePath = layout.SaveImageDir; |
| | | if (!AllProcessesPage.dicProcessControls.ContainsKey(title)) |
| | | //ckbSaveResultImage |
| | | if (GlobalVar.ControlStates.TryGetValue("ckbSaveResultImage_CheckBox", out object oSaveResultImage)) |
| | | { |
| | | continue; |
| | | } |
| | | |
| | | RunBll.GetImage(layout, out InputImage, out RecordImage); |
| | | AllProcessesPage.dicProcessControls[title].ShowHoImage(RecordImage); |
| | | |
| | | if (!string.IsNullOrEmpty(layout.SaveImageDir)) |
| | | { |
| | | string fileNameHead = layout.SaveImageHead; |
| | | |
| | | string result = Regex.Replace(fileNameHead, @"\{[^}]+\}", match => |
| | | if (oSaveResultImage != null && oSaveResultImage is bool) |
| | | { |
| | | // 去除{}只保留括号内的内容 |
| | | string content = match.Value; |
| | | content = content.Trim('{', '}'); // 去除首尾的{} |
| | | |
| | | RunBll.GetStringOutput(content, out string str); |
| | | return str; |
| | | }); |
| | | |
| | | string fileName = $"{result}-[{DateTime.Now.ToString("HH.mm.ss.ffff")}]"; |
| | | |
| | | // 使用正则表达式替换所有非法字符 |
| | | string invalidChars = Regex.Escape(new string(Path.GetInvalidFileNameChars())); |
| | | string pattern = $"[{invalidChars}]"; |
| | | fileName = Regex.Replace(fileName, pattern, "-"); |
| | | |
| | | strImagePath = Regex.Replace(strImagePath, @"\{[^}]+\}", match => |
| | | { |
| | | // 去除{}只保留括号内的内容 |
| | | string content = match.Value; |
| | | content = content.Trim('{', '}'); // 去除首尾的{} |
| | | |
| | | RunBll.GetStringOutput(content, out string str); |
| | | return str; |
| | | }); |
| | | |
| | | if (bSaveRunImage) |
| | | { |
| | | // 最后一级目录必须为年月日,会根据时间来删除旧图片 |
| | | string directoryPath = Path.Combine(strImagePath, $"{ProcessName}\\原图\\{RunBll.Result}\\{DateTime.Now.ToString("yyyyMMdd")}\\"); |
| | | LB_SmartVision.Tool.Tool.AddRealImage(InputImage, directoryPath, fileName, strImageType, lImageQuality); |
| | | } |
| | | |
| | | if (bSaveResultImage) |
| | | { |
| | | // 最后一级目录必须为年月日,会根据时间来删除旧图片 |
| | | string directoryPath = Path.Combine(strImagePath, $"{ProcessName}\\截图\\{RunBll.Result}\\{DateTime.Now.ToString("yyyyMMdd")}\\"); |
| | | LB_SmartVision.Tool.Tool.AddRealImage(RecordImage, directoryPath, fileName, "jpg", 50L); |
| | | bSaveResultImage = (bool)oSaveResultImage; |
| | | } |
| | | } |
| | | } |
| | | |
| | | foreach (var csv in GlobalVar.dicCsvSetting.Values |
| | | .Where(csv => csv.ProcessName == ProcessName) |
| | | .ToList()) |
| | | { |
| | | if (RunBll.GetCsv(csv |
| | | , out List<string> DataTitle, out Dictionary<string, object> ResultData)) |
| | | //txtImageQuality |
| | | if (GlobalVar.ControlStates.TryGetValue("txtImageQuality_TextBox", out object oImageQuality)) |
| | | { |
| | | string filePath = Path.Combine(GlobalVar.strPathCsv, $"{ProcessName}.csv"); |
| | | LB_SmartVision.Tool.Tool.SaveData(filePath, DataTitle, ResultData); |
| | | if (oImageQuality != null && oImageQuality is string) |
| | | { |
| | | lImageQuality = Convert.ToInt64((string)oImageQuality); |
| | | } |
| | | } |
| | | |
| | | //cmbImageType |
| | | if (GlobalVar.ControlStates.TryGetValue("cmbImageType_ComboBox", out object oImageType)) |
| | | { |
| | | try |
| | | { |
| | | // 动态解析ComboBox数据 |
| | | var json = JsonConvert.SerializeObject(oImageType); |
| | | var comboData = JsonConvert.DeserializeAnonymousType(json, new |
| | | { |
| | | Items = new List<object>(), |
| | | SelectedIndex = 0 |
| | | }); |
| | | |
| | | if (comboData != null && comboData.Items.Count > 0) |
| | | { |
| | | strImageType = comboData.Items[comboData.SelectedIndex].ToString(); |
| | | } |
| | | } |
| | | catch { } |
| | | } |
| | | |
| | | // 生成图片并显示到控件中 |
| | | HObject InputImage = null; |
| | | HObject RecordImage = null; |
| | | |
| | | foreach (var layout in GlobalVar.dicLayout.Values |
| | | .Where(layout => layout.ProcessName == ProcessName) |
| | | .ToList()) |
| | | { |
| | | string title = layout.Title; |
| | | string strImagePath = layout.SaveImageDir; |
| | | if (!AllProcessesPage.dicProcessControls.ContainsKey(title)) |
| | | { |
| | | continue; |
| | | } |
| | | |
| | | RunBll.GetImage(layout, out InputImage, out RecordImage); |
| | | AllProcessesPage.dicProcessControls[title].ShowHoImage(RecordImage); |
| | | |
| | | if (!string.IsNullOrEmpty(layout.SaveImageDir)) |
| | | { |
| | | string fileNameHead = layout.SaveImageHead; |
| | | |
| | | string result = Regex.Replace(fileNameHead, @"\{[^}]+\}", match => |
| | | { |
| | | // 去除{}只保留括号内的内容 |
| | | string content = match.Value; |
| | | content = content.Trim('{', '}'); // 去除首尾的{} |
| | | |
| | | RunBll.GetStringOutput(content, out string str); |
| | | return str; |
| | | }); |
| | | |
| | | string fileName = $"{result}-[{DateTime.Now.ToString("HH.mm.ss.ffff")}]"; |
| | | |
| | | // 使用正则表达式替换所有非法字符 |
| | | string invalidChars = Regex.Escape(new string(Path.GetInvalidFileNameChars())); |
| | | string pattern = $"[{invalidChars}]"; |
| | | fileName = Regex.Replace(fileName, pattern, "-"); |
| | | |
| | | strImagePath = Regex.Replace(strImagePath, @"\{[^}]+\}", match => |
| | | { |
| | | // 去除{}只保留括号内的内容 |
| | | string content = match.Value; |
| | | content = content.Trim('{', '}'); // 去除首尾的{} |
| | | |
| | | RunBll.GetStringOutput(content, out string str); |
| | | return str; |
| | | }); |
| | | |
| | | if (bSaveRunImage) |
| | | { |
| | | // 最后一级目录必须为年月日,会根据时间来删除旧图片 |
| | | string directoryPath = Path.Combine(strImagePath, $"{ProcessName}\\原图\\{RunBll.Result}\\{DateTime.Now.ToString("yyyyMMdd")}\\"); |
| | | LB_SmartVision.Tool.Tool.AddRealImage(InputImage, directoryPath, fileName, strImageType, lImageQuality); |
| | | } |
| | | |
| | | if (bSaveResultImage) |
| | | { |
| | | // 最后一级目录必须为年月日,会根据时间来删除旧图片 |
| | | string directoryPath = Path.Combine(strImagePath, $"{ProcessName}\\截图\\{RunBll.Result}\\{DateTime.Now.ToString("yyyyMMdd")}\\"); |
| | | LB_SmartVision.Tool.Tool.AddRealImage(RecordImage, directoryPath, fileName, "jpg", 50L); |
| | | } |
| | | } |
| | | } |
| | | |
| | | foreach (var csv in GlobalVar.dicCsvSetting.Values |
| | | .Where(csv => csv.ProcessName == ProcessName) |
| | | .ToList()) |
| | | { |
| | | if (RunBll.GetCsv(csv |
| | | , out List<string> DataTitle, out Dictionary<string, object> ResultData)) |
| | | { |
| | | string filePath = Path.Combine(GlobalVar.strPathCsv, $"{ProcessName}.csv"); |
| | | LB_SmartVision.Tool.Tool.SaveData(filePath, DataTitle, ResultData); |
| | | } |
| | | } |
| | | #endregion |
| | | } |
| | | #endregion |
| | | } |
| | | is3D = false; |
| | | }); |
| | | }); |
| | | }); |
| | | } |
| | | else if (!is2D && !name.Contains("L")) |
| | | { |
| | | Task.Factory.StartNew(() => |
| | | { |
| | | if (!name.Contains("L")) |
| | | { |
| | | is2D = true; |
| | | } |
| | | Parallel.ForEach(matchedItems, item => |
| | | { |
| | | string ProcessName = item.Value["流程名"]; |
| | | LogInfo($"流程[{ProcessName}]开始运行", LogInfoType.INFO); |
| | | |
| | | if (!GlobalVar.dicProcesses.ContainsKey(ProcessName)) |
| | | { |
| | | LogInfo(string.Format("流程[{0}]不存在,请检查流程设置", ProcessName), LogInfoType.ERROR); |
| | | return; |
| | | } |
| | | |
| | | ProcessRunBll RunBll = GlobalVar.dicProcesses[ProcessName]; |
| | | if (RunBll == null || RunBll.bRuning) |
| | | { |
| | | LogInfo(string.Format("流程[{0}]上次未运行完成,触发失败", ProcessName) |
| | | , LogInfoType.ERROR); |
| | | return; |
| | | } |
| | | |
| | | try |
| | | { |
| | | bool result = false; |
| | | string msg = string.Empty; |
| | | int times = Convert.ToInt32(item.Value["重测次数"].ToString()); |
| | | string ConnecResult = item.Value["关联结果"]; |
| | | |
| | | if (times < 0) |
| | | { |
| | | result = RunBll.Run(); |
| | | msg = RunBll.Msg; |
| | | |
| | | if (!(string.IsNullOrEmpty(ConnecResult) || ConnecResult.Trim() == "未关联")) |
| | | { |
| | | if (!result) |
| | | { |
| | | LogInfo(string.Format("流程[{0}]运行结果失败,原因是[{1}]", ProcessName, msg), LogInfoType.NOSHOW); |
| | | } |
| | | RunBll.GetBooleanOutput(ConnecResult, out result); |
| | | RunBll.Result = result; |
| | | } |
| | | |
| | | if (!result) |
| | | { |
| | | LogInfo($"流程[{ProcessName}]被强制运行成功", LogInfoType.WARN); |
| | | RunBll.Result = true; |
| | | result = true; |
| | | } |
| | | } |
| | | else |
| | | { |
| | | while (times >= 0) |
| | | { |
| | | result = RunBll.Run(); |
| | | msg = RunBll.Msg; |
| | | if (!(string.IsNullOrEmpty(ConnecResult) || ConnecResult.Trim() == "未关联")) |
| | | { |
| | | if (!result) |
| | | { |
| | | LogInfo(string.Format("流程[{0}]运行结果失败,原因是[{1}]", ProcessName, msg), LogInfoType.NOSHOW); |
| | | } |
| | | |
| | | RunBll.GetBooleanOutput(ConnecResult, out result); |
| | | RunBll.Result = result; |
| | | } |
| | | |
| | | if (result) |
| | | { |
| | | break; |
| | | } |
| | | else if (!result && times > 0) |
| | | { |
| | | LogInfo(string.Format("流程[{0}]运行失败重新测试,剩余次数[{1}]", ProcessName, times), LogInfoType.WARN); |
| | | } |
| | | |
| | | times--; |
| | | } |
| | | } |
| | | |
| | | string ConnectProcess = item.Value["关联流程"]; |
| | | if (!(ConnectProcess == null || string.IsNullOrEmpty(ConnectProcess) || ConnectProcess.Trim() == "")) |
| | | { |
| | | //用逗号或者分号去间隔关联流程 |
| | | string[] arrConnectProcess; |
| | | if (ConnectProcess.Split(';').Length >= ConnectProcess.Split(',').Length) |
| | | { |
| | | arrConnectProcess = ConnectProcess.Split(';'); |
| | | } |
| | | else |
| | | { |
| | | arrConnectProcess = ConnectProcess.Split(','); |
| | | } |
| | | |
| | | foreach (string strConnectProcess in arrConnectProcess) |
| | | { |
| | | if (GlobalVar.dicProcesses.ContainsKey(strConnectProcess)) |
| | | { |
| | | ProcessRunBll ConnectRunBll = GlobalVar.dicProcesses[strConnectProcess]; |
| | | |
| | | int waitTime = 10; |
| | | DateTime startTime = DateTime.Now; |
| | | while ((DateTime.Now - startTime).TotalSeconds < waitTime |
| | | && (ConnectRunBll.bRuning || !ConnectRunBll.bCompleted)) |
| | | { |
| | | LogInfo(string.Format("关联流程[{0}]未运行完成,剩余等待[{1}]s", strConnectProcess, (waitTime - ((DateTime.Now - startTime).TotalSeconds))) |
| | | , LogInfoType.NOSHOW); |
| | | Thread.Sleep(1000); |
| | | continue; |
| | | } |
| | | |
| | | if (ConnectRunBll.bRuning || !ConnectRunBll.bCompleted) |
| | | { |
| | | GlobalVar.dicProcesses[ProcessName].Msg = string.Format("流程[{0}]未运行完成", ProcessName); |
| | | LogInfo(string.Format("关联流程[{0}]未运行完成", strConnectProcess), LogInfoType.ERROR); |
| | | result = false; |
| | | break; |
| | | } |
| | | else if (!ConnectRunBll.bRuning && ConnectRunBll.bCompleted) |
| | | { |
| | | LogInfo(string.Format("关联流程[{0}]运行完成", strConnectProcess), LogInfoType.INFO); |
| | | } |
| | | |
| | | result &= ConnectRunBll.Result; |
| | | if (!ConnectRunBll.Result) |
| | | { |
| | | LogInfo($"流程[{ProcessName}]的关联流程[{strConnectProcess}]运行失败", LogInfoType.ERROR); |
| | | msg = $"关联流程[{strConnectProcess}]运行失败"; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | LogInfo(result ? $"流程[{ProcessName}]运行成功" : $"流程[{ProcessName}]运行失败,原因是{msg}", result ? LogInfoType.PASS : LogInfoType.ERROR); |
| | | |
| | | string SendComName = result ? item.Value["成功通讯"] : item.Value["失败通讯"]; |
| | | string SendMsg = result ? item.Value["成功字符"] : item.Value["失败字符"]; |
| | | if (GlobalVar.dicCommunicators.ContainsKey(SendComName) && (!string.IsNullOrEmpty(SendMsg) || SendMsg.Trim() != "")) |
| | | { |
| | | GlobalVar.dicCommunicators[SendComName].SendMessage(SendMsg); |
| | | LogInfo(string.Format("发送给[{0}]了消息\"{1}\"", SendComName, SendMsg), LogInfoType.INFO); |
| | | } |
| | | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogInfo(string.Format("流程[{0}]运行发生了意外,原因是:{1}", ProcessName, ex.Message + $"【{ex.StackTrace}】"), LogInfoType.ERROR); |
| | | |
| | | RunBll.Result = false; |
| | | RunBll.Msg = $"[意外]{ex.Message}"; |
| | | } |
| | | finally |
| | | { |
| | | #region 从RunSettingPage和Layout中获取是否保存图片 |
| | | |
| | | string strImageType = "jpeg"; |
| | | bool bSaveRunImage = false; |
| | | bool bSaveResultImage = false; |
| | | long lImageQuality = 100L; |
| | | |
| | | //ckbSaveRunImage |
| | | if (GlobalVar.ControlStates.TryGetValue("ckbSaveRunImage_CheckBox", out object oSaveRunImage)) |
| | | { |
| | | if (oSaveRunImage != null && oSaveRunImage is bool) |
| | | { |
| | | bSaveRunImage = (bool)oSaveRunImage; |
| | | } |
| | | } |
| | | |
| | | //ckbSaveResultImage |
| | | if (GlobalVar.ControlStates.TryGetValue("ckbSaveResultImage_CheckBox", out object oSaveResultImage)) |
| | | { |
| | | if (oSaveResultImage != null && oSaveResultImage is bool) |
| | | { |
| | | bSaveResultImage = (bool)oSaveResultImage; |
| | | } |
| | | } |
| | | |
| | | //txtImageQuality |
| | | if (GlobalVar.ControlStates.TryGetValue("txtImageQuality_TextBox", out object oImageQuality)) |
| | | { |
| | | if (oImageQuality != null && oImageQuality is string) |
| | | { |
| | | lImageQuality = Convert.ToInt64((string)oImageQuality); |
| | | } |
| | | } |
| | | |
| | | //cmbImageType |
| | | if (GlobalVar.ControlStates.TryGetValue("cmbImageType_ComboBox", out object oImageType)) |
| | | { |
| | | try |
| | | { |
| | | // 动态解析ComboBox数据 |
| | | var json = JsonConvert.SerializeObject(oImageType); |
| | | var comboData = JsonConvert.DeserializeAnonymousType(json, new |
| | | { |
| | | Items = new List<object>(), |
| | | SelectedIndex = 0 |
| | | }); |
| | | |
| | | if (comboData != null && comboData.Items.Count > 0) |
| | | { |
| | | strImageType = comboData.Items[comboData.SelectedIndex].ToString(); |
| | | } |
| | | } |
| | | catch { } |
| | | } |
| | | |
| | | // 生成图片并显示到控件中 |
| | | HObject InputImage = null; |
| | | HObject RecordImage = null; |
| | | |
| | | foreach (var layout in GlobalVar.dicLayout.Values |
| | | .Where(layout => layout.ProcessName == ProcessName) |
| | | .ToList()) |
| | | { |
| | | string title = layout.Title; |
| | | string strImagePath = layout.SaveImageDir; |
| | | if (!AllProcessesPage.dicProcessControls.ContainsKey(title)) |
| | | { |
| | | continue; |
| | | } |
| | | |
| | | RunBll.GetImage(layout, out InputImage, out RecordImage); |
| | | AllProcessesPage.dicProcessControls[title].ShowHoImage(RecordImage); |
| | | |
| | | if (!string.IsNullOrEmpty(layout.SaveImageDir)) |
| | | { |
| | | string fileNameHead = layout.SaveImageHead; |
| | | |
| | | string result = Regex.Replace(fileNameHead, @"\{[^}]+\}", match => |
| | | { |
| | | // 去除{}只保留括号内的内容 |
| | | string content = match.Value; |
| | | content = content.Trim('{', '}'); // 去除首尾的{} |
| | | |
| | | RunBll.GetStringOutput(content, out string str); |
| | | return str; |
| | | }); |
| | | |
| | | string fileName = $"{result}-[{DateTime.Now.ToString("HH.mm.ss.ffff")}]"; |
| | | |
| | | // 使用正则表达式替换所有非法字符 |
| | | string invalidChars = Regex.Escape(new string(Path.GetInvalidFileNameChars())); |
| | | string pattern = $"[{invalidChars}]"; |
| | | fileName = Regex.Replace(fileName, pattern, "-"); |
| | | |
| | | strImagePath = Regex.Replace(strImagePath, @"\{[^}]+\}", match => |
| | | { |
| | | // 去除{}只保留括号内的内容 |
| | | string content = match.Value; |
| | | content = content.Trim('{', '}'); // 去除首尾的{} |
| | | |
| | | RunBll.GetStringOutput(content, out string str); |
| | | return str; |
| | | }); |
| | | |
| | | if (bSaveRunImage) |
| | | { |
| | | // 最后一级目录必须为年月日,会根据时间来删除旧图片 |
| | | string directoryPath = Path.Combine(strImagePath, $"{ProcessName}\\原图\\{RunBll.Result}\\{DateTime.Now.ToString("yyyyMMdd")}\\"); |
| | | LB_SmartVision.Tool.Tool.AddRealImage(InputImage, directoryPath, fileName, strImageType, lImageQuality); |
| | | } |
| | | |
| | | if (bSaveResultImage) |
| | | { |
| | | // 最后一级目录必须为年月日,会根据时间来删除旧图片 |
| | | string directoryPath = Path.Combine(strImagePath, $"{ProcessName}\\截图\\{RunBll.Result}\\{DateTime.Now.ToString("yyyyMMdd")}\\"); |
| | | LB_SmartVision.Tool.Tool.AddRealImage(RecordImage, directoryPath, fileName, "jpg", 50L); |
| | | } |
| | | } |
| | | } |
| | | |
| | | foreach (var csv in GlobalVar.dicCsvSetting.Values |
| | | .Where(csv => csv.ProcessName == ProcessName) |
| | | .ToList()) |
| | | { |
| | | if (RunBll.GetCsv(csv |
| | | , out List<string> DataTitle, out Dictionary<string, object> ResultData)) |
| | | { |
| | | string filePath = Path.Combine(GlobalVar.strPathCsv, $"{ProcessName}.csv"); |
| | | LB_SmartVision.Tool.Tool.SaveData(filePath, DataTitle, ResultData); |
| | | } |
| | | } |
| | | #endregion |
| | | } |
| | | is2D = false; |
| | | }); |
| | | }); |
| | | } |
| | | |
| | | //Task.Factory.StartNew(() => |
| | | //{ |
| | | // Parallel.ForEach(matchedItems, item => |
| | | // { |
| | | // string ProcessName = item.Value["流程名"]; |
| | | // LogInfo($"流程[{ProcessName}]开始运行", LogInfoType.INFO); |
| | | |
| | | // if (!GlobalVar.dicProcesses.ContainsKey(ProcessName)) |
| | | // { |
| | | // LogInfo(string.Format("流程[{0}]不存在,请检查流程设置", ProcessName), LogInfoType.ERROR); |
| | | // return; |
| | | // } |
| | | |
| | | // ProcessRunBll RunBll = GlobalVar.dicProcesses[ProcessName]; |
| | | // if (RunBll == null || RunBll.bRuning) |
| | | // { |
| | | // LogInfo(string.Format("流程[{0}]上次未运行完成,触发失败", ProcessName) |
| | | // , LogInfoType.ERROR); |
| | | // return; |
| | | // } |
| | | |
| | | // try |
| | | // { |
| | | // bool result = false; |
| | | // string msg = string.Empty; |
| | | // int times = Convert.ToInt32(item.Value["重测次数"].ToString()); |
| | | // string ConnecResult = item.Value["关联结果"]; |
| | | |
| | | // if (times < 0) |
| | | // { |
| | | // result = RunBll.Run(); |
| | | // msg = RunBll.Msg; |
| | | |
| | | // if (!(string.IsNullOrEmpty(ConnecResult) || ConnecResult.Trim() == "未关联")) |
| | | // { |
| | | // if (!result) |
| | | // { |
| | | // LogInfo(string.Format("流程[{0}]运行结果失败,原因是[{1}]", ProcessName, msg), LogInfoType.NOSHOW); |
| | | // } |
| | | // RunBll.GetBooleanOutput(ConnecResult, out result); |
| | | // RunBll.Result = result; |
| | | // } |
| | | |
| | | // if (!result) |
| | | // { |
| | | // LogInfo($"流程[{ProcessName}]被强制运行成功", LogInfoType.WARN); |
| | | // RunBll.Result = true; |
| | | // result = true; |
| | | // } |
| | | // } |
| | | // else |
| | | // { |
| | | // while (times >= 0) |
| | | // { |
| | | // result = RunBll.Run(); |
| | | // msg = RunBll.Msg; |
| | | // if (!(string.IsNullOrEmpty(ConnecResult) || ConnecResult.Trim() == "未关联")) |
| | | // { |
| | | // if (!result) |
| | | // { |
| | | // LogInfo(string.Format("流程[{0}]运行结果失败,原因是[{1}]", ProcessName, msg), LogInfoType.NOSHOW); |
| | | // } |
| | | |
| | | // RunBll.GetBooleanOutput(ConnecResult, out result); |
| | | // RunBll.Result = result; |
| | | // } |
| | | |
| | | // if (result) |
| | | // { |
| | | // break; |
| | | // } |
| | | // else if (!result && times > 0) |
| | | // { |
| | | // LogInfo(string.Format("流程[{0}]运行失败重新测试,剩余次数[{1}]", ProcessName, times), LogInfoType.WARN); |
| | | // } |
| | | |
| | | // times--; |
| | | // } |
| | | // } |
| | | |
| | | // string ConnectProcess = item.Value["关联流程"]; |
| | | // if (!(ConnectProcess == null || string.IsNullOrEmpty(ConnectProcess) || ConnectProcess.Trim() == "")) |
| | | // { |
| | | // //用逗号或者分号去间隔关联流程 |
| | | // string[] arrConnectProcess; |
| | | // if (ConnectProcess.Split(';').Length >= ConnectProcess.Split(',').Length) |
| | | // { |
| | | // arrConnectProcess = ConnectProcess.Split(';'); |
| | | // } |
| | | // else |
| | | // { |
| | | // arrConnectProcess = ConnectProcess.Split(','); |
| | | // } |
| | | |
| | | // foreach (string strConnectProcess in arrConnectProcess) |
| | | // { |
| | | // if (GlobalVar.dicProcesses.ContainsKey(strConnectProcess)) |
| | | // { |
| | | // ProcessRunBll ConnectRunBll = GlobalVar.dicProcesses[strConnectProcess]; |
| | | |
| | | // int waitTime = 10; |
| | | // DateTime startTime = DateTime.Now; |
| | | // while ((DateTime.Now - startTime).TotalSeconds < waitTime |
| | | // && (ConnectRunBll.bRuning || !ConnectRunBll.bCompleted)) |
| | | // { |
| | | // LogInfo(string.Format("关联流程[{0}]未运行完成,剩余等待[{1}]s", strConnectProcess, (waitTime - ((DateTime.Now - startTime).TotalSeconds))) |
| | | // , LogInfoType.NOSHOW); |
| | | // Thread.Sleep(1000); |
| | | // continue; |
| | | // } |
| | | |
| | | // if (ConnectRunBll.bRuning || !ConnectRunBll.bCompleted) |
| | | // { |
| | | // GlobalVar.dicProcesses[ProcessName].Msg = string.Format("流程[{0}]未运行完成", ProcessName); |
| | | // LogInfo(string.Format("关联流程[{0}]未运行完成", strConnectProcess), LogInfoType.ERROR); |
| | | // result = false; |
| | | // break; |
| | | // } |
| | | // else if (!ConnectRunBll.bRuning && ConnectRunBll.bCompleted) |
| | | // { |
| | | // LogInfo(string.Format("关联流程[{0}]运行完成", strConnectProcess), LogInfoType.INFO); |
| | | // } |
| | | |
| | | // result &= ConnectRunBll.Result; |
| | | // if (!ConnectRunBll.Result) |
| | | // { |
| | | // LogInfo($"流程[{ProcessName}]的关联流程[{strConnectProcess}]运行失败", LogInfoType.ERROR); |
| | | // msg = $"关联流程[{strConnectProcess}]运行失败"; |
| | | // } |
| | | // } |
| | | // } |
| | | // } |
| | | |
| | | // LogInfo(result ? $"流程[{ProcessName}]运行成功" : $"流程[{ProcessName}]运行失败,原因是{msg}", result ? LogInfoType.PASS : LogInfoType.ERROR); |
| | | |
| | | // string SendComName = result ? item.Value["成功通讯"] : item.Value["失败通讯"]; |
| | | // string SendMsg = result ? item.Value["成功字符"] : item.Value["失败字符"]; |
| | | // if (GlobalVar.dicCommunicators.ContainsKey(SendComName) && (!string.IsNullOrEmpty(SendMsg) || SendMsg.Trim() != "")) |
| | | // { |
| | | // GlobalVar.dicCommunicators[SendComName].SendMessage(SendMsg); |
| | | // LogInfo(string.Format("发送给[{0}]了消息\"{1}\"", SendComName, SendMsg), LogInfoType.INFO); |
| | | // } |
| | | |
| | | // } |
| | | // catch (Exception ex) |
| | | // { |
| | | // LogInfo(string.Format("流程[{0}]运行发生了意外,原因是:{1}", ProcessName, ex.Message + $"【{ex.StackTrace}】"), LogInfoType.ERROR); |
| | | |
| | | // RunBll.Result = false; |
| | | // RunBll.Msg = $"[意外]{ex.Message}"; |
| | | // } |
| | | // finally |
| | | // { |
| | | // #region 从RunSettingPage和Layout中获取是否保存图片 |
| | | |
| | | // string strImageType = "jpeg"; |
| | | // bool bSaveRunImage = false; |
| | | // bool bSaveResultImage = false; |
| | | // long lImageQuality = 100L; |
| | | |
| | | // //ckbSaveRunImage |
| | | // if (GlobalVar.ControlStates.TryGetValue("ckbSaveRunImage_CheckBox", out object oSaveRunImage)) |
| | | // { |
| | | // if (oSaveRunImage != null && oSaveRunImage is bool) |
| | | // { |
| | | // bSaveRunImage = (bool)oSaveRunImage; |
| | | // } |
| | | // } |
| | | |
| | | // //ckbSaveResultImage |
| | | // if (GlobalVar.ControlStates.TryGetValue("ckbSaveResultImage_CheckBox", out object oSaveResultImage)) |
| | | // { |
| | | // if (oSaveResultImage != null && oSaveResultImage is bool) |
| | | // { |
| | | // bSaveResultImage = (bool)oSaveResultImage; |
| | | // } |
| | | // } |
| | | |
| | | // //txtImageQuality |
| | | // if (GlobalVar.ControlStates.TryGetValue("txtImageQuality_TextBox", out object oImageQuality)) |
| | | // { |
| | | // if (oImageQuality != null && oImageQuality is string) |
| | | // { |
| | | // lImageQuality = Convert.ToInt64((string)oImageQuality); |
| | | // } |
| | | // } |
| | | |
| | | // //cmbImageType |
| | | // if (GlobalVar.ControlStates.TryGetValue("cmbImageType_ComboBox", out object oImageType)) |
| | | // { |
| | | // try |
| | | // { |
| | | // // 动态解析ComboBox数据 |
| | | // var json = JsonConvert.SerializeObject(oImageType); |
| | | // var comboData = JsonConvert.DeserializeAnonymousType(json, new |
| | | // { |
| | | // Items = new List<object>(), |
| | | // SelectedIndex = 0 |
| | | // }); |
| | | |
| | | // if (comboData != null && comboData.Items.Count > 0) |
| | | // { |
| | | // strImageType = comboData.Items[comboData.SelectedIndex].ToString(); |
| | | // } |
| | | // } |
| | | // catch { } |
| | | // } |
| | | |
| | | // // 生成图片并显示到控件中 |
| | | // HObject InputImage = null; |
| | | // HObject RecordImage = null; |
| | | |
| | | // foreach (var layout in GlobalVar.dicLayout.Values |
| | | // .Where(layout => layout.ProcessName == ProcessName) |
| | | // .ToList()) |
| | | // { |
| | | // string title = layout.Title; |
| | | // string strImagePath = layout.SaveImageDir; |
| | | // if (!AllProcessesPage.dicProcessControls.ContainsKey(title)) |
| | | // { |
| | | // continue; |
| | | // } |
| | | |
| | | // RunBll.GetImage(layout, out InputImage, out RecordImage); |
| | | // AllProcessesPage.dicProcessControls[title].ShowHoImage(RecordImage); |
| | | |
| | | // if (!string.IsNullOrEmpty(layout.SaveImageDir)) |
| | | // { |
| | | // string fileNameHead = layout.SaveImageHead; |
| | | |
| | | // string result = Regex.Replace(fileNameHead, @"\{[^}]+\}", match => |
| | | // { |
| | | // // 去除{}只保留括号内的内容 |
| | | // string content = match.Value; |
| | | // content = content.Trim('{', '}'); // 去除首尾的{} |
| | | |
| | | // RunBll.GetStringOutput(content, out string str); |
| | | // return str; |
| | | // }); |
| | | |
| | | // string fileName = $"{result}-[{DateTime.Now.ToString("HH.mm.ss.ffff")}]"; |
| | | |
| | | // // 使用正则表达式替换所有非法字符 |
| | | // string invalidChars = Regex.Escape(new string(Path.GetInvalidFileNameChars())); |
| | | // string pattern = $"[{invalidChars}]"; |
| | | // fileName = Regex.Replace(fileName, pattern, "-"); |
| | | |
| | | // strImagePath = Regex.Replace(strImagePath, @"\{[^}]+\}", match => |
| | | // { |
| | | // // 去除{}只保留括号内的内容 |
| | | // string content = match.Value; |
| | | // content = content.Trim('{', '}'); // 去除首尾的{} |
| | | |
| | | // RunBll.GetStringOutput(content, out string str); |
| | | // return str; |
| | | // }); |
| | | |
| | | // if (bSaveRunImage) |
| | | // { |
| | | // // 最后一级目录必须为年月日,会根据时间来删除旧图片 |
| | | // string directoryPath = Path.Combine(strImagePath, $"{ProcessName}\\原图\\{RunBll.Result}\\{DateTime.Now.ToString("yyyyMMdd")}\\"); |
| | | // LB_SmartVision.Tool.Tool.AddRealImage(InputImage, directoryPath, fileName, strImageType, lImageQuality); |
| | | // } |
| | | |
| | | // if (bSaveResultImage) |
| | | // { |
| | | // // 最后一级目录必须为年月日,会根据时间来删除旧图片 |
| | | // string directoryPath = Path.Combine(strImagePath, $"{ProcessName}\\截图\\{RunBll.Result}\\{DateTime.Now.ToString("yyyyMMdd")}\\"); |
| | | // LB_SmartVision.Tool.Tool.AddRealImage(RecordImage, directoryPath, fileName, "jpg", 50L); |
| | | // } |
| | | // } |
| | | // } |
| | | |
| | | // foreach (var csv in GlobalVar.dicCsvSetting.Values |
| | | // .Where(csv => csv.ProcessName == ProcessName) |
| | | // .ToList()) |
| | | // { |
| | | // if (RunBll.GetCsv(csv |
| | | // , out List<string> DataTitle, out Dictionary<string, object> ResultData)) |
| | | // { |
| | | // string filePath = Path.Combine(GlobalVar.strPathCsv, $"{ProcessName}.csv"); |
| | | // LB_SmartVision.Tool.Tool.SaveData(filePath, DataTitle, ResultData); |
| | | // } |
| | | // } |
| | | // #endregion |
| | | // } |
| | | // }); |
| | | //}); |
| | | } |
| | | |
| | | private void VisionForm_FormClosing(object sender, FormClosingEventArgs e) |
| | |
| | | { |
| | | camera.Dispose(); |
| | | } |
| | | if (GlobalVar.dicCommunicators != null && GlobalVar.dicCommunicators.Count > 0 && |
| | | GlobalVar.dicCommunicators[GlobalVar.currentCommunicators] != null && |
| | | GlobalVar.dicCommunicators[GlobalVar.currentCommunicators].bConnected) |
| | | { |
| | | foreach (var item in GlobalVar.allPlcSettings[GlobalVar.currentCommunicators].Signals) |
| | | { |
| | | if (item.SignalName.Equals("视觉Ready信号")) |
| | | { |
| | | if (!string.IsNullOrEmpty("0")) |
| | | { |
| | | object convertedValue = ConvertValue(item.DataType, "0"); |
| | | ((SiemensLBS7)GlobalVar.dicCommunicators[GlobalVar.currentCommunicators]).WriteSignal(item, convertedValue); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | StopPLC(); |
| | | |
| | | foreach (BaseCommunicator communicator in GlobalVar.dicCommunicators.Values) |
| | | { |
| | | communicator.Disconnect(); |
| | | } |
| | | |
| | | FormClosing -= VisionForm_FormClosing; |
| | | |
| | | //try |
| | | //{ |
| | | // Process[] processes = System.Diagnostics.Process.GetProcesses(); //获得所有进程 |
| | | // foreach (Process p in processes) |
| | | // { |
| | | // if (p.ProcessName == "LB_SmartVision" && p.StartTime < DateTime.Now.AddMilliseconds(-300)) |
| | | // { |
| | | // p.Kill(); |
| | | // } |
| | | // } |
| | | //} |
| | | //catch { } |
| | | |
| | | KillAllTargetProcesses(); |
| | | } |
| | |
| | | |
| | | private void btn_Run_Click(object sender, EventArgs e) |
| | | { |
| | | var matchedItems = GlobalVar.dicProcessSetting |
| | | .Where(item => |
| | | { |
| | | var value = item.Value; |
| | | var triggerComm = value["触发通讯"]; |
| | | var triggerChar = value["触发字符"]; |
| | | return triggerComm != null && !triggerComm.Equals("无"); |
| | | //return triggerComm != null && triggerComm.Equals(name) && |
| | | // (string.IsNullOrEmpty(triggerChar?.ToString()) || |
| | | // msg.StartsWith(triggerChar.ToString())); |
| | | }) |
| | | .ToList(); // 避免重复字典访问和装箱操作 |
| | | if (matchedItems.Count <= 0) |
| | | //var matchedItems = GlobalVar.dicProcessSetting |
| | | // .Where(item => |
| | | // { |
| | | // var value = item.Value; |
| | | // var triggerComm = value["触发通讯"]; |
| | | // var triggerChar = value["触发字符"]; |
| | | // return triggerComm != null && !triggerComm.Equals("无"); |
| | | // //return triggerComm != null && triggerComm.Equals(name) && |
| | | // // (string.IsNullOrEmpty(triggerChar?.ToString()) || |
| | | // // msg.StartsWith(triggerChar.ToString())); |
| | | // }) |
| | | // .ToList(); // 避免重复字典访问和装箱操作 |
| | | //if (matchedItems.Count <= 0) |
| | | //{ |
| | | // return; |
| | | //} |
| | | //foreach (var item in matchedItems) |
| | | //{ |
| | | // Task.Factory.StartNew(() => |
| | | // { |
| | | // TriggerRunMessageReceived(item.Value["触发通讯"], item.Value["触发字符"]); |
| | | // }); |
| | | //} |
| | | foreach (var item in GlobalVar.dicCameras.Keys) |
| | | { |
| | | return; |
| | | } |
| | | foreach (var item in matchedItems) |
| | | { |
| | | Task.Factory.StartNew(() => |
| | | { |
| | | TriggerRunMessageReceived(item.Value["触发通讯"], item.Value["触发字符"]); |
| | | }); |
| | | GlobalVar.dicCameras[item].StartSingleGrab(); |
| | | } |
| | | } |
| | | |
| | | private void btn_RunContinuously_Click(object sender, EventArgs e) |
| | | { |
| | | if (btn_RunContinuously.Text.Equals("连续运行")) |
| | | if (this.ckbAllowRun.Checked) |
| | | { |
| | | _isTabSwitchAllowed = false; |
| | | btn_RunContinuously.Text = "暂停运行"; |
| | | // 关闭使能 |
| | | SelectMainPage(); |
| | | btn_GlobalVar.Enabled = false; |
| | | btn_Login.Enabled = false; |
| | | btn_SingleRun.Enabled = false; |
| | | ckbAllowRun.Enabled = false; |
| | | Task.Factory.StartNew(() => |
| | | if (btn_RunContinuously.Text.Equals("连续运行")) |
| | | { |
| | | var matchedItems = GlobalVar.dicProcessSetting |
| | | .Where(item => |
| | | { |
| | | var value = item.Value; |
| | | var triggerComm = value["触发通讯"]; |
| | | var triggerChar = value["触发字符"]; |
| | | return triggerComm != null && !triggerComm.Equals("无"); |
| | | //return triggerComm != null && triggerComm.Equals(name) && |
| | | // (string.IsNullOrEmpty(triggerChar?.ToString()) || |
| | | // msg.StartsWith(triggerChar.ToString())); |
| | | }) |
| | | .ToList(); // 避免重复字典访问和装箱操作 |
| | | while (btn_RunContinuously.Text.Equals("暂停运行")) |
| | | _isTabSwitchAllowed = false; |
| | | btn_RunContinuously.Text = "暂停运行"; |
| | | |
| | | if (GlobalVar.dicCommunicators != null && GlobalVar.dicCommunicators.Count > 0 && |
| | | GlobalVar.dicCommunicators[GlobalVar.currentCommunicators] != null && |
| | | GlobalVar.dicCommunicators[GlobalVar.currentCommunicators].bConnected) |
| | | { |
| | | foreach (var item in matchedItems) |
| | | foreach (var item in GlobalVar.allPlcSettings[GlobalVar.currentCommunicators].Signals) |
| | | { |
| | | Task.Factory.StartNew(() => |
| | | if (item.SignalName.Equals("视觉Ready信号")) |
| | | { |
| | | TriggerRunMessageReceived(item.Value["触发通讯"], item.Value["触发字符"]); |
| | | }); |
| | | if (!string.IsNullOrEmpty("1")) |
| | | { |
| | | object convertedValue = ConvertValue(item.DataType, "1"); |
| | | ((SiemensLBS7)GlobalVar.dicCommunicators[GlobalVar.currentCommunicators]).WriteSignal(item, convertedValue); |
| | | } |
| | | } |
| | | } |
| | | //TriggerRunMessageReceived(matchedItems[0].Value["触发通讯"], matchedItems[0].Value["触发字符"]); |
| | | Thread.Sleep(3500); |
| | | } |
| | | //this.Invoke(() => |
| | | StartPLC(); |
| | | // 关闭使能 |
| | | SelectMainPage(); |
| | | btn_GlobalVar.Enabled = false; |
| | | btn_Login.Enabled = false; |
| | | btn_SingleRun.Enabled = false; |
| | | ckbAllowRun.Enabled = false; |
| | | foreach (var item in GlobalVar.dicCameras.Keys) |
| | | { |
| | | GlobalVar.dicCameras[item].StartContinuousGrab(); |
| | | } |
| | | //Task.Factory.StartNew(() => |
| | | //{ |
| | | // var matchedItems = GlobalVar.dicProcessSetting |
| | | // .Where(item => |
| | | // { |
| | | // var value = item.Value; |
| | | // var triggerComm = value["触发通讯"]; |
| | | // var triggerChar = value["触发字符"]; |
| | | // return triggerComm != null && !triggerComm.Equals("无"); |
| | | // //return triggerComm != null && triggerComm.Equals(name) && |
| | | // // (string.IsNullOrEmpty(triggerChar?.ToString()) || |
| | | // // msg.StartsWith(triggerChar.ToString())); |
| | | // }) |
| | | // .ToList(); // 避免重复字典访问和装箱操作 |
| | | // while (btn_RunContinuously.Text.Equals("暂停运行")) |
| | | // { |
| | | // foreach (var item in matchedItems) |
| | | // { |
| | | // Task.Factory.StartNew(() => |
| | | // { |
| | | // TriggerRunMessageReceived(item.Value["触发通讯"], item.Value["触发字符"]); |
| | | // }); |
| | | // } |
| | | // //TriggerRunMessageReceived(matchedItems[0].Value["触发通讯"], matchedItems[0].Value["触发字符"]); |
| | | // Thread.Sleep(3500); |
| | | // } |
| | | // //this.Invoke(() => |
| | | // //{ |
| | | // //}); |
| | | //}); |
| | | }); |
| | | } |
| | | else if (btn_RunContinuously.Text.Equals("暂停运行")) |
| | | { |
| | | btn_RunContinuously.Text = "连续运行"; |
| | | foreach (var item in GlobalVar.dicCameras.Keys) |
| | | { |
| | | GlobalVar.dicCameras[item].StopGrabbing(); |
| | | } |
| | | // 启动使能 |
| | | UnSelectMainPage(); |
| | | btn_GlobalVar.Enabled = true; |
| | | btn_Login.Enabled = true; |
| | | btn_SingleRun.Enabled = true; |
| | | ckbAllowRun.Enabled = true; |
| | | Thread.Sleep(100); |
| | | _isTabSwitchAllowed = true; |
| | | if (GlobalVar.dicCommunicators != null && GlobalVar.dicCommunicators.Count > 0 && |
| | | GlobalVar.dicCommunicators[GlobalVar.currentCommunicators] != null && |
| | | GlobalVar.dicCommunicators[GlobalVar.currentCommunicators].bConnected) |
| | | { |
| | | foreach (var item in GlobalVar.allPlcSettings[GlobalVar.currentCommunicators].Signals) |
| | | { |
| | | if (item.SignalName.Equals("视觉Ready信号")) |
| | | { |
| | | if (!string.IsNullOrEmpty("0")) |
| | | { |
| | | object convertedValue = ConvertValue(item.DataType, "0"); |
| | | ((SiemensLBS7)GlobalVar.dicCommunicators[GlobalVar.currentCommunicators]).WriteSignal(item, convertedValue); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | StopPLC(); |
| | | } |
| | | } |
| | | else if (btn_RunContinuously.Text.Equals("暂停运行")) |
| | | { |
| | | btn_RunContinuously.Text = "连续运行"; |
| | | |
| | | // 启动使能 |
| | | UnSelectMainPage(); |
| | | btn_GlobalVar.Enabled = true; |
| | | btn_Login.Enabled = true; |
| | | btn_SingleRun.Enabled = true; |
| | | ckbAllowRun.Enabled = true; |
| | | Thread.Sleep(100); |
| | | _isTabSwitchAllowed = true; |
| | | } |
| | | |
| | | } |
| | | |
| | | private void cmbProduct_SelectedValueChanged(object sender, EventArgs e) |
| | |
| | | } |
| | | materialCombobox1.SelectedItem = materialTabControl.SelectedTab.Text; |
| | | } |
| | | |
| | | |
| | | #region PLC心跳线程 |
| | | private bool threadStart = false; |
| | | private AutoResetEvent mAutoResetEventWorking = new AutoResetEvent(false); |
| | | |
| | | private void startPLCThread() |
| | | { |
| | | var taskworking = Task.Factory.StartNew(() => |
| | | { |
| | | PLCHeartBeatWorkThread(); |
| | | }); |
| | | } |
| | | private void StartPLC() |
| | | { |
| | | threadStart = true; |
| | | mAutoResetEventWorking.Set(); |
| | | AsyncLogHelper.Info("开启心跳通讯线程!"); |
| | | } |
| | | private void StopPLC() |
| | | { |
| | | threadStart = false; |
| | | Thread.Sleep(10); |
| | | } |
| | | bool isStart = true; |
| | | object plcHeartBeatWorkThread = new object(); |
| | | private bool plcHeartBeat = false; |
| | | private void PLCHeartBeatWorkThread() |
| | | { |
| | | while (isStart) |
| | | { |
| | | try |
| | | { |
| | | lock (plcHeartBeatWorkThread) |
| | | { |
| | | if (threadStart == false) |
| | | { |
| | | mAutoResetEventWorking.WaitOne();//阻塞等待 |
| | | } |
| | | else |
| | | { |
| | | if (threadStart) |
| | | { |
| | | try |
| | | { |
| | | Thread.Sleep(500); |
| | | } |
| | | catch { } |
| | | try |
| | | { |
| | | if (GlobalVar.dicCommunicators != null && GlobalVar.dicCommunicators.Count > 0 && |
| | | GlobalVar.dicCommunicators[GlobalVar.currentCommunicators] != null && |
| | | GlobalVar.dicCommunicators[GlobalVar.currentCommunicators].bConnected) |
| | | { |
| | | var TaskHeartBeat = Task.Factory.StartNew(() => |
| | | { |
| | | try |
| | | { |
| | | if (!plcHeartBeat) |
| | | { |
| | | plcHeartBeat = true; |
| | | foreach (var item in GlobalVar.allPlcSettings[GlobalVar.currentCommunicators].Signals) |
| | | { |
| | | if (item.SignalName.Equals("心跳信号")) |
| | | { |
| | | if (!string.IsNullOrEmpty(plcHeartBeat.ToString())) |
| | | { |
| | | object convertedValue = ConvertValue(item.DataType, plcHeartBeat.ToString()); |
| | | ((SiemensLBS7)GlobalVar.dicCommunicators[GlobalVar.currentCommunicators]).WriteSignal(item, convertedValue); |
| | | } |
| | | } |
| | | } |
| | | this.textBox_CommunicationSetting.BackColor = Color.SpringGreen; |
| | | } |
| | | else |
| | | { |
| | | plcHeartBeat = false; |
| | | foreach (var item in GlobalVar.allPlcSettings[GlobalVar.currentCommunicators].Signals) |
| | | { |
| | | if (item.SignalName.Equals("心跳信号")) |
| | | { |
| | | if (!string.IsNullOrEmpty(plcHeartBeat.ToString())) |
| | | { |
| | | object convertedValue = ConvertValue(item.DataType, plcHeartBeat.ToString()); |
| | | ((SiemensLBS7)GlobalVar.dicCommunicators[GlobalVar.currentCommunicators]).WriteSignal(item, convertedValue); |
| | | } |
| | | } |
| | | } |
| | | this.textBox_CommunicationSetting.BackColor = Color.Red; |
| | | } |
| | | } |
| | | catch |
| | | { |
| | | |
| | | } |
| | | }); |
| | | } |
| | | } |
| | | catch |
| | | { |
| | | } |
| | | } |
| | | } |
| | | } |
| | | //Thread.Sleep(1000); |
| | | } |
| | | catch { } |
| | | } |
| | | } |
| | | public void stopPLC() |
| | | { |
| | | if (btn_RunContinuously.Text.Equals("连续运行")) |
| | | { |
| | | var taskstopPLC = Task.Factory.StartNew(() => |
| | | { |
| | | try |
| | | { |
| | | if (GlobalVar.dicCommunicators != null && GlobalVar.dicCommunicators.Count > 0 && |
| | | GlobalVar.dicCommunicators[GlobalVar.currentCommunicators] != null && |
| | | GlobalVar.dicCommunicators[GlobalVar.currentCommunicators].bConnected) |
| | | { |
| | | //S7NETplusStaticClass.plc.Write(VVar.softWareParams.StopNG, true); |
| | | AsyncLogHelper.Error("[" + DateTime.Now.ToString("yyyy:MM:dd:HH:mm:ss:fff") + "]" + "连续NG次数为:" + ", 已发出停线信号!请人为排查并复位异常。"); |
| | | } |
| | | } |
| | | catch |
| | | { |
| | | AsyncLogHelper.Error("[" + DateTime.Now.ToString("yyyy:MM:dd:HH:mm:ss:fff") + "]" + "连续NG停线异常!请检查工控机到PLC的网线连接!"); |
| | | } |
| | | }); |
| | | } |
| | | } |
| | | private void abortHeartB() |
| | | { |
| | | try |
| | | { |
| | | threadStart = false; |
| | | } |
| | | catch { } |
| | | } |
| | | private void stopPLCHB() |
| | | { |
| | | //发送给PLC的IsRead为false; |
| | | if (GlobalVar.dicCommunicators != null && GlobalVar.dicCommunicators.Count > 0 && |
| | | GlobalVar.dicCommunicators[GlobalVar.currentCommunicators] != null && |
| | | GlobalVar.dicCommunicators[GlobalVar.currentCommunicators].bConnected) |
| | | { |
| | | foreach (var item in GlobalVar.allPlcSettings[GlobalVar.currentCommunicators].Signals) |
| | | { |
| | | if (item.SignalName.Equals("视觉Ready信号")) |
| | | { |
| | | ((SiemensLBS7)GlobalVar.dicCommunicators[GlobalVar.currentCommunicators]).WriteSignal(item, "0"); |
| | | } |
| | | } |
| | | threadStart = false; |
| | | AsyncLogHelper.Info("暂停心跳通讯线程!"); |
| | | if (GlobalVar.dicCommunicators != null && GlobalVar.dicCommunicators.Count > 0 && |
| | | GlobalVar.dicCommunicators[GlobalVar.currentCommunicators] != null && |
| | | GlobalVar.dicCommunicators[GlobalVar.currentCommunicators].bConnected) |
| | | { |
| | | this.textBox_CommunicationSetting.BackColor = Color.SpringGreen; |
| | | } |
| | | else |
| | | { |
| | | textBox_CommunicationSetting.BackColor = Color.Red; |
| | | } |
| | | } |
| | | else |
| | | { |
| | | AsyncLogHelper.Error("停止通讯异常!PLC连接异常,请检查工控机到PLC的网线连接!"); |
| | | } |
| | | Thread.Sleep(100); |
| | | } |
| | | #endregion |
| | | // 数据类型转换辅助方法 |
| | | private object ConvertValue(string dataType, string valueStr) |
| | | { |
| | | switch (dataType) |
| | | { |
| | | case "Bool": |
| | | { |
| | | if (bool.TryParse(valueStr, out bool b)) return b; |
| | | if (valueStr == "1") return true; |
| | | if (valueStr == "0") return false; |
| | | throw new Exception("无效的布尔值"); |
| | | } |
| | | case "Byte": |
| | | { |
| | | return byte.Parse(valueStr); |
| | | } |
| | | case "Int": |
| | | { |
| | | return short.Parse(valueStr); |
| | | } |
| | | case "DInt": |
| | | { |
| | | return int.Parse(valueStr); |
| | | } |
| | | case "Real": |
| | | { |
| | | return float.Parse(valueStr); |
| | | } |
| | | case "String": |
| | | { |
| | | return valueStr; |
| | | } |
| | | default: |
| | | { |
| | | throw new NotSupportedException($"不支持的类型: {dataType}"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |
| | | |