From dc18021539bdf4ee176ec9d9b88eb7d9ad581600 Mon Sep 17 00:00:00 2001
From: C3204 <zhengyabo@lanpucloud.cn>
Date: 星期五, 17 四月 2026 17:00:35 +0800
Subject: [PATCH] 新增心跳等常规信号配置&&修改相机轴读取配置等
---
LB_SmartVision/VisionForm.cs | 1728 +++++++++++++++++++++++++++++++++++++++++++++++++----------
1 files changed, 1,429 insertions(+), 299 deletions(-)
diff --git a/LB_SmartVision/VisionForm.cs b/LB_SmartVision/VisionForm.cs
index 3167069..c899473 100644
--- a/LB_SmartVision/VisionForm.cs
+++ b/LB_SmartVision/VisionForm.cs
@@ -1,6 +1,7 @@
锘縰sing HalconDotNet;
using LB_SmartVision.Forms;
using LB_SmartVision.Forms.Pages;
+using LB_SmartVision.Forms.Pages.BarcodeReaderPage;
using LB_SmartVision.Forms.Pages.CameraPage;
using LB_SmartVision.Forms.Pages.CommunicatorPage;
using LB_SmartVision.Forms.Pages.HistoricalData;
@@ -13,9 +14,12 @@
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 LB_VisionProcesses.BarcodeReaders;
+using LB_VisionProcesses.BarcodeReaders.Huayray;
using LB_VisionProcesses.Cameras;
using LB_VisionProcesses.Cameras.HikCameras;
using LB_VisionProcesses.Cameras.HRCameras;
@@ -30,6 +34,7 @@
using log4net.Config;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
+using OpenCvSharp;
using Serilog;
using Serilog.Events;
using Sunny.UI;
@@ -46,15 +51,18 @@
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
+using System.Windows.Media.Media3D;
namespace LB_SmartVision
{
public partial class VisionForm : Form
{
+ #region Variable
public string NowBom = string.Empty;
AllProcessesPage AllProcessesPage = new AllProcessesPage();
CamerasEditPage CamerasEditPage = new CamerasEditPage();
+ BarcodeReadersEditPage BarcodeReadersEditPage = new BarcodeReadersEditPage();
HistoricalDataEditPage HistoricalDataEditPage = new HistoricalDataEditPage();
CommunicatorsEditPage CommunicatorsEditPage = new CommunicatorsEditPage();
SettingEditPage SettingEditPage = new SettingEditPage();
@@ -77,6 +85,19 @@
// 绐椾綋绫荤殑鍏ㄥ眬鍙橀噺锛氭爣璁版槸鍚﹀厑璁稿垏鎹ab锛堥粯璁ゅ厑璁革級
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
+
public VisionForm()
{
InitializeComponent();
@@ -94,10 +115,15 @@
HistoricalDataEditPage.LogInfo += LogInfo;
materialTabControl.Controls.Add(new MyPage(HistoricalDataEditPage));
+
lEDLightSourceControlEditPage.LogInfo += LogInfo;
materialTabControl.Controls.Add(new MyPage(lEDLightSourceControlEditPage));
+
CamerasEditPage.LogInfo += LogInfo;
materialTabControl.Controls.Add(new MyPage(CamerasEditPage));
+
+ BarcodeReadersEditPage.LogInfo += LogInfo;
+ materialTabControl.Controls.Add(new MyPage(BarcodeReadersEditPage));
CommunicatorsEditPage.LogInfo += LogInfo;
materialTabControl.Controls.Add(new MyPage(CommunicatorsEditPage));
@@ -140,6 +166,7 @@
materialTabControl.ControlRemoved += (s, e) => UpdateOverflowComboBox();
//HOperatorSet.SetSystem("max_mem_cache", 2048);
+ startPLCThread();
}
string removeName = string.Empty;
@@ -644,6 +671,32 @@
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
@@ -743,6 +796,15 @@
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)
{
@@ -757,6 +819,14 @@
{
LogInfo("閫氳鍔犺浇澶辫触", LogInfoType.ERROR);
}
+ if (LoadAllPLCSettings(GlobalVar.allPlcSettingsPath))
+ {
+ LogInfo("PLC閫氳鍔犺浇鎴愬姛", LogInfoType.PASS);
+ }
+ else
+ {
+ LogInfo("PLC閫氳鍔犺浇澶辫触", LogInfoType.ERROR);
+ }
//鍔犺浇鐩告満
foreach (BaseCamera camera in GlobalVar.dicCameras.Values)
{
@@ -770,6 +840,20 @@
else
{
LogInfo("鐩告満鍔犺浇澶辫触", LogInfoType.ERROR);
+ }
+ //鍔犺浇璇荤爜鍣�
+ foreach (var reader in GlobalVar.dicBarcodeReaders.Values)
+ {
+ reader.Dispose();
+ }
+ GlobalVar.dicBarcodeReaders.Clear();
+ if (LoadAllBarcodeReaders(GlobalVar.allBarcodeReadersConnectionStringPath))
+ {
+ LogInfo("璇荤爜鍣ㄥ姞杞芥垚鍔�", LogInfoType.PASS);
+ }
+ else
+ {
+ LogInfo("璇荤爜鍣ㄥ姞杞藉け璐�", LogInfoType.ERROR);
}
//鍔犺浇鍏ㄥ眬鍙橀噺
IProcess.dicGlobalVars.Clear();
@@ -904,7 +988,7 @@
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);
}
}
}
@@ -936,7 +1020,110 @@
SaveAllCsv();
SaveMotionControlDatas();
SaveSerialPorts();
+ SaveAllBarcodeReaders();
+ SaveAllPLCSettings();
}
+
+
+ public bool LoadAllBarcodeReaders(string allBarcodeReadersConnectionStringPath)
+ {
+ if (!File.Exists(allBarcodeReadersConnectionStringPath))
+ {
+ Debug.WriteLine("璇荤爜鍣ㄩ厤缃枃浠朵笉瀛樺湪锛屽垱寤虹┖鏂囦欢");
+ SaveAllBarcodeReaders();
+ return true;
+ }
+ string strJson = string.Empty;
+ using (StreamReader streamReader = new StreamReader(allBarcodeReadersConnectionStringPath, Encoding.UTF8))
+ {
+ strJson = streamReader.ReadToEnd();
+ streamReader.Close();
+ }
+ GlobalVar.allBarcodeReadersConnectionString = JsonConvert.DeserializeObject<ConcurrentDictionary<string, string>>(strJson);
+ if (GlobalVar.allBarcodeReadersConnectionString == null)
+ {
+ MessageBox.Show("璇荤爜鍣ㄥ姞杞藉け璐�!", "寮傚父");
+ return false;
+ }
+ BarcodeReaderBase reader = null;
+ foreach (var ReaderConnectionString in GlobalVar.allBarcodeReadersConnectionString)
+ {
+ Enum.TryParse<BarcodeReaderBrand>(ReaderConnectionString.Value, out BarcodeReaderBrand brand);
+ switch (brand)
+ {
+ case BarcodeReaderBrand.Huayray:
+ {
+ reader = new HRBarcodeReader();
+ break;
+ }
+ default:
+ {
+ MessageBox.Show($"[{ReaderConnectionString.Key}]璇荤爜鍣ㄥ搧鐗屼笉鏀寔!", "寮傚父");
+ continue;
+ }
+ }
+ reader.SN = ReaderConnectionString.Key;
+ if (!reader.Open(ReaderConnectionString.Key))
+ {
+ LogInfo($"鍒濆鍖栬鐮佸櫒[{ReaderConnectionString.Key}]澶辫触", LogInfoType.ERROR);
+ reader.IsConnected = false;
+ reader.IsGrabbing = false;
+ }
+ else
+ {
+ LogInfo($"鍒濆鍖栬鐮佸櫒[{ReaderConnectionString.Key}]鎴愬姛", LogInfoType.PASS);
+ }
+
+ GlobalVar.dicBarcodeReaders.TryAdd(ReaderConnectionString.Key, reader);
+ }
+
+ return true;
+ }
+
+ public bool SaveAllBarcodeReaders()
+ {
+ try
+ {
+ string strJson = string.Empty;
+ GlobalVar.allBarcodeReadersConnectionString = new ConcurrentDictionary<string, string>();
+
+ foreach (var item in GlobalVar.dicBarcodeReaders)
+ {
+ string ReaderSN = item.Value.SN;
+ string ReaderBrand = item.Value.Brand.ToString();
+
+ if (string.IsNullOrEmpty(ReaderSN) || string.IsNullOrEmpty(ReaderBrand))
+ {
+ break;
+ }
+ GlobalVar.allBarcodeReadersConnectionString.TryAdd(ReaderSN, ReaderBrand);
+ }
+ var settings = new JsonSerializerSettings
+ {
+ Formatting = Formatting.Indented,
+ ContractResolver = new DefaultContractResolver
+ {
+ NamingStrategy = new CamelCaseNamingStrategy()
+ }
+ };
+
+ strJson = JsonConvert.SerializeObject(GlobalVar.allBarcodeReadersConnectionString, settings);
+ string directoryPath = Path.GetDirectoryName(GlobalVar.allBarcodeReadersConnectionStringPath);
+ if (!Directory.Exists(directoryPath))
+ {
+ try
+ {
+ Directory.CreateDirectory(directoryPath);
+ }
+ catch (Exception)
+ { }
+ }
+ File.WriteAllText(GlobalVar.allBarcodeReadersConnectionStringPath, strJson, Encoding.UTF8);
+ return true;
+ }
+ catch { return false; }
+ }
+
public bool LoadSerialPorts(string allSerialPortPath)
{
@@ -966,7 +1153,7 @@
{
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
{
@@ -1003,7 +1190,7 @@
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
{
@@ -1090,7 +1277,13 @@
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);
}
@@ -1354,7 +1547,10 @@
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; }
@@ -1427,6 +1623,47 @@
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)
@@ -1760,6 +1997,8 @@
}
}
+ bool is3D = false;
+ bool is2D = false;
private void TriggerRunMessageReceived(string name, string msg)
{
@@ -1834,6 +2073,7 @@
}
}
+
var matchedItems = GlobalVar.dicProcessSetting
.Where(item =>
{
@@ -1849,6 +2089,23 @@
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;
}
@@ -1860,294 +2117,888 @@
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 浠嶳unSettingPage鍜孡ayout涓幏鍙栨槸鍚︿繚瀛樺浘鐗�
-
- 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 浠嶳unSettingPage鍜孡ayout涓幏鍙栨槸鍚︿繚瀛樺浘鐗�
+
+ string strImageType = "jpeg";
+ bool bSaveRunImage = false;
+ bool bSaveResultImage = false;
+ long lImageQuality = 100L;
+
+ //ckbSaveRunImage
+ if (GlobalVar.ControlStates.TryGetValue("ckbSaveRunImage_CheckBox", out object oSaveRunImage))
{
- // 鍔ㄦ�佽В鏋怌omboBox鏁版嵁
- 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")}]";
-
- // 浣跨敤姝e垯琛ㄨ揪寮忔浛鎹㈡墍鏈夐潪娉曞瓧绗�
- 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
+ {
+ // 鍔ㄦ�佽В鏋怌omboBox鏁版嵁
+ 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")}]";
+
+ // 浣跨敤姝e垯琛ㄨ揪寮忔浛鎹㈡墍鏈夐潪娉曞瓧绗�
+ 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 浠嶳unSettingPage鍜孡ayout涓幏鍙栨槸鍚︿繚瀛樺浘鐗�
+
+ 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
+ {
+ // 鍔ㄦ�佽В鏋怌omboBox鏁版嵁
+ 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")}]";
+
+ // 浣跨敤姝e垯琛ㄨ揪寮忔浛鎹㈡墍鏈夐潪娉曞瓧绗�
+ 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 浠嶳unSettingPage鍜孡ayout涓幏鍙栨槸鍚︿繚瀛樺浘鐗�
+
+ // 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
+ // {
+ // // 鍔ㄦ�佽В鏋怌omboBox鏁版嵁
+ // 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")}]";
+
+ // // 浣跨敤姝e垯琛ㄨ揪寮忔浛鎹㈡墍鏈夐潪娉曞瓧绗�
+ // 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)
@@ -2164,25 +3015,31 @@
{
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();
}
@@ -2237,76 +3094,136 @@
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;
- //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;
+ GlobalVar.dicCameras[item].StartSingleGrab();
}
- TriggerRunMessageReceived(matchedItems[0].Value["瑙﹀彂閫氳"], matchedItems[0].Value["瑙﹀彂瀛楃"]);
}
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;
- //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)
{
-
- TriggerRunMessageReceived(matchedItems[0].Value["瑙﹀彂閫氳"], matchedItems[0].Value["瑙﹀彂瀛楃"]);
- Thread.Sleep(3500);
+ foreach (var item in GlobalVar.allPlcSettings[GlobalVar.currentCommunicators].Signals)
+ {
+ if (item.SignalName.Equals("瑙嗚Ready淇″彿"))
+ {
+ if (!string.IsNullOrEmpty("1"))
+ {
+ object convertedValue = ConvertValue(item.DataType, "1");
+ ((SiemensLBS7)GlobalVar.dicCommunicators[GlobalVar.currentCommunicators]).WriteSignal(item, convertedValue);
+ }
+ }
+ }
}
- //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)
@@ -2410,6 +3327,219 @@
}
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鍋滅嚎寮傚父锛佽妫�鏌ュ伐鎺ф満鍒癙LC鐨勭綉绾胯繛鎺ワ紒");
+ }
+ });
+ }
+ }
+ private void abortHeartB()
+ {
+ try
+ {
+ threadStart = false;
+ }
+ catch { }
+ }
+ private void stopPLCHB()
+ {
+ //鍙戦�佺粰PLC鐨処sRead涓篺alse;
+ 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("鍋滄閫氳寮傚父锛丳LC杩炴帴寮傚父锛岃妫�鏌ュ伐鎺ф満鍒癙LC鐨勭綉绾胯繛鎺ワ紒");
+ }
+ 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}");
+ }
+ }
+ }
+
}
}
--
Gitblit v1.9.3