From 4d6c9ad3c9714d5f8520c6f52c3ed6ecb75b76b8 Mon Sep 17 00:00:00 2001
From: C3204 <zhengyabo@lanpucloud.cn>
Date: 星期三, 01 四月 2026 10:53:13 +0800
Subject: [PATCH] 集成曹工添加的读码器功能以及工具集

---
 LB_SmartVision/VisionForm.cs |  128 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 128 insertions(+), 0 deletions(-)

diff --git a/LB_SmartVision/VisionForm.cs b/LB_SmartVision/VisionForm.cs
index 3167069..77e0d8f 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;
@@ -16,6 +17,8 @@
 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;
@@ -51,10 +54,12 @@
 {
     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 +82,8 @@
         // 绐椾綋绫荤殑鍏ㄥ眬鍙橀噺锛氭爣璁版槸鍚﹀厑璁稿垏鎹ab锛堥粯璁ゅ厑璁革級
         private bool _isTabSwitchAllowed = true;
 
+        #endregion
+
         public VisionForm()
         {
             InitializeComponent();
@@ -94,10 +101,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));
@@ -771,6 +783,20 @@
             {
                 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();
             if (LoadAllProcessVars(GlobalVar.allProcessVarsPath))
@@ -936,8 +962,110 @@
             SaveAllCsv();
             SaveMotionControlDatas();
             SaveSerialPorts();
+            SaveAllBarcodeReaders();
         }
 
+
+        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)
         {
             try

--
Gitblit v1.9.3