From b09b3c33a36929412d1266d56b9db3b38db89556 Mon Sep 17 00:00:00 2001 From: wenjy Date: Tue, 30 Jan 2024 11:14:39 +0800 Subject: [PATCH] =?UTF-8?q?change=20-=20PLC=20=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SlnMesnac.Plc/PlcPool.cs | 79 +++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 41 deletions(-) diff --git a/SlnMesnac.Plc/PlcPool.cs b/SlnMesnac.Plc/PlcPool.cs index 1b523fd..476857b 100644 --- a/SlnMesnac.Plc/PlcPool.cs +++ b/SlnMesnac.Plc/PlcPool.cs @@ -39,26 +39,16 @@ namespace SlnMesnac.Plc } _logger.LogInformation("HslCommunication 11.8.1.0激活成功"); - try + if (_appConfig.plcConfig != null) { - if (_appConfig.plcConfig != null) - { - Task.Run(() => - { - foreach (var item in _appConfig.plcConfig) - { - AddPlc(item.plcType, item.plcIp, item.plcPort, item.plcKey); - } - }); - } - else + foreach (var item in _appConfig.plcConfig) { - _logger.LogInformation("PLC配置信息为空"); + AddPlc(item.plcType, item.plcIp, item.plcPort, item.plcKey); } } - catch (Exception e) + else { - _logger.LogError($"PLC初始化连接异常:{e.Message}"); + _logger.LogInformation("PLC配置信息为空"); } } @@ -71,41 +61,48 @@ namespace SlnMesnac.Plc /// public void AddPlc(string plcType, string ip, int port, string key) { - PlcAbsractFactory _plc = null; - switch (plcType) - { - case "InovancePlc": - _plc = _inovance; - break; - case "MelsecBinaryPlc": - _plc = _melsecBinary; - break; - case "OmronNJPlc": - _plc = _omronNj; - break; - case "SiemensPlc": - _plc = _siemens; - break; - default: - break; - } - var connectResult = _plc.Connect(ip, port); - if (connectResult) + try { - _logger.LogInformation($"PLC:{ip}:{port};连接成功,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); - keyValuePairs.Add(key, _plc); - if (!keyValuePairs.ContainsKey(key)) + PlcAbsractFactory _plc = null; + switch (plcType) + { + case "InovancePlc": + _plc = _inovance; + break; + case "MelsecBinaryPlc": + _plc = _melsecBinary; + break; + case "OmronNJPlc": + _plc = _omronNj; + break; + case "SiemensPlc": + _plc = _siemens; + break; + default: + break; + } + var connectResult = _plc.Connect(ip, port); + if (connectResult) { + _logger.LogInformation($"PLC:{ip}:{port};连接成功,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); keyValuePairs.Add(key, _plc); + if (!keyValuePairs.ContainsKey(key)) + { + keyValuePairs.Add(key, _plc); + } + else + { + keyValuePairs[key] = _plc; + } } else { - keyValuePairs[key] = _plc; + _logger.LogInformation($"PLC:{ip}:{port};连接失败,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); } } - else + catch (Exception e) { - _logger.LogInformation($"PLC:{ip}:{port};连接失败,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); + _logger.LogError($"PLC初始化连接异常:{e.Message}"); } }