| | |
| | | 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; |
| | |
| | | Assembly_LB_VisionProcessesDll = Assembly.Load(Assembly_LB_VisionProcessesBytes); |
| | | |
| | | GlobalVar.dicCommunicators.DictionaryChanged += CommunicatorsChanged; |
| | | GlobalVar.dicCameras.DictionaryChanged += CamerasChanged; |
| | | GlobalVar.dicProcesses.DictionaryChanged += ProcessRunBllChanged; |
| | | |
| | | //最开始就清空所有Tab页 |
| | |
| | | |
| | | private void CommunicatorsChanged(object? sender, DictionaryChangedEventArgs<string, BaseCommunicator> e) |
| | | { |
| | | |
| | | try |
| | | { |
| | | switch (e.ChangeType) |
| | | { |
| | | case DictionaryChangeType.Added: |
| | | e.NewValue.TriggerRunMessageReceived += TriggerRunMessageReceived; |
| | | LogInfo($"通讯口[{e.NewValue.CommunicatorName}]加载触发通讯", LogInfoType.INFO); |
| | | e.NewValue.CommunicatorName = e.NewKey; |
| | | break; |
| | | case DictionaryChangeType.Renamed: |
| | | string OldCommunicatorName = e.OldKey; |
| | | string NewCommunicatorName = e.NewKey; |
| | | LogInfo(string.Format("重命名通讯口名[{0}]修改为[{1}]", OldCommunicatorName, NewCommunicatorName), LogInfoType.INFO); |
| | | e.NewValue.CommunicatorName = NewCommunicatorName; |
| | | break; |
| | | case DictionaryChangeType.Removed: |
| | | if (e.OldValue != null && e.OldValue is BaseCommunicator) |
| | | e.OldValue.Disconnect(); |
| | | e.OldValue.TriggerRunMessageReceived -= TriggerRunMessageReceived; |
| | | LogInfo($"通讯口[{e.OldValue.CommunicatorName}]移除触发通讯", LogInfoType.INFO); |
| | | break; |
| | | } |
| | | } |
| | | catch { } |
| | | } |
| | | |
| | | private void LogInfo(string strLog, LogInfoType infoType) |
| | |
| | | } |
| | | } |
| | | |
| | | private void CamerasChanged(object sender, DictionaryChangedEventArgs<string, BaseCamera> e) |
| | | { |
| | | try |
| | | { |
| | | switch (e.ChangeType) |
| | | { |
| | | case DictionaryChangeType.Added: |
| | | e.NewValue.TriggerRunMessageReceived += TriggerRunMessageReceived; |
| | | LogInfo($"相机[{e.NewValue.SN}]加载触发通讯", LogInfoType.INFO); |
| | | e.NewValue.SN = e.NewKey; |
| | | break; |
| | | case DictionaryChangeType.Removed: |
| | | if (e.OldValue != null && e.OldValue is BaseCommunicator) |
| | | e.OldValue.CloseDevice(); |
| | | e.OldValue.TriggerRunMessageReceived -= TriggerRunMessageReceived; |
| | | LogInfo($"相机[{e.OldValue.SN}]移除触发通讯", LogInfoType.INFO); |
| | | break; |
| | | } |
| | | } |
| | | catch { } |
| | | } |
| | | |
| | | private void VisionForm_Load(object sender, EventArgs e) |
| | | { |
| | | XmlConfigurator.Configure(new System.IO.FileInfo("log4net.config")); |
| | |
| | | { |
| | | LogInfo("全局变量加载失败", LogInfoType.ERROR); |
| | | } |
| | | |
| | | GlobalVar.dicMotionControlData.Clear(); |
| | | if (LoadMotionControlDatas(GlobalVar.allMotionControlDataPath)) |
| | | { |
| | | LogInfo("运动控制参数加载成功", LogInfoType.PASS); |
| | | } |
| | | else |
| | | { |
| | | LogInfo("运动控制参数加载失败", LogInfoType.ERROR); |
| | | } |
| | | //加载流程 |
| | | GlobalVar.dicProcesses.Clear(); |
| | | if (LoadAllProcess(GlobalVar.allProcessConnectionStringPath)) |
| | |
| | | com_ProductName.Items.Add("新增"); |
| | | com_ProductName.Text = GlobalVar.strProductName; |
| | | this.WindowState = FormWindowState.Maximized; |
| | | DatabaseRecordProductDataHelper.InitializeDatabase(); |
| | | } |
| | | |
| | | public void SaveAllSetting() |
| | |
| | | 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) |
| | |
| | | if (!string.IsNullOrEmpty(CommunicatorAddress) && CommunicatorAddress.Contains("SiemensLBS7")) |
| | | { |
| | | // 定义正则表达式以提取协议、IP 地址和端口 |
| | | //1. \((.*?)\):\(和 \) 是用于匹配括号的转义字符。 |
| | | // (.*?) 是一个非贪婪的匹配,用来匹配类名(MyProcesses.Communicators.TCPServer 或 MyProcesses.Communicators.UARTPort)。 |
| | | //2. ([^:] +):匹配冒号之前的部分,即地址(127.0.0.1 或 COM5)。这里使用了[^:] 来匹配除了冒号之外的任意字符。 |
| | | //3. (\d +) :匹配端口号,确保它匹配一个或多个数字。 |
| | | |
| | | string pattern = @"^\((?<ClassName>[^)]+)\)\[(?<IP>[^]]+)\]\[(?<Slot>[^]]+)\]\[(?<CpuType>[^]]+)\]\[(?<PlcAddress>[^]]+)\]$"; |
| | | // 更新正则以支持可选的数据类型字段 |
| | | string pattern = @"^\((?<ClassName>[^)]+)\)\[(?<IP>[^]]+)\]\[(?<Slot>[^]]+)\]\[(?<CpuType>[^]]+)\]\[(?<PlcAddress>[^]]+)\](?:\[(?<DataType>[^]]+)\])?$"; |
| | | Match match = Regex.Match(CommunicatorAddress, pattern); |
| | | |
| | | if (match.Success) |
| | |
| | | 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"; |
| | | |
| | | if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(Slot) || string.IsNullOrEmpty(CpuType) || string.IsNullOrEmpty(PlcAddress)) |
| | | break; |
| | | |
| | |
| | | Communicator.CommunicatorConnections.Add("端口", Slot); |
| | | Communicator.CommunicatorConnections.Add("型号", CpuType); |
| | | Communicator.CommunicatorConnections.Add("变量地址", PlcAddress); |
| | | Communicator.CommunicatorConnections.Add("数据类型", DataType); |
| | | Communicator.CommunicatorName = CommunicatorName; |
| | | if (!Communicator.Connect()) |
| | | { |
| | |
| | | { |
| | | string strJson = string.Empty; |
| | | GlobalVar.allCommunicatorsConnectionString = new ConcurrentDictionary<string, string>(); |
| | | |
| | | foreach (var item in GlobalVar.dicCommunicators) |
| | | { |
| | | string ClassName = item.Value.GetType().FullName;// "TCP" |
| | |
| | | { |
| | | string CpuType = item.Value.CommunicatorConnections["型号"].ToString(); |
| | | string PlcAddress = item.Value.CommunicatorConnections["变量地址"].ToString(); |
| | | string DataType = item.Value.CommunicatorConnections.Contains("数据类型") ? |
| | | item.Value.CommunicatorConnections["数据类型"].ToString() : "String"; |
| | | |
| | | if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(PORT) || string.IsNullOrEmpty(CpuType) || string.IsNullOrEmpty(PlcAddress)) |
| | | { |
| | | break; |
| | | } |
| | | string CommunicatorConnectionString = $"({ClassName})[{IP}][{PORT}][{CpuType}][{PlcAddress}]"; |
| | | string CommunicatorConnectionString = $"({ClassName})[{IP}][{PORT}][{CpuType}][{PlcAddress}][{DataType}]"; |
| | | GlobalVar.allCommunicatorsConnectionString.TryAdd(item.Key, CommunicatorConnectionString); |
| | | } |
| | | else |
| | |
| | | string CommunicatorConnectionString = $"({ClassName})[{IP}][{PORT}]"; |
| | | GlobalVar.allCommunicatorsConnectionString.TryAdd(item.Key, CommunicatorConnectionString); |
| | | } |
| | | GlobalVar.dicCommunicators[item.Key].ClassName = ClassName; |
| | | } |
| | | var settings = new JsonSerializerSettings |
| | | { |
| | |
| | | { } |
| | | } |
| | | 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; } |