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}"); } }