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