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.
lj_plc/Mesnac.PlcUtils/PlcBusiness.cs

86 lines
2.5 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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