增加日志
This commit is contained in:
@ -16,10 +16,11 @@ using HslCommunication;
|
||||
using HslCommunication.Enthernet;
|
||||
using HslCommunication.Profinet.XINJE;
|
||||
using OpenCvSharp;
|
||||
using LogLevel = DH.Commons.Enums.EnumHelper.LogLevel;
|
||||
|
||||
namespace DH.Devices.PLC
|
||||
{
|
||||
public class XinJEPLCTcpNet : PLCBase
|
||||
public class XinJEPLCTcpNet : PLCBase, ILogger
|
||||
{
|
||||
private static XinJEPLCTcpNet _instance;
|
||||
public static XinJEPLCTcpNet Instance
|
||||
@ -31,13 +32,18 @@ namespace DH.Devices.PLC
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public LoggerHelper LoggerHelper { get; set; } = new LoggerHelper();
|
||||
public event Action<LogMsg> OnLog;
|
||||
private XinJETcpNet TcpNet = new XinJETcpNet();
|
||||
|
||||
public override bool PLCConnect()
|
||||
{
|
||||
try
|
||||
{
|
||||
LoggerHelper.LogPath = "D://";
|
||||
LoggerHelper.LogPrefix = "PLC";
|
||||
|
||||
TcpNet.IpAddress = IP;
|
||||
TcpNet.Port = Port;
|
||||
TcpNet.ConnectTimeOut = 5000;
|
||||
@ -63,11 +69,13 @@ namespace DH.Devices.PLC
|
||||
Connected = true;
|
||||
//初始化流程
|
||||
InitProcess();
|
||||
LogAsync(DateTime.Now, LogLevel.Information, $"{IP}:{Port}PLC连接成功");
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Connected = false;
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC初始化失败");
|
||||
throw new Exception($"{IP}:{Port}PLC连接失败!");
|
||||
}
|
||||
|
||||
@ -76,6 +84,7 @@ namespace DH.Devices.PLC
|
||||
catch(Exception ex)
|
||||
{
|
||||
Connected = false;
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"{IP}:{Port}PLC连接失败!失败原因:{ex.ToString()}");
|
||||
throw new Exception($"{IP}:{Port}PLC连接失败!失败原因:{ex.ToString()}");
|
||||
}
|
||||
|
||||
@ -95,18 +104,20 @@ namespace DH.Devices.PLC
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC操作读取int16失败,地址{address}");
|
||||
throw new Exception($"PLC操作读取int16失败,地址{address}");
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC未连接,地址{address}");
|
||||
throw new Exception($"PLC未连接,地址{address}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC操作读取int16失败,地址{address},失败原因:{ex.ToString()}");
|
||||
throw new Exception($"PLC操作读取int16失败,地址{address},失败原因:{ex.ToString()}");
|
||||
}
|
||||
}
|
||||
@ -125,17 +136,20 @@ namespace DH.Devices.PLC
|
||||
}
|
||||
else
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC操作读取int32失败,地址{address}");
|
||||
|
||||
throw new Exception($"PLC操作读取int32失败,地址{address}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC未连接,地址{address}");
|
||||
throw new Exception($"PLC未连接,地址{address}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC操作读取int32失败,地址{address},失败原因:{ex.ToString()}");
|
||||
throw new Exception($"PLC操作读取int32失败,地址{address},失败原因:{ex.ToString()}");
|
||||
}
|
||||
}
|
||||
@ -155,6 +169,7 @@ namespace DH.Devices.PLC
|
||||
}
|
||||
else
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC操作读取uint16失败,地址{address}");
|
||||
|
||||
throw new Exception($"PLC操作读取uint16失败,地址{address}");
|
||||
|
||||
@ -162,12 +177,14 @@ namespace DH.Devices.PLC
|
||||
}
|
||||
else
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC未连接,地址{address}");
|
||||
throw new Exception($"PLC未连接,地址{address}");
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC操作读取uint16失败,地址{address},失败原因:{ex.ToString()}");
|
||||
throw new Exception($"PLC操作读取uint16失败,地址{address},失败原因:{ex.ToString()}");
|
||||
}
|
||||
}
|
||||
@ -186,17 +203,20 @@ namespace DH.Devices.PLC
|
||||
}
|
||||
else
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC操作uint32失败,地址{address}");
|
||||
|
||||
throw new Exception($"PLC操作uint32失败,地址{address}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC未连接,地址{address}");
|
||||
throw new Exception($"PLC未连接,地址{address}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC操作读取uint32失败,地址{address},失败原因:{ex.ToString()}");
|
||||
throw new Exception($"PLC操作读取uint32失败,地址{address},失败原因:{ex.ToString()}");
|
||||
}
|
||||
}
|
||||
@ -216,16 +236,19 @@ namespace DH.Devices.PLC
|
||||
}
|
||||
else
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC操作读取float失败,地址{address}");
|
||||
throw new Exception($"PLC操作读取float失败,地址{address}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC未连接,地址{address}");
|
||||
throw new Exception($"PLC未连接,地址{address}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC操作读取float失败,地址{address},失败原因:{ex.ToString()}");
|
||||
throw new Exception($"PLC操作读取float失败,地址{address},失败原因:{ex.ToString()}");
|
||||
}
|
||||
}
|
||||
@ -243,16 +266,19 @@ namespace DH.Devices.PLC
|
||||
}
|
||||
else
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC操作读取bool失败,地址{address}");
|
||||
throw new Exception($"PLC操作读取bool失败,地址{address}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC未连接,地址{address}");
|
||||
throw new Exception($"PLC未连接,地址{address}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC操作读取bool失败,地址{address},失败原因:{ex.ToString()}");
|
||||
throw new Exception($"PLC操作读取bool失败,地址{address},失败原因:{ex.ToString()}");
|
||||
}
|
||||
}
|
||||
@ -291,6 +317,7 @@ namespace DH.Devices.PLC
|
||||
repeatTime--;
|
||||
if (repeatTime <= 0)
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC操作写入uint32失败,地址{address},失败原因:{ex.ToString()}");
|
||||
throw new Exception($"PLC操作写入uint32失败,地址{address},失败原因:{ex.ToString()}");
|
||||
}
|
||||
}
|
||||
@ -298,6 +325,8 @@ namespace DH.Devices.PLC
|
||||
}
|
||||
else
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC未连接,地址{address}");
|
||||
|
||||
throw new Exception($"PLC未连接,地址{address}");
|
||||
}
|
||||
return false;
|
||||
@ -336,6 +365,7 @@ namespace DH.Devices.PLC
|
||||
repeatTime--;
|
||||
if (repeatTime <= 0)
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC操作写入uint16失败,地址{address},失败原因:{ex.ToString()}");
|
||||
throw new Exception($"PLC操作写入uint16失败,地址{address},失败原因:{ex.ToString()}");
|
||||
}
|
||||
}
|
||||
@ -343,6 +373,8 @@ namespace DH.Devices.PLC
|
||||
}
|
||||
else
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC未连接,地址{address}");
|
||||
|
||||
throw new Exception($"PLC未连接,地址{address}");
|
||||
}
|
||||
return false;
|
||||
@ -383,6 +415,7 @@ namespace DH.Devices.PLC
|
||||
repeatTime--;
|
||||
if (repeatTime <= 0)
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC操作写入int32失败,地址{address},失败原因:{ex.ToString()}");
|
||||
throw new Exception($"PLC操作写入int32失败,地址{address},失败原因:{ex.ToString()}");
|
||||
}
|
||||
}
|
||||
@ -476,6 +509,7 @@ namespace DH.Devices.PLC
|
||||
repeatTime--;
|
||||
if (repeatTime <= 0)
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC操作写入float失败,地址{address},失败原因:{ex.ToString()}");
|
||||
throw new Exception($"PLC操作写入float失败,地址{address},失败原因:{ex.ToString()}");
|
||||
}
|
||||
}
|
||||
@ -483,6 +517,8 @@ namespace DH.Devices.PLC
|
||||
}
|
||||
else
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC未连接,地址{address}");
|
||||
|
||||
throw new Exception($"PLC未连接,地址{address}");
|
||||
}
|
||||
return false;
|
||||
@ -519,6 +555,7 @@ namespace DH.Devices.PLC
|
||||
repeatTime--;
|
||||
if (repeatTime <= 0)
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC操作写入bool失败,地址{address},失败原因:{ex.ToString()}");
|
||||
throw new Exception($"PLC操作写入bool失败,地址{address},失败原因:{ex.ToString()}");
|
||||
}
|
||||
}
|
||||
@ -528,6 +565,7 @@ namespace DH.Devices.PLC
|
||||
}
|
||||
else
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"PLC未连接,地址{address}");
|
||||
throw new Exception($"PLC未连接,地址{address}");
|
||||
}
|
||||
|
||||
@ -537,12 +575,14 @@ namespace DH.Devices.PLC
|
||||
|
||||
public override bool PLCDisConnect()
|
||||
{
|
||||
|
||||
if (Connected)
|
||||
{
|
||||
|
||||
var res = TcpNet.ConnectClose();
|
||||
if (res.IsSuccess)
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Information, $"{IP}:{Port}停止");
|
||||
Connected = false;
|
||||
return true;
|
||||
}
|
||||
@ -575,6 +615,7 @@ namespace DH.Devices.PLC
|
||||
/// int,int 轴号 捕获位置
|
||||
/// </summary>
|
||||
public event Action<int, uint> OnNewPieces;
|
||||
|
||||
|
||||
public void NewPieces(int axisIndex, uint pieceNumber)
|
||||
{
|
||||
@ -635,7 +676,7 @@ namespace DH.Devices.PLC
|
||||
//LogAsync(DateTime.Now, LogLevel.Information, $"转盘{0}产品入列 {piecesCountDic[0]} size:{sum}");
|
||||
if (tmpPieceNumber != piecesCount + 1)
|
||||
{
|
||||
//LogAsync(DateTime.Now, LogLevel.Information, $"入列触发丢失\t{tmpPieceNumber}");
|
||||
LogAsync(DateTime.Now, LogLevel.Information, $"入列触发丢失\t{tmpPieceNumber}");
|
||||
// Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.fff")}\t板卡{station}产品入列触发丢失,{piecesCountDic[station]}\t{tmpPieceNumber}");
|
||||
|
||||
}
|
||||
@ -660,20 +701,25 @@ namespace DH.Devices.PLC
|
||||
{
|
||||
//启用心跳
|
||||
OpenHeartbeat(true);
|
||||
// LogAsync(DateTime.Now, LogLevel.Information, $"启用心跳");
|
||||
//状态复位
|
||||
StatusReset();
|
||||
//LogAsync(DateTime.Now, LogLevel.Information, $"状态复位");
|
||||
//关闭定位
|
||||
VisionPos(false);
|
||||
|
||||
// LogAsync(DateTime.Now, LogLevel.Information, $"关闭定位");
|
||||
//写入流程加载点位配置
|
||||
InitProcessAction();
|
||||
|
||||
//LogAsync(DateTime.Now, LogLevel.Information, $"写入流程加载点位配置");
|
||||
//计数清零
|
||||
CountToZero();
|
||||
// LogAsync(DateTime.Now, LogLevel.Information, $"计数清零");
|
||||
//停止转盘
|
||||
TurnStart(false);
|
||||
//LogAsync(DateTime.Now, LogLevel.Information, $"停止转盘");
|
||||
//转盘使能
|
||||
TurnEnable(true);
|
||||
LogAsync(DateTime.Now, LogLevel.Information, $"启用心跳-状态复位-关闭定位-写入流程加载点位配置-计数清零-停止转盘-转盘使能");
|
||||
//开启入料监听
|
||||
MonitorPieces();
|
||||
|
||||
@ -683,12 +729,16 @@ namespace DH.Devices.PLC
|
||||
{
|
||||
//状态复位
|
||||
StatusReset();
|
||||
// LogAsync(DateTime.Now, LogLevel.Information, $"状态复位");
|
||||
//关闭定位
|
||||
VisionPos(false);
|
||||
// LogAsync(DateTime.Now, LogLevel.Information, $"关闭定位");
|
||||
//写入流程启动点位配置
|
||||
StartProcessAction();
|
||||
// LogAsync(DateTime.Now, LogLevel.Information, $"写入流程加载点位配置");
|
||||
//计数清零
|
||||
CountToZero();
|
||||
LogAsync(DateTime.Now, LogLevel.Information, $"状态复位-关闭定位-写入流程加载点位配置-计数清零");
|
||||
//转盘启动
|
||||
TurnStart(true);
|
||||
}
|
||||
@ -702,6 +752,7 @@ namespace DH.Devices.PLC
|
||||
TurnEnable(false);
|
||||
OpenHeartbeat(false);
|
||||
PLCDisConnect();
|
||||
// LogAsync(DateTime.Now, LogLevel.Information, $"PLC断开连接");
|
||||
}
|
||||
public void InitProcessAction() =>
|
||||
ProcessAction(ConfigModel.GlobalList?.FirstOrDefault()?.InitProcessList?.ToList() ?? new List<PLCItem>());
|
||||
@ -974,5 +1025,23 @@ namespace DH.Devices.PLC
|
||||
WriteUInt16($"D{currentRegister}", value);
|
||||
|
||||
}
|
||||
|
||||
public void LogAsync(LogMsg msg)
|
||||
{
|
||||
msg.MsgSource = "PLC";
|
||||
msg.ThreadId = Thread.CurrentThread.ManagedThreadId;
|
||||
|
||||
//OnLog?.BeginInvoke(msg, null, null);
|
||||
OnLog?.Invoke(msg);
|
||||
|
||||
//if (InitialConfig.IsEnableLog)
|
||||
{
|
||||
LoggerHelper.LogAsync(msg);
|
||||
}
|
||||
}
|
||||
public void LogAsync(DateTime dt, LogLevel logLevel, string msg)
|
||||
{
|
||||
LogAsync(new LogMsg(dt, logLevel, msg));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user