From 130802fa7ef10857db12f784956d5ea122a1eeb4 Mon Sep 17 00:00:00 2001
From: C3031 <shitiangui@lanpucloud.cn>
Date: 星期五, 30 一月 2026 09:27:32 +0800
Subject: [PATCH]
---
LB_SmartVision/VisionForm.cs | 206 +++++++++++++++++++++++++++++++++++++++++++++++++--
1 files changed, 196 insertions(+), 10 deletions(-)
diff --git a/LB_SmartVision/VisionForm.cs b/LB_SmartVision/VisionForm.cs
index e32c7f3..8a041a1 100644
--- a/LB_SmartVision/VisionForm.cs
+++ b/LB_SmartVision/VisionForm.cs
@@ -634,7 +634,15 @@
{
LogInfo("鍏ㄥ眬鍙橀噺鍔犺浇澶辫触", LogInfoType.ERROR);
}
-
+ GlobalVar.dicMotionControlData.Clear();
+ if (LoadMotionControlDatas(GlobalVar.allMotionControlDataPath))
+ {
+ LogInfo("杩愬姩鎺у埗鍙傛暟鍔犺浇鎴愬姛", LogInfoType.PASS);
+ }
+ else
+ {
+ LogInfo("杩愬姩鎺у埗鍙傛暟鍔犺浇澶辫触", LogInfoType.ERROR);
+ }
//鍔犺浇娴佺▼
GlobalVar.dicProcesses.Clear();
if (LoadAllProcess(GlobalVar.allProcessConnectionStringPath))
@@ -700,6 +708,54 @@
SaveAllProcessSetting();
SaveAllLayout();
SaveAllCsv();
+ SaveMotionControlDatas();
+ }
+ public bool LoadMotionControlDatas(string alMotionControlDataPath)
+ {
+ try
+ {
+ GlobalVar.dicMotionControlData = ConfigManager<ConcurrentDictionary<string, ConcurrentDictionary<string, RecordMotionControlData>>>.LoadConfig<ConcurrentDictionary<string, ConcurrentDictionary<string, RecordMotionControlData>>>(alMotionControlDataPath);
+ }
+ catch
+ {
+ return false;
+ }
+ return true;
+ }
+ public bool SaveMotionControlDatas()
+ {
+ try
+ {
+ ConcurrentDictionary<string, List<string>> removeCameraSN = new ConcurrentDictionary<string, List<string>>();
+ foreach (var item in GlobalVar.dicMotionControlData.Keys)
+ {
+ List<string> list = new List<string>();
+ foreach (var itemSN in GlobalVar.dicMotionControlData[item].Keys)
+ {
+ if (!GlobalVar.dicCameras.Keys.Contains(itemSN))
+ {
+ list.Add(itemSN);
+ }
+ }
+ if (list.Count > 0)
+ {
+ removeCameraSN.TryAdd(item, list);
+ }
+ }
+ foreach (var item in removeCameraSN.Keys)
+ {
+ foreach (var itemSN in removeCameraSN[item])
+ {
+ GlobalVar.dicMotionControlData[item].Keys.Remove(itemSN);
+ }
+ }
+ ConfigManager<ConcurrentDictionary<string, ConcurrentDictionary<string, RecordMotionControlData>>>.SaveConfig<ConcurrentDictionary<string, ConcurrentDictionary<string, RecordMotionControlData>>>(GlobalVar.dicMotionControlData, GlobalVar.allMotionControlDataPath);
+ }
+ catch
+ {
+ return false;
+ }
+ return true;
}
public bool LoadAllCameras(string allCamerasConnectionStringPath)
@@ -843,12 +899,8 @@
if (!string.IsNullOrEmpty(CommunicatorAddress) && CommunicatorAddress.Contains("SiemensLBS7"))
{
// 瀹氫箟姝e垯琛ㄨ揪寮忎互鎻愬彇鍗忚銆両P 鍦板潃鍜岀鍙�
- //1. \((.*?)\)锛歕(鍜� \) 鏄敤浜庡尮閰嶆嫭鍙风殑杞箟瀛楃銆�
- // (.*?) 鏄竴涓潪璐┆鐨勫尮閰嶏紝鐢ㄦ潵鍖归厤绫诲悕锛圡yProcesses.Communicators.TCPServer 鎴� MyProcesses.Communicators.UARTPort锛夈��
- //2. ([^:] +)锛氬尮閰嶅啋鍙蜂箣鍓嶇殑閮ㄥ垎锛屽嵆鍦板潃锛�127.0.0.1 鎴� COM5锛夈�傝繖閲屼娇鐢ㄤ簡[^:] 鏉ュ尮閰嶉櫎浜嗗啋鍙蜂箣澶栫殑浠绘剰瀛楃銆�
- //3. (\d +) 锛氬尮閰嶇鍙e彿锛岀‘淇濆畠鍖归厤涓�涓垨澶氫釜鏁板瓧銆�
-
- string pattern = @"^\((?<ClassName>[^)]+)\)\[(?<IP>[^]]+)\]\[(?<Slot>[^]]+)\]\[(?<CpuType>[^]]+)\]\[(?<PlcAddress>[^]]+)\]$";
+ // 鏇存柊姝e垯浠ユ敮鎸佸彲閫夌殑鏁版嵁绫诲瀷瀛楁
+ string pattern = @"^\((?<ClassName>[^)]+)\)\[(?<IP>[^]]+)\]\[(?<Slot>[^]]+)\]\[(?<CpuType>[^]]+)\]\[(?<PlcAddress>[^]]+)\](?:\[(?<DataType>[^]]+)\])?$";
Match match = Regex.Match(CommunicatorAddress, pattern);
if (match.Success)
@@ -856,8 +908,10 @@
string ClassName = match.Groups["ClassName"].Value; // "TCP"
string IP = match.Groups["IP"].Value; // "127.0.0.1"
string Slot = match.Groups["Slot"].Value; // "1111"
- string CpuType= match.Groups["CpuType"].Value;
+ string CpuType = match.Groups["CpuType"].Value;
string PlcAddress = match.Groups["PlcAddress"].Value;
+ string DataType = match.Groups["DataType"].Success ? match.Groups["DataType"].Value : "String";
+
if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(Slot) || string.IsNullOrEmpty(CpuType) || string.IsNullOrEmpty(PlcAddress))
break;
@@ -880,6 +934,7 @@
Communicator.CommunicatorConnections.Add("绔彛", Slot);
Communicator.CommunicatorConnections.Add("鍨嬪彿", CpuType);
Communicator.CommunicatorConnections.Add("鍙橀噺鍦板潃", PlcAddress);
+ Communicator.CommunicatorConnections.Add("鏁版嵁绫诲瀷", DataType);
Communicator.CommunicatorName = CommunicatorName;
if (!Communicator.Connect())
{
@@ -1030,7 +1085,6 @@
{
string strJson = string.Empty;
GlobalVar.allCommunicatorsConnectionString = new ConcurrentDictionary<string, string>();
-
foreach (var item in GlobalVar.dicCommunicators)
{
string ClassName = item.Value.GetType().FullName;// "TCP"
@@ -1040,11 +1094,14 @@
{
string CpuType = item.Value.CommunicatorConnections["鍨嬪彿"].ToString();
string PlcAddress = item.Value.CommunicatorConnections["鍙橀噺鍦板潃"].ToString();
+ string DataType = item.Value.CommunicatorConnections.Contains("鏁版嵁绫诲瀷") ?
+ item.Value.CommunicatorConnections["鏁版嵁绫诲瀷"].ToString() : "String";
+
if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(PORT) || string.IsNullOrEmpty(CpuType) || string.IsNullOrEmpty(PlcAddress))
{
break;
}
- string CommunicatorConnectionString = $"({ClassName})[{IP}][{PORT}][{CpuType}][{PlcAddress}]";
+ string CommunicatorConnectionString = $"({ClassName})[{IP}][{PORT}][{CpuType}][{PlcAddress}][{DataType}]";
GlobalVar.allCommunicatorsConnectionString.TryAdd(item.Key, CommunicatorConnectionString);
}
else
@@ -1056,6 +1113,7 @@
string CommunicatorConnectionString = $"({ClassName})[{IP}][{PORT}]";
GlobalVar.allCommunicatorsConnectionString.TryAdd(item.Key, CommunicatorConnectionString);
}
+ GlobalVar.dicCommunicators[item.Key].ClassName = ClassName;
}
var settings = new JsonSerializerSettings
{
@@ -1080,6 +1138,11 @@
{ }
}
File.WriteAllText(GlobalVar.allCommunicatorsConnectionStringPath, strJson, Encoding.UTF8);
+ foreach (var item in GlobalVar.dicCommunicators)
+ {
+ string ClassName = item.Value.GetType().FullName;// "TCP"
+ }
+ ConfigManager<ObservableConcurrentDictionary<string, BaseCommunicator>>.SaveConfig<ObservableConcurrentDictionary<string, BaseCommunicator>>(GlobalVar.dicCommunicators, GlobalVar.strApplicationPath + "\\鎵�鏈変骇鍝乗\" + GlobalVar.strProductName + "\\dicCommunicators.json");
return true;
}
catch { return false; }
@@ -1339,6 +1402,7 @@
GlobalVar.strProductName = com_ProductName.SelectedItem?.ToString();
foreach (BaseCamera camera in GlobalVar.dicCameras.Values)
{
+ camera.TriggerRunMessageReceived -= TriggerRunMessageReceived;
camera.Dispose();
}
GlobalVar.dicCameras.Clear();
@@ -1652,6 +1716,128 @@
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();
+ }
+
+ /// <summary>
+ /// 鏉�杩涚▼閫昏緫
+ /// </summary>
+ private void KillAllTargetProcesses()
+ {
+ try
+ {
+ // 1. 鑾峰彇褰撳墠杩涚▼ID锛岄伩鍏嶆潃姝昏嚜宸�
+ int currentProcessId = Process.GetCurrentProcess().Id;
+ // 2. 妯$硦鍖归厤杩涚▼鍚嶏紙蹇界暐澶у皬鍐欙級锛岃鐩杤shost/鍚庣紑绛夋儏鍐�
+ var targetProcesses = Process.GetProcesses()
+ .Where(p =>
+ p.ProcessName.IndexOf("LB_SmartVision", StringComparison.OrdinalIgnoreCase) >= 0
+ && p.Id == currentProcessId);
+
+ foreach (var p in targetProcesses)
+ {
+ try
+ {
+ if (!p.HasExited)
+ {
+ // 鍏堝皾璇曚紭闆呭叧闂璚inForm绋嬪簭锛堟瘮鐩存帴Kill鏇村弸濂斤級
+ p.CloseMainWindow();
+ // 绛夊緟500ms锛岀湅鏄惁姝e父閫�鍑�
+ if (!p.WaitForExit(500))
+ {
+ p.Kill(); // 寮哄埗鏉�姝�
+ p.WaitForExit(1000); // 绛夊緟杩涚▼瀹屽叏缁堟
+ }
+ }
+ p.Dispose(); // 閲婃斁杩涚▼璧勬簮锛岄伩鍏嶅彞鏌勬硠婕�
+ }
+ catch (Exception ex)
+ {
+ // 璁板綍寮傚父浣嗕笉涓柇锛屾瘮濡傛潈闄愪笉瓒崇殑鎯呭喌
+ System.IO.File.AppendAllText("kill_process_log.txt", $"[{DateTime.Now}] 鏉�姝昏繘绋嬪け璐�: {ex.Message}\r\n");
+ }
+ }
+
+ // 棰濆寤惰繜锛岀‘淇濇枃浠跺彞鏌勫畬鍏ㄩ噴鏀�
+ Thread.Sleep(1000);
+ }
+ catch (Exception)
+ {
+ // 闈欓粯澶辫触锛屼笉褰卞搷绋嬪簭閫�鍑�
+ }
+ }
+
+ 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)
+ {
+ return;
+ }
+ TriggerRunMessageReceived(matchedItems[0].Value["瑙﹀彂閫氳"], matchedItems[0].Value["瑙﹀彂瀛楃"]);
+ }
+
+ private void btn_RunContinuously_Click(object sender, EventArgs e)
+ {
+ if (btn_RunContinuously.Text.Equals("杩炵画杩愯"))
+ {
+ btn_RunContinuously.Text = "鏆傚仠杩愯";
+ Task.Factory.StartNew(() =>
+ {
+ 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("鏆傚仠杩愯"))
+ {
+
+ TriggerRunMessageReceived(matchedItems[0].Value["瑙﹀彂閫氳"], matchedItems[0].Value["瑙﹀彂瀛楃"]);
+ Thread.Sleep(500);
+ }
+ //this.Invoke(() =>
+ //{
+ //});
+ });
+ }
+ else if (btn_RunContinuously.Text.Equals("鏆傚仠杩愯"))
+ {
+ btn_RunContinuously.Text = "杩炵画杩愯";
+ Thread.Sleep(100);
+ }
+
}
}
}
--
Gitblit v1.9.3