123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using static MainForm.SQLHelper;
- using ZedGraph;
- using EIP_Protocol;
- using System.Numerics;
- using SqlSugar;
- namespace MainForm.FaForm
- {
- /// <summary>
- /// 1.弹窗 plc ==>pc cmdParam=1
- /// 2.扫码后 pc ==>plc cmdParam=2
- /// 3.plc确认绑定 plc ==>pc cmdParam=3
- /// 4.绑定成功 pc ==>plc cmdParam=4
- /// 5.绑定失败 pc ==>plc cmdParam=5
- /// </summary>
- public partial class Dialog_BandBarode_S1 : Form
- {
- public static bool isNoClose { get; set; }=false;
- //private string _strProductBarcode = "";
- //private string _CarrierBarcode = "";
- int plcNo = 1;
- string stationCode = "[OP10]";
- string stationName = "壳体清洁上料";
- string tagBaseName = "g_OP10_MES"; //标签变量名称
- string tagMesCommName = "mesCommToPC"; //标签变量名称
- string tagBarsetName = "BarcodeSet";
- OP10_MesData_t stPLC_MesData; //PLC的MES数据
- private Timer _inputTimer;
- Form_Home home=new Form_Home();
- public Dialog_BandBarode_S1()
- {
- InitializeComponent();
- //置顶
- this.TopMost = true;
- // 设置窗口相对于父窗口居中
- this.StartPosition = FormStartPosition.CenterParent;
- this.Shown += MainForm_Shown;
- }
- private void BandBarodeDialog_S1_Load(object sender, EventArgs e)
- {
- Form_Home.OpenDailogFalg=false;
- // 初始化定时器
- _inputTimer = new Timer();
- _inputTimer.Interval = 500; // 500毫秒
- _inputTimer.Tick += InputTimer_Tick;
- // 绑定 TextChanged 事件
- //GetBarcode.TextChanged += GetBarcode_TextChanged;
- //Form_Home.StopWhile = true;
- ProductBarcode.Text = "";
- CarrierBarcode.Text = "";
- Form_Home._strCarrierBarcode = "";
- Form_Home._strProductBarcode = "";
- home.FunsEip.Add(GlobalContext.IsUsePLCNow,
- new Inovance_EIP(GlobalContext.PC1Address, GlobalContext.Machine1Address)); //OP10 壳体清洁上料装备
- foreach (Inovance_EIP plcEIP in home.FunsEip.Values)
- {
- if (plcEIP != null)
- {
- try
- {
- (int, string) result = plcEIP.Connect();
- }
- catch (Exception ex)
- {
- MessageBox.Show($"PLC[{plcEIP._pcIPStr}]连接失败!失败信息:" + ex.Message,
- "PLC连接提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
- MessageBoxOptions.ServiceNotification);
- }
- }
- }
- List<Task> TaskReadProcess = new List<Task>(); // 线程-触发点位(PLC)的线程
- TaskReadProcess.Add(new Task(() => { ReadStation(); })); //OP10 壳体清洁上料装备
- // 开启PLC的业务处理线程-监听PLC点位+状态
- foreach (Task task in TaskReadProcess)
- {
- if (task != null)
- task.Start();
- }
- }
- private void InputTimer_Tick(object sender, EventArgs e)
- {
- int nRet = 0;
- string strRet = "";
- string stationNameStr = stationCode + stationName;
- // 定时器触发,表示输入已经完成
- _inputTimer.Stop();
- //模拟点击
- GetBarcode.Focus();
- //收到PLC确认反馈,关闭弹窗
- //if (isNoClose) {
- // closeWin();
- //}
- // 处理条码
- string Barcode = GetBarcode.Text.Trim();
- if (!string.IsNullOrEmpty(Barcode))
- {
- if (Barcode.Length > 9)
- ProductBarcode.Text = Barcode;
- else
- CarrierBarcode.Text= Barcode;
- Form_Home._strProductBarcode = ProductBarcode.Text.Trim();
- Form_Home._strCarrierBarcode = CarrierBarcode.Text.Trim();
- GetBarcode.Text = "";
- }
- if (CarrierBarcode.Text.Length < 9)
- {
- ErrorLab.Text = $"载具码格式不正确,请重新扫码!";
- return;
- }
- if (ProductBarcode.Text.Length < 47)
- {
- ErrorLab.Text = $"载具码格式不正确,请重新扫码!";
- return;
- }
- if (!string.IsNullOrEmpty(ProductBarcode.Text) && !string.IsNullOrEmpty(CarrierBarcode.Text))
- {
- CommandFromPLC CommandToPlC = new CommandFromPLC();
- CommandToPlC.cmd = 0;
- CommandToPlC.cmdParam = 2;
- CommandToPlC.cmdResult = 0;
- (nRet, strRet) = home.WriteResultToPlc(plcNo, stationNameStr, tagBaseName + "." + tagMesCommName, 1, CommandToPlC);
- if (nRet != 0)
- {
- ErrorLab.Text = $"PLC写入失败!失败原因:" + strRet;
- }
- else
- {
- ErrorLab.Text = $"扫码完成,请点击下方设备屏幕上确认按键!";
- }
- }
- }
- private void GetBarcode_TextChanged(object sender, EventArgs e)
- {
- // 每次文本变化时重置定时器
- _inputTimer.Stop();
- _inputTimer.Start();
- }
- private void closeWin()
- {
- this.DialogResult = DialogResult.OK;
- //Form_Home.StopWhile = false;
- Form_Home.OpenDailogFalg = true;
- this.Close();
- }
- private void BandBarodeDialog_S1_FormClosing(object sender, FormClosingEventArgs e)
- {
- Form_Home.OpenDailogFalg = true;
- //Form_Home.StopWhile = false;
- }
- private void ReadStation() {
- while (true)
- {
- (int, string) result = home.FunsEip[plcNo]
- .Read_SingleTag<OP10_MesData_t>(tagBaseName, 1, out stPLC_MesData, this); //读取单个结构体数据
- if (result.Item1==0)
- {
- if (stPLC_MesData.mesCommFrmPLC.cmdParam==3)
- {
- closeWin();
- };
- }
- }
- }
- private void MainForm_Shown(object sender, EventArgs e)
- {
- // 将焦点设置到 textBox1
- GetBarcode.Focus();
- }
- }
- }
|