Files
CheryFianlAssemblyLineNew/Analysis/BaseUnit/PlcWriteManager.cs
T
HM-CN\zhengxuan.zhang 8cf3fb42d6 #总装移交版本
2025-03-07 11:44:37 +08:00

111 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NSAnalysis
{
public class PlcWriteObj
{
public object WriteAddr;
public object WriteValue;
public int WriteLength; //长度,主要针对字符串
/// <summary>
/// 0=int, 1= float, 2=double, 3=string
/// </summary>
public int DataType = 0;
public PlcWriteObj()
{
WriteAddr = null;
WriteValue = null;
WriteLength = -1;
}
}
public class PlcWriteManager
{
private List<PlcWriteObj> LstPLcWrite = new List<PlcWriteObj>();
private static readonly object sync = new object();
private static PlcWriteManager _this = null;
public static PlcWriteManager Instance
{
get
{
if (_this == null)
{
lock (sync)
{
if (_this == null)
{
_this = new PlcWriteManager();
}
}
}
return _this;
}
}
public PlcWriteManager()
{
}
public PlcWriteObj OutQueue()
{
try
{
PlcWriteObj ccmdObj = null;
lock (this)
{
if (LstPLcWrite != null && LstPLcWrite.Count > 0)
{
ccmdObj = new PlcWriteObj();
ccmdObj = LstPLcWrite[0];
LstPLcWrite.RemoveAt(0);
}
}
return ccmdObj;
}
catch
{
throw;
}
}
public void RemoveQueueTop()
{
if (LstPLcWrite != null && LstPLcWrite.Count > 0)
{
LstPLcWrite.RemoveAt(0);
}
}
public void InsertQueue(PlcWriteObj data)
{
lock (this)
{
LstPLcWrite.Add(data);
}
}
public bool isExitData()
{
lock (this)
{
return LstPLcWrite.Count > 0;
}
}
public static void WritePlc(object Addr, object Value, int DataType)
{
PlcWriteObj m_DataObj = new PlcWriteObj();
m_DataObj.WriteAddr = Addr;
m_DataObj.WriteValue = Value;
m_DataObj.DataType = DataType;
Instance.InsertQueue(m_DataObj);
}
}
}