change - PLC 初始化设置

pull/1/head
wenjy 10 months ago
parent 886e64ad39
commit b09b3c33a3

@ -39,26 +39,16 @@ namespace SlnMesnac.Plc
} }
_logger.LogInformation("HslCommunication 11.8.1.0激活成功"); _logger.LogInformation("HslCommunication 11.8.1.0激活成功");
try if (_appConfig.plcConfig != null)
{ {
if (_appConfig.plcConfig != null) foreach (var item in _appConfig.plcConfig)
{
Task.Run(() =>
{
foreach (var item in _appConfig.plcConfig)
{
AddPlc(item.plcType, item.plcIp, item.plcPort, item.plcKey);
}
});
}
else
{ {
_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
/// <param name="key"></param> /// <param name="key"></param>
public void AddPlc(string plcType, string ip, int port, string key) public void AddPlc(string plcType, string ip, int port, string key)
{ {
PlcAbsractFactory _plc = null; try
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")}"); PlcAbsractFactory _plc = null;
keyValuePairs.Add(key, _plc); switch (plcType)
if (!keyValuePairs.ContainsKey(key)) {
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); keyValuePairs.Add(key, _plc);
if (!keyValuePairs.ContainsKey(key))
{
keyValuePairs.Add(key, _plc);
}
else
{
keyValuePairs[key] = _plc;
}
} }
else 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}");
} }
} }

Loading…
Cancel
Save