From 46431fb658701489f8d5de4475b02df728c51f36 Mon Sep 17 00:00:00 2001
From: chunxiaqiu <1057644574@qq.com>
Date: 星期三, 18 三月 2026 15:48:26 +0800
Subject: [PATCH] 新增:读码器功能模块,集成华睿读码器并初步实现读码功能

---
 LB_SmartVision/VisionForm.cs |  192 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 189 insertions(+), 3 deletions(-)

diff --git a/LB_SmartVision/VisionForm.cs b/LB_SmartVision/VisionForm.cs
index f4ed8c8..8a041a1 100644
--- a/LB_SmartVision/VisionForm.cs
+++ b/LB_SmartVision/VisionForm.cs
@@ -10,6 +10,7 @@
 using LB_SmartVision.Forms.Pages.SettingPage;
 using LB_SmartVision.Forms.Pages.UserManagementPage;
 using LB_SmartVision.ProcessRun;
+using LB_SmartVision.SQL;
 using LB_SmartVision.Tool;
 using LB_SmartVisionCommon;
 using LB_SmartVisionLoginUI;
@@ -633,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))
@@ -687,6 +696,7 @@
             com_ProductName.Items.Add("鏂板");
             com_ProductName.Text = GlobalVar.strProductName;
             this.WindowState = FormWindowState.Maximized;
+            DatabaseRecordProductDataHelper.InitializeDatabase();
         }
 
         public void SaveAllSetting()
@@ -698,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)
@@ -850,7 +908,7 @@
                             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";
 
@@ -1027,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"
@@ -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