轮胎外观检测添加思谋语义分割模型检测工具
C3204
2026-04-01 eda17eddf88e6108cadbf8dcef5c2195c1a7b708
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;
@@ -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 @@
        // 窗体类的全局变量:标记是否允许切换Tab(默认允许)
        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