注塑喷涂
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.

401 lines
17 KiB

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using MESClassLibrary.BLL.Log;
using MESClassLibrary.BLL.PunchAndWeld;
using OPCAutomation;
using PunchAndWeld.DataSouce;
namespace PunchAndWeld
{
public partial class FrmLaser : Form
{
public FrmLaser()
{
InitializeComponent();
}
public static string workClass = "";
OPCHelper opcHelper = new OPCHelper();
private string strHostIP = "";
private int deviceState_A = 0, deviceState_B = 0;
private bool lFlag_A = false, rFlag_A = false, lFlag_B = false, rFlag_B = false;
private void FrmLaser_Load(object sender, EventArgs e)
{
string stationNo = ConfigurationManager.AppSettings["StationNo"];
label1.Text = ProScreenFunc.GetStation(stationNo);
Control.CheckForIllegalCrossThreadCalls = false;
Thread t = new Thread(new ThreadStart(TimeGo));
t.Start();
textBox1.TabIndex = 0;
textBox1.Select();
GetWorkClass();
ProScreenFunc.GetScanCountByClass(workClass);
InitList();
PunchAdressBLL bll = new PunchAdressBLL();
try
{
if (opcHelper.CreateServer())
{
if (opcHelper.ConnectServer(strHostIP, "Kepware.KEPServerEX.V6"))
{
opcHelper.Connected = true;
opcHelper.opcGroups = opcHelper.opcServer.OPCGroups;
opcHelper.opcGroup1 = opcHelper.opcGroups.Add("DeviceRead");
opcHelper.opcGroup2 = opcHelper.opcGroups.Add("DeviceWrite");
opcHelper.SetGroupProperty(opcHelper.opcGroup1, 500);
opcHelper.SetGroupProperty(opcHelper.opcGroup2, 500);
opcHelper.opcItems1 = opcHelper.opcGroup1.OPCItems;
opcHelper.opcItems1.DefaultIsActive = true;
DataTable dt = bll.SearchInfo(ConfigurationManager.AppSettings["DeviceNo"], 0);
if (dt != null && dt.Rows.Count > 0)
{
opcHelper.opcItemm1 = new OPCItem[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
opcHelper.opcItemm1[i] = opcHelper.opcItems1.AddItem(dt.Rows[i]["Address"].ToString(), i);
}
}
opcHelper.opcItems2 = opcHelper.opcGroup2.OPCItems;
opcHelper.opcItems2.DefaultIsActive = true;
DataTable dt1 = bll.SearchInfo(ConfigurationManager.AppSettings["DeviceNo"], 1);
if (dt1 != null && dt1.Rows.Count > 0)
{
opcHelper.opcItemm2 = new OPCItem[dt1.Rows.Count];
for (int i = 0; i < dt1.Rows.Count; i++)
{
opcHelper.opcItemm2[i] = opcHelper.opcItems2.AddItem(dt1.Rows[i]["Address"].ToString(), i);
}
}
opcHelper.opcGroup1.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(opcGroup1_DataChange);
deviceState_A = opcHelper.ReadSingleValueFromOPC2(0);
textBox2.Text = deviceState_A == 1 ? "运行中" : "准备OK";
lFlag_A = opcHelper.ReadSingleValueFromOPC3(0).ToLower() == "true" ? true : false;
rFlag_A = opcHelper.ReadSingleValueFromOPC3(1).ToLower() == "true" ? true : false;
deviceState_B = opcHelper.ReadSingleValueFromOPC2(1);
textBox3.Text = deviceState_B == 1 ? "运行中" : "准备OK";
lFlag_B = opcHelper.ReadSingleValueFromOPC3(2).ToLower() == "true" ? true : false;
rFlag_B = opcHelper.ReadSingleValueFromOPC3(3).ToLower() == "true" ? true : false;
}
}
else
{
MessageBox.Show("创建OPC服务失败!", "提示", MessageBoxButtons.OK);
opcHelper.Connected = false;
LogHelper.WriteErrLogBase("工位:" + label1.Text + "创建OPC服务失败!", MethodBase.GetCurrentMethod().Name);
return;
}
}
catch (Exception ex)
{
LogHelper.WriteErrLogBase(ex.ToString(), MethodBase.GetCurrentMethod().Name);
}
}
void opcGroup1_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues,
ref Array Qualities, ref Array TimeStamps)
{
try
{
for (int i = 1; i <= NumItems; i++)
{
int handle = Convert.ToInt32(ClientHandles.GetValue(i).ToString().Trim());
string value = ItemValues.GetValue(i).ToString();
string address = opcHelper.opcItemm1[(Convert.ToInt32(ClientHandles.GetValue(i)))].ItemID;
if (handle == 0)
{
deviceState_A = Convert.ToInt32(value);
textBox2.Text = deviceState_A == 1 ? "运行中" : "准备OK";
}
if (handle == 1)
{
deviceState_B = Convert.ToInt32(value);
textBox3.Text = deviceState_B == 1 ? "运行中" : "准备OK";
}
}
}
catch (Exception ex)
{
LogErrBLL.AddInfo(ex.ToString(),MethodBase.GetCurrentMethod());
}
}
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)
{
label9.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
GetWorkClass();
label3.Text= ProScreenFunc.GetScanCountByClass(workClass);
Thread.Sleep(500);
}
private void GetWorkClass()
{
if (DateTime.Now > DateTime.Parse(DateTime.Now.ToShortDateString() + " 07:30:00") &&
DateTime.Now < DateTime.Parse(DateTime.Now.ToShortDateString() + " 19:30:00"))
{
workClass = "白班";
}
if (DateTime.Now > DateTime.Parse(DateTime.Now.ToShortDateString() + " 19:30:00") &&
DateTime.Now < DateTime.Parse(DateTime.Now.ToShortDateString() + " 23:59:59"))
{
workClass = "夜班";
}
if (DateTime.Now > DateTime.Parse(DateTime.Now.ToShortDateString() + " 00:00:00") &&
DateTime.Now < DateTime.Parse(DateTime.Now.ToShortDateString() + " 07:30:00"))
{
workClass = "夜班";
} workClass = "夜班";
//}
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.KeyCode == Keys.Enter)
{
if (string.IsNullOrWhiteSpace(textBox1.Text))
{
MessageBox.Show("条码不能为空", "提示", MessageBoxButtons.OK);
textBox1.SelectAll();
textBox1.Focus();
return;
}
string barcode = textBox1.Text.Trim();
if (barcode.Contains("."))
{
barcode = ProScreenFunc.UniteBarCodeToOne(barcode);
}
4 months ago
if (ProScreenFunc.SjBarCodeIsInStockIn(barcode) == false)
{
MessageBox.Show($"扫描条码[{barcode}]不存在喷涂下线记录,请更换其他油漆件重新操作!");
textBox1.Text = "";
return;
}
string oneBarCode = barcode;
if (barcode.Length != 20)
{
MessageBox.Show("条码长度有误", "提示", MessageBoxButtons.OK);
textBox1.SelectAll();
textBox1.Focus();
return;
}
if (ProScreenFunc.IsScan(oneBarCode))
{
MessageBox.Show("此条码已被扫描过", "提示", MessageBoxButtons.OK);
textBox1.SelectAll();
textBox1.Focus();
return;
}
lFlag_A = opcHelper.ReadSingleValueFromOPC3(0).ToLower() == "true" ? true : false;
rFlag_A = opcHelper.ReadSingleValueFromOPC3(1).ToLower() == "true" ? true : false;
lFlag_B = opcHelper.ReadSingleValueFromOPC3(2).ToLower() == "true" ? true : false;
rFlag_B = opcHelper.ReadSingleValueFromOPC3(3).ToLower() == "true" ? true : false;
if (barcode.Substring(0, 10) == "6000000175" || barcode.Substring(0, 10) == "6000000176")
{
if (deviceState_A != 2)
{
MessageBox.Show("214设备未准备OK,无法扫描", "提示", MessageBoxButtons.OK);
textBox1.SelectAll();
textBox1.Focus();
return;
}
if (!rFlag_A && !lFlag_A)
{
if (ProScreenFunc.GetPartNameByStock(barcode.Substring(0, 10)).Contains("左"))
{
opcHelper.opcItemm2[0].Write(1);
}
else
{
opcHelper.opcItemm2[1].Write(1);
}
}
else
{
if (ProScreenFunc.GetPartNameByStock(barcode.Substring(0, 10)).Contains("左") )
{
if (lFlag_A)
{
MessageBox.Show("当前应扫描214右侧门槛", "提示", MessageBoxButtons.OK);
textBox1.SelectAll();
textBox1.Focus();
return;
}
opcHelper.opcItemm2[0].Write(1);
}
if (ProScreenFunc.GetPartNameByStock(barcode.Substring(0, 10)).Contains("右") )
{
if (rFlag_A)
{
MessageBox.Show("当前应扫描214左侧门槛", "提示", MessageBoxButtons.OK);
textBox1.SelectAll();
textBox1.Focus();
return;
}
opcHelper.opcItemm2[1].Write(1);
}
}
}
else if (barcode.Substring(0, 10) == "1230007081" || barcode.Substring(0, 10) == "1230007094")
{
if (deviceState_B != 2)
{
MessageBox.Show("294设备未准备OK,无法扫描", "提示", MessageBoxButtons.OK);
textBox1.SelectAll();
textBox1.Focus();
return;
}
if (!rFlag_B && !lFlag_B)
{
if (ProScreenFunc.GetPartNameByStock(barcode.Substring(0, 10)).Contains("左"))
{
opcHelper.opcItemm2[2].Write(1);
}
else
{
opcHelper.opcItemm2[3].Write(1);
}
}
else
{
if (ProScreenFunc.GetPartNameByStock(barcode.Substring(0, 10)).Contains("左"))
{
if (lFlag_B)
{
MessageBox.Show("当前应扫描294右侧门槛", "提示", MessageBoxButtons.OK);
textBox1.SelectAll();
textBox1.Focus();
return;
}
opcHelper.opcItemm2[2].Write(1);
}
if (ProScreenFunc.GetPartNameByStock(barcode.Substring(0, 10)).Contains("右"))
{
if (rFlag_B)
{
MessageBox.Show("当前应扫描294左侧门槛", "提示", MessageBoxButtons.OK);
textBox1.SelectAll();
textBox1.Focus();
return;
}
opcHelper.opcItemm2[3].Write(1);
}
}
}
else
{
MessageBox.Show("条码有误!", "提示", MessageBoxButtons.OK);
textBox1.SelectAll();
textBox1.Focus();
return;
}
ProScreenFunc.AddScanRecord(oneBarCode);
textBox1.SelectAll();
textBox1.Focus();
InitList();
}
}
catch (Exception ex)
{
LogHelper.WriteErrLogBase(ex.ToString(), MethodBase.GetCurrentMethod().Name);
}
}
private void InitList()
{
try
{
listView1.Columns.Clear();
listView1.Clear();
listView1.Columns.Add("条码", 350, HorizontalAlignment.Left);
listView1.Columns.Add("扫描时间", 305, HorizontalAlignment.Left);
DataTable dt = ProScreenFunc.GetScanRecord(workClass);
if (dt != null && dt.Rows.Count > 0)
{
label3.Text = dt.Rows.Count.ToString();
for (int i = 0; i < dt.Rows.Count; i++)
{
ListViewItem lvi = new ListViewItem(dt.Rows[i]["BarCode"].ToString());
lvi.SubItems.Add(dt.Rows[i]["ScanTime"].ToString()); //后面添加的Item都为SubItems ,即为子项
listView1.Items.Add(lvi);
}
}
}
catch (Exception ex)
{
LogHelper.WriteErrLogBase(ex.ToString(), MethodBase.GetCurrentMethod().Name);
}
}
private void label9_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
//Brush s = new SolidBrush(Color.FromArgb(39, 46, 64));
//e.Graphics.FillRectangle(s, e.Bounds);//采⽤特定颜⾊绘制标题列
//e.DrawText();//采⽤默认⽅式绘制标题⽂本
}
private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
{
//e.DrawDefault = true;//采⽤系统默认⽅式绘制项
}
private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
//e.DrawDefault = true;//采⽤系统默认⽅式绘制项
}
}
}