| | |
| | | using LB_VisionProcesses.Cameras; |
| | | using LB_VisionProcesses.Cameras.HRCameras; |
| | | using LB_VisionProcesses.Communicators; |
| | | using LB_VisionProcesses.Communicators.SiemensS7; |
| | | using LB_VisionProcesses.Communicators.TCom; |
| | | using LB_VisionProcesses.Forms; |
| | | using log4net.Config; |
| | |
| | | { |
| | | AllProcessesPage AllProcessesPages = new AllProcessesPage(); |
| | | CamerasEditPage CamerasEditPage = new CamerasEditPage(); |
| | | HistoricalDataEditPage HistoricalDataEditPage = new HistoricalDataEditPage(); |
| | | HistoricalDataEditPage HistoricalDataEditPage = new HistoricalDataEditPage(); |
| | | CommunicatorsEditPage CommunicatorsEditPage = new CommunicatorsEditPage(); |
| | | SettingEditPage SettingEditPage = new SettingEditPage(); |
| | | MESEditPage MESEditPage = new MESEditPage(); |
| | |
| | | { |
| | | string CommunicatorName = CommunicatorConnectionString.Key; |
| | | string CommunicatorAddress = CommunicatorConnectionString.Value; |
| | | |
| | | // 定义正则表达式以提取协议、IP 地址和端口 |
| | | //1. \((.*?)\):\(和 \) 是用于匹配括号的转义字符。 |
| | | // (.*?) 是一个非贪婪的匹配,用来匹配类名(MyProcesses.Communicators.TCPServer 或 MyProcesses.Communicators.UARTPort)。 |
| | | //2. ([^:] +):匹配冒号之前的部分,即地址(127.0.0.1 或 COM5)。这里使用了[^:] 来匹配除了冒号之外的任意字符。 |
| | | //3. (\d +) :匹配端口号,确保它匹配一个或多个数字。 |
| | | |
| | | string pattern = @"^\((?<ClassName>[^)]+)\)\[(?<IP>[^]]+)\]\[(?<PORT>[^]]+)\]$"; |
| | | Match match = Regex.Match(CommunicatorAddress, pattern); |
| | | |
| | | if (match.Success) |
| | | if (!string.IsNullOrEmpty(CommunicatorAddress) && CommunicatorAddress.Contains("SiemensLBS7")) |
| | | { |
| | | string ClassName = match.Groups["ClassName"].Value; // "TCP" |
| | | string IP = match.Groups["IP"].Value; // "127.0.0.1" |
| | | string PORT = match.Groups["PORT"].Value; // "1111" |
| | | // 定义正则表达式以提取协议、IP 地址和端口 |
| | | //1. \((.*?)\):\(和 \) 是用于匹配括号的转义字符。 |
| | | // (.*?) 是一个非贪婪的匹配,用来匹配类名(MyProcesses.Communicators.TCPServer 或 MyProcesses.Communicators.UARTPort)。 |
| | | //2. ([^:] +):匹配冒号之前的部分,即地址(127.0.0.1 或 COM5)。这里使用了[^:] 来匹配除了冒号之外的任意字符。 |
| | | //3. (\d +) :匹配端口号,确保它匹配一个或多个数字。 |
| | | |
| | | if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(PORT)) |
| | | break; |
| | | string pattern = @"^\((?<ClassName>[^)]+)\)\[(?<IP>[^]]+)\]\[(?<Slot>[^]]+)\]\[(?<CpuType>[^]]+)\]\[(?<PlcAddress>[^]]+)\]$"; |
| | | Match match = Regex.Match(CommunicatorAddress, pattern); |
| | | |
| | | //利用反射创建实例 |
| | | Type type = IProcess.GetExecutingAssembly().GetType(ClassName); |
| | | if (type == null) |
| | | if (match.Success) |
| | | { |
| | | Debug.WriteLine("Class not found."); |
| | | return false; |
| | | } |
| | | var Communicator = Activator.CreateInstance(type, CommunicatorName) as BaseCommunicator; |
| | | 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 PlcAddress = match.Groups["PlcAddress"].Value; |
| | | if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(Slot) || string.IsNullOrEmpty(CpuType) || string.IsNullOrEmpty(PlcAddress)) |
| | | break; |
| | | |
| | | if (Communicator == null) |
| | | { |
| | | Debug.WriteLine("BaseCommunicator not found."); |
| | | return false; |
| | | } |
| | | //利用反射创建实例 |
| | | Type type = IProcess.GetExecutingAssembly().GetType(ClassName); |
| | | if (type == null) |
| | | { |
| | | Debug.WriteLine("Class not found."); |
| | | return false; |
| | | } |
| | | var Communicator = Activator.CreateInstance(type, CommunicatorName) as BaseCommunicator; |
| | | |
| | | //TCP客户端最后再连接 |
| | | if (Communicator is TCPClient) |
| | | { |
| | | clientsCommunicatorsConnectionString.TryAdd(CommunicatorConnectionString.Key, CommunicatorConnectionString.Value); |
| | | continue; |
| | | } |
| | | if (Communicator == null) |
| | | { |
| | | Debug.WriteLine("BaseCommunicator not found."); |
| | | return false; |
| | | } |
| | | |
| | | Communicator.CommunicatorConnections.Add("地址", IP); |
| | | Communicator.CommunicatorConnections.Add("端口", PORT); |
| | | Communicator.CommunicatorName = CommunicatorName; |
| | | if (!Communicator.Connect()) |
| | | { |
| | | LogInfo($"初始化通讯口[{CommunicatorName}]失败,原因是{Communicator.Msg}", LogInfoType.ERROR); |
| | | Communicator.CommunicatorConnections.Add("地址", IP); |
| | | Communicator.CommunicatorConnections.Add("端口", Slot); |
| | | Communicator.CommunicatorConnections.Add("型号", CpuType); |
| | | Communicator.CommunicatorConnections.Add("变量地址", PlcAddress); |
| | | Communicator.CommunicatorName = CommunicatorName; |
| | | if (!Communicator.Connect()) |
| | | { |
| | | LogInfo($"初始化通讯口[{CommunicatorName}]失败,原因是{Communicator.Msg}", LogInfoType.ERROR); |
| | | } |
| | | else |
| | | { |
| | | LogInfo($"初始化通讯口[{CommunicatorName}]成功", LogInfoType.PASS); |
| | | } |
| | | GlobalVar.dicCommunicators.TryAdd(CommunicatorName, Communicator); |
| | | } |
| | | else |
| | | { |
| | | LogInfo($"初始化通讯口[{CommunicatorName}]成功", LogInfoType.PASS); |
| | | Debug.WriteLine("No match found."); |
| | | } |
| | | GlobalVar.dicCommunicators.TryAdd(CommunicatorName, Communicator); |
| | | } |
| | | else |
| | | { |
| | | Debug.WriteLine("No match found."); |
| | | } |
| | | // 定义正则表达式以提取协议、IP 地址和端口 |
| | | //1. \((.*?)\):\(和 \) 是用于匹配括号的转义字符。 |
| | | // (.*?) 是一个非贪婪的匹配,用来匹配类名(MyProcesses.Communicators.TCPServer 或 MyProcesses.Communicators.UARTPort)。 |
| | | //2. ([^:] +):匹配冒号之前的部分,即地址(127.0.0.1 或 COM5)。这里使用了[^:] 来匹配除了冒号之外的任意字符。 |
| | | //3. (\d +) :匹配端口号,确保它匹配一个或多个数字。 |
| | | |
| | | string pattern = @"^\((?<ClassName>[^)]+)\)\[(?<IP>[^]]+)\]\[(?<PORT>[^]]+)\]$"; |
| | | 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 PORT = match.Groups["PORT"].Value; // "1111" |
| | | |
| | | if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(PORT)) |
| | | break; |
| | | |
| | | //利用反射创建实例 |
| | | Type type = IProcess.GetExecutingAssembly().GetType(ClassName); |
| | | if (type == null) |
| | | { |
| | | Debug.WriteLine("Class not found."); |
| | | return false; |
| | | } |
| | | var Communicator = Activator.CreateInstance(type, CommunicatorName) as BaseCommunicator; |
| | | |
| | | if (Communicator == null) |
| | | { |
| | | Debug.WriteLine("BaseCommunicator not found."); |
| | | return false; |
| | | } |
| | | |
| | | //TCP客户端最后再连接 |
| | | if (Communicator is TCPClient) |
| | | { |
| | | clientsCommunicatorsConnectionString.TryAdd(CommunicatorConnectionString.Key, CommunicatorConnectionString.Value); |
| | | continue; |
| | | } |
| | | |
| | | Communicator.CommunicatorConnections.Add("地址", IP); |
| | | 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 |
| | | { |
| | | Debug.WriteLine("No match found."); |
| | | } |
| | | } |
| | | } |
| | | |
| | | //TCP客户端最后连接 |
| | |
| | | string ClassName = item.Value.GetType().FullName;// "TCP" |
| | | string IP = item.Value.CommunicatorConnections["地址"].ToString();//"127.0.0.1" |
| | | string PORT = item.Value.CommunicatorConnections["端口"].ToString();//"1111" |
| | | |
| | | if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(PORT)) |
| | | if (!string.IsNullOrEmpty(ClassName) && ClassName.Contains("SiemensLBS7")) |
| | | { |
| | | break; |
| | | string CpuType = item.Value.CommunicatorConnections["型号"].ToString(); |
| | | string PlcAddress = item.Value.CommunicatorConnections["变量地址"].ToString(); |
| | | if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(PORT) || string.IsNullOrEmpty(CpuType) || string.IsNullOrEmpty(PlcAddress)) |
| | | { |
| | | break; |
| | | } |
| | | string CommunicatorConnectionString = $"({ClassName})[{IP}][{PORT}][{CpuType}][{PlcAddress}]"; |
| | | GlobalVar.allCommunicatorsConnectionString.TryAdd(item.Key, CommunicatorConnectionString); |
| | | } |
| | | string CommunicatorConnectionString = $"({ClassName})[{IP}][{PORT}]"; |
| | | GlobalVar.allCommunicatorsConnectionString.TryAdd(item.Key, CommunicatorConnectionString); |
| | | else |
| | | { |
| | | 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 |
| | | { |