using EIP_Protocol; using Sunny.UI; using System; using System.Collections.Generic; using System.Numerics; using System.Threading.Tasks; using System.Windows.Forms; namespace MainForm.FaForm { public partial class Dialog_MaterialVerify_S1 : Form { Form_Home home =new Form_Home(); int plcNo = 1; string stationCode = ""; string stationName = ""; string tagBaseName = ""; //标签变量名称 string tagMesCommName = "mesCommToPC"; //标签变量名称 string tagBarsetName = "BarcodeSet"; (int, string) resultPLC; OP30_MesData_t OP30PLC_MesData; //PLC的MES数据 OP70_MesData_t OP70PLC_MesData; //PLC的MES数据 private Timer _inputTimer; public Dialog_MaterialVerify_S1(string Code,string Name,string tagName) { InitializeComponent(); //置顶 this.TopMost = true; // 设置窗口相对于父窗口居中 this.StartPosition = FormStartPosition.CenterParent; this.Shown += MainForm_Shown; stationCode = Code; stationName = Name; tagBaseName = tagName; } private void Dialog_MaterialVerify_S1_Load(object sender, System.EventArgs e) { 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 TaskReadProcess = new List(); // 线程-触发点位(PLC)的线程 TaskReadProcess.Add(new Task(() => { ReadStation(); })); // 开启PLC的业务处理线程-监听PLC点位+状态 foreach (Task task in TaskReadProcess) { if (task != null) task.Start(); } } private void button2_Click(object sender, System.EventArgs e) { // 初始化定时器 _inputTimer = new Timer(); _inputTimer.Interval = 500; // 500毫秒 _inputTimer.Tick += InputTimer_Tick; } private void button1_Click(object sender, System.EventArgs e) { } private void ReadStation() { while (true) { if (stationCode.Contains("30")) { resultPLC = home.FunsEip[plcNo] .Read_SingleTag(tagBaseName, 1, out OP30PLC_MesData, this); //读取单个结构体数据 } if (stationCode.Contains("70")) { resultPLC = home.FunsEip[plcNo] .Read_SingleTag(tagBaseName, 1, out OP30PLC_MesData, this); //读取单个结构体数据 } if (resultPLC.Item1 == 0) { } } } private void MainForm_Shown(object sender, EventArgs e) { // 将焦点设置到 textBox1 Barcode.Focus(); } private void InputTimer_Tick(object sender, EventArgs e) { // 定时器触发,表示输入已经完成 _inputTimer.Stop(); //模拟点击 Barcode.Focus(); string strBarcode = Barcode.Text.Trim(); if (!string.IsNullOrEmpty(strBarcode)) { SN.Text = strBarcode; Barcode.Text = ""; } } } }