|
|
using Mesnac.PlcUtils.common;
|
|
|
using Mesnac.PlcUtils.enumInfo;
|
|
|
using Mesnac.PlcUtils.Impl;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Reflection;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
namespace Mesnac.PlcUtils
|
|
|
{
|
|
|
public sealed class PlcBusiness
|
|
|
{
|
|
|
|
|
|
private static readonly Lazy<PlcBusiness> lazy = new Lazy<PlcBusiness>(() => new PlcBusiness());
|
|
|
public static PlcBusiness Instance
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
return lazy.Value;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private PlcBusiness() { }
|
|
|
|
|
|
private static IPlc plcInstance = null;
|
|
|
|
|
|
public void InitPlcConnect(PlcType plcType, string plcAddress, int plcPort)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
string str = System.Environment.CurrentDirectory;
|
|
|
str = str + "\\Mesnac.PlcUtils.dll";
|
|
|
Assembly assembly = Assembly.LoadFile(str); // 加载程序集(EXE 或 DLL)
|
|
|
string AssemName = "Mesnac.PlcUtils.Impl." + plcType.GetDescription();
|
|
|
var obj = assembly.CreateInstance(AssemName, true);
|
|
|
plcInstance = obj as IPlc;
|
|
|
if (plcInstance == null)
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error("PLC初始化失败!!!");
|
|
|
return;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
if (!plcInstance.IsConnected)
|
|
|
{
|
|
|
bool connectResult = plcInstance.Connect(plcAddress, plcPort);
|
|
|
if (connectResult)
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug("PLC连接成功!!!");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error("PLC连接失败!!!");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error($"PLC初始化异常:{ex.Message}");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void Dispose()
|
|
|
{
|
|
|
plcInstance.DisConnect();
|
|
|
}
|
|
|
|
|
|
|
|
|
public bool readBoolValue(string address)
|
|
|
{
|
|
|
if(plcInstance != null)
|
|
|
{
|
|
|
return plcInstance.readBoolByAddress(address);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|