注塑喷涂
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

253 lines
8.1 KiB

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using OPCAutomation;
using MESClassLibrary;
using MESClassLibrary.BLL.BasicInfo;
using MESClassLibrary.Model;
using MESClassLibrary.BLL.Log;
using MESClassLibrary.BLL.Injection;
using MESClassLibrary.DAL.Injection;
namespace InjectionPLC
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region 变量
private string strHostIP;
private string strHostName;
private OPCServer opcServer;
private OPCGroups opcGroups;
private OPCGroup opcGroup;
private OPCItems opcItems;
private OPCItem[] opcItemm;
private string[] ItemIDs;
object ItemValues;
object Qualities;
object TimeStamps;
public bool Connected = false;
#endregion
private void Form1_Load(object sender, EventArgs e)
{
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
CreateServer();
ConnectServer(strHostIP, "Kepware.KEPServerEX.V6");
Connected = true;
opcGroups = opcServer.OPCGroups;
opcGroup = opcGroups.Add("111");
SetGroupProperty(opcGroup, 500);
opcGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(opcGroup_DataChange);
opcItems = opcGroup.OPCItems;
opcItems.DefaultIsActive = true;
opcItemm = new OPCItem[6];
opcItems = opcGroup.OPCItems;
opcItemm[0] = opcItems.AddItem("IM.IM01.DI0", 0);
opcItemm[1] = opcItems.AddItem("IM.IM02.DI0", 1);
opcItemm[2] = opcItems.AddItem("IM.IM03.DI0", 2);
opcItemm[3] = opcItems.AddItem("IM.IM04.DI0", 3);
opcItemm[4] = opcItems.AddItem("IM.IM05.DI0", 4);
opcItemm[5] = opcItems.AddItem("IM.IM06.DI0", 5);
Thread.Sleep(200);
}
/// <summary>
/// 创建服务
/// </summary>
/// <returns></returns>
private bool CreateServer()
{
try
{
opcServer = new OPCServer();
}
catch
{
return false;
}
return true;
}
/// <summary>
/// 连接到服务器
/// </summary>
/// <param name="strHostIP"></param>
/// <param name="strHostName"></param>
/// <returns></returns>
private bool ConnectServer(string strHostIP, string strHostName)
{
try
{
opcServer.Connect(strHostName, strHostIP);
}
catch
{
return false;
}
return true;
}
/// <summary>
/// 设置组的属性
/// </summary>
/// <param name="opcGroup"></param>
/// <param name="updateRate"></param>
private void SetGroupProperty(OPCGroup opcGroup, int updateRate)
{
opcGroup.IsActive = true;
opcGroup.DeadBand = 0;
opcGroup.UpdateRate = updateRate;
opcGroup.IsSubscribed = true;
}
void opcGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
{
InjectionDownRecordModel md=new InjectionDownRecordModel();
InjectionDownRecordBLL bll = new InjectionDownRecordBLL();
for (int i = 1; i <= NumItems; i++)
{
#region IM01
if (Convert.ToInt32(ClientHandles.GetValue(i).ToString().Trim()) == 0)
{
md.StationID = GetStationID("IM01");
if (ItemValues.GetValue(i).ToString() == "True")
{
md.ID = Guid.NewGuid().ToString();
md.StartTime = DateTime.Now;
bll.AddInfo(md);
}
else
{
md.EndTime = DateTime.Now;
bll.UpdateInfo(md);
}
}
#endregion
#region IM02
if (Convert.ToInt32(ClientHandles.GetValue(i).ToString().Trim()) == 1)
{
md.StationID = GetStationID("IM02");
if (ItemValues.GetValue(i).ToString() == "True")
{
md.ID = Guid.NewGuid().ToString();
md.StartTime = DateTime.Now;
bll.AddInfo(md);
}
else
{
md.EndTime = DateTime.Now;
bll.UpdateInfo(md);
}
}
#endregion
#region IM03
if (Convert.ToInt32(ClientHandles.GetValue(i).ToString().Trim()) == 2)
{
md.StationID = GetStationID("IM03");
if (ItemValues.GetValue(i).ToString() == "True")
{
md.ID = Guid.NewGuid().ToString();
md.StartTime = DateTime.Now;
bll.AddInfo(md);
}
else
{
md.EndTime = DateTime.Now;
bll.UpdateInfo(md);
}
}
#endregion
#region IM04
if (Convert.ToInt32(ClientHandles.GetValue(i).ToString().Trim()) == 3)
{
md.StationID = GetStationID("IM04");
if (ItemValues.GetValue(i).ToString() == "True")
{
md.ID = Guid.NewGuid().ToString();
md.StartTime = DateTime.Now;
bll.AddInfo(md);
}
else
{
md.EndTime = DateTime.Now;
bll.UpdateInfo(md);
}
}
#endregion
#region IM05
if (Convert.ToInt32(ClientHandles.GetValue(i).ToString().Trim()) == 4)
{
md.StationID = GetStationID("IM05");
if (ItemValues.GetValue(i).ToString() == "True")
{
md.ID = Guid.NewGuid().ToString();
md.StartTime = DateTime.Now;
bll.AddInfo(md);
}
else
{
md.EndTime = DateTime.Now;
bll.UpdateInfo(md);
}
}
#endregion
#region IM06
if (Convert.ToInt32(ClientHandles.GetValue(i).ToString().Trim()) == 5)
{
md.StationID = GetStationID("IM06");
if (ItemValues.GetValue(i).ToString() == "True")
{
md.ID = Guid.NewGuid().ToString();
md.StartTime = DateTime.Now;
bll.AddInfo(md);
}
else
{
md.EndTime = DateTime.Now;
bll.UpdateInfo(md);
}
}
#endregion
}
}
private string GetStationID(string stationNo)
{
string stationID = "";
StationBLL sbll = new StationBLL();
DataTable sdt = sbll.SearchInfoByNo(stationNo);
if (sdt != null && sdt.Rows.Count > 0)
{
stationID = sdt.Rows[0]["StationID"].ToString();
sdt.Dispose();
}
return stationID;
}
}
}