一厂MES,含注塑,喷涂,冲孔
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.
 
 
 
 
 

332 lines
9.9 KiB

using foda;
using foda.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
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;
namespace FacOneZPStation
{
public partial class Form1 : Form
{
string stationNo = ConfigurationManager.AppSettings["StationNo"];
string ProductNum = "";
string BadNum = "";
string OrderNum = "";
#region 变量
private string strHostIP;
private string strHostName;
private OPCServer opcServer;
private OPCGroups opcGroups;
private OPCGroup opcGroup1;
private OPCItems opcItems1;
private OPCItem[] opcItemm1;
private OPCGroup opcGroup2;
private OPCItems opcItems2;
private OPCItem[] opcItemm2;
private string[] ItemIDs;
object ItemValues;
object Qualities;
object TimeStamps;
public bool Connected = false;
#endregion
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
InitPage();
LoadPlan();
textBox1.TabIndex = 0;
#region 连接PLC
if (CreateServer())
{
if (ConnectServer(strHostIP, "Kepware.KEPServerEX.V6"))
{
Connected = true;
#region 创建组
opcGroups = opcServer.OPCGroups;
opcGroup1 = opcGroups.Add("Mould");
SetGroupProperty(opcGroup1, 500);
opcGroup2 = opcGroups.Add("Break");
SetGroupProperty(opcGroup2, 500);
#endregion
#region 创建项
opcItems1 = opcGroup1.OPCItems;
opcItems1.DefaultIsActive = true;
List<tb_PLC_Mould> mouldList = DF.GetPLCMould();
List<tb_PLC_Break> breakList = DF.GetPLCBreak();
if (mouldList != null && mouldList.Count > 0)
{
opcItemm1 = new OPCItem[mouldList.Count];
for (int i = 0; i < mouldList.Count; i++)
{
opcItemm1[i] = opcItems1.AddItem(mouldList[i].Address, i);
}
}
Thread.Sleep(200);
opcItems2 = opcGroup2.OPCItems;
opcItems2.DefaultIsActive = true;
if (breakList != null && breakList.Count > 0)
{
opcItemm2 = new OPCItem[breakList.Count];
for (int i = 0; i < breakList.Count; i++)
{
opcItemm2[i] = opcItems2.AddItem(breakList[i].Address, i);
}
}
Thread.Sleep(200);
opcGroup1.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(opcGroup1_DataChange);
#endregion
}
else
{
MessageBox.Show("连接到OPC服务器失败!", "提示", MessageBoxButtons.OK);
Connected = false;
LogHelper.WriteLog("连接到OPC服务器失败!");
return;
}
}
else
{
MessageBox.Show("创建OPC服务失败!", "提示", MessageBoxButtons.OK);
Connected = false;
LogHelper.WriteLog("创建OPC服务失败!");
return;
}
#endregion
NumToPLC();
}
#region showtime
public void InitPage()
{
try
{
Thread t = new Thread(new ThreadStart(TimeGo));
t.Start();
}
catch (Exception ex)
{ }
}
private void TimeGo()
{
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 10;
timer.Enabled = true;
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Tick);
}
private void timer_Tick(object sender, EventArgs e)
{
labTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
Thread.Sleep(500);
}
private void labTime_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
#endregion
#region createPLC
/// <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;
}
#endregion
void opcGroup1_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
{
}
public void LoadPlan()
{
List<DPPM> list = DF.GetZPP(stationNo);
if (list != null && list.Count > 0)
{
labStationNo.Text = list[0].StationNo;
labOrderNo.Text = list[0].OrderNo;
labPartNo.Text = list[0].PartNo;
labPName.Text = list[0].PName;
labNum.Text = list[0].ProductCount.ToString() + "/" + list[0].OrderCount.ToString();
labBadCount.Text = list[0].BadCount.ToString();
labStationNo.Tag = list[0].ID;
ProductNum = list[0].ProductCount.ToString();
OrderNum = list[0].OrderCount.ToString();
BadNum = list[0].BadCount.ToString();
}
}
public void NumToPLC()
{
//todo:
#region 初始化
#endregion
#region 信号下发
#endregion
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//Product Match Plan?
string product = "";
product = textBox1.Text.Trim();
string barcodePartNo = "";
//todo:根据条码规则解析条码,看PartNo与页面PartNo是否相同
if (!(barcodePartNo == labPartNo.Text))
{
labErrMessage.Text = "条码与产品不匹配,重新扫描";
textBox1.Text = "";
}
else
{
labErrMessage.Text = "条码与产品匹配,继续生产";
#region todo:读取PLC完成数、故障信息
string completeNum = "10";
string errMessage = "aaa";
string id = labStationNo.Tag.ToString();
tb_ZPPlan md = new tb_ZPPlan();
md.ID = id;
#endregion
if (errMessage == "不合格")
{
//增加不合格数
md.BadCount = CommonTools.StrToInt(BadNum) + 1;
DF.AddBadCount(md);
}
else if (errMessage == "合格")
{
//打总成标签并增加合格数
md.ProductCount = CommonTools.StrToInt(ProductNum) + 1;
DF.AddProductCount(md);
#region todo:打总成标签
#endregion
}
else
{
}
LoadPlan(); //刷新页面
#region 查看计划是否完成,完成则将计划置为完成查询下一计划,未完成则提示扫描条码进行生产
int productN = CommonTools.StrToInt(ProductNum);
int badN = CommonTools.StrToInt(BadNum);
int orderN = CommonTools.StrToInt(OrderNum);
if ((productN + badN) == orderN)
{
//计划置为完成
md.IsFinish = 1;
DF.PlanFinish(md);
LoadPlan();
}
else
{
labErrMessage.Text = "计划未完成,继续扫描";
textBox1.Text = "";
textBox1.TabIndex = 0;
}
#endregion
}
}
}
}
}