BaoKai_202508-Wms-Jingwang..../ServerSystem/PlcFactory.cs

277 lines
7.2 KiB
C#
Raw Normal View History

2025-08-24 09:35:55 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading.Tasks;
using ACTMULTILib;
//using ACTMULTILib;
namespace PLCCommon
{
/// <summary>
/// plc实现工厂类
/// </summary>
public class PlcFactory
{
private object l = new object();
private static PlcFactory m_instance;
private static object m_lock = new object();
// public List<Label> tbLable = new List<Label>();
#region Singletonle模式返回当前实例
/// <summary>
/// 通过Singleton模式返回当前实例
/// </summary>
public static PlcFactory Instance()
{
if (m_instance == null)
{
lock (m_lock)
{
if (m_instance == null)
{
m_instance = new PlcFactory();
}
}
}
return m_instance;
}
int PlcSts = -1;
public string ErrText = string.Empty;
public PlcFactory()
{
}
#endregion
public Dictionary<string, List<string>> typeClass = new Dictionary<string, List<string>>();
/// <summary>
/// 同步读取plc 返回数据
/// </summary>
/// <param name="plcNmae"></param>
/// <returns></returns>
///
object plcLock = new object();
public string plcClass(string plcName,ref string errText)
{
lock (plcLock)
{
string plcValue = string.Empty;
try
{
foreach(KeyValuePair<string,List<string>>item in typeClass)
{
//int key= item.Key.IndexOf("%");
string xmlName = item.Key;
if (xmlName== plcName)
{
for (int i = 0; i < item.Value.Count; i++)
{
plcValue=item.Value[i].ToString();
}
break;
}
}
}
catch (Exception ex)
{
errText = ex.Message.ToString();
return null;
}
return plcValue;
}
}
public string ReadPlcDbValue(string plcName,ref string errText)
{
lock (plcName)
{
try
{
int val = 0;
int plcSts = 1;
PLC_Station.Open();
string plcItem = plcClass(plcName, ref errText);
plcSts = PLC_Station.ReadDeviceRandom(plcItem + "\n", 1, out val);
if (plcSts == 0)
{
errText = string.Empty;
}
else
{
errText = "PLC读取错误";
}
return val.ToString();// errText = string.Empty;
// PLC_Station.Close();
}
catch (Exception ex)
{
errText = ex.Message.ToString();
return "01";// throw ex;
}
}
}
public string ReadPlcDbValuefyd(string plcName, ref string errText)
{
lock (plcName)
{
try
{
PLC_Station1.ActLogicalStationNumber = 1;
int val = 0;
int plcSts = 1;
PLC_Station1.Open();
string plcItem = plcClass(plcName, ref errText);
plcSts = PLC_Station1.ReadDeviceRandom(plcItem + "\n", 1, out val);
if (plcSts == 0)
{
errText = string.Empty;
}
else
{
errText = "PLC读取错误";
}
return val.ToString();// errText = string.Empty;
// PLC_Station.Close();
}
catch (Exception ex)
{
errText = ex.Message.ToString();
return "01";// throw ex;
}
}
}
public ActEasyIFClass PLC_Station = new ActEasyIFClass();
public ActEasyIFClass PLC_Station1= new ActEasyIFClass();
/// <summary>
///
/// </summary>
public string WirtePlcContianer(string plcName,string value )
{
lock ("")
{
string errText = string.Empty;
int val = int.Parse(value);
string plcItem = plcClass(plcName, ref errText);
int plcChange = PLC_Station.WriteDeviceRandom(plcItem + "\n", 1, ref val);
if (plcChange != 0)
{
errText = "PLC写入错误" + plcChange.ToString();
return errText;
}
return errText;
}
}
public string WirtePlcContianerfyd(string plcName, string value)
{
PLC_Station1.ActLogicalStationNumber = 1;
string errText = string.Empty;
int val = int.Parse(value);
string plcItem = plcClass(plcName, ref errText);
int plcChange = PLC_Station1.WriteDeviceRandom(plcItem + "\n", 1, ref val);
if (plcChange != 0)
{
errText = "PLC写入错误" + plcChange.ToString();
return errText;
}
return errText;
}
 
/// <summary>
/// 读取plc时存储已经读取的plc点的类
/// </summary>
// public Dictionary<string, PLClock> plcCollection = new Dictionary<string, PLClock>();
/// <summary>
/// 读取config的值
/// </summary>
public Dictionary<string, string> conFig = new Dictionary<string, string>();
#region plc是否连接成功
/// <summary>
/// 判断plc是否连接成功
/// </summary>
/// <param name="ipPlc"></param>
/// <returns></returns>
public bool ConnectPlc(string ipPlc)
{
return true;
}
#endregion
#region
public void SysonyExce(string name,string value)
{
lock (name)
{
if (name == "1")
{
string s = value;
}
if (name == "2")
{
string s = value;
}
}
}
/// <summary>
#endregion
}
}