You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
2.7 KiB
C#
106 lines
2.7 KiB
C#
using HighWayIot.Log4net;
|
|
using HighWayIot.Plc.Impl;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace HighWayIot.Plc
|
|
{
|
|
public sealed class PlcPool
|
|
{
|
|
private static readonly Lazy<PlcPool> lazy = new Lazy<PlcPool>(() => new PlcPool());
|
|
public static PlcPool Instance
|
|
{
|
|
get
|
|
{
|
|
return lazy.Value;
|
|
}
|
|
}
|
|
|
|
private LogHelper logHelper = LogHelper.Instance;
|
|
|
|
private Dictionary<string, IPlc> keyValuePairs = new Dictionary<string, IPlc>();
|
|
|
|
private PlcPool() { }
|
|
|
|
/// <summary>
|
|
/// 初始化Plc
|
|
/// </summary>
|
|
/// <param name="plcType"></param>
|
|
/// <param name="ip"></param>
|
|
/// <param name="port"></param>
|
|
/// <param name="key"></param>
|
|
public void InitPlc(string plcType, string ip, int port, string key)
|
|
{
|
|
IPlc _plc = null;
|
|
switch (plcType)
|
|
{
|
|
case "InovancePlc":
|
|
_plc = new InovancePlc();
|
|
break;
|
|
case "MelsecBinaryPlc":
|
|
_plc = new MelsecBinaryPlc();
|
|
break;
|
|
case "OmronNJPlc":
|
|
_plc = new OmronNJPlc();
|
|
break;
|
|
case "SiemensPlc":
|
|
_plc = new SiemensPlc();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
var connectResult = _plc.Connect(ip, port);
|
|
if (connectResult)
|
|
{
|
|
_plc.IsConnected = true;
|
|
keyValuePairs.Add(key, _plc);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取PLC
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public IPlc GetPlcByKey(string key)
|
|
{
|
|
return keyValuePairs[key];
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取所有PLC信息
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public Dictionary<string, IPlc> GetAll()
|
|
{
|
|
return keyValuePairs;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 关闭所有连接
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool disConnectAll()
|
|
{
|
|
bool result = false;
|
|
try
|
|
{
|
|
foreach (var kv in keyValuePairs)
|
|
{
|
|
if (kv.Value != null)
|
|
{
|
|
kv.Value.DisConnect();
|
|
}
|
|
}
|
|
result = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logHelper.Error("关闭PLC连接异常", ex);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|