C3204
2025-12-24 cb61a204c29e9b14d0b3f4e677d8489fc40cba51
LB_SmartVision/VisionForm.cs
@@ -519,71 +519,97 @@
            }
            LB_SmartVision.Tool.Tool.ReadStringConfig("User ID", out string User_ID);
            LB_SmartVision.Tool.Tool.ReadStringConfig("Password", out string Password);
            GlobalVar.strProductName = DateBaseName;
            //加载通讯
            foreach (BaseCommunicator com in GlobalVar.dicCommunicators.Values)
            {
                com.Disconnect();
            }
            GlobalVar.dicCommunicators.Clear();
            if (LoadAllCommunicators(GlobalVar.allCommunicatorsConnectionStringPath))
            {
                LogInfo("通讯加载成功", LogInfoType.PASS);
            }
            else
            {
                LogInfo("通讯加载失败", LogInfoType.ERROR);
            }
            //加载相机
            foreach (BaseCamera camera in GlobalVar.dicCameras.Values)
            {
                camera.Dispose();
            }
            GlobalVar.dicCameras.Clear();
            if (LoadAllCameras(GlobalVar.allCamerasConnectionStringPath))
            {
                LogInfo("相机加载成功", LogInfoType.PASS);
            }
            else
            {
                LogInfo("相机加载失败", LogInfoType.ERROR);
            //必须先加载相机和通讯端口,因为流程加载过程中会用到相机和通讯口
            }
            //加载全局变量
            IProcess.dicGlobalVars.Clear();
            if (LoadAllProcessVars(GlobalVar.allProcessVarsPath))
            {
                LogInfo("全局变量加载成功", LogInfoType.PASS);
            }
            else
            {
                LogInfo("全局变量加载失败", LogInfoType.ERROR);
            }
            //加载流程
            GlobalVar.dicProcesses.Clear();
            if (LoadAllProcess(GlobalVar.allProcessConnectionStringPath))
            {
                LogInfo("流程加载成功", LogInfoType.PASS);
            }
            else
            {
                LogInfo("流程加载失败", LogInfoType.ERROR);
            }
            //加载触发设置
            if (LoadAllProcessSetting(GlobalVar.allProcessSettingStringPath))
            {
                LogInfo("流程设置加载成功", LogInfoType.PASS);
            }
            else
            {
                LogInfo("流程设置加载失败", LogInfoType.ERROR);
            }
            //加载流程布局
            GlobalVar.dicLayout.Clear();
            if (LoadAllLayout(GlobalVar.allLayoutPath))
            {
                LogInfo("流程布局加载成功", LogInfoType.PASS);
            }
            else
            {
                LogInfo("流程布局加载失败", LogInfoType.ERROR);
            }
            //加载流程表格
            GlobalVar.dicCsvSetting.Clear();
            if (LoadAllCsv(GlobalVar.allCsvPath))
            {
                LogInfo("流程表格加载成功", LogInfoType.PASS);
            }
            else
            {
                LogInfo("流程表格加载失败", LogInfoType.ERROR);
            }
            //显示所有产品
            com_ProductName.Items.Clear();
            LB_SmartVision.Tool.Tool.ReadStringConfig("产品列表", out string Products);
            List<string> lstProduct = (Products.Split(',')).ToList();
            foreach (string DatabaseName in lstProduct)
            {
                com_ProductName.Items.Add(DatabaseName);
            }
            com_ProductName.Items.Add("新增");
            com_ProductName.Text = GlobalVar.strProductName;
        }
@@ -614,7 +640,6 @@
                strJson = streamReader.ReadToEnd();
                streamReader.Close();
            }
            GlobalVar.allCamerasConnectionString = JsonConvert.DeserializeObject<ConcurrentDictionary<string, string>>(strJson);
            if (GlobalVar.allCamerasConnectionString == null)
            {
@@ -628,16 +653,21 @@
                switch (brand)
                {
                    case CameraBrand.HRCamera:
                        {
                        camera = new HRCamera();
                        break;
                        }
                    case CameraBrand.LBCamera:
                        {
                        //camera = new LBCamera();
                        break;
                        }
                    default:
                        {
                        MessageBox.Show($"[{CameraConnectionString.Key}]品牌不支持!", "异常");
                        continue;
                }
                }
                camera.SN = CameraConnectionString.Key;
                if (!camera.InitDevice(CameraConnectionString.Key, this.Handle))
                {
@@ -665,8 +695,9 @@
                    string CameraBrand = item.Value.Brand.ToString();//"1111"
                    if (string.IsNullOrEmpty(CameraSN) || string.IsNullOrEmpty(CameraBrand))
                    {
                        break;
                    }
                    GlobalVar.allCamerasConnectionString.TryAdd(CameraSN, CameraBrand);
                }
                var settings = new JsonSerializerSettings
@@ -776,9 +807,13 @@
                        Communicator.CommunicatorConnections.Add("端口", PORT);
                        Communicator.CommunicatorName = CommunicatorName;
                        if (!Communicator.Connect())
                        {
                            LogInfo($"初始化通讯口[{CommunicatorName}]失败,原因是{Communicator.Msg}", LogInfoType.ERROR);
                        }
                        else
                        {
                            LogInfo($"初始化通讯口[{CommunicatorName}]成功", LogInfoType.PASS);
                        }
                        GlobalVar.dicCommunicators.TryAdd(CommunicatorName, Communicator);
                    }
                    else
@@ -810,7 +845,9 @@
                        string PORT = match.Groups[3].Value;        // "1111"
                        if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(PORT))
                        {
                            break;
                        }
                        //利用反射创建实例
                        Type type = IProcess.GetExecutingAssembly().GetType(ClassName);
@@ -831,10 +868,13 @@
                        Communicator.CommunicatorConnections.Add("端口", PORT);
                        Communicator.CommunicatorName = CommunicatorName;
                        if (!Communicator.Connect())
                        {
                            LogInfo($"初始化通讯口[{CommunicatorName}]失败,原因是{Communicator.Msg}", LogInfoType.ERROR);
                        }
                        else
                        {
                            LogInfo($"初始化通讯口[{CommunicatorName}]成功", LogInfoType.PASS);
                        }
                        GlobalVar.dicCommunicators.TryAdd(CommunicatorName, Communicator);
                    }
                    else
@@ -863,12 +903,12 @@
                    string PORT = item.Value.CommunicatorConnections["端口"].ToString();//"1111"
                    if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(PORT))
                    {
                        break;
                    }
                    string CommunicatorConnectionString = $"({ClassName})[{IP}][{PORT}]";
                    GlobalVar.allCommunicatorsConnectionString.TryAdd(item.Key, CommunicatorConnectionString);
                }
                var settings = new JsonSerializerSettings
                {
                    Formatting = Formatting.Indented,
@@ -919,17 +959,19 @@
                List<string> lstProcessName = JsonConvert.DeserializeObject<List<string>>(strJson);
                if (lstProcessName == null)
                {
                    return false;
                }
                // 使用方式
                var sortedKeys = lstProcessName
                    .OrderBy(k => k, new NaturalStringComparer())
                    .ToList();
                GlobalVar.dicProcesses.Clear();
                foreach (var ProcessName in sortedKeys)
                {
                    GlobalVar.dicProcesses.TryAdd(ProcessName
                        , new ProcessRunBll(ProcessName, GlobalVar.dicCameras, GlobalVar.dicCommunicators));
                }
                return true;
            }
            catch { return false; }
@@ -966,7 +1008,9 @@
                foreach (var process in GlobalVar.dicProcesses.Values)
                {
                    if (!process.Save(out string msg))
                    {
                        LogInfo($"流程[{process.Name}]保存失败,原因:{msg}", LogInfoType.NOSHOW);
                    }
                }
                try
@@ -1440,6 +1484,27 @@
                }
            });
        }
        private void VisionForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            SaveAllSetting();
            if (MessageBox.Show("是否关闭软件?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK)//
            {
                e.Cancel = true;
                return;
            }
            //关闭窗体释放资源
            AsyncLogHelper.Dispose();
            foreach (BaseCamera camera in GlobalVar.dicCameras.Values)
            {
                camera.Dispose();
            }
            foreach (BaseCommunicator communicator in GlobalVar.dicCommunicators.Values)
            {
                communicator.Disconnect();
            }
            FormClosing -= VisionForm_FormClosing;
        }
    }
}