C3032
2026-03-20 19bf97fd8ec4f435e9111d72969c4ebf00d97991
LB_SmartVision/VisionForm.cs
@@ -1,6 +1,7 @@
using 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;
@@ -18,6 +19,8 @@
using LB_VisionProcesses.Cameras;
using LB_VisionProcesses.Cameras.HRCameras;
using LB_VisionProcesses.Cameras.LBCameras;
using LB_VisionProcesses.BarcodeReaders;
using LB_VisionProcesses.BarcodeReaders.Huayray;
using LB_VisionProcesses.Communicators;
using LB_VisionProcesses.Communicators.SiemensS7;
using LB_VisionProcesses.Communicators.TCom;
@@ -46,6 +49,7 @@
    {
        AllProcessesPage AllProcessesPages = new AllProcessesPage();
        CamerasEditPage CamerasEditPage = new CamerasEditPage();
        BarcodeReadersEditPage BarcodeReadersEditPage = new BarcodeReadersEditPage();
        HistoricalDataEditPage HistoricalDataEditPage = new HistoricalDataEditPage();
        CommunicatorsEditPage CommunicatorsEditPage = new CommunicatorsEditPage();
        SettingEditPage SettingEditPage = new SettingEditPage();
@@ -68,6 +72,7 @@
            GlobalVar.dicCommunicators.DictionaryChanged += CommunicatorsChanged;
            GlobalVar.dicCameras.DictionaryChanged += CamerasChanged;
            GlobalVar.dicBarcodeReaders.DictionaryChanged += BarcodeReadersChanged;
            GlobalVar.dicProcesses.DictionaryChanged += ProcessRunBllChanged;
            //最开始就清空所有Tab页
@@ -80,6 +85,9 @@
            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));
@@ -573,6 +581,26 @@
            catch { }
        }
        private void BarcodeReadersChanged(object sender, DictionaryChangedEventArgs<string, BarcodeReaderBase> e)
        {
            try
            {
                switch (e.ChangeType)
                {
                    case DictionaryChangeType.Added:
                        LogInfo($"读码器[{e.NewValue.SN}]加载成功", LogInfoType.INFO);
                        e.NewValue.SN = e.NewKey;
                        break;
                    case DictionaryChangeType.Removed:
                        if (e.OldValue != null)
                            e.OldValue.Dispose();
                        LogInfo($"读码器[{e.OldValue.SN}]已移除", LogInfoType.INFO);
                        break;
                }
            }
            catch { }
        }
        private void VisionForm_Load(object sender, EventArgs e)
        {
            XmlConfigurator.Configure(new System.IO.FileInfo("log4net.config"));
@@ -623,6 +651,21 @@
            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();
@@ -705,6 +748,7 @@
            SaveAllProcessVars();
            SaveAllCommunicators();
            SaveAllCameras();
            SaveAllBarcodeReaders();
            SaveAllProcessSetting();
            SaveAllLayout();
            SaveAllCsv();
@@ -862,6 +906,105 @@
            catch { return false; }
        }
        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 LoadAllCommunicators(string allCommunicatorsConnectionStringPath)
        {