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
{
///
/// plc实现工厂类
///
public class PlcFactory
{
private object l = new object();
private static PlcFactory m_instance;
private static object m_lock = new object();
// public List tbLable = new List();
#region 通过Singletonle模式返回当前实例
///
/// 通过Singleton模式返回当前实例
///
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> typeClass = new Dictionary>();
///
/// 同步读取plc 返回数据
///
///
///
///
object plcLock = new object();
public string plcClass(string plcName,ref string errText)
{
lock (plcLock)
{
string plcValue = string.Empty;
try
{
foreach(KeyValuePair>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();
///
///
///
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;
}
///
/// 读取plc时,存储已经读取的plc点的类
///
// public Dictionary plcCollection = new Dictionary();
///
/// 读取config的值
///
public Dictionary conFig = new Dictionary();
#region 判断plc是否连接成功
///
/// 判断plc是否连接成功
///
///
///
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;
}
}
}
///
#endregion
}
}