using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpcRcw.Da;
using System.Data;
using Common;
using System.Threading.Tasks;
namespace PLCCommon
{
///
/// plc实现工厂类
///
public class PlcFactory
{
///
/// 读取plc时,存储已经读取的plc点的类
///
public Dictionary plcCollection = new Dictionary();
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;
}
#endregion
public Dictionary> typeClass = new Dictionary>();
///
/// 同步读取plc 返回数据
///
///
///
public PLClock plcClass(string plcName,ref string errText)
{
lock (this)
{
try
{
errText = string.Empty;
PLClock plcRead;
if (plcCollection.Keys.Count(r => r == plcName) > 0)
{
plcRead = plcCollection[plcName];
return plcRead;
}
plcRead = new PLClock();
plcRead.ConnectRemoteServer();
plcRead.PLCGroupAdd();
foreach(KeyValuePair>item in typeClass)
{
string xmlName = item.Key.Substring(0, item.Key.Length-1);
if (xmlName== plcName)
{
OpcRcw.Da.OPCITEMDEF[] Item = new OPCITEMDEF[item.Value.Count];
for (int i = 0; i < item.Value.Count; i++)
{
Item[i].szAccessPath = "";
Item[i].szItemID = item.Value[i].ToString();
Item[i].bActive = 1;//是否激活
Item[i].hClient = i + 1;//表示ID
Item[i].dwBlobSize = 0;
Item[i].pBlob = IntPtr.Zero;
// Item[i].vtRequestedDataType = 4;
plcRead.PLCItemAdd(Item);
}
break;
}
}
plcCollection.Add(plcName, plcRead);
return plcRead;
}
catch (Exception ex)
{
errText = ex.Message.ToString();
return null;
}
}
}
public string ReadPlcDbValue(string plcName,ref string errText)
{
lock (plcName)
{
try
{
errText = string.Empty;
PLClock plcRead = plcClass(plcName, ref errText);
if (errText.Trim().Length > 0)
{
return "01";
}
List read = plcRead.ReadPlc();
if (read == null)
return "01";
if (read.Count == 0)
return "01";
string valsz = "";
foreach (object a in read)
{
if (a is int[])
{
foreach (object c in a as int[])
{
if (valsz.Trim().Length > 0)
{
valsz = valsz + "," + c.ToString();
}
else
{
valsz = c.ToString();
}
}
}
else
{ return read[0].ToString(); }
}
return valsz;
}
catch (Exception ex)
{
errText = ex.Message.ToString();
return "01";// throw ex;
}
}
}
///
/// 走箱子
///
public string WirtePlcContianer(string plcName,object[] value )
{
string errText = string.Empty;
PLClock plcRead= plcClass(plcName,ref errText);
if(errText.Trim().Length>0)
{
return errText;
}
errText = plcRead.WirtePlc(value);
return errText;
}
///
/// 读取plc时,存储已经读取的plc点的类
///
// public Dictionary plcCollection = new Dictionary();
///
/// 读取config的值
///
public Dictionary conFig = new Dictionary();
#region 判断plc是否连接成功
///
/// 判断plc是否连接成功
///
///
///
public bool DisConnection(string ipPlc)
{
foreach ( KeyValuePair plccon in plcCollection)
{
plccon.Value.DisConnection();
}
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;
}
}
}
///
/// 异步调用
///
public void SysonyData()
{
//ReadDBPlcCheckgz rdg= new ReadDBPlcCheckgz();
// new ReaddbPLCDevicesError();
// new ReadDBPlcConveyorError();
// new ReadDBPlcConveyorLocation();
// new ReadDBPlcConveyorMode();
// new ReadDBPlcConveyorPlcid();
}
#endregion
}
}