using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OPCAutomation;
using System.Diagnostics;
using System.Threading;
using CommonDll;
using System.Data;
namespace AndonForm
{
public class PLCThread
{
///
/// OPC SERVER Interface
///
private static OPCServer opcServerKEP = null;
private static OPCGroup opcGrpKEP_Run = null;
private static OPCGroup opcGrpKEP_Run1 = null;
public static DataTable dt = null;
private static int runSeconds = 0;
public static DoWorkThread _thread = null;//OPC监听线程
///
/// 初始第一次读取标志位
///
public static bool IsReadFirst = true;
public static bool IsReadFirst1 = true;
///
/// 数据源
///
public static DBSourse SQL;
///
/// 基础信息列表
///
private static List
public static List
{
get { return _listAndonInterface; }
}
public static void Init()
{
try
{
SQL = new DBSourse();
SQL.Open();
runSeconds = CONFIG.GetInt("Run", "Times", 6);
_thread = new DoWorkThread(RunThread);
_thread.Start();
LogUtils.WriteLogByMonth(DateTime.Now.ToString() + "数据库连接成功");
_listAndonInterface = SQL.InitAndon_PLCAddress();
if (!ConnectToOPCServer()) return;
}
catch (Exception e)
{
LogUtils.WriteLogByMonth(DateTime.Now.ToString() + "Init初始化失败;" + e.Message);
}
}
public static bool ConnectToOPCServer()
{
bool result;
try
{
opcServerKEP = new OPCServer();
opcServerKEP.Connect("KEPware.KEPServerEx.V4", "127.0.0.1");
InitOPC();
CreateItems();
result = true;
}
catch (Exception e)
{
LogUtils.WriteLogByMonth(DateTime.Now.ToString() + "OPCServer初始化失败" + e.Message);
result = false;
}
return result;
}
public static void InitOPC()
{
try
{
opcGrpKEP_Run = opcServerKEP.OPCGroups.Add("KEP_Run");
opcGrpKEP_Run.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(opcGrpKEP_Run_DataChange);
opcGrpKEP_Run1 = opcServerKEP.OPCGroups.Add("KEP_Run1");
opcGrpKEP_Run1.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(opcGrpKEP_Run1_DataChange);
}
catch (Exception e)
{
LogUtils.WriteLogByMonth(DateTime.Now.ToString() + "初始化OPC事件失败" + e.Message);
}
}
static void opcGrpKEP_Run1_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
{
try
{
int i = 1;
while (i <= ClientHandles.Length)
{
long ReadID = (long)Convert.ToInt32(ClientHandles.GetValue(i));//哪个Item触发了DataChange事件,读取到对应的ID
string ReadValue = Convert.ToInt16(ItemValues.GetValue(i)).ToString();//触发点读取到的值为多少
int IsBad = Convert.ToInt32(Qualities.GetValue(i));
checked
{
if (!IsReadFirst1)
{
Andon_Interface _Single = ListAndonInterface.Find(e => e.RunId == ReadID);
if (_Single != null)
{
if (ReadValue != "0")
{
dt.Rows[_Single.Id - 1]["Arrive"] = 0;
dt.Rows[_Single.Id - 1]["ISActive"] = 0;
dt.Rows[_Single.Id - 1]["RunTime"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
SQL.UpdateRunTime(_Single.RFIDSTEP);
}
}
}
}
i++;
}
IsReadFirst1 = false;
}
catch
{
}
}
public static void CreateItems()
{
try
{
foreach (Andon_Interface item in ListAndonInterface)
{
opcGrpKEP_Run.OPCItems.AddItem(item.TagNameArrive, item.Id);
}
foreach (Andon_Interface item in ListAndonInterface)
{
opcGrpKEP_Run1.OPCItems.AddItem(item.TagNameRun, item.RunId);
}
opcGrpKEP_Run.IsActive = true;
opcGrpKEP_Run.IsSubscribed = true;
opcGrpKEP_Run.UpdateRate = 500;
opcGrpKEP_Run1.IsActive = true;
opcGrpKEP_Run1.IsSubscribed = true;
opcGrpKEP_Run1.UpdateRate = 500;
dt = new DataTable("Rundt");
DataColumn dc1 = new DataColumn("ID", Type.GetType("System.String"));
DataColumn dc2 = new DataColumn("Arrive", Type.GetType("System.Int32"));
DataColumn dc3 = new DataColumn("ISActive", Type.GetType("System.Int32"));
DataColumn dc4 = new DataColumn("FRIDSTEP", Type.GetType("System.String"));
DataColumn dc5 = new DataColumn("ArriveTime", Type.GetType("System.String"));
DataColumn dc6 = new DataColumn("RunTime", Type.GetType("System.String"));
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
dt.Columns.Add(dc4);
dt.Columns.Add(dc5);
dt.Columns.Add(dc6);
foreach (Andon_Interface item in ListAndonInterface)
{
DataRow dr = dt.NewRow();
dr["ID"] = item.Id;
dr["Arrive"] = 0;
dr["ISActive"] = 0;
dr["FRIDSTEP"] = item.RFIDSTEP;
dr["ArriveTime"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
dr["RunTime"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
dt.Rows.Add(dr);
}
}
catch (Exception e)
{
LogUtils.WriteLogByMonth(DateTime.Now.ToString() + "OPC CreateItems创建失败" + e.Message);
}
}
public static void opcGrpKEP_Run_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
{
try
{
int i = 1;
while (i <= ClientHandles.Length)
{
long ReadID = (long)Convert.ToInt32(ClientHandles.GetValue(i));//哪个Item触发了DataChange事件,读取到对应的ID
string ReadValue = Convert.ToInt16(ItemValues.GetValue(i)).ToString();//触发点读取到的值为多少
int IsBad = Convert.ToInt32(Qualities.GetValue(i));
checked
{
if (!IsReadFirst)
{
Andon_Interface _Single = ListAndonInterface.Find(e => e.Id == ReadID);
if (_Single != null)
{
if (ReadValue == "1")
{
dt.Rows[_Single.Id - 1]["Arrive"] = 1;
dt.Rows[_Single.Id - 1]["ISActive"] = 1;
dt.Rows[_Single.Id - 1]["ArriveTime"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
SQL.InsertIntoArriverTime(_Single.RFIDSTEP);
}
}
}
}
i++;
}
IsReadFirst = false;
}
catch
{
}
}
static void RunThread()
{
Thread.Sleep(200);
if (dt != null && dt.Rows.Count > 0)
{
foreach (DataRow item in dt.Select("ISActive=1"))
{
if ((DateTime.Now - Convert.ToDateTime(item["ArriveTime"])).TotalSeconds > runSeconds)
{
Andon_Interface _Single = ListAndonInterface.Find(e => e.Id == Convert.ToInt32(item["ID"]));
if (_Single != null)
{
if (_Single.IsControl == "0")
{
opcGrpKEP_Run1.OPCItems.Item(_Single.TagNameRun).Write(_Single.RunValue);
LogUtils.WriteLogByMonth(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " 大于" + runSeconds + "秒强制放行," + "点位:" + _Single.TagNameRun);
}
}
}
}
}
}
public static bool DisConnecOPCServer()
{
bool result;
try
{
opcServerKEP.OPCGroups.RemoveAll();
opcGrpKEP_Run = null;
opcServerKEP.Disconnect();
opcServerKEP = null;
result = true;
}
catch
{
result = false;
}
return result;
}
}
}