230 lines
7.1 KiB
C#
230 lines
7.1 KiB
C#
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
|
||
{
|
||
|
||
/// <summary>
|
||
/// plc实现工厂类
|
||
/// </summary>
|
||
public class PlcFactory
|
||
{
|
||
/// <summary>
|
||
/// 读取plc时,存储已经读取的plc点的类
|
||
/// </summary>
|
||
public Dictionary<string, PLClock> plcCollection = new Dictionary<string, PLClock>();
|
||
|
||
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;
|
||
}
|
||
|
||
#endregion
|
||
|
||
public Dictionary<string, List<string>> typeClass = new Dictionary<string, List<string>>();
|
||
/// <summary>
|
||
/// 同步读取plc 返回数据
|
||
/// </summary>
|
||
/// <param name="plcNmae"></param>
|
||
/// <returns></returns>
|
||
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<string,List<string>>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<object> 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;
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
/// <summary>
|
||
/// 走箱子
|
||
/// </summary>
|
||
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;
|
||
}
|
||
|
||
|
||
/// <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 DisConnection(string ipPlc)
|
||
{
|
||
foreach ( KeyValuePair<string ,PLClock> 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;
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 异步调用
|
||
/// </summary>
|
||
public void SysonyData()
|
||
{
|
||
|
||
//ReadDBPlcCheckgz rdg= new ReadDBPlcCheckgz();
|
||
// new ReaddbPLCDevicesError();
|
||
// new ReadDBPlcConveyorError();
|
||
// new ReadDBPlcConveyorLocation();
|
||
// new ReadDBPlcConveyorMode();
|
||
// new ReadDBPlcConveyorPlcid();
|
||
|
||
}
|
||
#endregion
|
||
|
||
}
|
||
|
||
}
|